Jump to content

An array in ascending order in C: Difference between revisions

From IdeaWazaWiki
wikademia>JWSchmidt
wikademia>Mickraus
main now ANSI/ISO; formatting changes
Line 1: Line 1:
<pre>/* TO PRINT THE NUMBERS IN ASCENDING AND DESCENDING ORDER USING ARRAYS */
<pre>
/* PERFORMED BY MILAN */
/* PERFORMED BY MILAN */


#include <stdio.h>
#include <stdio.h>
#include<conio.h>


void main()
/* main: sort an array and display in ascending and descending order */
int main(void)
{
{
int a[10], i=0, j=0, n, t;
    int a[10], i=0, j=0, n, t;
clrscr();


printf ("\n Enter the no. of elements: ");
    printf("\nEnter the no. of elements: ");
scanf ("%d", &n);
    scanf("%d", &n);
printf ("\n");
    printf("\n");


for (i = 0; i <n; i++)
    for (i = 0; i < n; i++)
{
    {
printf ("\n Enter the %dth element: ", (i+1));
        printf("\nEnter element %d: ", (i+1));
scanf ("%d", &a[i]);
        scanf("%d", &a[i]);
}
    }


for (j=0 ; j<(n-1) ; j++)
    for (i = 0; i < (n-1); i++)
{
    {
for (i=0 ; i<(n-1) ; i++)
        for (j = 0; j < (n-1); j++)
        {
            if (a[j+1] < a[j])
{
{
if (a[i+1] < a[i])
                t = a[j];
{
                a[j] = a[j + 1];
t = a[i];
                a[j + 1] = t;
a[i] = a[i + 1];
            }
a[i + 1] = t;
        }
}
    }
}
 
}
    printf ("\nAscending order: ");
    for (i = 0; i < n; i++)
    {
        printf (" %d", a[i]);
    }


printf ("\n Ascending order: ");
    printf ("\nDescending order: ");
for (i=0 ; i<n ; i++)
    for (i = n; i > 0; i--)
{
    {
printf (" %d", a[i]);
        printf (" %d", a[i-1]);
}
    }


printf ("\n Descending order: ");
    /* indicate success */
for (i=n ; i>0 ; i--)
    return 0;
{
printf (" %d", a[i-1]);
}
getch();
}
}
/* OUTPUT:
 
/* EXAMPLE OUTPUT:


  Enter the no. of elements: 5
  Enter the no. of elements: 5

Revision as of 15:36, 30 January 2007

/* PERFORMED BY MILAN */

#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;
}

/* EXAMPLE OUTPUT:

 Enter the no. of elements: 5


 Enter the 1th element: 25

 Enter the 2th element: 50

 Enter the 3th element: 75

 Enter the 4th element: 35

 Enter the 5th element: 100

 Ascending order:  25 35 50 75 100
 Descending order:  100 75 50 35 25
 */