You are given a student data file which consists of the student\'s name, student
ID: 639507 • Letter: Y
Question
You are given a student data file which consists of the student's name, student ID, and GPA. Data set is provided below: Name ID GPA Alice 8234 2.7 Mark 2672 3.0 Joanne 1234 3.7 John 3291 2.0 Tim 3212 3.8 Alford 1034 2.7 Benson 6290 2.5 Nancy 4234 3.7 Oscar 1672 1.0 Larry 1004 2.7 Write a program to process this student data. First, input these records into a record structure. Then, sort them in ascending order using Student ID as a key. Display the sorted output. You are limited to use ONLY the concepts of one dimension array and record (struct). using c++
Explanation / Answer
#include<iostream>
#include<iomanip>
#include<string.h>
using namespace std;
struct Student
{
char name[30];
int ID;
float CGPA;
};
void showInfo(Student s)
{
cout.width(25); cout << left <<s.name;
cout.width(15); cout << left <<s.ID;
cout << left <<s.CGPA<<" ";
}
void sortbyID(Student s[], int number_of_student)
{
int i,j,index,tmpID;
char tmpname[30];
float tmpCGPA;
for(i=0;i<number_of_student;i++)
{
for(j=0;j<number_of_student-i-1;j++)
if(s[j].ID>s[j+1].ID)
{
tmpID=s[j+1].ID;
tmpCGPA=s[j+1].CGPA;
strcpy(tmpname,s[j+1].name);
s[j+1].ID=s[j].ID;
s[j+1].CGPA=s[j].CGPA;
strcpy(s[j+1].name,s[j].name);
s[j].ID=tmpID;
s[j].CGPA=tmpCGPA;
strcpy(s[j].name,tmpname);
}
}
}
int main()
{
int i,number_of_student=10;
Student s[]={ {"Alice", 8234, 2.7}, {"Mark", 2672, 3.0}, {"Joanne", 1234, 3.7},
{"John", 3291, 2.0}, {"Tim", 3212, 3.8}, {"Alford", 1034, 2.7},
{"Benson",6290, 2.5}, {"Nancy", 4234, 3.7},{ "Oscar", 1672, 1.0},
{"Larry",1004,2.7}};
cout<<" NAME ID CGPA ";
for(i=0;i<number_of_student;i++)
{
showInfo(s[i]);
}
sortbyID(s,number_of_student);
cout<<" After sorting by ID ";
cout<<" NAME ID CGPA ";
for(i=0;i<number_of_student;i++)
{
showInfo(s[i]);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.