In: Computer Science
#include <stdio.h>
//declaring global constant
const int NUM_TESTS = 4;
//declare a global structure
struct Grade{
//declaring variables
char name[50];
int id;
int scores[4];
float average;
char grade;
};
//declaration of the function get info
void getInfo(struct Grade *g,int n);
//declaration of the function show info
void showInfo(struct Grade *g,int n);
//main function
int main()
{
int n,i,j;
//getting number of students
printf("How many Students? ");
scanf("%d",&n);
//declaring array of structures
struct Grade g[n];
//input values for array of structures
for(i=0;i<n;i++){
printf("Name: ");
//That space in front of % is very important to get space
//separated string input without skipping of input
// if you leave the space in front of % it will skip the input
command
scanf(" %[^\n]s",&g[i].name);
printf("Id: ");
scanf("%d",&g[i].id);
for(j=0;j<NUM_TESTS;j++){
printf("Test #%d: ",j+1);
scanf("%d",&g[i].scores[j]);
}
printf("\n");
}
//calling get info method
getInfo(g,n);
printf("---------------------------\n");
printf("---------------------------\n");
printf(" Course Grade Report \n");
printf("---------------------------\n");
printf("---------------------------\n");
//calling show info method
showInfo(g,n);
return 0;
}
//function for getting and calculation of values
void getInfo(struct Grade *g,int n){
int i,j;
//calculating average
for(i=0;i<n;i++){
int sum=0;
for(j=0;j<NUM_TESTS;j++){
sum = sum+g[i].scores[j];
}
g[i].average= sum/NUM_TESTS;
//calculating grades
if (g[i].average>90){
g[i].grade = 'A';
}
else if(g[i].average>80){
g[i].grade = 'B';
}
else if(g[i].average>70){
g[i].grade = 'C';
}
else if(g[i].average>60){
g[i].grade = 'D';
}else{
g[i].grade = 'F';
}
}
}
//function to print structure elements
void showInfo(struct Grade *g,int n){
int i;
//printing array if structure values
for(int i=0;i<n;i++){
printf("Student Name: %s\n",g[i].name);
printf("Student id: %d\n",g[i].id);
printf("Average Test Score: %.1f\n",g[i].average);
printf("Grade %c\n",g[i].grade);
printf("\n");
}
}