Hello studentlearningcplusplus,
Some things to help get you started.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
using namespace std;
constexpr int MAXSIZE{ 10 };
void BubbleSort(int a[], int p)
{
cout << "pass " << p << ":";
for (int i = 0; i < MAXSIZE; i++)
{
cout << a[i] << " ";
}
cout << endl;
}
|
You can change "MAXSIZE" to anything you like, but I would keep the capital letters. It helps to remind you that it is a constant variable and can not be changed.
You call the function "BubbleSort", but all it does is print to the screen. It should be called "Print" or "Display" because that is what you actually do in the function.
If you want the function to sort then move the nested for loops in "main" to the function.
In your updated function you added
cin.ignore();
. Since you do not have a
cin >> something;
I am curios as to how this fixed a problem and what the problem was in the first place?
In "main"
srand(static_cast<unsigned int>(time(nullptr)));
is better written this way. "srand()" takes an "unsigned int" as its seed, but "time" does not return an "unsigned int". I believe from C++11 on "nullptr" is the better choice, but (0) and "NULL" still work. This video is worth watching to better understand the problems with using "rand()".
https://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful
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
|
int main()
{
//srand(time(0));
srand(static_cast<size_t>(time(nullptr)));
int a[MAXSIZE];
for (int i = 0; i < MAXSIZE; i++)
{
a[i] = rand() % 100 + 1;
}
for (int i = 0; i < MAXSIZE - 1; i++)
{
for (int j = 0; j < 10 - i - 1; j++)
{
if (a[j] > a[j + 1])
{
int t = a[j];
a[j] = a[j + 1];
a[j + 1] = t;
}
}
BubbleSort(a, i);
}
|
You are good down to the sorting loops.
The outer for loop is OK. And notice the change I made.
The inner for loop is a problem as is the if statement. Just thinking about it
10 - i - 1
makes me think that as "i" increases "j" will not check the entire array. The other problem is "j" should start at "i" + 1
for (int j = i + 1
. This way "i" is where you are at and "j" is the next element in the array. Then the if condition would be
(a[i] > a[j])
. Now you are comparing 2 different elements of the array until the outer loop is finished. And since the outer loop stops 1 short of the end "j" will never go past the end of the array.
for (int j = i + 1; j < MAXSIZE; j++)
.
After that if you want to keep the function "BubbleSort" then move these for loops to that function and create a new function to print or display the array.
To your second post.
If you want the program to create the array then print out the unsorted array then tell the program to do so. The idea would be:
for loop to give the array numbers.
print array (unsorted array).
sort array.
print array (sorted array).
Other than testing there is no reason to print the array during the sort function.
At the end of main I use this code to pause the program before the window closes.
1 2 3 4 5 6 7
|
// A fair C++ replacement for "system("pause")". Or a way to pause the program.
// The next line may not be needed. If you have to press enter to see the prompt it is not needed.
//std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
std::cout << "\n\n Press Enter to continue: ";
std::cin.get();
return 0; // <--- Not required, but makes a good break point.
|
Line 3 is commented out because there is nothing to ignore. Otherwise you would have to press "Enter" twice to end the program.
Andy