Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have a file of student records and am trying to program a quickSort function t

ID: 3865840 • Letter: I

Question

I have a file of student records and am trying to program a quickSort function that sorts the students by their student ID. Can someone please take a look at my code and show me how to correctly do it? here is my code so far and output and student file.

student file (name,address,student id,10testscores,gpa)

///////////////////////////////////////////////////////////////

main code:

#include <iostream>

#include <fstream>

#include <string>

#include <vector>

using namespace std;

struct student{

string sName;

string sAddress;

int sId,sTestavg,grade1,grade2,grade3,grade4,grade5,grade6,grade7,grade8,grade9,grade10;

double sGpa;

void getInfo(){

cout<< "Student Id: "<< sId<<" "<< sName << " Address: "<< sAddress << " Test grades: " << grade1 << " , "<<grade2<<" , "<<grade3<<" , "<<

grade4<<" , "<<grade5<<" , "<<grade6<<" , "<<grade7<<" , "<<grade8<<" , "<<grade9<<" , "<<grade10<<" Gpa: "<<

sGpa<<endl;

}

};

void swap(int &value1,int &value2){

int temp = value1;

value1 = value2;

value2 = temp;

}

int partition(vector<student> stud,int start,int end){

int pivotValue,pivotIndex,mid;

mid=(start + end)/2;

swap(stud[start],stud[mid]);

pivotIndex = start;

pivotValue = stud[start].sId;

for(int scan = start+1;scan<=end;scan++){

if(stud[scan].sId<pivotValue){

pivotIndex++;

swap(stud[pivotIndex],stud[scan]);

}

}

swap(stud[start],stud[pivotIndex]);

return pivotIndex;

}

void quickSort(vector<student> stud,int start,int end){

int pivotPoint;

if(start<end){

//get the pivot point.

pivotPoint = partition(stud,start,end);

//sort the second sublist.

quickSort(stud,pivotPoint+1,end);

}

}

int main() {

ifstream file_;

file_.open("src/studentRec.txt");

if(!file_){

cout<<"error opening file";

return 1;

}

vector<student>studentRec;

student studen;

string str;

while(!file_.eof()){

file_>> studen.sName >> studen.sAddress >>studen.sId>>studen.grade1>>studen.grade2>>studen.grade3>>studen.grade4>>studen.grade5>>studen.grade6>>

studen.grade7>>studen.grade8>>studen.grade9>>studen.grade10>>studen.sTestavg>>studen.sGpa;

studentRec.push_back(studen);

}

cout<<"Here is the first set of unsorted student records: ";

for(unsigned int i = 0; i<studentRec.size()-1;i++){

studentRec[i].getInfo();

}

quickSort(studentRec,0,studentRec.size()-1);

cout<<" Here is first set of sorted student records: ";

for(unsigned int i=0;i<studentRec.size()-1;i++){

studentRec[i].getInfo();

}

return 0;

}

/////////////////////////////////////////////////////////////////////////////

output:

1 Johnny WonderWorld 1234 68 78 90 99 56 45 86 90 84 76 77 3.0 2 Chris CraddockAvenue 1989 90 90 90 90 90 85 86 95 89 93 86 3.0 3 Marc AquarenaSpringsDr 1275 90 90 90 90 90 90 95 95 96 96 92 4.0 4 Philip ConchoStreet 2006 80 80 80 80 80 85 85 85 85 85 82 3.60 5 Morgan WonderWorld 1021 70 70 70 70 70 65 65 65 65 65 67 2.0 6 Carol ThorpeLane 1589 90 90 90 90 90 90 95 95 96 96 92 4.0 7 Logan WonderWorld 1954 80 80 80 80 80 85 85 85 85 85 82 3.0 8 Mike CraddockAvenue 1689 68 78 90 99 56 45 86 90 84 76 77 3.0 9 Blake HopkinsDr 1902 80 80 80 80 80 85 85 85 85 85 82 3.0 10 Caitlyn MillStreet 1701 70 70 70 70 70 65 65 65 65 65 67 2.0 11 Bianca PostRoad 2316 80 80 80 80 80 85 85 85 85 85 82 3.0 12 Kristie GuadalupeStreet 1308 90 90 90 90 90 90 95 95 96 96 92 4.0 13 Howard ConchoStreet 2314 90 90 90 90 90 85 86 95 89 93 86 3.0 14 Jordan SessomRoad 1876 80 80 80 80 80 85 85 85 85 85 82 3.0 15 Trinity RanchRoad 2345 70 70 70 70 70 65 65 5 65 65 67 2.0 2 8 2

Explanation / Answer

The main problem in the code is if you look at the quicksort function you were only quicksorting only the second sublist and forgot to add the first sublist. For that add these two lines after this statement(pivotPoint = partition(stud,start,end);)

//sort the first sublist.
quickSort(stud,start,pivotPoint-1);

---------------------------------------------------------

**If you partition function and swap function are not working correctly, replace them with my version of parition and swap function code below

**The remaining code is same

void swap(int* first, int* second)

{

    int temp = *first;

    *first = *second;

    *second = temp;

}

int partition(vector<student> stud,int start,int end){

int pivotValue = std[end]; //Contains Pivot Value

int i = start - 1; //The index of smaller number

for(int j = start; j<=end-1; j++)

{

//Checks if the current element is smaller than or equal to pivot

if(stud[j].sId <= pivotValue)

{

i++; //To increment the index of the smaller number

swap(&stud[i].sId, &stud[j].sId);

}

swap(&stud[i+1].sId, &stud[end].sId);

}

return (i+1);

}

**Note: Can adding questions like these please give the contents of the test cases to test the code. For the above code I was unable to test it because the test case is given as a image not as text. Please do test the code after the adding the above bolded lines.