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

Use vi to create a file named lab9.c containing aC program that reads integer nu

ID: 3610896 • Letter: U

Question

           Use vi to create a file named lab9.c containing aC program that reads integer numbers from a file, prints them onthe screen, and calculates their average. Make sure your name andcourse are at the top of the file.

           Your main( ) function must read the values,accumulate and count them, and pass these last two values to afunction named avg( ) that returns the resultingaverage. Since the input might be empty, your main( ) function mustavoid division by zero. Thus, it should display “The averageis: 0” if no values were read from the file. Test my solutionwith an empty file to see how it works. Your program must read thenumbers from a file named input9.txt. Each numberis an integer. For each integer in the input, display it precededby its order in the file. After displaying all the numbers, displaytheir average.

           Function avg( ) must be defined in a separate filenamed lab9fun.c. It calculates the average as aninteger division.

           You must create a makefile named make_lab9 thatdefines the rules for creating the object codes for both sourcefiles and for creating an executable file namedlab9.

Example: for the values in the provided inputfile, the output should look like the one below.

The values read from the file are:

1: 100

2: 90

3: 85

4: 0

5: 78

6: 0

7: 90

The average is: 63

Explanation / Answer

int main(){
   
    FILE *fp;
    int num,count=0;
    double sum=0,average;
    char str[100];
    fp = fopen("input9.txt","r");
   
    if(fp == NULL)
    {
    printf(" error in opening input9.txtfile");
    return 0;
    }
    printf(" File contents are ");
    while(fgets(str,sizeof(str),fp)!=NULL)
    {
    count++;
    num = atoi(str);
    printf("%d. %d ",count,num);
    sum+=num;
    }
    average = sum/count;
    printf(" Average is %lf ",average);

}

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