Using the code template below, create a function that computes the average of all the elements in the array using a 'for' loop to sum the array. Use an array pointer to get the values from the array. The average is the return value from the function. Then, implement the function into the main() method.
C PROGRAMMING
Instructions:
Using the code template below, create a function that computes the average of all the elements in the array using a 'for' loop to sum the array. Use an array pointer to get the values from the array. The average is the return value from the function. Then, implement the function into the main() method.
/*_____________________________________________________________________________*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
float data[] = {2.5, 3.33, 4.2, 8.0, 5.1};
float sum = 0;
printf("Index\tValues\n");
for(int i = 0; i < 5; i++)
{
printf("%d\t%f", i, data[i]);
printf("\n");
sum = sum + data[i];
}
sum = sum / 5;
printf("Avg\t%f", sum);
return 0;
}
Trending now
This is a popular solution!
Step by step
Solved in 5 steps with 3 images