Solved: Write a program that uses a structure to store the…

$30.00

Category:

Description

Solved: Write a program that uses a structure to store the…

Write a program that uses a structure to store the following student data items:

First Name

Last Name

Student ID (10 digit integer – store as long int)

Major GPA (double)

The program should allow the user to enter data for up to twenty students, the data should be stored in an array of the structure type described above. The program should sort the array elements in ascending order by the student’s last name and first name. After sorting the array, display a table that shows the student data in a table with the following column headings in order:

Last Name, First Name, Student ID, Major and GPA.

Sample output from a program that satisfies the requirements of this exercise are provided below.

Enter the first student’s first name or -1 to end input: Mary Catherine

Enter student #1’s last name: Van Pelt

Enter student #1’s student ID number: 2011567890

Enter student #1’s major: Biomedical Science

Enter student #1’s grade point average: 3.52

Enter student #2’s first name or -1 to end input: Daniel

Enter student #2’s last name: Smith

Enter student #2’s student ID number: 2012196321

Enter student #2’s major: Electrical Engineering

Enter student #2’s grade point average: 3.33

Last Name First Name Student ID Major GPA

_________________________________________________________________________

Smith Daniel 2012196321 Electrical Engineering 3.33

Van Pelt Mary Catherine 2011567890 Biomedical Science 3.52

Here’s what I have so far:

#include<stdio.h>
#include<conio.h>
#include<string.h>
#define MAX 20

struct info
{
char firstName[30];
char lastName[30];
char tlastName[30];
long int studentId[10];
char major[30];
double gpa;

};

int main()
{

char hold [30];
int i, j, k, num;
struct info student[MAX];

for(i = 1;i <= MAX; i++)
{
printf(“Enter student #%d’s first name or -1 to end input: “, i);
gets(student[i].firstName);
printf(“Enter student #%d’s last name: “, i);
gets(student[i].lastName);
printf(“Enter student #%d’s student ID number: “, i);
scanf(“%d”, &student[i].studentId);
printf(“Enter student #%d’s major: “, i);
gets(student[i].major);
printf(“Enter student #%d’s grade point average: “, i);
scanf(“%f”, &student[i].gpa);
strcpy( student[i].tlastName, student[i].lastName);
num++;
}

for (i = 0; i < num – 1 ; i++)
{
for (j = i + 1; j < num; j++)
{
if (strcmp(student[i].lastName, student[j].lastName) > 0)
{
strcpy(hold, student[i].lastName);
strcpy(student[i].lastName, student[j].lastName);
strcpy(student[j].lastName, hold);
}
}
}

printf(“Last Name\t\t\tFirst Name\t\t\tStudent ID\tMajor\t\t\tGPA\n”);
printf(“____________________________________________________________”);

for(i = 1;i <= num; i++)
{
printf(“\n”);
puts(student[i].firstName);
printf(“\t\t\t”);
puts(tstudent[i].lastName);
printf(“\t\t\t”);
printf(“%d\t”, student[i].studentId);
printf(“%s\t\t\t”, student[i].major);
printf(“%f”, student[i].gpa);

}
getch();
return 0;
}

 


error: Content is protected !!