Write a C program, grade.c , which includes a function, calculateAverage() , tha
ID: 669798 • 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 whileloop 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.
use only #include<stdio.h>
no arrays either
the simplest form i guess
Explanation / Answer
See... There is a slight problem with your main() function. You opened your required file here, and you want me to do all the required operations(1. reading scores, calculating their sum, and then their average, and storing the average into the file which is opened in main() function) in the function calculateAverage().
The two ways we can achieve this is:
Either pass the file pointer to the function, so that the updation can be done there.
Or just denying the file pointer there, and simply reopening the file in the called function(which is not a good practice).
But, as you insisted me not to change the main() function, I should dwell on the second choice.
I suggest you to modify the main, i.e., the function call, with a parameter the file pointer being passed to the function, so that it can be used.
One more this is that, you missed to declare the variable csis, which should be of type file pointer. So, I'm declaring that variable in the main() function.
The code goes here:
#include <stdio.h>
void calculateAverage(FILE *csis)
{
int score = 0,total = 0,n = 0; /*Initialising the variables score, total, n*/
float avg = 0.0; /*Initialising the variable avg*/
printf("Enter the test scores. (Use -1 to terminate): "); /*Prompting the user to enter test scores.*/
while(score != -1) /*As long as the entered score not equals -1*/
{
total += score; /*Adding the score to total*/
scanf("%i",&score); /*Reading the next score*/
if(score != -1) /*If it is a valid score*/
n++; /*Increment the counter n*/
}
if(n == 0) /*If n equals zero, which will lead to a divide by zero error.*/
avg = 0; /*Just assign zero to avg*/
else
avg = total / (float)n; /*Finally calculating the average.*/
printf("Average of given test scores is: %.2f. ",avg); /*Displaying the average to screen.*/
fprintf(csis,"Average of given test scores is: %.2f. ",avg); /*Sending the same text to file.*/
}
int main (void) {
int i;
FILE *csis = fopen("csis.txt", "w");
for (i = 1; i <= 4; ++i) {
calculateAverage(csis);
}
fclose(csis);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.