Bubble sort function

I'm working on a function that uses the bubble sort. Our teacher gave us this to show us how it works:

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
Variables: List is an array (index values start at 0 and go up to Length-1)
           Length is the number of elements in list
           Greatest is the "largest element so far" during each pass
           IndexOfGreatest, i, and LastElement are array indices

Start
 
  For LastElement = Length - 1 down to 1 by -1 

         Greatest = List[0] // start by assuming element 0 is the largest
         IndexOfGreatest = 0

          // if a larger value is found, remember the value and its index
	  For  i = 1 to LastElement
               If List[i] is greater than Greatest then 
                     Greatest = List[i]
                     IndexOfGreatest = i
               End If
          End loop

          // IndexOfGreatest now has index of largest element
          Swap List[LastElement] and List[IndexOfGreatest]

  End loop
Done


Now, my question is, since this is the first time i've used bubble sort, do I change the variable names to what I have in my code? This is what I have for code:

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;
}
That is not C++, it is psuedocode. This means you have to look at the psuedocode and translate it yourself into C++.
Okay, that's what I thought. I will try to translate it and post what I come up with.
I'm really confused...we have to put the student names and totals in decending order
ex: totals[0] would be the highest score and the student name that correstponds with that score should also be printed at the top of the list...

1
2
3
4
5
6
7
8
9
10
11
12
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, Length, i;
	LastElement = Length - 1;
	  
    // Go through the array once, swapping anything that's out of order
    if(i=0;i<LastElement)
    if (totals[i] > totals[i+1])  // if these are out of order
        totals[i] = totals[i+1];

}
I now have this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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 Flag = 1; // set Flag to 1 just to pass the loop test the first time.
	int i, j;

	do
	{
		for(i=0;i<count;i++)
		{
			if(totals[i] > totals[i+1])
			totals[i+1] = j;
			j=i;//Saves the storage spot from being over-written.
		}
	}
	while (Flag = 0);
			
	return 0;
}
That do while statement will only ever execute once, since you will never set flag to 0.
You should initially set flag to 0, then set it to one when the array is sorted.
Your for loop should loop to count - 1,because otherwise totals[i+1] will access outside the array on the last iteration.
Last edited on
Topic archived. No new replies allowed.