Monday, December 27, 2010

sorting array in descending order

/****************************
* Umang B Bhatt *
* bhatt.umang7@gmail.com *
*****************************/

/**
* program for scannign n numbers and sorting them in descending order
*/

#include<stdio.h>
void main()
{


int
n = 0 , i =0 , j = 0;

int
no[100];
int
temp; // for swapping the number
printf("How many numbers you want to scan ? ");

scanf("%d",&n);
if
(n < 0 || n>100)
{


printf("Invalid number. It must be brtween 1 to 100");
}

else

{

// logic for scanning the numbers
for(i = 0 ; i < n ;i++)
{


printf("Enter element %d: ", i);
scanf("%d",&no[i]);
}



// the logic for sorting the numbers
for(i = 0 ; i < n ;i++)
{

for
(j = 0 ; j < n ; j++ )
{


if
(no[i] > no[j])
{

// if the number at index i is greater than the number at index j then swap the numbers
temp = no[i];

no[i] = no[j];
no[j] = temp;
}
}
}


for
(i = 0 ; i < n ;i++)
{

printf("\nnumber at index %d is %d ", i , no[i]);
}
}


printf("\n");
}

No comments:

Post a Comment