Help with bubble sort

I need help with my bubble sort algorithm. I'm not sure if i've coded it correct, but I want to sort the inputted array values in ascending order, and I want to output that. The code for this is in it's own function.

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
82
83
84
85
86
87
88
89
90
91
92
  #include <iostream>
#include <iomanip>

using namespace std;

void programInfo();
void inputList(double arr[], int &count);
void printList(double arr[], int n, int &count);
void sortAscending(double arr[], int n, int &count);

int main()
{
	double endProgram = 0;
	const int size = 30;
	double arr[size];
	int count = 0;
	int n = 0;
	while(endProgram != -1 || endProgram != -1.0)
	{
		programInfo();
		inputList(arr, count);
		printList(arr, n, count);
		sortAscending(arr, n, count);
		cout << "\n";
		cout << "Run program again? Enter -1 or -1.0 to end program."<< endl;
		cin >> endProgram;
			if (endProgram == -1 || endProgram == -1.0)
			{
				cout << "Thank you for using my program." << endl;
			}
	}
}

void programInfo()
{
	 cout << "Vishant's Statistical Calculator." << endl;
	 cout << "Please follow instructions carefully." << endl;
	 cout << "Enter one value at a time up to 30 values." << endl;
	 cout << "You must enter valid data or program will not work." << endl;
	 cout << "Enter -1 to signal end of data (-1 or -1.0)" << endl;
}

void inputList(double arr[], int &count)
{
	count = 0;
	char answer;
	cout <<"Input at least three values minimum, thirty values maximum." << endl;
	while (count < 30)
	{
		cout <<"Please enter a value." << endl;
		cin >> arr[count];
		count++;
		if (count == 3)
		{
			cout <<"Do you want to stop inputting values? (y/n)" << endl;
			cin >> answer;
			if (answer == 'y')
			{
				break;
			}
		}
	}
}

void printList(double arr[], int n, int &count)
{
	
	cout <<"Here is the list of values entered:" << endl;
	for (n = 0; n < count; n++)
		{
			cout << setw(8) << arr[n];
		}
}

void sortAscending(double arr[], int n, int &count)
{
	double temp;
	for (n = 0; n < count; n++)
	{
		for (int i = 0; i < count - 1; i++)
		{
			if(arr[i] < arr[n])
               {
                   temp = arr[n];
                   arr[n] = arr[i];
                   arr[i] = temp;
               }
        }
	}
}

bump
closed account (SECMoG1T)
Hi check it here

http://www.cplusplus.com/forum/beginner/147214/
that link is still confusing... there's a ton of replies and people arguing over "correct" code
Here is some sample code that sorts last name, first name.
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
#include<iostream>
#include<string>
using namespace std;

class student 
{
public:
	student(string last, string first, double gpa) : last(last), first(first), gpa(gpa){}
	string last;
	string first;
	float gpa;
	student() {};
};
void sort(student arr[], int n);

 main() 
{
	const int ARR_SIZE = 50;
	student arr[ARR_SIZE];
	int count=0;

	string last, first;
	double gpa;

	cin>>last;
	while(cin)
	{cin >> first;
	cin >> gpa;
	arr[count++] = student(last, first, gpa);
	cin >> last;
	}
	sort(arr, count);

	for(int i=0; i < count; i++)
		cout<<arr[i].last<< " " << arr[i].first<< " "<< arr[i].gpa<<endl;
}
void sort(student arr[], int n) 
{
	for (int last = n-1; last>0; last --)
		for (int j=0; j< last; j++)
			if(arr[j].last > arr[j+1].last || 
				arr[j].last == arr[j+1].last  && arr[j].first > 
				arr[j+1].first)
			{
				student temp = arr[j];
				arr[j] = arr[j+1];
				arr[j+1] = temp;
			}
}
Last edited on
thanks. i have my sort working but i have one more problem with code. In my standard deviation function, i don't want the user to input new values, i want the values to be from the array previously entered. How would I change this portion of the 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
 double stdDeviation(double arr[], int n, int &count)
{
	int i;
	double mean, sd, var, dev, sum = 0.0,
	sdev = 0.0;

	cout << "Please enter values again." << endl;
	for (i = 1; i <= count; ++i)
	{
		cin >> arr[i];
		sum = sum + arr[i];
	}

	mean = sum / count;

	for (i = 1; i <= count; ++i)
	{
		dev = (arr[i] - mean)*(arr[i] - mean);
		sdev = sdev + dev;
	}

	var = sdev / (count - 1);
	sd = sqrt(var);

	return sd;
}
Topic archived. No new replies allowed.