Thread: Putting numbers in order

  1. #1
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342

    Putting numbers in order

    Is there a way to take 100 random numbers and print them all out in order from 1 to 99?
    (I can randamize, just not put them in order.)
    Thanks, August.

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Yes its called sorting

  3. #3
    *this
    Join Date
    Mar 2005
    Posts
    498
    Ya, you can implement any type of "sort" technique, I just did this about 2 weeks ago. Search google for bubble sort, insertion sort, selection sort, merge sort, and quick sort. Any of those will get the job done, however bubble is the slowest but for 100 numbers it doesnt really matter. You can google it to see some code, if you want I can show you an example. But the precondition of each sort, is that the numbers are in an array.
    Last edited by JoshR; 05-01-2005 at 12:28 PM.

  4. #4
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    I am always open to examples.

  5. #5
    *this
    Join Date
    Mar 2005
    Posts
    498
    Here a piece of aprogram I,ve made. You should probably get a sence of how to use the sorting techniques:

    Code:
    void testSort()
    {
       ifstream infile("Test.txt", ios::in);
       string line;
       char letter;
       int count = 0;
       vector< string > names(5);
       
       cout << "Testing string sort..." << endl << endl;
       
       while (infile.get(letter) && count < 5)
       {
          line = "";
          while (letter != '\n' && !infile.eof())
          {   
             line += letter;
             infile.get(letter);
          }
          names[count++] = line;
       }
       
       print (names);
       selectionSort(names);
       
       cout << endl;
       print (names);
    }
    
    void swap (string &a, string &b)
    {
          string temp = a; a = b; b = temp;
    } 
    
    void selectionSort (vector< string > &list)
    {
       int flag;
       
       for (int outer = 0; outer < list.size(); outer++)
       {
          flag = outer;
          for (int inner = outer + 1; inner < list.size(); inner++)
             if (list[inner] < list[flag])
                flag = inner;
          swap(list[outer], list[flag]);
       }
    }
    You need so search cprogramming.com or google and find out for yourself which sorting technique is the best for the situation.
    Last edited by JoshR; 05-01-2005 at 01:02 PM.

  6. #6
    *this
    Join Date
    Mar 2005
    Posts
    498
    nvm...
    Last edited by JoshR; 05-01-2005 at 01:01 PM.

  7. #7
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Josh lets not do his homework for him

  8. #8
    *this
    Join Date
    Mar 2005
    Posts
    498
    Ooops wasnt thinkin, didnt realize it could be his homework.
    Last edited by JoshR; 05-01-2005 at 01:04 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bubble sort not sorting numbers in order.
    By rushhour in forum C++ Programming
    Replies: 21
    Last Post: 02-19-2009, 01:40 PM
  2. Putting in order scanned numbers?
    By Tarento in forum C Programming
    Replies: 5
    Last Post: 06-02-2006, 02:25 PM
  3. Sorting numbers in descending order
    By Yunasnk in forum C++ Programming
    Replies: 2
    Last Post: 11-23-2003, 05:55 PM
  4. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 11:15 AM
  5. display numbers in order
    By mike in forum C++ Programming
    Replies: 2
    Last Post: 09-24-2001, 09:16 PM