An array in ascending order in C: Difference between revisions
wikademia>JWSchmidt |
wikademia>Dr. Eme m moved Beta:An array in ascending order in C to An array in ascending order in C over redirect |
||
| (7 intermediate revisions by 5 users not shown) | |||
| Line 1: | Line 1: | ||
< | <source lang="c"> | ||
/* PERFORMED BY MILAN */ | /* PERFORMED BY MILAN */ | ||
#include <stdio.h> | #include <stdio.h> | ||
/* main: sort an array and display in ascending and descending order */ | |||
int main(void) | |||
{ | { | ||
int a[10], i=0, j=0, n, t; | |||
printf("\nEnter the no. of elements: "); | |||
scanf("%d", &n); | |||
printf("\n"); | |||
for (i = 0; i < n; i++) | |||
{ | |||
printf("\nEnter element %d: ", (i+1)); | |||
scanf("%d", &a[i]); | |||
} | |||
for (i = 0; i < (n-1); i++) | |||
{ | |||
for (j = 0; j < (n-1); j++) | |||
{ | |||
if (a[j+1] < a[j]) | |||
{ | { | ||
t = a[j]; | |||
a[j] = a[j + 1]; | |||
a[j + 1] = t; | |||
} | |||
} | |||
} | |||
printf ("\nAscending order: "); | |||
for (i = 0; i < n; i++) | |||
{ | |||
printf (" %d", a[i]); | |||
} | |||
printf ("\nDescending order: "); | |||
for (i = n; i > 0; i--) | |||
{ | |||
printf (" %d", a[i-1]); | |||
} | |||
/* indicate success */ | |||
return 0; | |||
} | } | ||
/* OUTPUT: | |||
/* EXAMPLE OUTPUT: | |||
Enter the no. of elements: 5 | Enter the no. of elements: 5 | ||
| Line 64: | Line 66: | ||
Descending order: 100 75 50 35 25 | Descending order: 100 75 50 35 25 | ||
*/ | */ | ||
</ | </source> | ||
[[Category:C source code examples]] | [[Category:C source code examples]] | ||