Tadanashi
Tadanashi

Reputation: 11

c++ bubble sort returning weird values

I'm trying to use a bubble sort to sort an array of 10 numbers. The program asks for 10 numbers from the user then outputs the unsorted array. This part works fine. It then runs a bubble sort and outputs the sorted array. In my tests I only entered positive integers, however the first value in the sorted array is always a really small number expressed like "2.6812368e-317" or something similar. The rest of the values in the array then appear after that number sorted as they should be. After the sorted array displays Windows then comes up with an error saying the program has stopped working. My code is as follows:

int main(int argc, char** argv) {

double arrSort[10];// declare array to store numbers to be sorted

cout << "Please enter 10 numbers to be sorted" << endl;
// ask for values from user and input them in array
for (int i = 0; i < 10; i++)
    {
        cin >> arrSort[i];
    }

// display unsorted array
cout << "Unsorted Array: " << endl;
for (int i = 0; i < 10; i++)
    {
        if (i < 9)
            cout << arrSort[i] << ", ";
        else
            cout << arrSort[i] << endl;
    }

bool changed = true; // variable to store whether a change has been made
double temp; // variable to temporarily store a value while swapping

//start looping the array
do
{
    changed = false; // change to false so that if no changes are made to array the loop exits
    for (int i = 0; i < 10; i++) // start loop within array to check values
    {
        if (arrSort[i] > arrSort[i + 1]) // check if current index is greater than next index
        {
            // swap values
            temp = arrSort[i]; // store current index in temp variable
            arrSort[i] = arrSort[i + 1]; // assign next index to current index
            arrSort[i + 1] = temp; // assign temp value to next index

            changed = true; // set changed to true to run another loop
        }
    }
}while (changed); // if array was changed loop through again, if not changed exit loop

// output results of sorted array
cout << "Sorted Array: " << endl;
for (int i = 0; i < 10; i++)
{
    if (i < 9)
        cout << arrSort[i] << ", ";
    else
        cout << arrSort[i] << endl;
}
return 0;

}

Here is a screenshot of a test run of the program: Sorted Array output

Upvotes: 0

Views: 719

Answers (2)

apologiessirnoclue
apologiessirnoclue

Reputation: 216

I think the problem is here:

if (arrSort[i] > arrSort[i + 1])

When

i=9

Your array have 10 elements and you try to compare

arrSort[9] > arrSort[9+1]

And

arrSort[10]

Does not exist

Upvotes: 1

Jakub Stasiak
Jakub Stasiak

Reputation: 113

for (int i = 0; i < 10; i++) // <-- here's problem. 
{
    if (arrSort[i] > arrSort[i + 1]) 
    {
        // swap values
    }
}

i variable should be less than 9 not 10. As you can see in if statement you are checking arrSort[i + 1], so in last element you are checking number which is out of your table range (arrSort[10] doesn't exist). I'm not able to check it right now, but I guess it's the problem.

Upvotes: 1

Related Questions