December 17th, 2012, 02:10 AM

Help! Ascending and Descending order
I'm trying to create a program that will display numbers in ascending and descending order. I just encountering a little problem.. everytime I run the program, the ascending order always display zero even though it's not the numbered entered.
Thanks in advance
Code:
#include<stdio.h>
#include<stdlib.h>
#define SIZE 10
int a[10],i,j,k,n;
void sort(int *a,int n);
void swap(int *x,int *y);
int main()
{
system("cls");
printf("Enter the 10 numbers:\n\n");
for (i=0;i<SIZE;++i)
{
scanf("%d",&a[i]);
}
sort(a,n);
printf("\nDescending order: \n");
for (k=0;k<SIZE;k++)
{
printf("\n%d",a[k]);
}
printf("\nAscending order:");
for (k=10;k>0;k)
{
printf("\n%d",a[k]);
}
system("pause");
return 0;
}
void sort(int *a,int n)
{
int p=SIZE1;
while (p>=0)
{
for(i=0;i<=(p1);++i)
{
if (a[i]<=a[i+1])
swap(&a[i],&a[i+1]);
else
continue;
}
p;
}
}
void swap(int *x,int *y)
{
int t;
t=*x;
*x=*y;
*y=t;
}
Comments on this post
December 17th, 2012, 03:28 AM

> for (k=10;k>0;k)
You're off by one, at both ends.
You start at SIZE1, and end at zero.
Code:
for ( i = 0 ; i < SIZE ; i++ ) printf("%d\n", i );
for ( i = SIZE ; i > 0 ; i ) printf("%d\n", i );
Fix the second loop so you get the same indices, but in reverse.
December 17th, 2012, 07:45 AM

Originally Posted by salem
> for (k=10;k>0;k)
You're off by one, at both ends.
You start at SIZE1, and end at zero.
Code:
for ( i = 0 ; i < SIZE ; i++ ) printf("%d\n", i );
for ( i = SIZE ; i > 0 ; i ) printf("%d\n", i );
Fix the second loop so you get the same indices, but in reverse.
Thanks man!!!