C Programing Write a program that will create an array of structures for storing
ID: 3760938 • Letter: C
Question
C Programing
Write a program that will create an array of structures for storing student data. The program should read in 3 records containing student ID, name and GPA from the user, then store this information in array of structures. The program should then print out the records back to the user.
Here is a sample run of the program
enter student record 0
enter student name
student1
enter student id
101
enter student gpa
2
enter student record 1
enter student name
student2
enter student id
102
enter student gpa
3.8
enter student record 2
enter student name
student4
enter student id
222
enter student gpa
3.1
Record 0:, student1
ID:101
GPA: 2.000000
Record 1:, student2
ID:102
GPA: 3.800000
Record 2:, student4
ID:222
GPA: 3.100000
Press any key to continue . . .
This is an example of how to create a structure of type student with the needed fields
struct student{
char name[20];
int id;
double gpa;
};
To create an array of structures of type student you can use the following
struct student studentarray[3];
To read in and print out the data use for loops for example
this would read in the data
for (i=0;i<3;i++)
{
printf("enter student record %d ",i);
printf("enter student name ");
scanf("%s",studentarray[i].name);
// repeat for id and gpa
}
// then use a similar for loop with printf statements to print out the data in the array
Explanation / Answer
#include<stdio.h>
#include<string.h>
struct student
{
char name[20];
int studentId;
float gpa;
};
int main()
{
student arr[3];
printf("Enter 3 student records : ");
for(int i=0;i<3;i++)
{
printf("Enter student record %d : ",i);
printf("enter student name : ");
scanf("%s",arr[i].name);
printf("enter student id : ");
scanf("%d",&arr[i].studentId);
printf("enter student gpa : ");
scanf("%f",&arr[i].gpa);
}
for(int i=0;i<3;i++)
{
printf("Record %d : ",i);
printf("student name : %s ",arr[i].name);
printf("student id : %d ",arr[i].studentId);
printf("student gpa : %f ",arr[i].gpa);
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.