In: Computer Science
I am also atteching input text file which contains 5 records for referance
this is test data
5
12 Gaurav 9 5
Gujarat,India
90 98 99 100 96
10 ABC 8 4
Mumbai,India
90 98 99 96
16 Utsav 9 4
Surat,India
88 98 99 100
1 Vivek 10 5
Gujarat,India
90 98 99 100 96
2 Akshit 9 5
Amd,India
90 90 88 100 96.
#include <bits/stdc++.h>
using namespace std;
//Structure Defining Student Details
struct Student
{
int studentID;
string Name;
int GPA;
string Address;
int *scores;
int no_Of_scores;
};
//Print the records
void displayRecords(vector<Student>&v){
for(int i=0;i<v.size();i++){
cout<<"Details of Student "<<i+1<<endl;
cout<<"StudentId: "<<v[i].studentID<<endl;
cout<<"Name: "<<v[i].Name<<endl;
cout<<"Address: "<<v[i].Address<<endl;
cout<<"GPA: "<<v[i].GPA<<endl;
cout<<"Scores: ";
for(int j=0;j<v[i].no_Of_scores;j++){
cout<<v[i].scores[j]<<" ";
}
cout<<endl<<endl;
}
}
//If you want to sort according to GPA change v[index].studentId
to v[index].GPA
int Partition(vector<Student>&v,int low,int high){
int pivot =v[high].studentID;
int i = (low - 1);
for (int j = low; j <= high - 1; j++)
{
if (v[j].studentID < pivot)
{
i++;
swap(v[i], v[j]);
}
}
swap(v[i + 1], v[high]);
return (i + 1);
}
//recursive function to sort vector of student
void QuickSort(vector<Student>&v,int low,int high){
if(low<high){
int partition_index=Partition(v,low,high);
QuickSort(v,low,partition_index-1);
QuickSort(v,partition_index+1,high);
}
}
int main()
{
//Uncomment the cout parts if you want to enter the details
//From console.That is for referance and better readability
//of code
//Reading file input
//Input format:
//No of student
//studentId,Name,GPA,No of Scores
//Address
//scores eg 5 10 12 13 ...
freopen("input.txt", "r", stdin);
//cout<<"Enter Required Details\n";
//cout<<"Enter Number Of Students\n";
int no_of_students;
cin >> no_of_students;
vector<Student> v;
int index=0;
while(index<no_of_students){
Student temp;
// cout<<"Enter StudentId,Name,GPA,Number Of Subjects
Scores\n";
cin>>temp.studentID>>temp.Name>>temp.GPA>>temp.no_Of_scores;
//cout<<"Enter Address\n";
cin>>temp.Address;
temp.scores=new int[temp.no_Of_scores];
//cout<<"Enter Scores \n";
for(int i=0;i<temp.no_Of_scores;i++)
{
cin>>temp.scores[i];
}
v.push_back(temp);
index++;
}
cout<<"\n===Unsorted Details===\n\n";
displayRecords(v);
QuickSort(v,0,v.size()-1);
cout<<"\n===Sorted Details===\n\n";
displayRecords(v);
}
THANK YOU!! PLEASE VOTE