CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Oct 2013
    Posts
    3

    bubble sort is making up its own number

    i cant figure out what im doing wrong with my bubble sort. when i cout the sorted test scores, the first number(testScore[0]) is always wrong after the sort, thank you for the help.this is the code.

    #include <iostream>
    using namespace std;

    int main()
    {
    int i;
    int j;

    int testScore[5];
    int count;

    for (count = 0; count < 5; count++)
    {
    cout<< "Enter the 5 test scores "
    << (count + 1 ) << ":";
    cin >> testScore[count];
    }

    cout << "the test scores you entered are:" << endl;
    for (count = 0; count < 5; count++)
    {
    cout<< " " << testScore[count];
    cout << endl;
    }

    for(i = 0; i<= 4; i++)
    {
    for (j = i+1; j<= 5; j++)
    {
    int hold = 0;

    if(testScore[i] > testScore[j])
    {
    hold = testScore[i];
    testScore[i] = testScore[j];
    testScore[j] = hold;
    }
    }
    }

    cout << "The test scores from lowest to highest are:" << endl;

    for (count = 0; count < 5; count++)
    {
    cout<< " " << testScore[count];
    cout << endl;
    }

    system ("pause");

    0}
    Last edited by dthunderchunky; December 10th, 2013 at 02:19 AM.

  2. #2
    Join Date
    Jun 2002
    Location
    Stockholm, Sweden
    Posts
    1,641

    Re: bubble sort is making up its own number

    It is because you are reading an element which is after the last element of your array, and therefore undefined.

    Programming isn't magic, and if you learn to use a debugger you won't have to ask questions like this.
    Nobody cares how it works as long as it works

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: bubble sort is making up its own number

    You should also use code tags when posting code and not quote tags. Go advanced, select the code and click '#'.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured