1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
|
#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];
int totals[50], count;
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;
i=0;
while(getline(StudentFile,names[i])) // process students until end of file
{
StudentFile >> test1; // assume that if we got a name, 3 test scores DO follow
StudentFile >> test2;
StudentFile >> test3;
totals[i] = test1 + test2 + test3;
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.
{
Start
For LastElement = Length - 1 down to 1 by -1
// Go through the array once, swapping anything that's out of order
Loop with i going from 0 to LastElement-1
If List[i] > List[i+1] // if these are out of order
Swap List[i] and List[i+1]
End if
End loop
// Last item is in position
End loop
Done
}
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++)
{
cout<<names[i];
cout<<totals[i];
}
return 0;
}
|