Thread: sorting numbers

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    26

    sorting numbers

    Hello!
    I want to make a program to arrange numbers in ascending order using arrays.
    Here is what I wrote :
    Code:
     #include <iostream>
    using namespace std;
    
    int main()
    {
         int i,n,a[100],aux,found;
         cout << "n = " ;
         cin >> n;
    
         for (i = 0; i < n; i++) {
          cout << "a[" << i+1 << "]=";
          cin >> a[i];
          found = 0;
    
        if (a[i] > a[i+1] ) {
         aux = a[i];
         a[i] = a[i+1];
         a[i+1] = aux;
         found = 1;
       }
      }
       while (found = 1) {
         for (i = 1; i < n;i++) {
         cout << a[i];
       }
       }
    return 0;
    }
    Something is wrong in my program . What is it? The 'while'?
    Thanks in advance !

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Understand the algorithm first.
    What you're doing is somewhat jumbled up(trying to sort while taking the input and so on..)
    If this is supposed to be bubble sort, read the wikipedi article on it.


    If you aren't required to write the sort function yourself ; do something like this :
    Code:
    #include<algorithm>
    #include<array>
    #include<iostream>
    using namespace std;
    bool comp(int x,int y){return x<y;}
    int main()
    {
            array<int, 5> foo = {4,67,22,5,7};
            sort(foo.begin(),foo.end(),comp);
            for(auto x:foo)cout<<x<<endl;
            return 0;
    }
    Last edited by manasij7479; 10-15-2011 at 04:53 AM.

  3. #3
    Registered User
    Join Date
    Aug 2011
    Posts
    26
    Thank you very much for your reply but I want to know why my program doesn't work and what am I supposed to do to fix the errors.
    Actually,I have to find out how many ascending sequence are in the n numbers of the array and I don't know how to do this...

  4. #4
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    what am I supposed to do to fix the errors
    Make up your mind. Do you want to sort or "find out how many ascending sequence are in the n numbers of the array" .

    case 1: Read up on sorting algorithms and implement one of the basic ones correctly.

    case 2: Think about the problem; how you'd find it out in your mind. Make a flow chart. After that attempt writing the code.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    250
    Quote Originally Posted by Sky_Daughter View Post
    while (found = 1) {
    You should read up on the differences between assignment and comparison.
    iMalc: Your compiler doesn't accept misspellings and bad syntax, so why should we?
    justin777: I have no idea what you are talking about sorry, I use a laptop and there is no ascii eject or something

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by manasij7479 View Post
    Understand the algorithm first.
    What you're doing is somewhat jumbled up(trying to sort while taking the input and so on..)
    If this is supposed to be bubble sort, read the wikipedi article on it.


    If you aren't required to write the sort function yourself ; do something like this :
    Code:
    #include<algorithm>
    #include<array>
    #include<iostream>
    using namespace std;
    bool comp(int x,int y){return x<y;}
    int main()
    {
            array<int, 5> foo = {4,67,22,5,7};
            sort(foo.begin(),foo.end(),comp);
            for(auto x:foo)cout<<x<<endl;
            return 0;
    }
    There should be no need for a comparator function here since you are using integers.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Something is wrong in my program . What is it? The 'while'?
    May be a good idea to post the errors?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting the numbers
    By minyoungan in forum C Programming
    Replies: 3
    Last Post: 09-29-2010, 11:10 AM
  2. help with sorting numbers
    By artistunknown in forum C Programming
    Replies: 12
    Last Post: 02-07-2010, 12:00 AM
  3. Sorting a set of numbers.... I should remember this....
    By advancedk in forum C Programming
    Replies: 3
    Last Post: 12-17-2008, 10:28 PM
  4. sorting an array of numbers in C
    By JVincent08 in forum C Programming
    Replies: 2
    Last Post: 03-14-2008, 01:42 PM
  5. Sorting Random Numbers
    By kid kash in forum C++ Programming
    Replies: 4
    Last Post: 12-07-2002, 04:47 AM