In: Computer Science
Write a C-based language program in visual studio that uses an array of structs that stores student information including name, age, GPA as a float, and grade level as a string (e.g., “freshmen,”).
Write the same program in the same language without using structs.
// do comment if any problem arises
1-> using structs
// code
#include <stdio.h>
#include <string.h>
// structure student
struct student
{
char name[100];
int age;
float GPA;
char grade[100];
};
int main()
{
// creating array of 100structs
struct student Students[100];
//creating an example student
strcpy(Students[0].name, "Harish");
Students[0].age = 25;
strcpy(Students[0].grade, "freshmen");
Students[0].GPA = 8.1;
//printing created student
printf("First student:\nName: %s\n", Students[0].name);
printf("Age: %d\nGPA: %.1f\nGrade: %s", Students[0].age, Students[0].GPA, Students[0].grade);
}
Output:
2-> without structs:
// code
#include <stdio.h>
#include <string.h>
int main()
{
//for solving this problem without struct multiple arrays have to be used
char name[100][100];
int age[100];
float GPA[100];
char grade[100][100];
//creating an example student
strcpy(name[0], "Harish");
age[0] = 25;
strcpy(grade[0], "freshmen");
GPA[0] = 8.1;
//printing created student
printf("First student:\nName: %s\n", name[0]);
printf("Age: %d\nGPA: %.1f\nGrade: %s", age[0], GPA[0], grade[0]);
}
output:
It can be seen that both programs yields same output.