In: Computer Science
Assignment (C language, not C ++)
Start a new project to store grade information for 5 students using structures.
Example output:
NAME SID SCORE GRADE Hopper, Grace 135792468 97.5% A Turing, Alan 198765432 82.9% B Babbage, Charles 165754329 79.0% C Lovelace, Ida 147632391 92.4% A Neumann, John 200638730 99.0% A
Code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct student_t
{
char first_name[20];
char last_name[20];
int id;
float per;
char grad[1];
};
struct student_t s[5];
struct student_t* getStudentInfo(){
for(int i=0; i<5; i++)
{
printf("Enter %d student's first name : ",i+1);
scanf("%s",s[i].first_name);
printf("Enter %d student's last name : ",i+1);
scanf("%s",s[i].last_name);
printf("Enter %d student ID: ",i+1);
scanf("%d",&s[i].id);
printf("Enter %d student percentage: ",i+1);
scanf("%f",&s[i].per);
}
return s;
}
void calStudentGrade(struct student_t* s){
for(int i=0; i<5; i++)
{
if(s[i].per>=90)
strcpy(s[i].grad,"A");
else if(s[i].per>=80 && s[i].per<90)
strcpy(s[i].grad,"B");
else if(s[i].per>=70 && s[i].per<80)
strcpy(s[i].grad,"C");
else if(s[i].per>=60 && s[i].per<70)
strcpy(s[i].grad,"D");
else if(s[i].per>=60 && s[i].per<70)
strcpy(s[i].grad,"E");
else
strcpy(s[i].grad,"F");
}
}
void printStudentInfo(struct student_t* s){
printf("NAME\t\t\tSID\t\t\tSCORE\t\tGRADE\n");
for(int i = 0;i<5;i++)
{
printf("%s, %s\t%d\t\t\t%.2f\t\t%s\n",s[i].last_name,s[i].first_name,s[i].id,s[i].per,s[i].grad);
}
}
int main()
{
struct student_t* st = getStudentInfo();
calStudentGrade(st);
printStudentInfo(st);
return 0;
}
(Feel free to throw an upvote or comment below if you have any doubts)