Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a function, named avg, that receives an array of integer values and return

ID: 3822776 • Letter: W

Question

Write a function, named avg, that receives an array of integer values and returns the average of those values in that array. Since this function is computing the average, it should return a float. Remember, when giving an array to a function you must also give the function the size of the array because it will not be able to determine the size of an array on its own. Test your function with three different integer arrays of sizes, 5, 7, and 10. You do not have to ask the user for values.  Since the function returns a value you should assign its result to a variable and print it out. Perform this is code blocks using C.

Explanation / Answer

#include <stdio.h>
float avg(int data[], int size)
{
int i;
        int sum;
        float aver;
        sum = 0;

        for (i=0; i<size; i++)
        {
   sum = sum + data[i];
        }
aver = sum/size;
        return aver;
}

void main()
{
   int data1[5] = {1,2,3,4,5};
   int data2[7] = {1,2,3,4,5,6,7};
   int data3[10] = {1,2,3,4,5,6,7,8,9,10};
   float avg1, avg2, avg3;

   avg1 = avg(data1,5);
   avg2 = avg(data2,7);
   avg3 = avg(data3,10);
   printf("%f %f %f ", avg1, avg2, avg3);
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote