I am having issue with my code, the code is suppose to sort a set amount a number in ascending order, but every time I enter the 2nd last number the program ends, and in the place of what should be the 10th number is -858993460
here is my code:
#include<iostream>
using namespace std;
int main()
{
int x, y, arry[61], z, interim;
cout << "Please enter how many numbers you would like to sort:\n>> ";
cin >> x;
cout << "Please enter " << x << "numbers\n";
for (y = 0; y < (x - 1); y )
cin >> arry[y];
cout << "Give bubble sort a minute to do its thing.....";
for (y = 0; y < (x - 1); y )
{
for (z = 0; z < (x - y - 1); z )
{
if (arry[z] > arry[z 1])
{
interim = arry[z];
arry[z] = arry[z 1];
arry[z 1] = interim;
}
}
}
cout << "\nBubble sorted as went through the array in accending order\n Arry:";
for (y = 0; y < x; y )
cout << arry[y] << " ";
cout << endl;
return 0;
}
I tried to change the value of the array but not really tried anything else, and I'm expecting it to be able to let me input that last number but it never does.
CodePudding user response:
it looks like you made a tiny error in the for...
loops. When you ask the user to input x
numbers, your for...
loop cuts the users reply short. The next for...
loop following that one, where you tell the user to wait a minute for Bubble Sort, is also cut short.
Instead of:
cout << "Please enter " << x << "numbers\n";
for (y = 0; y < (x - 1); y )
cin >> arry[y];
Your for...
loop should look like this:
cout << "Please enter " << x << "numbers\n";
for (y = 0; y < x; y )
cin >> arry[y];
This will allow for exactly x
numbers to be saved from the user input.
and in the following for...
loop, instead of:
cout << "Give bubble sort a minute to do its thing.....";
for (y = 0; y < (x - 1); y )
{
//... your code here
}
It should be:
cout << "Give bubble sort a minute to do its thing.....";
for (y = 0; y < x; y )
{
//... your code here
}
After making these adjustments, I tested out your code and it works as intended.
every time I enter the 2nd last number the program ends
This happened because the first for...
loop cut your user input short by 1 value, where your looping condition was y < (x-1)
. If you set x
to be a small number like 5
and you count from y = 0
up to (x-1)
(or 5-1
in this case), like in your original for...
loop, you'll notice that your for...
loop only loops 4 times, and only records 4 values from the user input, not 5
values.
For future reference, remember that arrays are 0-indexed, meaning that they start with the index 0. So if you want to loop over 5 elements of an array, you would loop through the indices 0, 1, 2, 3, 4
. This is the same as:
for(y = 0, y < 5; y )
The loop goes up to the 5th index, meaning it iterates over exactly 5 elements.
This leads me into your next question:
and in the place of what should be the 10th number is -858993460
You are getting this value because it is just a place holder in your array. This happened because you never got to assign the very last element from the user BUT when you print the results, notice how your for...
loop prints out your array:
for (y = 0; y < x; y )
See how you're printing all the elements? But since you never assigned the last element from the user input, this causes undefined behavior in c . In this case, it just print the memory address of where the last element is supposed to be in your array.
For a fun exercise, make an empty array and print it, you'll get the same result, with it printing random values (possibly memory addresses).
int arry[10];
for(int i = 0, i < 10; i )
{
cout << arry[i] << " ";
}
Sorry for the long explanation but I hope this helps!! c:
CodePudding user response:
I have done this for (y = 0; y < (x - 1); y )
twice when line I should have put for(y=0; y<x; y )