In: Computer Science
Program in C:
Write a program in C that reorders the elements in an array in ascending order from least to greatest. The array is {1,4,3,2,6,5,9,8,7,10}. You must use a swap function and a main function in the code. (Hint: Use void swap and swap)
voiud
The Program Code is :
#include<stdio.h>
void swap(int *a,int *b)///Swap
Function definition
{
int temp;
temp = *a; /// Here swaping the elements
*a = *b;
*b = temp;
}
int main()
{
int arr[] = {1,4,3,2,6,5,9,8,7,10};
int i,j,k;
printf("Before sorting the array elements are : \n\n");
for(i = 0 ; i < 10; i++)
{
printf("%d\t",arr[i]);///printing the array elements
}
/// Sorting the array
elements
for(i = 0; i < 10; i++)
{
for(j = i+1; j < 10; j++)
{
if(arr[i] > arr[j])
{
swap(&arr[i], &arr[j]);
///SWAP Function calling
}
}
}
printf("\n\nAfter sorting the array
elements are : \n\n");
for(i = 0 ; i < 10; i++)
{
printf("%d\t",arr[i]);///printing the array elements
}
return 0;
}