Hello Spiderman30,
First I will apologize for defining the original and then "BubbleSort" function to pass the array by reference. I should have changed that to be less confusing. I first did that when testing the function so I could see the entire array not just the first element of the array. In a function definition an array that is passed by value generally degrades to a pointer, so when debugging a program all I can see is the first element pointed to in the array. Passing by reference allows me to see every element of the array, but this is not necessary for your program.
In the following code I added some blank lines to make the code easier to read. A good habit to get into.
Line 3 defines MAXSIZE. The reason for doing this here is so the entire file can have access to this variable because it is a global variable. The second reason for doing this here is so that only one place has to be changed to change the size of the array and so you do not have to go through the entire program changing values because the size of the array has changed.
Lines 16 and 17 are the proper way to call the functions. Your way part may work, but using "[MXSIZE]" does not work. The use of the []s is incorrect and not needed in a function call.
The for loops with the function call is unnecessary. you are calling the sort functions eight times sorting a sorted array seven more times than needed. The point of the for loop is to print out the sorted array and nothing else.
Line 42 is the proper way to define the function. Using the pass by reference that I used is not needed. But it is OK if you leave it. It does not make any difference
What you call the variables in the function makes no difference, but it is better to use a name that reflects which array it is using. Looking at your two sort functions one would think that they are sorting the same array, but the function call says different. It still works the way it is because a function can use a different name because it is local to that function. Also different functions can use the same names for the same reason being local to that function.
The following code is your corrected code. I have made notes i the code and commented out what should not be there. I redid the "BubbleSort" function definition and proto type to what it should be just to keep it simple for now. You can delete any commented line if you need to or keep a copy of this for reference.
I would suggest that you understand how this code works before you try changing it trying for something different.
Should you have a need for a search function I would reefer bck to your original "FindIndexOfSmallest" function for ideas.
This code does compile and works. Be sure to read the comments and if you have any questions let me know.
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
|
#include <iostream>
const int MAXSIZE{ 8 };
void BubbleSort(int A[], int N);
void SelectionSort(int A[], int Size);
int main()
{
//int N{}; // <--- Not used in main. Also should initialize the variables. I used {}.
//int Size{}; // <--- Not used in main. Also should initialize the variables. I used {}.
int A[MAXSIZE] = { 50, 30, 66, 10, 168, 45, 99, 20 };
int B[MAXSIZE] = { 50, 30, 66, 10, 168, 45, 99, 20 };
BubbleSort(A, MAXSIZE); // <--- You call the function this way.
SelectionSort(B, MAXSIZE); // <--- You call the function this way.
std::cout << "\n The array: \n";
for (int i = 0; i < MAXSIZE; i++)
{
//BubbleSort((&A)[MAXSIZE], N); // <--- Do not use in the for loop.
std::cout << A[i] << std::endl;
}
std::cout << "\n Lowest number is: " << A[0] << std::endl;
std::cout << "\n The array: \n";
for (int i = 0; i < MAXSIZE; i++)
{
//SelectionSort((&B)[MAXSIZE], Size); // <--- Do not use in the for loop.
std::cout << B[i] << std::endl;
}
std::cout << "\n Lowest number is: " << B[0] << std::endl;
return 0;
}
void BubbleSort(int A[], int N) // <--- Proper function definition. "N" equals the value of MAXSIZE.
{
int temp; // <--- Should initialize to be safe, but it works.
bool Swap; // <--- Should initialize to be safe, but it works.
do
{
Swap = false;
for (int Count = 0; Count < N - 1; Count++)
{
if (A[Count] > A[Count + 1])
{
temp = A[Count];
A[Count] = A[Count + 1];
A[Count + 1] = temp;
Swap = true;
}
}
} while (Swap);
}
void SelectionSort(int A[], int Size) // <--- Size equals the value of MAXSIZE.
{
int startScan, minIndex, minValue;
for (startScan = 0; startScan < (Size - 1); startScan++)
{
minIndex = startScan;
minValue = A[startScan];
for (int index = startScan + 1; index < Size; index++)
{
if (A[index] < minValue)
{
minValue = A[index];
minIndex = index;
}
}
A[minIndex] = A[startScan];
A[startScan] = minValue;
}
}
|
Hope that helps,
Andy