Programming requires you to tell the compiler
exactly what you want it to do. It isn't like person-to-person instructions. So you need to understand exactly what the different things in your program do.
Right now GetArray is populating the array declared at line 23. But since that's a local variable, it disappears when GetArray returns to main(). So you should declare the array inside main and pass it to GetArray().
1 2 3 4 5 6 7 8 9 10 11 12 13
|
// Read "a" integers from cin and put them into "array"
int GetArray(int a, int array[]) {
...
}
...
int main()
{
int a;
int array[1000];
cin >> a;
GetArray(a, array);
...
}
|
Make sure you understand what's going on here. You will need to change your implementation of GetArray to use the "a" parameter that's passed in. In other words, GetArray() should not read
a
, it should just popualate the array.
Next, I would creat a separate function to print out the array:
1 2 3 4 5
|
// Print the values in array, which has "size" elements in it.
void PrintArray(int size, int array[])
{
....
}
|
You'll need this anyway, but the for now, you can use it to make sure that GetArray is working. Just call PrintArray right after calling GetArray. This will tell you if you're reading the numbers correctly. Trust me, I've seen plenty of people go nuts trying to get their sorting routine to work, only to discover that they weren't reading the input correctly in the first place.
Now the BubbleSort() function should
only sort the numbers. Right now BubbleSort is trying to read numbers from cin also.
You need to tell BubbleSort how many numbers are in the array. Also, it shouldn't return anything. It just sorts the array. So it should be
1 2 3
|
void BubbleSort(int size, int array[]) {
....
}
|
Print the result of the sorted array in main, right after you call BubbleSort(). For testing, you might want to print the array after each pass through the loop inside BubbleSort also. You can remove that code once the sort is working correctly.