In: Computer Science
GPA calculator in C language
To understand the value of records in a programming language, write a small program in a C-based language that uses an array of structs that store student information, including name, age, GPA as a float, and grade level as a string (e.g., “freshmen,” etc.).
Note:Code and Output Screenshots
//c code for reading students information and computing GPA based on percentage
#include<stdio.h>
struct student
{
char name[20];
int age;
float percentage;
float GPA;
char gradelevel[20];
} s[3];//store information of 3 students
int main()
{
int i;
printf("Enter information of students:\n");
// storing information
for(i=0; i<3; ++i)
{
printf("Enter name: ");
scanf("%s",s[i].name);
printf("Enter Age: ");
scanf("%d",&s[i].age);
printf("Enter percentage: ");
scanf("%f",&s[i].percentage);
printf("Enter gradelevel: ");
scanf("%s",s[i].gradelevel);
//computing GPA
if(s[i].percentage>=94)
s[i].GPA=4.0;
else
if(s[i].percentage>=90&&s[i].percentage<94)
s[i].GPA= 3.7;
else
if(s[i].percentage>=87&&s[i].percentage<90)
s[i].GPA= 3.3;
else
if(s[i].percentage>=83&&s[i].percentage<87)
s[i].GPA= 3.0;
else
if(s[i].percentage>=80&&s[i].percentage<84)
s[i].GPA= 2.7;
else
if(s[i].percentage>=77&&s[i].percentage<80)
s[i].GPA= 2.3;
else
if(s[i].percentage>=73&&s[i].percentage<77)
s[i].GPA= 2.0;
else
if(s[i].percentage>=70&&s[i].percentage<73)
s[i].GPA= 1.7;
else
if(s[i].percentage>=67&&s[i].percentage<70)
s[i].GPA= 1.3;
else
if(s[i].percentage>=60&&s[i].percentage<67)
s[i].GPA= 1.0;
else
s[i].GPA=0.0;
printf("\n---------------------------------\n");
}
printf("Displaying Information:\n\n");
// displaying information
for(i=0; i<3; i++)
{
printf ("\n--------student :%d---------",i+1);
printf("\nName: %s",s[i].name);
printf("\nAge: %d\n",s[i].age);
printf("\npercentage: %.2f",s[i].percentage);
printf("\nGPA : %.1f",s[i].GPA);
printf("GradeLevel : %s",s[i].gradelevel);
}
return 0;
}
program without input, storing two student records and prints GPA and gradelevel
#include<string.h>
#include<stdio.h>
struct student
{
char name[20];
int age;
float percentage;
float GPA;
char gradelevel[20];
} s[3];//store information of 3 students
int main()
{
int i;
strcpy(s[0].name,"Alice");
s[0].age=23;
s[0].GPA=4.0;
strcpy(s[0].gradelevel,"freshmen");
strcpy(s[1].name,"BOB");
s[1].age=24;
s[1].GPA=3.0;
strcpy(s[1].gradelevel,"junior");
for(i=0; i<2; i++)
{
printf("\n--------student-%d---------\n",i+1);
printf("\nGPA : %.1f\n",s[i].GPA);
printf("\nGradeLevel : %s\n\n",s[i].gradelevel);
}
return 0;
}