177k views
3 votes
During a drawing competition, the review system is implemented by assigning 3 to 5 reviewers per submission. Each drawing is ranked by 3 to 5 reviewers on a continuous scale of 1 to 5 (1 being the lowest score and 5 being the highest, decimal numbers are not allowed).

Final results are saved in the reviews file (given separately). (Format of each line: author_name,score_1score_2............score_5)
James, 24152
Ann, 145
Thomas 5345
Mark,432
Karen, 222
Write a C program to read, process, and output data as explained:
1. Read and process the content of the reviews text file.
2. Calculate the average score per contestant. (average = sum of all the scores / number of scores)
3. Display results in a formatted chart. For each contestant, list the name and the 3 to 5 scores followed by the average score.
Comments are not required, define functions where necessary.
Name S1 S2 S3 S4 S5 Ave
James 2 4 1 5 2 2.8
Ann 1 4 5 3.33
Thomas 5 3 4 5 4.25
Mark 4 3 2 3
Karen 2 2 2 2
James, 24152
Ann, 145
Thomas, 5345
Mark, 432
Karen, 222

1 Answer

6 votes

Answer:

See explaination

Step-by-step explanation:

//include <stdio.h>

#include <stdlib.h> // For exit()

#include <stdbool.h>

int main()

{

FILE *fptr;

char filename[100], c;

// Open file

fptr = fopen("review.txt", "r");

if (fptr == NULL)

{

printf("Cannot open file \\");

exit(0);

}

// Read contents from file

c = fgetc(fptr);

printf("Name S1 S2 S3 S4 S5 Ave\\");

bool flag = false;

int sum = 0;

int count = 0;

while (c != EOF)

{

if(c == '\\'){

printf ("%f ", (double)sum/count);

count = 0;

sum = 0;

flag = false;

}

if(c != ',')

{

if(flag){

printf ("%c ", c);

sum = sum + (int)(c)-48;

count++;

}

else

printf("%c", c);

}

if(c == ',')

{

flag = true;

printf(" ");

}

c = fgetc(fptr);

}

printf ("%d ", sum/count);

fclose(fptr);

return 0;

}

User Jon Sakas
by
8.5k points