Some problems with selected short

This is a discussion on Some problems with selected short within the C++ Programming forums, part of the General Programming Boards category; Hi guys, I'm learning C++ and one of the exercises is to create a program that sorts an unsorted array. ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    2

    Some problems with selected short

    Hi guys,

    I'm learning C++ and one of the exercises is to create a program that sorts an unsorted array. So I created a prog, but it won't work, only the first 2 numbers and the last 2 were sorted, the rest remained to it's original position.

    So I took a look at my code, rewrote it 2 times and still it has the same issue! Can somebody please help me out?

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int n[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
        int i, j, p;
        
        i = 0;
        j = 0;
        p = 0;
        
        // the unsorted list
        for (i = 0; i < 10; i++){
            cout << "[" << i << "] = ";
            cin >> n[i];
        }
        
        i = 0;
        j = 0;
        
        cout << endl;
        cout << "Unsorted list = ";
        for (i = 0; i < 10; i++)
            cout << n[i] << ", ";
        
        cout << endl;
        cout << "Sorting..." << endl;
        // sorting...    
        i = 0;
        while (true){
              if (n[i] < n[i + 1]){
                       ++i;
                       if (i = 8)
                             break;
              }
              if (n[i] > n[i + 1]){
                       // swap --------
                       p = n[i + 1];
                       n[i + 1] = n[i];
                       n[i] = p;
                       //--------------
                       i = 0;
              }
              if (n[i] == n[i + 1])
                       ++i;
        }
        
        // the sorted list
        cout << "Sorted list = ";
        for (i = 0; i < 10; i++)
            cout << n[i] << ", ";
        
    }

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,299
    That looks more like Gnome sort to me.
    This line sets i to 8:
    Code:
    if (i = 8)
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    2
    OMFG, thank you

    I hate myself...and I love you!!!!!!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. beginner help!!!
    By robski in forum C++ Programming
    Replies: 4
    Last Post: 03-23-2010, 01:26 PM
  2. Trouble with two two dimensional arrays
    By scmurphy64 in forum C Programming
    Replies: 5
    Last Post: 12-06-2009, 05:57 PM
  3. Problems with bmp-reading program
    By Amanita in forum C++ Programming
    Replies: 8
    Last Post: 10-25-2007, 04:13 AM
  4. Weirdest error with '==' operator.
    By Blackroot in forum C++ Programming
    Replies: 6
    Last Post: 02-07-2006, 05:30 PM
  5. Short to 16bit 555 format
    By Coder87C in forum C# Programming
    Replies: 3
    Last Post: 02-04-2006, 05:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21