basic-c/sources/arrayaverage.c

35 lines
757 B
C

#include <stdio.h>
#include <stdlib.h>
void printArray(int* array, int length) {
int i;
for (i = 0; i < length; i++)
printf("%d ", array[i]);
printf("\n");
}
float average(int* array, int length) {
float result = 0;
int i;
for (i = 0; i < length; i++)
result += *(array + i);
return result / length;
}
int main(int argc, const char** argv) {
#define ARRAY_LENGTH 10
int arr[ARRAY_LENGTH];
int i = 0;
float result = 0;
while (i < ARRAY_LENGTH) {
printf("Enter value %d:", i);
scanf("%d", &arr[i]);
i++;
}
printf("Our array is: ");
printArray(arr, ARRAY_LENGTH);
printf("And the average is: %f \n",
average(arr, ARRAY_LENGTH));
return 0;
}