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

Create a C program that is ..... Problem 1. File in txt stores the information f

ID: 3681242 • Letter: C

Question

Create a C program that is .....

Problem 1. File in txt stores the information for multiple students including ID, name and score of math and physics. The first line of in.txt indicates the number of students. 2. Your program need to read the content of in txt, calculate the sum of students score and write students' ID, name and total score to the file out.txt. 3. The students in the file out.txt are in reversed order of students in file in txt. 4. Dynamic structure array allocation is needed. Sample Input The content of in txt is 101 Tom 76 74 103 Jim 67 88 203 Lucy 78 74 185 Sam 57 67 Sample output The content of out.txt is 185 Sam 124 203 Lucy 152 103 Jim 155 101 Tom 150

Explanation / Answer

#include<stdio.h>
#include<stdlib.h>

struct Student{
  
       char name[20];
       int physics;
       int math;
       int ID;
};

int main(){
  
      
FILE *reads,*writes;

int N;
  
reads=fopen("in.txt", "r");
writes=fopen("out.txt", "w");
  
if (reads==NULL) {
perror("Error");
return 1;
}
  
fscanf(reads, "%d", &N);
  
// creating dynamic array of struct Student of size N
  
struct Student* students = (struct Student *)malloc(N*sizeof(struct Student));
int i=0;
  
while(!feof(reads)) {
      
fscanf(reads,"%d %s %d %d ", &students[i].ID, students[i].name, &students[i].math, &students[i].physics);
i++;
   }
  
//writting to output file
i = N-1;
while(i>=0){
       fprintf(writes,"%d %s %d ", students[i].ID, students[i].name, (students[i].math + students[i].physics));
       i--;
   }
  
printf("Data has successfilly writen to file ");
fclose(reads);
fclose(writes);
   return 0;
}

/*

Output:

in.txt:

4
101 Tom 76 74
103 Jim 67 88
203 Lucy 78 74
185 Sam 57 67

out.txt:

185 Sam 124
203 Lucy 152
103 Jim 155
101 Tom 150

*/