Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <limits.h>
- #include <string.h>
- int search(int *, int, int);
- int getInt(int, int, const char*);
- int *copy(int *, int);
- void searchFuntion(int *, int);
- void allocMemory(int **, int *);
- void addToLast(int *, int);
- void addValue(int **, int *);
- void sortAsc(int *, int);
- void display(int *, int);
- void clean(void);
- void menu(void); //CHANGED'
- void displayInARange(int *, int, int, int); //ADDED
- int main(void) {
- int *array = NULL;
- int array_size = 0;
- int choose;
- int value;
- int min, max; //ADDED
- int keepgoing = 1;
- do { //CHANGED FROM HERE
- menu();
- scanf("%d", &choose);
- switch (choose) {
- case 1:
- addValue(&array, &array_size);
- break;
- case 2:
- if (!array_size) {
- puts("No element in array, please input first");
- addValue(&array, &array_size);
- } else {
- searchFuntion(array, array_size);
- }
- break;
- case 3:
- display(array, array_size);
- break;
- case 4:
- printf("Input min value: ");
- min = getInt(INT_MIN, INT_MAX, "Please enter an integer: ");
- printf("Input max value: ");
- max = getInt(min, INT_MAX, "Please enter an integer: ");
- printf("Print in range from %d to %d:\n", min, max);
- displayInARange(array, array_size, min, max);
- break;
- case 5:
- sortAsc(array, array_size);
- puts("After sort:");
- display(array, array_size);
- break;
- default:
- keepgoing = !1;
- }
- } while (keepgoing);
- free(array); //freeing memory
- return 0;
- }
- /**
- * Print element of array that in range from (max) to (min)
- * @param array: the source array
- * @param array_size: size of the source array
- * @param min: min value, inclusively
- * @param max: max value, inclusively
- */
- void displayInARange(int* array, int array_size, int min, int max) {
- if (array_size) {
- int i;
- for (i = 0; i < array_size; i++) {
- if (array[i] >= min && array[i] <= max) {
- printf("(%d) ", array[i]);
- }
- }
- } else {
- puts("Empty array . . .");
- }
- }
- /**
- * This function's job is allocation the memory for the array passed into it
- * @param array: the array need to alloc memory
- * @param array_size: the size of the array
- */
- void allocMemory(int **array, int *array_size) {
- /*
- * if the array point to NULL them malloc it with one element
- * also make the size to 0
- */
- if (!*array) {
- *array_size = 0;
- (*array_size)++;
- *array = malloc(*array_size * sizeof (int));
- return;
- }
- /*
- * if not null, them realloc it
- */
- *array = realloc(*array, *array_size * sizeof (int));
- }
- /**
- * Make user input an integer them assigned it to the last element
- * @param array: the array need to add element to last
- * @param array_size: size of array
- */
- void addToLast(int *array, int array_size) {
- printf("Enter a value: ");
- int number = getInt(INT_MIN, INT_MAX, "Please enter an integer: ");
- //assign number's value to last element
- array[array_size - 1] = number;
- }
- /**
- * This funtion increase the size of array by one,
- * and add an element to the last
- * @param array: the array need to add element to last
- * @param size: size of array
- */
- void addValue(int **array, int *array_size) {
- (*array_size)++;
- allocMemory(array, array_size);
- addToLast(*array, *array_size);
- }
- /**
- * Simply print out the menu
- */
- void menu(void) {
- puts("\n");
- puts("===================================================================");
- puts("1. Add a value");
- puts("2. Search a value");
- puts("3. Print out the array");
- puts("4. Print out values in a range of inputted min and max values, inclusively");
- puts("5. Sort the array in ascending order");
- puts("Others - Quit");
- printf("Enter your choice: ");
- }
- /**
- * Sort the array Ascending
- * @param array: the array need to sort
- * @param array_size: size of the array
- */
- void sortAsc(int *array, int array_size) {
- int i;
- for (i = 0; i < array_size; i++) {
- int j;
- for (j = i + 1; j < array_size; j++) {
- if (array[i] > array[j]) {
- //swapping
- int temp = array[i];
- array[i] = array[j];
- array[j] = temp;
- }
- }
- }
- }
- /**
- * Print out all element of the array
- * @param array: array to be printed
- * @param array_size: size of array
- */
- void display(int *array, int array_size) {
- if (array_size) {
- int i;
- for (i = 0; i < array_size; i++) {
- printf("(%d) ", array[i]);
- }
- } else {
- puts("Empty array . . .");
- }
- }
- /**
- * Search an value in the array
- * @param array: array to be search
- * @param array_size: size of array
- */
- void searchFuntion(int *array, int array_size) {
- printf("Enter value to search: ");
- int value = getInt(INT_MIN, INT_MAX, "Please enter an integer: ");
- int index = search(array, array_size, value);
- if (index == -1) {
- printf("No element in array has value %d.", value);
- } else {
- printf("Element %d located at %d position.", value, index);
- }
- }
- /**
- * Search an value in the array and return it's index
- * @param array: array to be search
- * @param array_size: size of the array
- * @param value: the value to search
- * @return the index of the first element in the array that have
- * value
- */
- int search(int *array, int array_size, int value) {
- int index = -1;
- int i;
- for (i = 0; i < array_size; i++) {
- if (array[i] == value) {
- index = i;
- break;
- }
- }
- return index;
- }
- /**
- * Function to validate int input from user
- * @param min minimum value user can enter
- * @param max maximum value user can enter
- * @param error the String print when user enter invalid input
- * @return valid number
- */
- int getInt(int min, int max, const char* error) {
- int number;
- int k;
- char c;
- while (1) {
- k = scanf("%d%c", &number, &c);
- if (k != 2 || c != '\n') {
- clean();
- printf("%s", error);
- } else if (number < min || number > max)
- printf("Please input number form %d to %d: ", min, max);
- else return number;
- }
- }
- /**
- * Cleaning the '\n' character in the stream
- */
- void clean(void) {
- while (getchar() != '\n');
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement