Tuesday, July 28, 2009

How will i sort 5 numbers in ascending order in an array using c language??

I don't have a crystal ball, so I can't read the future and therefore can't predict how you will do it.

How will i sort 5 numbers in ascending order in an array using c language??
Do your own homework. You'll learn more.
Reply:i=i+1
Reply:void sort(void);


int c,a[20],l;


void main()


{


clrscr();


printf("Enter no. of elements in the list:- ");


scanf ("%d",%26amp;l);


printf("


CHOICE:-");


printf("


(1) Sort in ascending order.");


printf("


(2) Sort in descending order.");


printf("


CHOICE:- ");


scanf("%d",%26amp;c);


if (c!=1 %26amp;%26amp; c!=2)


{


printf("


ERROR");


getch();


exit(0);


}


sort();


getch();


}








void sort(void)


{


int n,i,j,temp=0,min,k;


for (i=1;i%26lt;=l;i++)


{


printf("


Enter no.:- ");


scanf("%d",%26amp;a[i]);


}


for (i=1;i%26lt;=(l-1);i++)


{


min=a[i];


k=i;


for (j=(i+1);j%26lt;=l;j++)


{


if (a[j]%26lt;min)


{


min=a[j];


k=j;


}


}


temp=a[k];


a[k]=a[i];


a[i]=temp;


}





switch(c)


{


case 1:


printf("


Elements in ascending order are:-");


for (i=1;i%26lt;=l;i++)


printf("


%d",a[i]);


break;


case 2:


printf("


Elements in descending order are:-");


for (i=l;i%26gt;=1;i--)


printf("


%d",a[i]);


break;


default:


printf("


ERROR");


}


return;


}

floral

No comments:

Post a Comment