i am having trouble with my selection sort functions, the SortByName Function an
ID: 3546568 • Letter: I
Question
i am having trouble with my selection sort functions, the SortByName Function and the SortByScore function, the rest of the program works great.
the SortByName function needs to alphabetize the names from A to Z
the SortByScore functions needs to list the students by their score from highest to lowest
both these functions need to be selection sort and not bubble sort.
please do not give me something other then selection sort!!
thank you!
#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>
using namespace std;
struct StudentType
{
string studentName;
int testScore;//Between 0 and 100
char grade;
};
void PrintNameHeader(ostream& out);
bool OpenInputFile(ifstream& inFile, string& infilename ); //OPEN input file
void Pause();// Pause
void ReadStudentData(ifstream& infile, StudentType student[], int& );// Read student info including first and last name and test score
void AssignGrades(StudentType student[], int);//assign grades to each student
int HighestScore(const StudentType student[], int );//Get the highest scores
void PrintNamesWithHighestScore(const StudentType student[], int);//Print name/s with highest Scores
void DisplayAllStudents(const StudentType student[], int);//Display all students
void GetLowHighRangeValues(const StudentType student[], int, int& , int&);//for example a student types 50 100 , it will get all students within that range
void DisplayStudentsInRange(const StudentType student[], int, int, int);// display students in that range
void SortStudentsByName(StudentType student[], int);// sort students by name
void SortStudentsByScore(StudentType student[], int);// sort students by test score highest to lowest
void FormatNameScoreGrade(ostream& out);
void EndOfList(ostream& out);
const int NUM_STUDENTS = 20;
int main()
{
ifstream infile;
string inFilename;
StudentType student[NUM_STUDENTS];
int numStudents = 0,
lowRange,
highRange;
char answer;
PrintNameHeader(cout);
if(!OpenInputFile(infile,inFilename)) return 1;
ReadStudentData(infile, student, numStudents);
infile.close();
AssignGrades(student, numStudents);
cout <<"List of All the Students in the class" << endl;
DisplayAllStudents(student, numStudents);
cout <<"There are " << numStudents <<" students in this class." << endl;
PrintNamesWithHighestScore(student, numStudents);
Pause();//Pause
do {
GetLowHighRangeValues(student, numStudents, lowRange, highRange);
DisplayStudentsInRange(student, numStudents, lowRange, highRange);
cout <<"Do you want to see another range of scores [y/n]?";
cin >> answer;
} while( answer == 'y' || answer == 'Y');
SortStudentsByName(student, numStudents);
Pause();//Pause
SortStudentsByScore(student, numStudents);
return 0;
}
//Function definitions
void PrintNameHeader(ostream& out)
{
//Display name header on screen
}
void FormatNameScoreGrade(ostream& out)
{
out << setw(10) << " Student Name" << setw(20) <<"Test Score" << setw(10) <<"Grade" << endl;
out <<" ------------------------------------------" << endl;
}
void EndOfList(ostream & out)
{
cout <<" " <<"End of List." << endl << endl;
}
bool OpenInputFile(ifstream& inFile, string& infilename)
{
cout << "Enter the name of the .txt file that you want to open for input. ";
cout << "Do not put spaces in the file name : ";
cin >> infilename;
cout << endl;
inFile.open(infilename.c_str());
if (inFile.fail())
{
cout << "Sorry, the input file " << infilename <<" was not found"<< endl;
return false;
}
cout << "Input file " << infilename << " is open for reading. ";
return true;
}
void Pause()
{
cout << endl;
cin.ignore(80, ' ');
cout<<"Please hit the enter key to continue... ";
cin.get();
}
void ReadStudentData(ifstream& infile, StudentType student[], int& numStudents)
{
string firstName, lastName;
int testScore;
int count = 0;
while(count < NUM_STUDENTS && infile >> firstName >> lastName >> testScore)
{
if( testScore >= 0 && testScore <=100)
{
student[count].studentName = lastName + ", " + firstName;
student[count].testScore = testScore;
count++;
}
}
numStudents = count;
}
void AssignGrades(StudentType student[], int numStudents)
{
int testscore;
char grade;
for(int i = 0; i < numStudents; i++)
{
testscore = student[i].testScore;
if(testscore >= 90 && testscore <=100)
grade = 'A';
else if(testscore >= 80 && testscore <90)
grade = 'B';
else if(testscore >= 70 && testscore <80)
grade = 'C';
else if(testscore >= 60 && testscore <70)
grade = 'D';
else
grade = 'F';
student[i].grade = grade;
}
}
int HighestScore(const StudentType student[], int numStudents)
{
int highest = student[0].testScore;
for(int i = 1; i < numStudents; i++)
{
if(highest < student[i].testScore) highest = student[i].testScore;
}
return highest;
}
void PrintNamesWithHighestScore(const StudentType student[], int numStudents)
{
int highest = HighestScore(student, numStudents);
cout << endl << "The following student(s) have the highest score" << endl << endl;
FormatNameScoreGrade(cout);
for(int i = 0; i < numStudents; i++)
{
if (student[i].testScore == highest)
{
cout << setw(20) << student[i].studentName << setw(10) << student[i].testScore << setw(10) << student[i].grade
<< endl;
}
}
cout << endl;
EndOfList(cout);
}
void DisplayAllStudents(const StudentType student[], int numStudents)
{
cout << endl;
FormatNameScoreGrade(cout);
for(int i = 0; i < numStudents; i++)
{
cout << setw(20) << student[i].studentName << setw(10) << student[i].testScore << setw(10) << student[i].grade << endl;
}
cout << endl;
EndOfList(cout);
}
void GetLowHighRangeValues(const StudentType student[], int numStudents, int& lowRange, int& highRange)
{
cout <<"Enter two values from 0-100 for the range of test scores you want to view: ";
cin >> lowRange >> highRange;
while ( (lowRange < 0 )|| (highRange < 0) || (lowRange > highRange) || (lowRange > 100) || (highRange > 100) )
{
cout <<"Please re-enter 2 values of test scores that you want to view from ";
cout <<"within the range 0-100: ";
cin >> lowRange >> highRange;
cout << endl;
}
}
void DisplayStudentsInRange(const StudentType student[], int numStudents, int lowRange, int highRange)
{
cout << endl << "List of students with scores in the range " << lowRange << " to " << highRange << endl << endl;
FormatNameScoreGrade(cout);
for(int i = 0; i < numStudents; i++)
{
if(student[i].testScore >= lowRange && student[i].testScore <= highRange)
{
cout << setw(20) << student[i].studentName << setw(10) << student[i].testScore << setw(10) << student[i].grade << endl;
}
}
cout << endl;
EndOfList(cout);
}
void SortStudentsByName(StudentType student[], int numStudents)
{
int minIndex;
StudentType temp;
for(int startScan = 0;startScan < numStudents - 1;startScan++)
{
minIndex = startScan;
temp = student[startScan];
for(int index = startScan + 1; index < numStudents;index++)
{
if(student[startScan].studentName > student[index].studentName)
{
temp = student[index];
minIndex = index;
}
}
student[minIndex] = student[startScan];
student[startScan] = temp;
}
cout << endl;
cout << "List of Students sorted Alphabetically: "<< endl;
DisplayAllStudents(student, numStudents);
}
void SortStudentsByScore(StudentType student[], int numStudents)
{
int startScan,
minIndex,
lowest;
for (startScan = 0; startScan < (NUM_STUDENTS-1); startScan++)
{
minIndex = startScan;
lowest = student[0].testScore;
for ( int index = startScan+1; index < NUM_STUDENTS; index++)
{
if( student[index].testScore < lowest)
{
lowest = student[index].testScore;
minIndex = index;
}
}
student[minIndex].testScore = student[startScan].testScore;
student[startScan].testScore = lowest;
cout <<"List of Students sorted by Score from Highest to Lowest" << endl;
DisplayAllStudents(student, numStudents);
}
}
Explanation / Answer
void SortStudentsByName(StudentType student[], int numStudents)
{
int minIndex;
for(int startScan = 0;startScan < numStudents - 1;startScan++)
{
minIndex = startScan;
for(int index = startScan; index < numStudents;index++)
{
if(student[startScan].studentName.compare(student[index].studentName)>0)
minIndex = index;
}
if(minIndex!=startScan)
{
StudentType temp = student[minIndex];
student[minIndex] = student[startScan];
student[startScan] = temp;
}
}
cout << endl;
cout << "List of Students sorted Alphabetically: "<< endl;
DisplayAllStudents(student, numStudents);
}
void SortStudentsByScore(StudentType student[], int numStudents)
{
int startScan,
minIndex,
lowest;
for (startScan = 0; startScan < (numStudents-1); startScan++)
{
minIndex = startScan;
for ( int index = startScan; index < numStudents; index++)
{
if( student[index].testScore>student[minIndex].testScore)
minIndex = index;
}
if(minIndex!=startScan)
{
StudentType temp = student[minIndex];
student[minIndex] = student[startScan];
student[startScan] = temp;
}
}
cout <<"List of Students sorted by Score from Highest to Lowest" << endl;
DisplayAllStudents(student, numStudents);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.