Advertisement
Guest User

Untitled

a guest
Aug 25th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. #include<stdio.h>
  2. #define des //write des for DESCENDING defaul : ASCENDING
  3. int sort(int *,int ); //function to sort the array
  4. int ArrayIO(int *,int ,int ); //to scan and/or printthe elements in the array
  5. int main()
  6. {
  7. printf("Enter the size of array : "); //size of array
  8. int length;
  9. scanf("%d",&length);
  10. int arr[length];
  11. printf("Enter the elements now : \n");
  12. ArrayIO(arr,length,1); // 1 is for scanf
  13. printf("Org. array :\t");
  14. ArrayIO(arr,length,2); // 2 is for printf
  15. sort(arr,length); // sorting the array
  16. #ifdef des
  17. printf("ARRAY DESC :\t");
  18. #endif
  19. #ifndef des
  20. printf("ARRAY ASC :\t");
  21. #endif
  22. ArrayIO(arr,length,2); // printing sorted array
  23. }
  24.  
  25. int sort(int *arr,int length)
  26. {
  27. for(int i=0;i<length;i++)
  28. {
  29. for(int j=i;j<length;j++)
  30. {
  31. #ifdef des
  32. if (arr[i]<arr[j]) // swapping the elements in DESCENDING order
  33. {
  34. arr[i]=arr[j]+arr[i]; // without using another temporary variable
  35. arr[j]=arr[i]-arr[j];
  36. arr[i]=arr[i]-arr[j];
  37. }
  38. #endif
  39.  
  40. #ifndef des
  41. if (arr[i]>arr[j]) // swapping the elements in ASCENDING order
  42. {
  43. arr[i]=arr[j]+arr[i]; // without using another temporary variable
  44. arr[j]=arr[i]-arr[j];
  45. arr[i]=arr[i]-arr[j];
  46. }
  47. #endif
  48. }
  49. }
  50. }
  51.  
  52.  
  53. int ArrayIO(int *arr,int length,int operation)
  54. {
  55. for(int i=0;i<length;i++)
  56. {
  57. if(operation==1)
  58. {
  59. scanf("%d",&arr[i]);
  60. }
  61. else
  62. {
  63. printf("%d\t",arr[i]);
  64. }
  65. }
  66. printf("\n");
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement