Function to sort numbers showing wrong output, can't find the error

Hi guys, I got an assigment for cpp and it asks me to create a program to sort a list of integers, long values and double values using function overloaded, I have done it but it prints a different number that's not in my list and I can't figure out why, follow 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
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
93
94
95
96
97
98
99
100
101
102
103
104
 #include <iostream> 
#include <cmath>
#include <iomanip> 
 
using namespace std; 
  
void sortingNumbers (int valListInt[]){

	int intNum1,intNum2;
	int sorting;

	for (intNum1 = 0; intNum1 < 10; intNum1++)
	for (intNum2 = 0; intNum2 < 10; intNum2++)
	
		if (valListInt[intNum2] > valListInt[intNum2 + 1]){
			sorting = valListInt[intNum2];
			valListInt[intNum2] = valListInt[intNum2 +1];
			valListInt[intNum2 + 1] = sorting;
		}

	for (intNum1 = 0; intNum1 < 10; intNum1++)
		cout << valListInt[intNum1] << endl;
}

void sortingNumbers (long valListLong[]){

	int longNum1,longNum2;
	long sorting;

	for (longNum1 = 0; longNum1 < 10; longNum1++)
	for (longNum2 = 0; longNum2 < 10; longNum2++)
	
		if (valListLong[longNum2] > valListLong[longNum2 + 1]){
			sorting = valListLong[longNum2];
			valListLong[longNum2] = valListLong[longNum2 + 1];
			valListLong[longNum2 + 1] = sorting;
		}

	for (longNum1 = 0; longNum1 < 10; longNum1++)
		cout << valListLong[longNum1] << endl;
}

void sortingNumbers (double valListDouble[]){

	int doubleNum1,doubleNum2;
	double sorting;

	for (doubleNum1 = 0; doubleNum1 < 10; doubleNum1++)
	for (doubleNum2 = 0; doubleNum2 < 10; doubleNum2++)
	
		if (valListDouble[doubleNum2] > valListDouble[doubleNum2 + 1]){
			sorting = valListDouble[doubleNum2];
			valListDouble[doubleNum2] = valListDouble[doubleNum2 + 1];
			valListDouble[doubleNum2 + 1] = sorting;
		}

	for (doubleNum1 = 0; doubleNum1 < 10; doubleNum1++)
		cout << valListDouble[doubleNum1] << endl;
}

void main()
{
	int userMenu;

	bool stoploop = false;
	
	while (!stoploop){

	cout << "Which numbers do you want to see sorted?\n";
	cout << "1.	Integers\n" << "2.	Long Doubles\n" << "3.	Doubles\n";
	cout << "Enter your choice: ";
	cin >> userMenu;

	if (userMenu == 1){
			int valListInt[] = {23,2,34,23,43,22,32,32,43,34};
			sortingNumbers (valListInt);
			cout << "Continue (y/n)? "; 
			char userAnswer; 
			cin >> userAnswer; 
				if (toupper(userAnswer) != 'Y') 
					stoploop = true;

		} else if (userMenu == 2){
			long valListLong[] = {7000,15,34,2373645,43,22,9392929294,46,32,111143};
			sortingNumbers (valListLong);
			cout << "Continue (y/n)? "; 
			char userAnswer; 
			cin >> userAnswer; 
				if (toupper(userAnswer) != 'Y') 
					stoploop = true;

		} else if (userMenu == 3){
			double valListDouble[] = {23.3847239,2.3974,34.183734,23.0,43.36381,22.3,32.0,32.1919,43.938363,34.38364};
			sortingNumbers (valListDouble);
			cout << "Continue (y/n)? "; 
			char userAnswer; 
			cin >> userAnswer; 
				if (toupper(userAnswer) != 'Y') 
					stoploop = true;
		}

	}
	system("pause");
}


It prints a negative value if I put 1 for integers and different numbers when I choose the other options.

Thanks for the help :)

Gui
Last edited on
Look at the nested for loops in your sorting functions. There's several problems there. I'd recommend stepping through them with a debugger and watching all your index values to find what's wrong. More specifically, pay attention to what's happening when intNum2 = 9.
Last edited on
hello

problem is in your bubble sort functions .
you must choose loop indexes correctly .

lock at this 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
// Example program
#include <iostream>
#include <string>

void BubbleSort(int temp[], int len);// prototype

int main()
{
  int arr[]={21,32,65,97,4,65,13,48,98,
             9898,65,4787,3,1,3,1254,987,
             65,120,111,1,4,2,987,45,6,15};// unsorted array
  // len is 27
  BubbleSort(arr,27);
}
void BubbleSort(int temp[], int len)
{
    for( int i = len-1 ; i > 0 ; i-- ){
        for( int j = 0 ; j < i ; j++ ){
            if(temp[j]>temp[j+1])
            {
                std::swap(temp[j],temp[j+1]);
            }
        }
    }
    for(int i=0 ; i< len ; i++){
        std::cout<<temp[i]<<" ";
    }
}

unsorted array :
21 32 65 97 4 65 13 48 98 9898 65 4787 3 1 3 1254 987 65 120 111 1 4 2 987 45 6 15


output :
1 1 2 3 3 4 4 6 13 15 21 32 45 48 65 65 65 65 97 98 111 120 987 987 1254 4787 9898 



Oh yeah I got it haha OMG took me so long to understand that.

Fixed the problem already, thanks for your help guys!

Cheers :)
Topic archived. No new replies allowed.