Sorting Array

This is a discussion on Sorting Array within the C++ Programming forums, part of the General Programming Boards category; Hi, I need help in sorting numbers input by the use in ascending order. Here's what I have so far: ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    9

    Sorting Array

    Hi, I need help in sorting numbers input by the use in ascending order. Here's what I have so far:

    Code:
    #include <cstdlib>
    #include <iostream>
    #include <algorithm>
    
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        int numbers[10];
        int i = 0;
    
        
        cout << "Enter 10 numbers: ";
        cin >> numbers[i];
        
        sort(numbers, numbers+i);
        
        
        for (int i = 0; i < 10; i++)
        {
            cout << numbers[i];
        }
    
        
        cout << "\n\nSorted numbers: " << numbers[i] << endl;
        
        system("PAUSE");
        return 0;
    }
    The output keeps coming out incorrect. Thank for the help! =]

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,414
    Well, it looks like they are being entered incorrectly. Only the first number entered is actually placed into the numbers array.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,412
    The sort function operates on containers and only accepts iterators as arguments. If you want to sort an array, you have to come up with a function yourself. Otherwise, change your code to use a vector, for instance.

    There's another problem in your code. You are only populating your array with 1 element. Not 10.
    Code:
    int counter = 0;
    while (cin >> number && counter != 10) {
        myarray[counter++] = number;
    }
    that will populate your array correctly with up to 10 elements.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,414
    The sort function operates on containers and only accepts iterators as arguments. If you want to sort an array, you have to come up with a function yourself. Otherwise, change your code to use a vector, for instance.
    A C-style array is a sequential container, and a pointer is a random access iterator. eun-jin's idea will work, but the input part needs to be fixed. Currently the sort is just sorting one element.

    Oh, and one might say std::sort (and various other generic algorithms) operates on a range, not on a container, since the container itself is left unmodified.
    Last edited by laserlight; 06-08-2006 at 03:39 AM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,412
    Quote Originally Posted by laserlight
    A C-style array is a container, and a pointer is an iterator.
    Ugh! Terribly sorry, for that.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 03-31-2009, 12:34 PM
  2. two dimensional array sorting in C/C++
    By George2 in forum C Programming
    Replies: 16
    Last Post: 11-19-2006, 02:17 AM
  3. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 12:01 AM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM

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