Advertisement
_adv27

C.S.P0043 - Array - COMMENTED

Aug 23rd, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.44 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <limits.h>
  4. #include <string.h>
  5.  
  6. /*
  7.  * CONST DECLARATION
  8.  */
  9. int const SORT_ASC = 1;
  10. int const SORT_DESC = -1;
  11.  
  12. int const REMOVE_FIRST = 0;
  13. int const REMOVE_ALL = 1;
  14. // END of CONST DEDCLARATION
  15.  
  16. int search(int *, int, int);
  17. int getInt(int, int, const char*);
  18. int *copy(int *, int);
  19. void searchFuntion(int *, int);
  20.  
  21. void allocMemory(int **, int *);
  22. void addToLast(int *, int);
  23. void addValue(int **, int *);
  24.  
  25. void sort(int *, int, int);
  26. void sortAsc(int *, int);
  27. void sortDesc(int *, int);
  28.  
  29. void removeFunction(int **, int*, int);
  30. void removeFirst(int **, int *, int);
  31. void removeAll(int **, int *, int);
  32.  
  33. void display(int *, int);
  34. void clean(void);
  35. void menu(void);
  36.  
  37. int main(void) {
  38.     int *array = NULL;
  39.     int array_size = 0;
  40.  
  41.     int choose;
  42.  
  43.     int value;
  44.  
  45.     int keepgoing = 1;
  46.     do {
  47.         menu();
  48.         scanf("%d", &choose);
  49.         switch (choose) {
  50.             case 1:
  51.                 addValue(&array, &array_size);
  52.                 break;
  53.             case 2:
  54.                 if (!array_size) {
  55.                     puts("No element in array, please input first");
  56.                     addValue(&array, &array_size);
  57.                 } else {
  58.                     searchFuntion(array, array_size);
  59.                 }
  60.                 break;
  61.             case 3:
  62.                 removeFunction(&array, &array_size, REMOVE_FIRST);
  63.                 break;
  64.             case 4:
  65.                 removeFunction(&array, &array_size, REMOVE_ALL);
  66.                 break;
  67.             case 5:
  68.                 display(array, array_size);
  69.                 break;
  70.             case 6:
  71.                 sort(array, array_size, SORT_ASC);
  72.                 break;
  73.             case 7:
  74.                 sort(array, array_size, SORT_DESC);
  75.                 break;
  76.             default:
  77.                 keepgoing = !1;
  78.         }
  79.     } while (keepgoing);
  80.  
  81.     free(array); //freeing memory
  82.  
  83.     return 0;
  84. }
  85.  
  86. /**
  87.  * This function's job is allocation the memory for the array passed into it
  88.  * @param array: the array need to alloc memory
  89.  * @param array_size: the size of the array
  90.  */
  91. void allocMemory(int **array, int *array_size) {
  92.     /*
  93.      * if the array point to NULL them malloc it with one element
  94.      * also make the size to 0
  95.      */
  96.     if (!*array) {
  97.         *array_size = 0;
  98.         (*array_size)++;
  99.         *array = malloc(*array_size * sizeof (int));
  100.         return;
  101.     }
  102.     /*
  103.      * if not null, them realloc it
  104.      */
  105.     *array = realloc(*array, *array_size * sizeof (int));
  106. }
  107.  
  108. /**
  109.  * Make user input an integer them assigned it to the last element
  110.  * @param array: the array need to add element to last
  111.  * @param array_size: size of array
  112.  */
  113. void addToLast(int *array, int array_size) {
  114.     printf("Enter a value: ");
  115.     int number = getInt(INT_MIN, INT_MAX, "Please enter an integer: ");
  116.     //assign number's value to last element
  117.     array[array_size - 1] = number;
  118. }
  119.  
  120. /**
  121.  * This funtion increase the size of array by one,
  122.  * and add an element to the last
  123.  * @param array: the array need to add element to last
  124.  * @param size: size of array
  125.  */
  126. void addValue(int **array, int *array_size) {
  127.     (*array_size)++;
  128.     allocMemory(array, array_size);
  129.     addToLast(*array, *array_size);
  130. }
  131.  
  132. /**
  133.  * Simply print out the menu
  134.  */
  135. void menu(void) {
  136.     puts("\n");
  137.     puts("===================================================================");
  138.     puts("1. Add a value");
  139.     puts("2. Search a value");
  140.     puts("3. Remove the first existence of a value");
  141.     puts("4. Remove all existences of a value");
  142.     puts("5. Print out the array");
  143.     puts("6. Sort the array in ascending order (positions are preserved)");
  144.     puts("7. Sort the array in descending order (positions are preserved)");
  145.     puts("Others - Quit");
  146.     printf("Enter your choice: ");
  147. }
  148.  
  149. /**
  150.  * Return the copy version of the array passed in
  151.  * @param array: the array need to copy
  152.  * @param array_size: size of the source array
  153.  * @return a new array that copy the array passed in
  154.  */
  155. int *copy(int *array, int array_size) {
  156.     int *array_copy = NULL;
  157.     array_copy = malloc(array_size * sizeof (int));
  158.     memcpy(array_copy, array, array_size * sizeof (int)); //copying byte by byte
  159.     return array_copy;
  160. }
  161.  
  162. /**
  163.  * Sort the array in Ascending/Descending
  164.  * @param array: the array need to sort
  165.  * @param array_size: size of the array
  166.  * @param option: (1) means sort Ascending, (-1) means sort Descending
  167.  */
  168. void sort(int *array, int array_size, int option) {
  169.     int *array_copy = copy(array, array_size);
  170.     if (option == SORT_ASC) {
  171.         //sort Ascending
  172.         sortAsc(array_copy, array_size);
  173.     } else if (option == SORT_DESC) {
  174.         //sort Descending
  175.         sortDesc(array_copy, array_size);
  176.     }
  177.     display(array_copy, array_size);
  178.     free(array_copy);
  179. }
  180.  
  181. /**
  182.  * Sort the array Ascending
  183.  * @param array: the array need to sort
  184.  * @param array_size: size of the array
  185.  */
  186. void sortAsc(int *array, int array_size) {
  187.     int i;
  188.     for (i = 0; i < array_size; i++) {
  189.         int j;
  190.         for (j = i + 1; j < array_size; j++) {
  191.             if (array[i] > array[j]) {
  192.                 //swapping
  193.                 int temp = array[i];
  194.                 array[i] = array[j];
  195.                 array[j] = temp;
  196.             }
  197.         }
  198.     }
  199. }
  200.  
  201. /**
  202.  * Sort the array Descending
  203.  * @param array: the array need to sort
  204.  * @param array_size: size of the array
  205.  */
  206. void sortDesc(int *array, int array_size) {
  207.     int i;
  208.     for (i = 0; i < array_size; i++) {
  209.         int j;
  210.         for (j = i + 1; j < array_size; j++) {
  211.             if (array[i] < array[j]) {
  212.                 //swapping
  213.                 int temp = array[i];
  214.                 array[i] = array[j];
  215.                 array[j] = temp;
  216.             }
  217.         }
  218.     }
  219. }
  220.  
  221. /**
  222.  * This funtion help user to remove (all/one) element have the particular value
  223.  * @param array: array to remove element
  224.  * @param array_size: size of the source array
  225.  * @param option: (0) means REMOVE_FIRST, (1) means REMOVE_ALL
  226.  */
  227. void removeFunction(int **array, int *array_size, int option) {
  228.     printf("Enter value to remove %s:", option == REMOVE_ALL ? "all" : "first");
  229.  
  230.     int value = getInt(INT_MIN, INT_MAX, "Please enter an integer: ");
  231.  
  232.     if (option == REMOVE_ALL) {
  233.         removeAll(array, array_size, value);
  234.     } else if (option == REMOVE_FIRST) {
  235.         removeFirst(array, array_size, value);
  236.     }
  237. }
  238.  
  239. /**
  240.  * Remove all element has particular value
  241.  * @param array: the array need to remove element
  242.  * @param array_size: size of the array
  243.  * @param value: value to remove all
  244.  */
  245. void removeAll(int **array, int *array_size, int value) {
  246.     while (search(*array, *array_size, value) != -1) {
  247.         removeFirst(array, array_size, value);
  248.     }
  249. }
  250.  
  251. /**
  252.  * Remove the first element has particular value
  253.  * @param array: the array need to remove element
  254.  * @param array_size: size of the array
  255.  * @param value: value to remove first
  256.  */
  257. void removeFirst(int **array, int *array_size, int value) {
  258.     int index = search(*array, *array_size, value);
  259.     if (index != -1) {
  260.         int i;
  261.         for (i = index; i < *array_size; i++) {
  262.             (*array)[i] = (*array)[i + 1];
  263.         }
  264.         (*array_size)--; //decrease size
  265.         allocMemory(array, array_size);
  266.     }
  267. }
  268.  
  269. /**
  270.  * Print out all element of the array
  271.  * @param array: array to be printed
  272.  * @param array_size: size of array
  273.  */
  274. void display(int *array, int array_size) {
  275.     if (array_size) {
  276.         int i;
  277.         for (i = 0; i < array_size; i++) {
  278.             printf("(%d) ", array[i]);
  279.         }
  280.     } else {
  281.         puts("Empty array . . .");
  282.     }
  283. }
  284.  
  285. /**
  286.  * Search an value in the array
  287.  * @param array: array to be search
  288.  * @param array_size: size of array
  289.  */
  290. void searchFuntion(int *array, int array_size) {
  291.     printf("Enter value to search: ");
  292.     int value = getInt(INT_MIN, INT_MAX, "Please enter an integer: ");
  293.     int index = search(array, array_size, value);
  294.     if (index == -1) {
  295.         printf("No element in array has value %d.", value);
  296.     } else {
  297.         printf("Element %d located at %d position.", value, index);
  298.     }
  299. }
  300.  
  301. /**
  302.  * Search an value in the array and return it's index
  303.  * @param array: array to be search
  304.  * @param array_size: size of the array
  305.  * @param value: the value to search
  306.  * @return the index of the first element in the array that have
  307.  * value
  308.  */
  309. int search(int *array, int array_size, int value) {
  310.     int index = -1;
  311.     int i;
  312.     for (i = 0; i < array_size; i++) {
  313.         if (array[i] == value) {
  314.             index = i;
  315.             break;
  316.         }
  317.     }
  318.     return index;
  319. }
  320.  
  321. /**
  322.  * Function to validate int input from user
  323.  * @param min minimum value user can enter
  324.  * @param max maximum value user can enter
  325.  * @param error the String print when user enter invalid input
  326.  * @return valid number
  327.  */
  328. int getInt(int min, int max, const char* error) {
  329.     int number;
  330.     int k;
  331.     char c;
  332.     while (1) {
  333.         k = scanf("%d%c", &number, &c);
  334.         if (k != 2 || c != '\n') {
  335.             clean();
  336.             printf("%s", error);
  337.         } else if (number < min || number > max)
  338.             printf("Please input number form %d to %d: ", min, max);
  339.         else return number;
  340.     }
  341. }
  342.  
  343. /**
  344.  * Cleaning the '\n' character in the stream
  345.  */
  346. void clean(void) {
  347.     while (getchar() != '\n');
  348. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement