Thread: Creating a program which accepts 10 numbers & then displays them in descending order

  1. #1
    Registered User
    Join Date
    Jul 2016
    Posts
    2

    Creating a program which accepts 10 numbers & then displays them in descending order

    I apologize if I am not following rules or if this topic is posted elsewhere, but I have written some code to correspond to the topic I am having issues with and the problem I am running into is that my program continues to prompt me to enter a number and what I am attempting to design is a program which accepts at most 10 integers and then displays them in descending order.

    Code:
    #include <iostream>
    #include <algorithm>
    using namespace std;
    int main(){
        int numb [10];
        int i, j;
        
        for(i=0;1<=9;i++)
        {
         cout<<"Please enter numer: ";
         cin>> numb [i];
         }
        for(i=0;i<=8;i++)
        {
                     for(j=i+1;j<=8;j++)
                     {
                                        int temp;
                                        if(numb[i] < numb[j])
                                        {
                                                          temp = numb[i];
                                                          numb[i] = numb[j];
                                                          numb[j] = temp;
                                                          }
                                        }
                     }
    for(i=0;i<=9;i++)
    {
                     cout<< endl << numb [i]<<endl;
        system("pause");
        return 0;
    
    
        }
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You might have a font problem rather than a programming problem. Look carefully:
    Code:
    for(i=0;1<=9;i++)
    The loop condition is 1<=9, i.e., "one less than or equal to nine", but you probably wanted it to be i<=9, i.e., "value of i less than or equal to nine".

    By the way, instead of using i <= 9, I suggest i <= 10. You can then declare a named constant for the array size, and then use that instead of the magic number 10.

    Also, I recommend consistently indenting with 2 to 8 spaces or one tab character per indent level. My preference is 4 spaces, so:
    Code:
    #include <iostream>
    #include <algorithm>
    
    using namespace std;
    
    int main()
    {
        int numb[10];
        int i, j;
    
        for (i = 0; 1 <= 9; i++)
        {
            cout <<"Please enter numer: ";
            cin >> numb[i];
        }
    
        for (i = 0; i <= 8; i++)
        {
            for (j = i + 1; j <= 8; j++)
            {
                int temp;
                if (numb[i] < numb[j])
                {
                    temp = numb[i];
                    numb[i] = numb[j];
                    numb[j] = temp;
                }
            }
        }
    
        for (i = 0; i <= 9; i++)
        {
            cout << endl << numb[i] << endl;
            system("pause");
            return 0;
        }
    }
    With proper indentation another bug becomes evident: you place the return 0; (and perhaps the call to system) at a wrong place. Note that as a special case for the global main function, you don't actually need this return 0; as when control reaches the end of the main function, it will be as if you wrote return 0; there.

    Another thing to note: while you arguably don't need and should not use system("pause") here, if you do want to call the system function, you should #include <cstdlib>.
    Last edited by laserlight; 07-15-2016 at 12:39 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by laserlight View Post
    By the way, instead of using i <= 9, I suggest i <= 10. You can then declare a named constant for the array size, and then use that instead of the magic number 10.
    TYPO Alert: You likely meant "instead of using i <= 9, I suggest i < 10."

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  4. #4
    Registered User
    Join Date
    Jul 2016
    Posts
    2
    I greatly appreciate the pointers and the 1's (I noticed two places where I erred) were replaced with i's and now the program doesn't ask me for more than 10 numbers.

    Now the issue is, what is being displayed is just the largest number, 9. I am trying to display the 10 numbers which were input in descending order and I just can't grasp it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using pointers to sort 3 numbers in descending order
    By Tigertan in forum C Programming
    Replies: 2
    Last Post: 10-31-2012, 07:49 PM
  2. three numbers in descending order
    By jackson6612 in forum C++ Programming
    Replies: 3
    Last Post: 05-21-2011, 08:05 AM
  3. Replies: 9
    Last Post: 04-01-2011, 04:13 PM
  4. Replies: 12
    Last Post: 02-28-2008, 06:19 PM
  5. Sorting numbers in descending order
    By Yunasnk in forum C++ Programming
    Replies: 2
    Last Post: 11-23-2003, 05:55 PM

Tags for this Thread