Bubble Sorting Code Not Outputting As Desired

Hello, below is my code that currently runs.

However, it is outputting just "Bubble Sorter" and nothing else.
What is wrong with my code?

Thank you!

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
#include <iostream>
#include <stdlib.h>
#include <time.h> // Used to Generate Random Seed at an Exact Time Instance of Run
using namespace std;

void addRandomNumbers();
void headerSpaces();
void headerStars();
void arrayOutOfOrder();
void bubbleSort();
void arrayInOrder();

int main() 
{
	srand (time(NULL)); // Creates Random Seed, rand()

	void addRandomNumbers();
	void headerSpaces();
	cout << "Bubble Sorter" << endl << endl;
	void headerStars();
	
	void arrayOutOfOrder();
	void bubbleSort();
	void arrayInOrder();
	void headerStars();

	cin.get();
	cin.ignore();
	return 0;

} // End Int Main

void addRandomNumbers()
	{
		int bubbleArray[10];
		const int SIZE = 10;
		
		for (int randomNumbers = 0; randomNumbers < SIZE; randomNumbers++) // Puts Random Numbers into Array
		bubbleArray[randomNumbers] = rand();
	
		cout << endl;
	}

void headerSpaces()
	{
		for (int headerSpaces = 0; headerSpaces < 23; headerSpaces++)
		cout << " ";
	}

void headerStars()
	{
		for (int headerStars = 0; headerStars < 60; headerStars++)
		cout << "*";
	}

void arrayOutOfOrder()
	{
		const int SIZE = 10;
		int bubbleArray[10];
		cout << endl << endl << "Array Out of Order" << endl; 
			
		for (int randomNumbers = 0; randomNumbers < SIZE; randomNumbers++)
		cout << bubbleArray[randomNumbers] << " ";
	}

void bubbleSort()
	{
		const int SIZE = 10; // Initializes Predefined Array Size of 10
		int bubbleArray[10];

		for (int i = 0; i < SIZE; i++) 
			{
				for (int term = SIZE - 1; term >= i; term--) 
					{
						if (bubbleArray[term-1] > bubbleArray[term]) // If Previous Term is Bigger Than Current Term, Swaps
							{   
								int randomNumbers = bubbleArray[term-1]; // Puts Bigger Term into randomNumbers variable
								bubbleArray[term-1] = bubbleArray[term]; // Reidentifies Smaller Term as Bigger Term
								bubbleArray[term] = randomNumbers; // Replaces Bigger Number into Original Smaller Number's Position
							}
								// This loop occurs until fully reordered
					}
			}
	}
		
void arrayInOrder()
	{
		const int SIZE = 10; // Initializes Predefined Array Size of 10
		int bubbleArray[10];
		cout << endl << endl << "Array In Order" << endl; // Once fully reordered, the following cout's
		
		for (int randomNumbers = 0; randomNumbers < SIZE; randomNumbers++)
		cout << bubbleArray[randomNumbers] << " ";
		
		cout << endl << endl;
		
	}
Last edited on
I Remeber making a buble sort Algrothrim at school first input the number from the range then rand function to generate the numbers got smallest to biggest
I did this in python
There is something wrong with me using functions but I do not know what.
The code itself worked before I made these functions.
You're never filling the arrays with any values (other than the one in addRandomNumbers)
Keep in mind every time you write int bubbleArray[10]; you are creating a new array.
Are you at least getting the printout of stars?
@Lachlan Not even that. All that is showing is "Bubble Sorter".
I have to keep saying int bubbleArray[10] though or it won't compile. Ugh, so confusing.
Last edited on
Oh, obvious problem is obvious. When calling a function don't put the type (ie. don't put void in front of function calls) putting it there means you're declaring a new function.
@Lachlan

Well, that helped nearly solve everything. Except now, my arrays all have the same numbers in that, presorted and postsorted.

Array Out of Order:
-858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460

Array In Order:
-858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460

