Read and Sort 30 integers into One Dimension Array
Write a C programme that read 30 integer numbers into one dimension array then using a function to sort them in desinding order (this function should use another function to exchange any two elements int the array) after that print desorted elements?
RE: Read and Sort 30 integers into One Dimension Array
Hi Let break the question into three parts 1-Read 3o intgers and store them in one dimensional array. 2. Write one function to sort them in descending order.Let the function name is sort() Let suppose there are five elements 11 23 32 12 1 For sorting you need to compare the 1st elemnt that is 11 with the rest of the elements .While comapring the 2nd elelment you found that 11 is smaller than the 23. Then these elements need to be swapped .This swapping need to be done againg through afunction called swap() 3.swap() -this function is used to swap the element. 4.Printing should be done on the desorted array then before sorting you need to store it in another array.
Execute the following example and u will be clear.
#include<stdio.h> void sort(int a[]); void swap(int * int *);
int main() { int a[30] b[30]; int i j; printf("eneter 10 number"); for(i 0;i<30;i++) { scanf(" d" &a[i]); } memcpy(b a sizeof(a)); /* here we have stored the original array into array b sort(a); printf("the number in sorted and descending order is n"); for(i 0;i<30;i++) { printf(" d t" a[i]); } printf("n"); for(i 0;i<30;i++) { printf(" the original array d t" b[i]); }
return 0; }
void sort(int arr[]) /* this function is used to sort the array */ { int i j; for(i 0;i<10;i++) { for(j i+1;j<10;j++) { if(arr[i]<arr[j]) { swap(&arr[i] &arr[j]); } } } }
void swap(int *p int *q) /* This is used to swap the two elements */ { int temp; temp *p; *p *q; *q temp; }