# Thread: ascending and descending order

1. ## 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");
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.");
}```

2. 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. Kindly use code tags to write code... Thanks

4. ## 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