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?

Sort the two arrays in parallel (every move you make on the scores array, make on the names array so they will line up).

Go through a sort by hand and translate those steps involved into a set of loops that will do the same thing. A bubble sort will be the same thing as you would do by hand if you were forced to swap only one element at a time.

A hint, your code on line 60 replaces totals with totals[i+1] but leaves totals[i+1] alone, so there will be 2 copies.

Would it be something like this:

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 i, Largest=0, n;

	if (totals[i] > Largest)
	{
		Largest = totals[i];
		n=i; //Saves the storage spot where Largest was found.
	}

	return 0;
}

I have no idea how to incorporate the names with the totals :(

You don't want to find the largest. You have to swap the elements of the array to sort it, what you have only gets you the maximum.

some p-code:

For (over all the elements except the last)
   For(from the one after the last element sorted to the second to last)
       if(this element is less than the one next to it)
           Swap the two (hint, you need a temporary variable) since we want the  
           higher numbers on the left hand side for decending.

If you search for bubble sort on the net there should be sites that have animated examples, maybe one of those will help you visualize it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.