sorting numbers

This is a discussion on sorting numbers within the C++ Programming forums, part of the General Programming Boards category; Hello! I want to make a program to arrange numbers in ascending order using arrays. Here is what I wrote ...

  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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    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.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    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.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,421
    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,167
    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-06-2010, 11:00 PM
  3. Sorting a set of numbers.... I should remember this....
    By advancedk in forum C Programming
    Replies: 3
    Last Post: 12-17-2008, 09: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, 03:47 AM

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