In: Computer Science
Student Structure Assignment
Create a Student Structure globally consisting of student ID, name, completed credits, and GPA.
Define one student in main() and initialize the student with data.
Display all of the student’s data on one line separated by tabs.
Create another student in main().
Assign values to your second student (do not get input from the user).
Display the second student’s data on one line separated by tabs.
Create a third student in main().
Use a series of prompts to allow user to enter in all data for your third student.
Display the third student’s data on one line separated by tabs.
Create an array of 3 students.
Send the array to a function that will use a series of prompts to allow user to enter in all data using a loop.
From main(), send the array to another function that will print the students data in your array.
I want the program to work but I need one or two mistake.
Code:
#include <stdio.h>
#include<string.h>
//structure as asked in question
struct Student{
char Id[20];
char Name[50];
int cc;
char gpa[3];
};
void fill_students(struct Student arr[],int n){
//use a series of prompts to allow user to enter in all data using a loop.
for(int i=0;i<n;i++){
printf("Enter Student Id: ");
scanf("%s",arr[i].Id);
printf("Enter Student Name: ");
scanf("%s",arr[i].Name);
printf("Enter Student completed credits: ");
scanf("%d",&arr[i].cc);
printf("Enter Student gpa: ");
scanf("%s",arr[i].gpa);
printf("\n");
}
}
void print_students(struct Student arr[],int n){
//use a series of prompts to allow user to enter in all data using a loop.
for(int i=0;i<n;i++){
printf("%s\t%s\t%d\t%s\n",arr[i].Id,arr[i].Name,arr[i].cc,arr[i].gpa);
}
}
int main(void) {
//Defined one student in main() and initialize the student with data.
struct Student s1;
strcpy(s1.Id,"SN091551");
strcpy(s1.Name,"Sofia");
s1.cc=45;
strcpy(s1.gpa,"A+");
//Display all of the student’s data on one line separated by tabs
printf("%s\t%s\t%d\t%s\n",s1.Id,s1.Name,s1.cc,s1.gpa);
//Create another student in main().
struct Student s2;
//Assign values to your second student (do not get input from the user).
strcpy(s2.Id,"SN091552");
strcpy(s2.Name,"Samantha");
s2.cc=40;
strcpy(s2.gpa,"B+");
//Display the second student’s data on one line separated by tabs.
printf("%s\t%s\t%d\t%s\n",s2.Id,s2.Name,s2.cc,s2.gpa);
//Create a third student in main().
struct Student s3;
//Use a series of prompts to allow user to enter in all data for your third student.
printf("\nEnter Student Id: ");
scanf("%s",s3.Id);
printf("Enter Student Name: ");
scanf("%s",s3.Name);
printf("Enter Student completed credits: ");
scanf("%d",&s3.cc);
printf("Enter Student gpa: ");
scanf("%s",s3.gpa);
//Display the third student’s data on one line separated by tabs.
printf("\n%s\t%s\t%d\t%s\n",s3.Id,s3.Name,s3.cc,s3.gpa);
//Create an array of 3 students.
struct Student s_arr[3];
//Send the array to a function that will
fill_students(s_arr,3);
//send the array to another function that will print the students data in your array.
print_students(s_arr,3);
return 0;
}
Sample run:
SN091551 Sofia 45 A+
SN091552 Samantha 40 B+
Enter Student Id: SN091553
Enter Student Name: Sharan
Enter Student completed credits: 38
Enter Student gpa: B+
SN091553 Sharan 38 B+
Enter Student Id: SN091554
Enter Student Name: Salim
Enter Student completed credits: 35
Enter Student gpa: C+
Enter Student Id: SN091555
Enter Student Name: Salina
Enter Student completed credits: 30
Enter Student gpa: C
Enter Student Id: SN091556
Enter Student Name: Simon
Enter Student completed credits: 28
Enter Student gpa: D
SN091554 Salim 35 C+
SN091555 Salina 30 C
SN091556 Simon 28 D