#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2004
    Posts
    6
    Rep Power
    0

    Talking program in ascending and descending order..


    can you please help me to make a program that will accept 5 numbers then display the number in ascending and descending order..im just new in C.Tanx :)
  2. #2
  3. Crypto-Con
    Devshed Supreme Being (6500+ posts)

    Join Date
    Apr 2004
    Location
    Frisco, Texas
    Posts
    6,704
    Rep Power
    1236
    It's pretty simple, really.... You want C, right?

    Also, it'll matter if you want to only accept integers, or if you want to use doubles, I'll just use integers here....

    Code:
    int main() 
    {
       int num[5],idx,temp,unordered;
    
       // This scans in the numbers
       for (idx=0; idx < 5; idx++) {
          printf("Enter a number: ");
          scanf("%d", &num[idx]);
       }
    
       // This is the loop test we'll make to see if the list is ordered or not, if it isn't, we'll make our switches
       while (unordered) {
          unordered=0;
          idx--;
          for (idx=0; idx < idx; idx++) {
             if (num[idx] > num[idx+1]) {
                unordered = 1;
                temp = num[idx];
                num[idx] = num[idx+1];
                num[idx+1] = temp;
             }
          }
       }
       for (idx=0; idx < 5; idx++)
          printf("%d \n",num[idx]);
    
       return 0;
    }
    That should be right, I don't have a compiler handy :rolleyes:
    - "Cryptographically secure linear feedback shift register based stream ciphers" -- a phrase that'll get any party started.
    - Why know the ordinary when you can understand the extraordinary?
    - Sponsor my caffeine addiction! (36.70 USD received so far -- Latest donor: Mark Foxvog.
    )
  4. #3
  5. No Profile Picture
    =) wannabe?
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jul 2002
    Location
    florida
    Posts
    2,153
    Rep Power
    15
    i ran that and it just prints the numbers as i entered them
  6. #4
  7. Lord of Dorkness
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2004
    Location
    Central New York. Texan via Arizona, out of his element!
    Posts
    8,524
    Rep Power
    3314
    It doesn't execute the body of the "for", as idx is never less than idx. Anyway, displaying a list in forward and reverse orders doesn't require rearranging the values, just displaying in opposite directions.
    Functionality rules and clarity matters; if you can work a little elegance in there, you're stylin'.
    If you can't spell "u", "ur", and "ne1", why would I hire you? 300 baud modem? Forget I mentioned it.
    DaWei on Pointers Politically Incorrect.

IMN logo majestic logo threadwatch logo seochat tools logo