Advertisement
ossifrage

Untitled

Apr 8th, 2015
1,107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.70 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<time.h>
  4. #define MAX 100
  5.  
  6. void        display_menu();
  7. int     check_option(int option);
  8. int     check_size (int size);
  9. void        initialize_2Darray(int x[][MAX], int size);
  10. void        print_2Darray(int x[][MAX], int size);
  11. void        initialize_1Darray(int y[], int size);
  12. void        print_1Darray(int y[], int size);
  13. int     search_min (int x[][MAX], int r, int c, int size);
  14. int     count_match(int x[][MAX], int y[], int size, int r);
  15. int     closest_row(int x[][MAX], int y[], int size);
  16. void        sort_1Darray(int y[], int size);
  17. void        sort_2Darray(int x[][MAX], int size);
  18.  
  19. int
  20. main()
  21. {
  22.     int     size      , r, c, option;
  23.     int     exit = 0;
  24.     //exits program when = 1
  25.         int     x          [MAX][MAX], y[MAX];
  26.     srand(time(NULL));
  27.     printf("Enter the size: ");
  28.     scanf("%d", &size);
  29.     while (check_size(size) == 0) {
  30.         printf("\nInvalid input, enter the size of the array again: ");
  31.         scanf("%d", &size);
  32.     }
  33.     while (exit != 6) {
  34.         initialize_2Darray(x, size);
  35.         initialize_1Darray(y, size);
  36.         display_menu();
  37.         scanf("%d", &option);
  38.         while (check_option(option) == 0) {
  39.             printf("Invalid option, enter again: ");
  40.             scanf("%d", &option);
  41.         }
  42.         //Search Min Operation
  43.             if (option == 1) {
  44.             print_2Darray(x, size);
  45.             printf("Enter the row: ");
  46.             scanf("%d", &r);
  47.             printf("\nEnter the col: ");
  48.             scanf("%d", &c);
  49.             printf("The smallest number present in row %d and col %d is %d", r, c, search_min(x, r, c, size));
  50.         }
  51.         //Count Matches Op.
  52.             else if (option == 2) {
  53.             printf("Count Matches Operation\n\n2D array\n");
  54.             print_2Darray(x, size);
  55.             printf("\n1D array\n");
  56.             print_1Darray(y, size);
  57.             printf("\nEnter the row: ");
  58.             scanf("%d", &r);
  59.             if (count_match(x, y, size, r) > 0)
  60.                 printf("There are %d matches from 1D array present in 2D array", count_match(x, y, size, r));
  61.             else
  62.                 printf("There are no numbers from 1D array present in 2D array");
  63.         }
  64.         //Closest Row Op.
  65.             else if (option == 3) {
  66.             printf("\nClosest Row Operation\n\n2D array\n");
  67.             print_2Darray(x, size);
  68.             printf("\n1D array\n");
  69.             print_1Darray(y, size);
  70.             printf("Row closest to the 1D array is row %d", closest_row(x, y, size));
  71.         }
  72.         //Sort 1 D Array Op.
  73.             else if (option == 4) {
  74.             printf("Sort 1D Array Operation\n\n1D Array before sorting:\n");
  75.             print_1Darray(y, size);
  76.             sort_1Darray(y, size);
  77.             printf("\n\n1D Array after sorting:\n");
  78.             print_1Darray(y, size);
  79.         }
  80.         //Sort 2 D Array Op.
  81.             else if (option == 5) {
  82.             printf("\nSort 2D Array Option\n\n2D Array before sorting:\n");
  83.             print_2Darray(x, size);
  84.             sort_2Darray(x, size);
  85.             printf("\n\n2D Array after sorting:\n");
  86.             print_2Darray(x, size);
  87.         }
  88.         //Exit
  89.             else if (option == 6)
  90.             exit = 6;
  91.     }
  92.     return 0;
  93. }
  94.  
  95. void
  96. display_menu()
  97. {
  98.     printf("\nArray operations, your options are:\n\n1: Search Min\n2: Count Matches\n3: Closest Row\n4: Sort 1D Array\n5: Sort 2D Array\n6: Exit\nEnter the operation you want to perform: ");
  99. }
  100.  
  101. int
  102. check_option(int option)
  103. {
  104.     if (option > 0 && option < 7)
  105.         return 1;
  106.     else
  107.         return 0;
  108. }
  109.  
  110. int
  111. check_size(int size)
  112. {
  113.     if (size > 0 && size <= 100)
  114.         return 1;
  115.     else
  116.         return 0;
  117. }
  118.  
  119. void
  120. initialize_2Darray(int x[][MAX], int size)
  121. {
  122.     int     i         , s;
  123.     //counters
  124.         for (i = 0; i < size; i++) {
  125.         for (s = 0; s < size; s++) {
  126.             x[i][s] = rand() % 10;
  127.         }
  128.     }
  129. }
  130.  
  131. void
  132. print_2Darray(int x[][MAX], int size)
  133. {
  134.     int     i         , s;
  135.     //counters
  136.         for (i = 0; i < size; i++) {
  137.         printf("\n");
  138.         for (s = 0; s < size; s++) {
  139.             printf("%d ", x[i][s]);
  140.         }
  141.     }
  142.     printf("\n");
  143. }
  144.  
  145. void
  146. initialize_1Darray(int y[], int size)
  147. {
  148.     int     i         , r;
  149.     for (i = 0; i < size; i++) {
  150.         r = rand() % 10;
  151.         y[i] = r;
  152.     }
  153. }
  154.  
  155. void
  156. print_1Darray(int y[], int size)
  157. {
  158.     int     i;
  159.     //Prints array values until(s) ize is reached
  160.     for             (i = 0; i < size; i++) {
  161.         printf("%d ", y[i]);
  162.     }
  163. }
  164.  
  165. int
  166. search_min(int x[][MAX], int r, int c, int size)
  167. {
  168.     int     i         , j;
  169.     //counters
  170.         int     min = 9;
  171.     for (i = 0; i < size; i++) {
  172.         if (x[r][i] < min) {
  173.             min = x[r][i];
  174.         }
  175.     }
  176.     for (j = 0; j < size; j++) {
  177.         if (x[j][c] < min) {
  178.             min = x[j][c];
  179.         }
  180.     }
  181.     return min;
  182. }
  183.  
  184. int
  185. count_match(int x[][MAX], int y[], int size, int r)
  186. {
  187.     int     i         , j, count;
  188.     for (i = 0; i < size; i++) {
  189.         for (j = 0; j < size; j++) {
  190.             if (y[i] == x[r][j])
  191.                 count++;
  192.         }
  193.         return count;
  194.     }
  195.  
  196.     int     closest_row(int x[][MAX], int y[], int size){
  197.         int     l         , i, j;
  198.                       //counters
  199.         int     sum = 0;
  200.         int     dif = 0;
  201.         int     row       , totaldif;
  202.                       //best matching row & total dif
  203.         for             (l = 0; l < size; l++) {
  204.             for (i = 0; i < size; i++) {
  205.                 for (j = 0; j < size; j++) {
  206.                     dif = abs(y[j] - x[i][j]);
  207.                     sum += dif;
  208.                 }
  209.                 if              (sum < totaldif) {
  210.                     totaldif = sum;
  211.                     row = i;
  212.                 }
  213.                 sum = 0;
  214.             }
  215.         }
  216.         return row;
  217.     }
  218.  
  219.     void        sort_1Darray(int y[], int size){
  220.         int     a         , b, temp;
  221.                       //Loops through the array, swapping values until in proper order of ascension
  222.         for             (a = 0; a < size; a++) {
  223.             for (b = 0; b < size - 1; b++) {
  224.                 if (y[b] > y[b + 1]) {
  225.                     temp = y[b + 1];
  226.                     y[b + 1] = y[b];
  227.                     y[b] = temp;
  228.                 }
  229.             }
  230.         }
  231.     }
  232.  
  233.     void        sort_2Darray(int x[][MAX], int size){
  234.         int     a         , b, c, temp;
  235.                       //Loops through the array, swapping values until in proper order of ascension
  236.         for             (c = 0; c < size; c++) {
  237.             for (a = 0; a < size; a++) {
  238.                 for (b = 0; b < size - 1; b++) {
  239.                     if (x[a][b] > x[a][b + 1]) {
  240.                         temp = x[a][b + 1];
  241.                         x[a][b + 1] = x[a][b];
  242.                         x[a][b] = temp;
  243.                     }
  244.                 }
  245.             }
  246.         }
  247. /**INDENT** Error@234: Stuff missing from end of file */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement