Advertisement
dmilicev

sort_matrix_extract_unique_elements.c

Feb 17th, 2021
1,190
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  
  3.     sort_matrix_extract_unique_elements.c
  4.  
  5.     Sort the matrix and extract the unique elements.
  6.  
  7. https://www.faceprep.in/c/program-to-remove-duplicate-elements-in-an-array/
  8.  
  9.  
  10.     You can find all my C programs at Dragan Milicev's pastebin:
  11.  
  12.     https://pastebin.com/u/dmilicev
  13.  
  14. */
  15.  
  16. #include <stdio.h>
  17.  
  18. #define MAX_SIZE 10
  19. //#define ROWS 10
  20. //#define COLUMNS 10
  21.  
  22. // Displays array arr[] that has n elements
  23. // text[] describes the shown array arr[]
  24. void display_array(char text[],int arr[],int n)
  25. {
  26.     int i;
  27.  
  28.     printf("%s",text);
  29.     for(i=0;i<n;i++)
  30.         printf("%4d", arr[i]);
  31.  
  32.     printf("\n");
  33. }
  34.  
  35. // Displays a matrix M [] [] that has r rows and c columns
  36. void display_matrix( char *text, int M[][MAX_SIZE], int r, int c )
  37. {
  38.     int i, j;
  39.  
  40.     printf("\n%s\n\n",text);
  41.  
  42.     for(i=0;i<r;i++) {                  // print matrix M[][]
  43.         for(j=0;j<c;j++)
  44.             printf(" %4d", M[i][j]);
  45.  
  46.         printf("\n\n");                 // new row of matrix
  47.     }
  48. }
  49.  
  50. // function bubbleSort to sort the array arr[] of n elements in descending order
  51. void bubbleSort(int arr[], int n)
  52. {
  53.     int i, j, temp;
  54.  
  55.     for (i = 0; i < n-1; i++)
  56.         for (j = 0; j < n-i-1; j++)     // Last i elements are already in place
  57.             if (arr[j] < arr[j+1])      // arr[j] > arr[j+1] for ascending order
  58.             {
  59.                 temp = arr[j];
  60.                 arr[j] = arr[j+1];
  61.                 arr[j+1] = temp;
  62.             }
  63. }
  64.  
  65. // remove duplicate elements in an sorted array
  66. int remove_duplicate_elements(int arr[], int n)
  67. {
  68.     if (n==0 || n==1)
  69.         return n;
  70.  
  71.     int temp[n];
  72.     int j = 0;
  73.     int i;
  74.  
  75.     for (i=0; i<n-1; i++)
  76.         if (arr[i] != arr[i+1])
  77.             temp[j++] = arr[i];
  78.  
  79.     temp[j++] = arr[n-1];
  80.  
  81.     for (i=0; i<j; i++)
  82.         arr[i] = temp[i];
  83.  
  84.     return j;
  85. }
  86.  
  87.  
  88. int main(void)
  89. {
  90.     int i, j, k=0, r=3, c=3, n=r*c;
  91.     // r is the number of rows and c is the number of columns of the matrix M[][]
  92.  
  93.     int M[MAX_SIZE][MAX_SIZE] = { { 5, 4, 5 },
  94.                                   { 2, 0, 4 },
  95.                                   { 1, 5, 2 }  };   // matrix
  96.     int arr[n];     // array
  97.  
  98.     // collecting matrix elements in an array
  99.     for(i=0; i<r; i++)
  100.         for(j=0; j<c; j++)
  101.             arr[k++] = M[i][j];
  102.  
  103.     display_matrix(" \n MATRIX = \n", M, r, c);
  104.  
  105.     display_array("\n\tARRAY = ", arr, n);
  106.  
  107.     bubbleSort(arr,r*c);
  108.  
  109.     display_array("\n SORTED ARRAY = ", arr, n);
  110.  
  111.     n = remove_duplicate_elements(arr,n);
  112.  
  113.     display_array("\n UNIQUE ARRAY = ", arr, n);
  114.  
  115.  
  116.     return 0;
  117.  
  118. } // main()
  119.  
Advertisement
RAW Paste Data Copied
Advertisement