C program, it has to be place in a menu , export to a text file, and binary file
ID: 3889784 • Letter: C
Question
C program, it has to be place in a menu , export to a text file, and binary file if it exist
thank you
You are to write a C program that will load a 2D array with values. You can assume that the array has 4 columns. Each column will represent an score for a student. Each row will represent an individual student’s scores. The row subscript will become a ID for a student.
You need to create a menu that will added 4 scores for a new student each time it is executed
You need a menu choice that will display the average scores for a student
You need a menu choice that will display the average scores
You need a menu choice that will display the class average
You need a menu choice that will clear out the gradebook
You need a menu choice that will output the contents of the gradebook to a TEXT FILE
Each time the program ends the data will be saved to a binary file
Each time the programs the data will be loaded from the binary file if exists
Explanation / Answer
#include<stdio.h>
#include<stdlib.h>
void avg_stu(int **b,int n, int f){
if(f==1){
printf(" You have already clear your grade book now there is nothing to show... ");
return;
}
int s,j,avg = 0;
printf("Enter student ID:-");
scanf("%d",&s);
for(j=0;j<4;j++)
avg = avg + b[s][j];
avg = avg/4;
printf("The average score of Student ID %d is :- %d",s,avg);
}
void avg_scores(int **b,int n, int f){
int i, j, avg=0 ;
if(f==1){
printf(" You have already clear your grade book now there is nothing to show... ");
return;
}
for(i=0;i<4;i++){
avg = 0;
for(j=0;j<n;j++){
avg = avg+b[j][i];
}
avg = avg/n;
printf(" The average score for subject %d is :- %d ",i,avg);
}
}
void cls_avg(int **b, int n, int f){
int i, j , avg = 0, c_avg = 0;
if(f==1){
printf(" You have already clear your grade book now there is nothing to show... ");
return;
}
for(i=0;i<n;i++){
avg = 0;
for(j=0;j<4;j++){
avg = avg + b[i][j];
}
avg = avg/4;
c_avg = c_avg + avg;
}
c_avg = c_avg/n;
printf(" Average Score of Class is :- %d ", c_avg);
}
void file_output(int **b, int n){
FILE *fptr;
int i, j;
fptr = fopen("Text.txt", "a");
if(fptr == NULL)
{
printf("Error!");
exit(1);
}
for(i=0;i<n;i++){
fprintf(fptr,"Student ID %d ", i);
for(j=0;j<4;j++){
fprintf(fptr,"Score %d -> %d ",j+1, b[i][j]);
}
}
fclose(fptr);
}
int main(){
int **a, i, j, n, ch, flag=0;
printf("Enter the number of student:-");
scanf("%d",&n);
a=malloc(n* sizeof(int *));
for(i=0;i<n;i++)
a[i]=malloc(4 * sizeof(int));
for(i=0;i<n;i++){
printf("Enter Score for Student ID %d ", i);
for(j=0;j<4;j++){
printf("Enter score %d:-", j+1);
scanf("%d",&a[i][j]);
}
}
while(1){
printf(" 1.display the average scores for a student:-");
printf(" 2.display the average scores:-");
printf(" 3.display the class average:-");
printf(" 4.clear out the gradebook:");
printf(" 5.Output the contents of the gradebook to a TEXT FILE:-");
printf(" 6.Exit..");
printf(" Enter your choice...");
scanf("%d",&ch);
switch(ch){
case 1:avg_stu(a,n,flag);break;
case 2:avg_scores(a,n,flag);break;
case 3:cls_avg(a,n,flag);break;
case 4:
for(i=0;i<n;i++)
for(j=0;j<4;j++)
a[i][j] = 0;
flag = 1;
break;
case 5:file_output(a,n);break;
case 6:exit(0);
default:printf("Invalid ");
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.