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

1. Create file in.txt which will store the information for multiple students inc

ID: 3805403 • Letter: 1

Question

1. Create file in.txt which will store 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. Write a .c program that can read the content of in.txt, calculate the sum of students' score and write students' ID, name and total score to a file out.txt.

3. The students in out.txt should be in the order defned as follows. You need to use qsort function in header file stdlib.h to sort the students. Do not write your own code for sorting. (a)The student with the greatest total grade will rank first. (b)If two students have the same total grade, the student with less student ID will rank first. Students' IDs will always be unique.

Requirements: Use dynamic structure array allocation and deallocation. You just need to submit the .c source file. Do not submit the input file or the output file.

Explanation / Answer

//answer 1

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

struct student
{
int rollno,id;
char *name;
int m1,m2,m3;
}s1;

void main()
{
FILE *fp;
clrscr();
fp=fopen(“Record.dat”,”wb”);  
if(fp==NULL)
{
printf(“File could not open”);
exit(0);
}

printf(“Enter student detailsn”);
printf(“Roll No:”);
scanf(“%d”,&s1.rollno);
printf(“including ID:”);
scanf(“%s”,s1.id);
printf(“Marks in 2 subjects:”);
printf(" physics and maths is");
scanf(“%d%d”,&s1.m1,&s1.m2);

fwrite(&s1,sizeof(s1),1,fp);  
printf(“nRecord has been added successfully…!!”);
fclose(fp);
getch();
}

#include<stdio.h>
#include<stdlib.h>
typedef struct StudentType{
int SID;
char Name[30];
int Mrk1;
int Mrk2;
int Mrk3;
int Total;
float Avg;
int Result;
}Student;
Student s[20];
int N;
void fnMenu();
void fnEnterStudData();
void fnDispConsolR();
void highestmarks();
void fnDispIndRes();
main()
{
system("clear");
int ch;
printf("Program to create students marks card ");
fnMenu();
scanf("%d",&ch);
while(ch){
switch(ch){
case 1: fnEnterStudData();
fnMenu();
scanf("%d",&ch);
break;
case 2: fnDispConsolR();
fnMenu();
scanf("%d",&ch);
break;
case 3: fnDispIndRes();
fnMenu();
scanf("%d",&ch);
break;
case 4: exit(1);
break;
default:
fnMenu();
scanf("%d",&ch);
break;
}
}
}

void fnMenu(){
printf(" 1:Enter Student Data ");
printf(" 2:Display students Markshreet ");
printf(" 3:Display Individual Student Marksheet ");
printf(" 4:Exit ");
printf(" Enter Your Choice ");
}

void fnEnterStudData(){
int i;
printf("Enter Limit ");
scanf("%d",&N);
for(i=1;i<=N;i++){
printf("Enter student registration number ");
scanf("%d",&s[i].SID);
printf("Enter student name: ");
scanf("%s",&s[i].Name);
printf("Enter marks in chemistry ");
scanf("%d",&s[i].Mrk1);
printf("Enter marks in Physics ");
scanf("%d",&s[i].Mrk2);
printf("Enter marks in Maths ");
scanf("%d",&s[i].Mrk3);
s[i].Total=s[i].Mrk1+s[i].Mrk2+s[i].Mrk3;
s[i].Avg=s[i].Total/3;
if(s[i].Mrk1>=35&&s[i].Mrk2>=35&&s[i].Mrk3>=35&&s[i].Avg){
if(s[i].Avg>70){
s[i].Result=4;
}else if(s[i].Avg>60){
s[i].Result=1;
}else if(s[i].Avg>50){
s[i].Result=2;
}else if(s[i].Avg>40){
s[i].Result=3;
}else{
s[i].Result=0;
}
}
}
}

void fnDispConsolR(){
printf(" Aimit Student Result ");
printf("SID NAME CHEM PHYSIC MATH TOTAL AVG STATUS ");
int i;
for(i=1;i<=N;i++){
printf(" %d ",s[i].SID);
printf("%s ",s[i].Name);
printf(" %d ",s[i].Mrk1);
printf("%d ",s[i].Mrk2);
printf("%d ",s[i].Mrk3);
printf("%d ",s[i].Total);
printf("%f",s[i].Avg);
if(s[i].Result==4){
printf("Distinction ");
}else if(s[i].Result==1){
printf("First class ");
}else if(s[i].Result==2){
printf("Second class ");
}else if(s[i].Result==3){
printf("Pass class ");
}else{
printf("Fail ");
}
}
}

void fnDispIndRes(){
int i,skey,flag=0,id;
printf("Enter the Student SID to display Individual Marksheet: ");
scanf("%d",&skey);
printf("%d ",N);
for(i=1;i<=N;i++){
if(s[i].SID==skey){
flag=1;
id=i;
}
}
if(flag==1){
system("clear");
printf("SID NAME CHEM PHYSIC MATH TOTAL AVG STATUS ");
printf(" %d ",s[id].SID);
printf("%s ",s[id].Name);
printf(" %d ",s[id].Mrk1);
printf("%d ",s[id].Mrk2);
printf("%d ",s[id].Mrk3);
printf("%d ",s[id].Total);
printf("%f",s[id].Avg);
if(s[i].Result==4){
printf("Distinction ");
}else if(s[id].Result==1){
printf("First class ");
}else if(s[id].Result==2){
printf("Second class ");
}else if(s[id].Result==3){
printf("Pass class ");
}else{
printf("Fail ");
}

}else{
printf("Invalid Result ");
}
}