But yea, wow for that mistake of putting void in main haha
Last edited on
You should make int bubbleArray[10] global (put it before main), this makes it so all functions can 'see' the same array. This is largely frowned upon (including by me) as there are better (safer) ways to do global objects and variables, but you'll learn about that magic later on.
Same goes for SIZE that's a prime candidate to be global.
They aren't getting sorted because you're not putting anything _in_ the arrays. You're only filling the first one, (this would be solved if you made _one_ global array)
Last edited on
@Lachlan Well, putting int bubbleArray[10] and SIZE global did the trick. Thank you.
Last edited on
Glad I could help, that's what the forums are for.
How can I avoid global variables? I know I have to pass the array as a parameter but I'm not quite sure how to do so with my 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
[code]
#include <iostream>
#include <stdlib.h>
#include <time.h> // Used to Generate Random Seed at an Exact Time Instance of Run
using namespace std;

void addRandomNumbers();
void headerSpaces();
void headerStars();
void arrayOutOfOrder();
void bubbleSort();
void arrayInOrder();

//Global Variables
const int SIZE = 10; // Initializes Predefined Array Size of 10
int bubbleArray[10];

int main() 
{
	srand (time(NULL)); // Creates Random Seed, rand()

	addRandomNumbers();
	headerSpaces();
	cout << "Bubble Sorter" << endl << endl;
	headerStars();
	
	arrayOutOfOrder();
	bubbleSort();
	arrayInOrder();
	headerStars();

	cin.get();
	cin.ignore();
	return 0;

} // End Int Main

void addRandomNumbers()
	{
		for (int randomNumbers = 0; randomNumbers < SIZE; randomNumbers++) // Puts Random Numbers into Array
		bubbleArray[randomNumbers] = rand();
	
		cout << endl;
	}

void headerSpaces()
	{
		for (int headerSpaces = 0; headerSpaces < 23; headerSpaces++)
		cout << " ";
	}

void headerStars()
	{
		for (int headerStars = 0; headerStars < 60; headerStars++)
		cout << "*";
	}

void arrayOutOfOrder()
	{
		cout << endl << endl << "Array Out of Order" << endl; 
			
		for (int randomNumbers = 0; randomNumbers < SIZE; randomNumbers++)
		cout << bubbleArray[randomNumbers] << " ";
	}

void bubbleSort()
	{

		for (int i = 0; i < SIZE; i++) 
			{
				for (int term = SIZE - 1; term >= i; term--) 
					{
						if (bubbleArray[term-1] > bubbleArray[term]) // If Previous Term is Bigger Than Current Term, Swaps
							{   
								int randomNumbers = bubbleArray[term-1]; // Puts Bigger Term into randomNumbers variable
								bubbleArray[term-1] = bubbleArray[term]; // Reidentifies Smaller Term as Bigger Term
								bubbleArray[term] = randomNumbers; // Replaces Bigger Number into Original Smaller Number's Position
							}
								// This loop occurs until fully reordered
					}
			}
	}
		
void arrayInOrder()
	{
		cout << endl << endl << "Array In Order" << endl; // Once fully reordered, the following cout's
		
		for (int randomNumbers = 0; randomNumbers < SIZE; randomNumbers++)
		cout << bubbleArray[randomNumbers] << " ";
		
		cout << endl << endl;
		
	}
Last edited on
I know I have to pass the array as a parameter but I'm not quite sure how to do so with my code.


You can do it like this. Note the two dimensional array needs a size specified in the parameters. If you go above single dimensional arrays, you will have to include sizes for every dimension after the first.

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
#include <iostream>

using namespace std;

void FunctionThatAcceptsArrays(int nArray[], int nArray2[][3], const int nSize)
{
	
	for (int i = 0; i < nSize; i++)
	{
		cout << "FirstArray" << nArray[i] << endl;

		for (int j = 0; j < nSize; j++)
		{
			cout << "SecondArray" << nArray2[i][j] << endl;
		}
	}
	
}

int main()
{
	const int nSize = 3;
	int bubbleArray[3] = {0, 1, 2}; //
	int bubbleArray2[3][3] = {{0, 1, 2}, {3, 4, 5}, {6, 7, 8}};

	FunctionThatAcceptsArrays(bubbleArray, bubbleArray2, nSize);

	return 0;
}
Last edited on
I'm not sure why the old put-main-first method persists so much -- it was moronic to begin with.

