Blogger templates

Pages

Wednesday 27 June 2012

WAP of C Language that Sorts the Elements of Array in Ascending Order

Sorted Array

Statement of C Program: This Program arranges a set of floating-point numbers in an ascending order and prints them:

#include<stdio.h>
#include<conio.h>
void main()
{
float array[20];
int i, j, n, temp;
clrscr();
printf(" Enter the size of an Array\n");
scanf("%d" , &n);

printf(" Enter elements\n");
for(i=0 ; i<n ; i++)
{
scanf("%f" , &array[i]);
}

printf(" Input Array is\n");
for(i=0 ; i<n ; i++)
{
printf("%f\n" , array[i]);
}

for(i=0 ; i< n-1 ; i++)
{
for(j= i+1 ; j<n ; j++)
{
if(array[i] >= array[j])
{

temp = array[i];
array[i] = array[j];
array[j] = temp;

}                                                    /* End of if */

}                                             /* End of for loop of j */

}                                              /* End of for loop of i */

printf("Sorted Array is...\n");
for(i=0 ; i<n ; i++ )
{
printf("%f\n" , array[i] );
}                                            /* End of main() */

 Output:
 Enter the size of an Array
 4
 Enter elements
 10
 1
 5
 3
 Input Array is
 10
 1
 5
 3
 Sorted Array is...
 1
 3
 5
 10

                                                                   That's All

0 comments:

Post a Comment