C++ ascending and descending???
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: C++ ascending and descending???

  1. #1
    Join Date
    Jan 2007
    Posts
    9

    Exclamation C++ ascending and descending???

    IN c++... I get output on 10 numbers after i entered sufficient data, i want to make that numbers either in ascending or in descending orders ..

    here is my code but is asks for 10 numbers to print thats ok.. but i want to displayed all the numbers in different locations and want to print like ... number is 2.4.7,9,etc in ascendin orders....


    #include<iostream.h>
    #include<conio.h>
    class loops
    {
    int num;
    int times;
    public:
    void display()
    {
    times=0;
    for(;;times++)
    {
    if(times>=10)
    {
    break;
    }
    cout<<endl<<"Enter a number";
    cin>>num;
    num=num;
    }
    }
    void output();
    };
    void loops:utput()
    {
    num=0;
    for(;;num++)
    {
    if(num>=10)
    {
    break;
    }
    cout<<endl<<"Number is :"<<num;
    }
    }
    void main()
    {
    clrscr();
    loops l1;
    l1.display();
    l1.output();
    getch();
    }

    Would anybody pls know help me to solve this...
    Last edited by Reon; February 2nd, 2007 at 02:00 AM. Reason: updation needed

  2. #2
    Join Date
    Jan 2003
    Posts
    615

    Re: C++ ascending and descending???

    Please show us your code.
    Before post, make an effort yourself, try googling or search here.

    When posting, give a proper description of your problem, include code* and error messages.

    *All code should include code tags

  3. #3
    Join Date
    Oct 2005
    Location
    Bangalore
    Posts
    1,051

    Re: C++ ascending and descending???

    There are a lot of sortng methods. An easy and straigh forward one would be

    Code:
    smallest = number 1
    outerloop
    {
        inner loop
       {
               if number2 > smallest then
                       smallest  = number2
              else 
                       do the same with next number ( number3..4..and so on ) in innerloop
        }
            print smallest
            exclude the first smallest number 
            return to outerloop to find second smallest
    }
    - Sreehari
    "Sometimes I think the surest sign that intelligent life exists elsewhere in the universe is that none of it has tried to contact us."
    " Everybody is sent to Earth on a purpose. I am so Lagging behind that i won't die." Calvin

  4. #4
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: C++ ascending and descending???

    Search the web for sorting methods:
    • bubblesort
    • shakesort
    • shellsort
    • quicksort
    • radix sort

    Well, you can always use std::sort from <algorithms>
    Code:
    int array[10]; // fit it in
    
    std::sort(&array[0], &array[9]);
    or
    Code:
    std::vector<int> array; // fill it in
    
    std::sort(array.begin(), array.end());
    To decide the order, ascending or descending, use a boolean predicate as the third parameter. Look in MSDN for the std::sort documentation.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  5. #5
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    Re: C++ ascending and descending???

    Cilu you're too fast!

    My suggestion is to use vectors! Here's my suggestion's code anyways...

    Code:
    #include <iostream>
    #include <vector>
    #include <algorithm>
    
    using namespace std;
    
    int main()
    {
        vector<int>::const_iterator iter;
        vector<int> listOfNums;
        
        for(int i = 0; i < 10; ++i)
        {
            cout << "\n\n"<<i + 1<<". Enter a number: ";
            cin >> number;
            listOfNums.push_back(number);
        }
        
        sort(listOfNums.begin(), listOfNums.end());
        
        for(iter = listOfNums.begin(); iter != listOfNums.end(); ++iter)
        {
            cout << *iter << endl;
        }
        
        return 0;
    }
    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

  6. #6
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

  7. #7
    Join Date
    Jan 2007
    Posts
    9

    Re: C++ ascending and descending???

    I'm not familiar with vector and so.. i only know conio,iostream & fstream..i s there any probability to complete the program in that way....


    Quote Originally Posted by Mybowlcut
    Cilu you're too fast!

    My suggestion is to use vectors! Here's my suggestion's code anyways...

    Code:
    #include <iostream>
    #include <vector>
    #include <algorithm>
    
    using namespace std;
    
    int main()
    {
        vector<int>::const_iterator iter;
        vector<int> listOfNums;
        
        for(int i = 0; i < 10; ++i)
        {
            cout << "\n\n"<<i + 1<<". Enter a number: ";
            cin >> number;
            listOfNums.push_back(number);
        }
        
        sort(listOfNums.begin(), listOfNums.end());
        
        for(iter = listOfNums.begin(); iter != listOfNums.end(); ++iter)
        {
            cout << *iter << endl;
        }
        
        return 0;
    }

  8. #8
    Join Date
    Jan 2007
    Posts
    9

    Re: C++ ascending and descending???

    i only know conio,iostream & fstream..i s there any probability to complete the program in that way....


    Quote Originally Posted by Mybowlcut
    Cilu you're too fast!

    My suggestion is to use vectors! Here's my suggestion's code anyways...

    Code:
    #include <iostream>
    #include <vector>
    #include <algorithm>
    
    using namespace std;
    
    int main()
    {
        vector<int>::const_iterator iter;
        vector<int> listOfNums;
        
        for(int i = 0; i < 10; ++i)
        {
            cout << "\n\n"<<i + 1<<". Enter a number: ";
            cin >> number;
            listOfNums.push_back(number);
        }
        
        sort(listOfNums.begin(), listOfNums.end());
        
        for(iter = listOfNums.begin(); iter != listOfNums.end(); ++iter)
        {
            cout << *iter << endl;
        }
        
        return 0;
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center