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 - 15 of 15 Answers

Code
  1. void swap(int a[],j)

  2. {

  3. t=a[j];

  4. a[j]=a[j+1];

  5. a[j+1]=t;

  6. }

  7. void sort(int a[])

  8. {

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

  10. for(j=i;j<29;j++)

  11. if(a[j]>a[j+1])

  12. swap(a,j);

  13. }

  14. void main()

  15. {

  16. int a[35],i,j,t;

  17. printf("enter 30 numbersn");

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

  19. scanf("%d",&a[i]);

  20. sort(a);

  21. printf("sorted array is n");

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

  23. printf("%dt",a[i]);

  24. getch();

  25. }

  Was this answer useful?  Yes

Code
  1.  #include<stdio.h>

  2.  #include<conio.h>

  3. void swap(int a[],int j,int i)

  4. {

  5. int t;

  6. t=a[i];

  7. a[i]=a[j+1];

  8. a[j+1]=t;

  9. }

  10.  

  11.  

  12. void sort(int a[])

  13. {

  14. int i,j;

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

  16. for(j=i;j<4;j++)

  17. if(a[i]<a[j+1])

  18. swap(a,j,i);

  19. }

  20.  

  21.  

  22.  

  23. void main()

  24. {

  25. int a[6],i,j,t;

  26. printf("enter 5 numbersnn");

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

  28. scanf("%d", &a[i]);

  29. sort(a);

  30. printf("sorted array is n");

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

  32. printf("n%d", a[i]);

  33. getch();

  34. }

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. void sort(int a[]);

  3. void swap(int *,int *);

  4.  

  5.  

  6. int main()

  7. {

  8.  int a[30],b[30];        

  9.  int i,j;

  10.  printf("eneter 10 number");

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

  12.  {

  13.   scanf("%d",&a[i]);

  14.  }

  15.  memcpy(b,a,sizeof(a));   /* here we have stored the original array into array b

  16.  sort(a);

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

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

  19.  {

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

  21.  }

  22.  printf("n");

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

  24.  {

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

  26.  }

  27.  

  28.  return 0;

  29. }

  30.  

  31. void sort(int arr[])     /* this function is used to sort the array */

  32. {

  33.  int i,j;

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

  35.  {

  36.   for(j=i+1;j<10;j++)

  37.   {

  38.    if(arr[i]<arr[j])

  39.    {

  40.     swap(&arr[i],&arr[j]);

  41.    }

  42.   }

  43.  }

  44. }

  45.  

  46. void swap(int *p,int *q)  /* This is used to swap the two elements */

  47. {

  48.  int temp;

  49.  temp=*p;

  50.  *p=*q;

  51.  *q=temp;

  52. }

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.  */

  8. void getValues(FILE *stream, int *ival, size_t max_len, size_t *read_len)

  9. {

  10.   char inbuf[20]; // will read input values as text and covert with strtol

  11.  

  12.   *read_len = 0;

  13.   while (*read_len < max_len && fgets(inbuf, sizeof inbuf, stream) != NULL)

  14.   {

  15.     char *chk;

  16.     long tmp = strtol(inbuf, *chk, 10);

  17.     if (isspace(*chk) || *chk == 0)

  18.     {

  19.       ival[(*read_len)++] = (int) tmp;

  20.     }

  21.   }

  22.   if (ferror(stream))

  23.   {

  24.     fprintf(stderr, "getValues: error encountered during read; exiting...n");

  25.     exit(0);

  26.   }

  27. }

  28.  

  29. /**

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

  31.  */

  32. int desc(const void *v1, const void *v2)

  33. {

  34.   const int *iv1 = v1;

  35.   const int *iv2 = v2;

  36.  

  37.   if (*iv1 > *iv2) return -1;

  38.   else if (*iv1 < *iv2) return 1;

  39.   else return 0;

  40. }

  41.  

  42. /**

  43.  * Write sorted array contents to stream.

  44.  */

  45. void printArray(FILE *stream, int *ival, size_t len)

  46. {

  47.   size_t i;

  48.   for (i = 0; i < len; i++)

  49.     fprintf(stream, "%d ", ival[i]);

  50.   fputc(stream, 'n');

  51. }

  52.  

  53. /**

  54.  * Main.

  55.  */

  56. int main(void)

  57. {

  58.   int arr[30];

  59.   size_t nValues;

  60.  

  61.   printf("Gimme up to %zu integer values: ", sizeof arr / sizeof *arr);

  62.   fflush(stdout);

  63.  

  64.   getValues(stdin, arr, sizeof arr / sizeof *arr, &nValues);

  65.   /**

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

  67.    * comparison function.

  68.    */

  69.   qsort(arr, nValues, sizeof *arr, desc);

  70.  

  71.   printf("Sorted array as follows: n");

  72.   printArray(stdout, arr, nValues);

  73.  

  74.   return 0;

  75. }

  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