Program 43) Write a program that uses a function to sort an array of integers.
A program to sort an array of integers using the function sort( ). Its output clearly shows that a function can change the values in an array passed as an argument.
PROGRAM
void sort (int m, int x[ ]) ;
main ( )
{
int i ;
int marks [5] = {40, 90, 73, 81, 35} ;
printf ("Marks before sorting\n") ;
for (i = 0; i < 5; i++)
printf ("%d", marks [i]) ;
printf ("\n\n") ;
sort (5, marks) ;
printf ("Marks after sorting \n") ;
for (i = 0; i < 5; i++)
printf ("%4d", marks [i]) ;
printf ("\n") ;
}
void sort (intm, int x[ ])
{
int i, j, t ;
for (i = 1; i<= m - 1; i++)
for (j = 1; j<= m - i; j++)
if (x [j - 1] >= x [j])
{
t = x [j - 1] ;
x [j - 1] = x [j] ;
x [j] = t ;
}
}
Output: Marks before sorting
40 90 73 81 35
Marks after sorting
35 40 73 81 90
No comments:
Post a Comment