Put main() last. Everything else goes before it.
It obviates the need for all kinds of repetitive prototyping and the errors that go with it. (An overload for beginners, methinks.)

You could declare your array as global, especially for a small program like this, but it would be better just to learn to use argument passing.

Declare your array (and it's size) in main(), then pass it as argument to your functions.

Here's your code with those two modifications:

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
#include <iostream>
#include <stdlib.h>
#include <time.h> // Used to Generate Random Seed at an Exact Time Instance of Run
using namespace std;

void addRandomNumbers( int bubbleArray[], int size )
	{
		for (int randomNumbers = 0; randomNumbers < size; randomNumbers++) // Puts Random Numbers into Array
			{
				bubbleArray[randomNumbers] = rand();
			}
	
		cout << endl;
	}

void headerSpaces()
	{
		for (int headerSpaces = 0; headerSpaces < 23; headerSpaces++)
		cout << " ";
	}

void headerStars()
	{
		for (int headerStars = 0; headerStars < 60; headerStars++)
		cout << "*";
	}

void arrayOutOfOrder( int bubbleArray[], int size )
	{
		cout << endl << endl << "Array Out of Order" << endl; 
			
		for (int randomNumbers = 0; randomNumbers < size; randomNumbers++)
			{
				cout << bubbleArray[randomNumbers] << " ";
			}
	}

void bubbleSort( int bubbleArray[], int size )
	{
		for (int i = 0; i < size; i++) 
			{
				for (int term = size - 1; term >= i; term--) 
					{
						if (bubbleArray[term-1] > bubbleArray[term]) // If Previous Term is Bigger Than Current Term, Swaps
							{   
								int randomNumbers = bubbleArray[term-1]; // Puts Bigger Term into randomNumbers variable
								bubbleArray[term-1] = bubbleArray[term]; // Reidentifies Smaller Term as Bigger Term
								bubbleArray[term] = randomNumbers; // Replaces Bigger Number into Original Smaller Number's Position
							}
								// This loop occurs until fully reordered
					}
			}
	}
		
void arrayInOrder( int bubbleArray[], int size )
	{
		cout << endl << endl << "Array In Order" << endl; // Once fully reordered, the following cout's
		
		for (int randomNumbers = 0; randomNumbers < size; randomNumbers++)
			{
				cout << bubbleArray[randomNumbers] << " ";
			}
		
		cout << endl << endl;
		
	}

int main() 
	{
		srand (time(NULL)); // Creates Random Seed, rand()

		const int SIZE = 10;
		int bubbleArray[SIZE];

		addRandomNumbers(bubbleArray,SIZE);
		headerSpaces();
		cout << "Bubble Sorter" << endl << endl;
		headerStars();
	
		arrayOutOfOrder(bubbleArray,SIZE);
		bubbleSort(bubbleArray,SIZE);
		arrayInOrder(bubbleArray,SIZE);
		headerStars();

		cin.get();
		return 0;

	} // End Int Main 

A couple of other things:

(1) Notice that you have code to print the array in two different places (arrayInOrder() and arrayOutOfOrder()). Since what you are doing is just printing the array, make yourself a function (called something like printArray()) to do it. Then you can call it easily enough from main:

1
2
3
4
5
6
7
8
int main()
	{
		...

		cout << "Array Out Of Order\n";
		printArray(bubbleArray,SIZE);

		...

(2) The way you name things is the way you think about them. Name objects (like data) by what they ARE, as specifically as you can. Name functions by what they DO.

Continuing from (1), "arrayInOrder" sounds like an object. Looking at the name doesn't give you any clue that it actually prints the array to the console. It also implies that the array must be in order -- but it does nothing to guarantee that.

Hence my rename to "printArray". It tells you exactly what it does, nothing more. (The ordered-ness of the array is irrelevant.)

(3) You have been more or less consistent with indentation and the like, but you have goofed up in a couple of spots.

Hope this helps.
Topic archived. No new replies allowed.