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

Write a C program, grade.c , which includes a function, calculateAverage() , tha

ID: 669592 • Letter: W

Question

Write a C program, grade.c, which includes a function, calculateAverage(), that can calculate the average of an arbitary number of test scores. Your program will not know in advance how many test scores will be entered into your function so you will need to use a sentinel value (-1) to terminate the while loop used to collect the test scores. The test scores are integers in the range 0 to 100 and the test score average should be output to two deciaml places. Be sure that the user prompts and grades input into the program are displayed in the output file.

Provided below is the main () function for you to use for the program.

int main (void) {

int i;

csis = fopen("csis.txt", "w");

for (i = 1; i <= 4; ++i) {

calculateAverage();

}

fclose(csis);

return 0;

}

Note that the for loop invokes the calculateAverage() function four times. Each time the calculateAverage() functions is invoked, a different set of test grades will be entered. Please be sure to use the data shown below:

First : 78, 93, 45, 88, 89, -1

Second : 87, 68, 100, -1

Third: 84, 86, 90, 86, 96, 68, 82, -1

Fourth : -1

Output from your program should be sent to the terminal window (your screen) as well as the requested csis.txt output file.

Please do not change the main() function above.

Explanation / Answer

#include <stdio.h>
#include <stdlib.h>
void calculateAverage(int a[],int size){
int i=0;
double avg=0;
for(i=0;i<size;i++){
if(a[i]!=0){
avg=avg+a[i];
}else{
break;
}
}
avg=avg/size;
printf("Averaage:%f ",avg);
}
int main (void) {
int i=0,number,j;
FILE *csis;
int a[100],size=0;
csis = fopen("csis.txt", "r");
if( csis == NULL )
{
perror("Error while opening the file. ");
exit(EXIT_FAILURE);
}
else{

while((fscanf(csis,"%d,", &number))){
if(number!=-1){
a[size]=number;
printf("%d ",number);
size++;
}else{
calculateAverage(a,size);
  
for(j=0;j<size;j++){
  
a[j]=0;
}
size=0;
if(i==3){
break;
}
i++;
}

  

}
}
fclose(csis);
return 0;
}

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