Question: Revised version Better style wise You are still trying to recurse on functions -- you should only recurse when you are using recursion to solve

Revised version

Better style wise

You are still trying to recurse on functions -- you should only recurse when you are using recursion to solve a problem that is easier to solve that way. Recursion is not a good way to avoid using while loops -- more expensive in terms of time and memory. getSize should have either a while loop or a do/while loop.

In general, it is good practice to call srand in main -- you do not want it in a function where it might be called more than once in a program -- it resets the random sequence when it is called.

For loop in createArray still needs indentation

getInteger should validate that it is an integer, flush the buffer if it is not -- should also validate that it is in proper range

search is doing a linear search, not a binary search

if statements in main need better indentation

you are only searching for one value, you were to search for three

=======

Definitely better, please fix getInteger getValue and main

#include // sort the array #include // Header file for random numbers srand() and rand() #include // Header file for std::time() to get system time #include

const int LSIZE = 10; const int USIZE = 20; // Function to get the size of the array between // LSIZE and USIZE int getSize() { int n; std::cout << "Please enter integer between " << LSIZE << " and " << USIZE << " for size of array: "; std::cin >> n; // check if size is not in range of LSIZE and USIZE if (n >= LSIZE && n <= USIZE) return n; else { std::cout << "Number does not lie in between " << LSIZE << " and " << USIZE << ", try again. "; return getSize(); } } // Function that dynamically creates array of the given size and return the // pointer to the same int *createArray(int size) { // create dynamic array of size given by size variable int *array = new int[size]; // seed the random variable generator by the system time std::srand(std::time(0)); // fill the array with random variables in range of 1 to 99 for (int i = 0; i < size; ++i) *(array + i) = (std::rand() % 99) + 1; // return the address of dynamic array return array; } // sort the array using system sort available in algorithm // void sortArray(int *array, int size) { // std::sort(array, array + size); // } void sortArray(int *array, int size) { // using insertion sort int i, j; int key; // the array is divided into two parts // initially the left part is already sorted as it contains only 1 element // elements from the right part are inserted onto the correct position on the left // until the whole array is sorted for (j = 1; j < size; j++) { int i = j-1; key = *(array + j); while(key < *(array + i)) { *(array + i + 1) = *(array + i); i--; } *(array + i + 1) = key; } }

// display the array integers void displayArray(int *array, int size) { for (int i = 0; i < size; ++i) { std::cout << *(array + i) << ", "; // when 5 elements are displayed output to next line if ((i + 1) % 5 == 0) std::cout << std::endl; } std::cout << std::endl; } // funtion to get the integer to search in the array int getInteger() { int value; std::cout << "Enter integer to search in the array: "; std::cin >> value; return value; } // linear search in the array of the given element // if present the return true else false bool search(int *array, int size, int value) { for (int i = 0; i < size; ++i) if (value == *(array + i)) return true; return false; } int main(int argc, char const *argv[]) { int size = getSize(); int *array = createArray(size); sortArray(array, size); displayArray(array, size); int value = getInteger(); bool found = search(array, size, value); if (found) std::cout << "Element is in the array. "; else std::cout << "Element isn't in the array. "; return 0; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!