Question

2. (4 marks) Write a C function to find the average of all array elements in...

2. (4 marks) Write a C function to find the average of all array elements in an one-dimensional array. The function prototype is as follows, float array_average(float a[], int n); /* find the average of all array elements in array a, and n is the size of array a.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here I am providing the code and the output for the given problem

Code

#include <stdio.h> float array_average(float a[], int n); //function declaration float array_average(float a[], int n){ //fun

Output

The average of the array is 5.00

Code

#include <stdio.h>

float array_average(float a[] , int n); //function declaration

float array_average(float a[] , int n){ //function definition
  
int i;
  
float sum = 0; //initialising the variables
  
for(i = 0 ; i < n ; i++) //adding all the elements in the array
sum+=a[i];
  
return sum/n; //returning the average of array
  
  
}

int main()
{
int n = 9;
  
float a[9] = {1,2,3,4,5,6,7,8,9}; //declaring one dimensional array
  
  
float result = array_average(a,n); //calling function array_average
  
printf("The average of the array is %.2f",result); //displaying the result

  
}

Note : Hit Like if you got the correct output. Please comment if you have any doubts.Thank you

Add a comment
Know the answer?
Add Answer to:
2. (4 marks) Write a C function to find the average of all array elements in...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT