ascending and descending order

This is a discussion on ascending and descending order within the C Programming forums, part of the General Programming Boards category; Hi, New to C and learning it from a book. Stuck at this problem. User enters 10 numbers then receives ...

  1. #1
    ssk
    ssk is offline
    Registered User
    Join Date
    Mar 2011
    Posts
    2

    Question ascending and descending order

    Hi,

    New to C and learning it from a book. Stuck at this problem. User enters 10 numbers then receives the option to either sort it in ascending or descending order. The output always shows an "0" that was never entered.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    //funtion prototye
    void iInput();
    void iSorting();
    
    //global vaiable
    int iArrary[10];
    
    int main()
    {
        iInput();
        iSorting();
    
    }
    
    void iInput()
    {
        int x;
    
        for (x=0;x<10;x++)
        {
            printf("\n Enter #%d :",(x+1));
            scanf("%d",&iArrary[x]);
        }
    }
    
    void iSorting()
    {
        int x=0;
        int y=0;
        int z=0;
        int iLargeN=0;
        int iSmallN=0;
    
        //system("cls");
        printf("\n\n1\tSort numbers in Ascending order");
        printf("\n2\tSort numbers in Descending order");
        printf("\n\t Please enter your choise (1-2)");
        scanf("%d",&y);
    
    
        for (z=10;z>0;z--)
        {
    
                for(x=0;x<=10;x++)
                {
                    if(iArrary[z]<iArrary[x])
                        continue;
    
                    else
                    {
                        iLargeN=iArrary[z];
                        iSmallN=iArrary[x];
                        iArrary[z]=iSmallN;
                        iArrary[x]=iLargeN;
                        printf("\n%d\t%d",iArrary[z],iArrary[x]);
                     }
                }
        }
    
        if (y==1)
        {
            printf("\nNumbers in Decending Orders:");
    
            for(x=0;x<=10;x++)
                printf("\n %d",iArrary[x]);
        }
        else if (y==2)
        {
            printf("\nNumbers in Ascendin Order:");
            for(x=10;x>=0;x--)
                printf("\n %d",iArrary[x]);
    
        }
        else
            printf(" Invalid Entry.");
    }
    Last edited by Salem; 03-21-2011 at 02:40 AM. Reason: Added [code][/code] tags - learn to use them YOURSELF

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    There is no array[10] - it's 0 to 9 ONLY.

    So no X=10, and no X<=10 either.

    Please use code tags (click on the # in the advanced editor window and paste your code).

    And Welcome to the forum, SSK!

  3. #3
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    Kindly use code tags to write code... Thanks
    I don't care if someone doesn't like me, i was not put on earth to entertain everyone.

    No King, no Queen, I am the ACE of battle.

  4. #4
    ssk
    ssk is offline
    Registered User
    Join Date
    Mar 2011
    Posts
    2

    problem solved

    Thank you very much Adak for your help.

    Your ideas helped me solve the error.

    (In the future I will be using the code tag. )

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. modifying bubblesort function
    By tenhavenator in forum C Programming
    Replies: 3
    Last Post: 11-13-2009, 11:54 PM
  2. Sorting in descending order
    By michael- in forum C Programming
    Replies: 3
    Last Post: 12-12-2005, 12:07 PM
  3. Bubble sort
    By Loooke in forum C Programming
    Replies: 9
    Last Post: 12-08-2004, 01:29 PM
  4. Determine the order part.2
    By romeoz in forum C++ Programming
    Replies: 6
    Last Post: 07-06-2003, 06:46 PM
  5. selection sorting using going-down and going-up recursion
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 11-02-2001, 01:29 PM

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