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.