I've written a program that takes a file, students.txt, which is constructed as such:
Student1 name
test1 score
test2 score
test3 score
Student2 name
test1 score
test2 score
test3 score
etc.
The program has three functions, one that reads the file, one the prints the file and one that sorts the file in decending order by test score total. For example:
Mark
93
100
40
Kevin
80
100
89
Since Kevin has the highest test score of 269, and Mark has the second highest score of 233, the output is supposed to look like this:
Kevin
269
Mark
233
I'm writing the final function, the one that is supposed to do the decending order. We're supposed to use a bubble sort, and I'm not quite sure how to do this. This is what I have so far.
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <conio.h>
using namespace std;
int ReadArrays(string names[], int totals[]);
//The ReadArrays function receives the names and totals from the file student.txt. It returns the student count to main.
int PrintArrays(string names[], int totals[], int count);
//The PrintArrays function receives the names, totals and student count from the file students.txt. It prints the file.
int SortArrays(string names[], int totals[], int count);
//The SortArrays function receives the names, totals and student count from the file students.txt. It prints the names and totals in decending order by totals and prints the individual score of each student.
int main()
{
string names[50]; //Array of strings for each student name in the file.
int totals[50], count; //Array for each of the totals in the file. Count is a variable used to count all of the names in the file.
count = ReadArrays(names,totals);
SortArrays(names,totals,count);
PrintArrays(names,totals,count);
getch();
return 0;
}
int ReadArrays(string names[], int totals[])
//The ReadArrays function receives the names and totals from the file student.txt. It returns the student count to main.
{
ifstream StudentFile;
StudentFile.open("Desktop://student.txt"); //Opens the file student.txt.
int test1, test2, test3, i; //test1, test2 and test3 are variables for the three test scores. i is used as a counter variable.
i=0;
while(getline(StudentFile,names[i])) // process students until end of file.
{
StudentFile >> test1; // assume that if there is a name, three test scores follow.
StudentFile >> test2;
StudentFile >> test3;
totals[i] = test1 + test2 + test3; //add the three test scores and store in the totals array.
i++;
StudentFile.ignore(10,'\n'); // gets the carriage return after third number.
}
return i;
}
int SortArrays(string names[], int totals[], int count)
//The SortArrays function receives the names, totals and student count from the file students.txt. It prints the names and totals in decending order by totals and prints the individual score of each student.
{
int LastElement;
int i=0;
LastElement = count - 1;
// Go through the array once, swapping anything that's out of order
if (totals[i] > totals[i+1]) // if these are out of order
totals[i] = totals[i+1];
return 0;
}
int PrintArrays(string names[], int totals[], int count)
//The PrintArrays function receives the names, totals and student count from the file students.txt. It prints the file.
{
int i;
for (i=0; i < count; i++) //set i equal to zero. Loop as long as i is less than count, then incriment i.
{
cout<<names[i];
cout<<totals[i];
}
return 0;
}
Can someone help me construct the bubble sort in my SortArrays function?