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

The C program below was written to read a file called “scores.dat” and then outp

ID: 3804969 • Letter: T

Question

The C program below was written to read a file called “scores.dat” and then output another file called “scores.out.” The file scores.dat contains 4 columns of just integers, the first for student ID, the second, third and fourth columns are for student grades on tests. The program below just outputs the data with column titles. Please help me to modify this program in such a way that it will read the data from scores.dat file and calculate each student’s lowest score, highest score and average score. Then it must write student’s id number, student’s lowest score, highest score and average score to the output file scores.out. The scores.out file must have an appropriate title for each column.

#include <stdio.h>

int main(void)

{

            FILE *fPtr;

            FILE *fPtrOut;

            if ((fPtrOut = fopen("scores.out", "w")) == NULL) {

                        puts("cant open file to write");

            }

            if ((fPtr = fopen("scores.dat", "r")) == NULL) {

                        puts("cant open file to read");

            }

            else

            {

                        int id;

                        int score1;

                        int score2;

                        int score3;

                        printf("ID SCORE1 SCORE2 SCORE3 ");

                        fprintf(fPtrOut, "ID SCORE1 SCORE2 SCORE3 ");

                        fscanf(fPtr, "%d%d%d%d", &id, &score1, &score2, &score3);

                        while (!feof(fPtr)) {

                                    printf("%d %d %d %d ", id, score1, score2, score3);

                                    fprintf(fPtrOut, "%d %d %d %d ", id, score1, score2, score3);

                                   

                                    fscanf(fPtr, "%d%d%d%d", &id, &score1, &score2, &score3);

                        }

                        fclose(fPtr);

                        fclose(fPtrOut);

            }

}

Explanation / Answer

Program:-

#include <stdio.h>
int main(void)
{
int id,avg,min,min1,max,max1;
int score1,score2,score3;
FILE *fPtr;
FILE *fPtrOut;
if ((fPtrOut = fopen("scores.out", "w")) == NULL)
{
  puts("cant open file to write");
}
if ((fPtr = fopen("scores.dat", "r")) == NULL)
{
  puts("cant open file to read");
}
else
{
  fprintf(fPtrOut, "ID Min_SCORE   Max_SCORE   Avg_SCORE ");
  while (!feof(fPtr))
  {
   fscanf(fPtr, "%d%d%d%d", &id, &score1, &score2, &score3);
   //find minimum in three scores
   min1=(score2<=score3)?score2:score3;
   min=(score1<=min1)?score1:min1;
   //find maximum in three scores
   max1=(score1>=score2)?score1:score2;
   max=(score3>=max1)?score3:max1;
   //find average
   avg=(score1+score2+score3)/3;
   fprintf(fPtrOut, "%d %d %d %d ", id, min, max, avg);
  }
  fclose(fPtr);
  fclose(fPtrOut);
  printf("Operation complete open scores.out with notepad for results...");
}
}

Scores.dat file input:-

150514 25 44 66
154011 54 56 23
541122 76 45 53
541235 75 44 12
546566 14 45 18
758556 14 45 41
798213 65 58 54

Output:-

Operation complete open scores.out with notepad for results...

Process exited with return value 62
Press any key to continue . . .

Scores.out file Output:-

ID Min_SCORE   Max_SCORE   Avg_SCORE
150514 25 66 45
154011 23 56 44
541122 45 76 58
541235 12 75 43
546566 14 45 25
758556 14 45 33
798213 54 65 59

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