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?

Questions by Narmeen

Showing Answers 1 - 5 of 5 Answers

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.


Code
  1. #include<stdio.h>

  2. "eneter 10 number""%d"/* here we have stored the original array into array b

  3.  sort(a);

  4.  printf("the number in sorted and descending order is n");

  5.  for(i=0;i<30;i++)

  6.  {

  7.   printf("%d t",a[i]);

  8.  }

  9.  printf("n");

  10.  for(i=0;i<30;i++)

  11.  {

  12.   printf(" the original array %d t",b[i]);

  13.  }

  14.  

  15.  return 0;

  16. }

  17.  

  18. void sort(int arr[])     /* this function is used to sort the array *//* This is used to swap the two elements */

Code
  1. #include <stdio.h>

  2. #include <stdlib.h>

  3. #include <ctype.h>

  4.  

  5. /**

  6.  * Read up to max_len integer values from the specified stream into ival.

  7.  */// will read input values as text and covert with strtol"getValues: error encountered during read; exiting...n");

  8.     exit(0);

  9.   }

  10. }

  11.  

  12. /**

  13.  * Comparison function passed to qsort.  Orders elements in descending order

  14.  *//**

  15.  * Write sorted array contents to stream.

  16.  */"%d ", ival[i]);

  17.   fputc(stream, 'n');

  18. }

  19.  

  20. /**

  21.  * Main.

  22.  */"Gimme up to %zu integer values: "/**

  23.    * Use qsort library function to sort the array using the desc

  24.    * comparison function.

  25.    */"Sorted array as follows: n"

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions