Advertisement
_adv27

C.S.P0044 - COMMENTED

Aug 23rd, 2017
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.82 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <limits.h>
  4. #include <string.h>
  5.  
  6.  
  7.  
  8. int search(int *, int, int);
  9. int getInt(int, int, const char*);
  10. int *copy(int *, int);
  11. void searchFuntion(int *, int);
  12.  
  13. void allocMemory(int **, int *);
  14. void addToLast(int *, int);
  15. void addValue(int **, int *);
  16.  
  17. void sortAsc(int *, int);
  18.  
  19. void display(int *, int);
  20. void clean(void);
  21. void menu(void); //CHANGED'
  22.  
  23. void displayInARange(int *, int, int, int); //ADDED
  24.  
  25. int main(void) {
  26.     int *array = NULL;
  27.     int array_size = 0;
  28.  
  29.     int choose;
  30.  
  31.     int value;
  32.  
  33.     int min, max; //ADDED
  34.  
  35.     int keepgoing = 1;
  36.     do { //CHANGED FROM HERE
  37.         menu();
  38.         scanf("%d", &choose);
  39.         switch (choose) {
  40.             case 1:
  41.                 addValue(&array, &array_size);
  42.                 break;
  43.             case 2:
  44.                 if (!array_size) {
  45.                     puts("No element in array, please input first");
  46.                     addValue(&array, &array_size);
  47.                 } else {
  48.                     searchFuntion(array, array_size);
  49.                 }
  50.                 break;
  51.             case 3:
  52.                 display(array, array_size);
  53.                 break;
  54.             case 4:
  55.                 printf("Input min value: ");
  56.                 min = getInt(INT_MIN, INT_MAX, "Please enter an integer: ");
  57.                 printf("Input max value: ");
  58.                 max = getInt(min, INT_MAX, "Please enter an integer: ");
  59.                 printf("Print in range from %d to %d:\n", min, max);
  60.                 displayInARange(array, array_size, min, max);
  61.                 break;
  62.             case 5:
  63.                 sortAsc(array, array_size);
  64.                 puts("After sort:");
  65.                 display(array, array_size);
  66.                 break;
  67.             default:
  68.                 keepgoing = !1;
  69.         }
  70.     } while (keepgoing);
  71.  
  72.     free(array); //freeing memory
  73.  
  74.     return 0;
  75. }
  76.  
  77. /**
  78.  * Print element of array that in range from (max) to (min)
  79.  * @param array: the source array
  80.  * @param array_size: size of the source array
  81.  * @param min: min value, inclusively
  82.  * @param max: max value, inclusively
  83.  */
  84. void displayInARange(int* array, int array_size, int min, int max) {
  85.     if (array_size) {
  86.         int i;
  87.         for (i = 0; i < array_size; i++) {
  88.             if (array[i] >= min && array[i] <= max) {
  89.                 printf("(%d) ", array[i]);
  90.             }
  91.         }
  92.     } else {
  93.         puts("Empty array . . .");
  94.     }
  95. }
  96.  
  97. /**
  98.  * This function's job is allocation the memory for the array passed into it
  99.  * @param array: the array need to alloc memory
  100.  * @param array_size: the size of the array
  101.  */
  102. void allocMemory(int **array, int *array_size) {
  103.     /*
  104.      * if the array point to NULL them malloc it with one element
  105.      * also make the size to 0
  106.      */
  107.     if (!*array) {
  108.         *array_size = 0;
  109.         (*array_size)++;
  110.         *array = malloc(*array_size * sizeof (int));
  111.         return;
  112.     }
  113.     /*
  114.      * if not null, them realloc it
  115.      */
  116.     *array = realloc(*array, *array_size * sizeof (int));
  117. }
  118.  
  119. /**
  120.  * Make user input an integer them assigned it to the last element
  121.  * @param array: the array need to add element to last
  122.  * @param array_size: size of array
  123.  */
  124. void addToLast(int *array, int array_size) {
  125.     printf("Enter a value: ");
  126.     int number = getInt(INT_MIN, INT_MAX, "Please enter an integer: ");
  127.     //assign number's value to last element
  128.     array[array_size - 1] = number;
  129. }
  130.  
  131. /**
  132.  * This funtion increase the size of array by one,
  133.  * and add an element to the last
  134.  * @param array: the array need to add element to last
  135.  * @param size: size of array
  136.  */
  137. void addValue(int **array, int *array_size) {
  138.     (*array_size)++;
  139.     allocMemory(array, array_size);
  140.     addToLast(*array, *array_size);
  141. }
  142.  
  143. /**
  144.  * Simply print out the menu
  145.  */
  146. void menu(void) {
  147.     puts("\n");
  148.     puts("===================================================================");
  149.     puts("1. Add a value");
  150.     puts("2. Search a value");
  151.     puts("3. Print out the array");
  152.     puts("4. Print out values in a range of inputted min and max values, inclusively");
  153.     puts("5. Sort the array in ascending order");
  154.     puts("Others - Quit");
  155.     printf("Enter your choice: ");
  156. }
  157.  
  158. /**
  159.  * Sort the array Ascending
  160.  * @param array: the array need to sort
  161.  * @param array_size: size of the array
  162.  */
  163. void sortAsc(int *array, int array_size) {
  164.     int i;
  165.     for (i = 0; i < array_size; i++) {
  166.         int j;
  167.         for (j = i + 1; j < array_size; j++) {
  168.             if (array[i] > array[j]) {
  169.                 //swapping
  170.                 int temp = array[i];
  171.                 array[i] = array[j];
  172.                 array[j] = temp;
  173.             }
  174.         }
  175.     }
  176. }
  177.  
  178. /**
  179.  * Print out all element of the array
  180.  * @param array: array to be printed
  181.  * @param array_size: size of array
  182.  */
  183. void display(int *array, int array_size) {
  184.     if (array_size) {
  185.         int i;
  186.         for (i = 0; i < array_size; i++) {
  187.             printf("(%d) ", array[i]);
  188.         }
  189.     } else {
  190.         puts("Empty array . . .");
  191.     }
  192. }
  193.  
  194. /**
  195.  * Search an value in the array
  196.  * @param array: array to be search
  197.  * @param array_size: size of array
  198.  */
  199. void searchFuntion(int *array, int array_size) {
  200.     printf("Enter value to search: ");
  201.     int value = getInt(INT_MIN, INT_MAX, "Please enter an integer: ");
  202.     int index = search(array, array_size, value);
  203.     if (index == -1) {
  204.         printf("No element in array has value %d.", value);
  205.     } else {
  206.         printf("Element %d located at %d position.", value, index);
  207.     }
  208. }
  209.  
  210. /**
  211.  * Search an value in the array and return it's index
  212.  * @param array: array to be search
  213.  * @param array_size: size of the array
  214.  * @param value: the value to search
  215.  * @return the index of the first element in the array that have
  216.  * value
  217.  */
  218. int search(int *array, int array_size, int value) {
  219.     int index = -1;
  220.     int i;
  221.     for (i = 0; i < array_size; i++) {
  222.         if (array[i] == value) {
  223.             index = i;
  224.             break;
  225.         }
  226.     }
  227.     return index;
  228. }
  229.  
  230. /**
  231.  * Function to validate int input from user
  232.  * @param min minimum value user can enter
  233.  * @param max maximum value user can enter
  234.  * @param error the String print when user enter invalid input
  235.  * @return valid number
  236.  */
  237. int getInt(int min, int max, const char* error) {
  238.     int number;
  239.     int k;
  240.     char c;
  241.     while (1) {
  242.         k = scanf("%d%c", &number, &c);
  243.         if (k != 2 || c != '\n') {
  244.             clean();
  245.             printf("%s", error);
  246.         } else if (number < min || number > max)
  247.             printf("Please input number form %d to %d: ", min, max);
  248.         else return number;
  249.     }
  250. }
  251.  
  252. /**
  253.  * Cleaning the '\n' character in the stream
  254.  */
  255. void clean(void) {
  256.     while (getchar() != '\n');
  257. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement