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
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; }
CS 162 Programming Lab 4 For this exercise, you will create a dynamic array and manipulate it using pointers. Problem Description While the program will be an introduction to pointers and dynamic memory, it is also a review of functions, arrays, searching, and sorting. For this program you will get a value from the user between 10 and 20, create an array of this size using dynamic memory allocation, fill the array with random numbers between 1 and 99, sort the array, and then display the array. You will then ask the user for 3 numbers and for each value you will say whether it is in the array or not. In order to give you more practice with pointers, you are to do all array element accesses with a pointer instead of using subscripting. (int * ptr = array; *ptr = value; instead of array[0] = value;) Required functions getSize -- This function will ask the user for a value between 10 and 20, verify that they entered an integer in this range, and return it to main. createArray This function is passed the length of the desired array. It will create an array of that length using dynamic memory allocation (the new operator) and fill it with random numbers between 1 and 99. For all memory accesses you are to use pointer dereferencing (*array) instead of array subscripting (array[0]). Once the array is created and filled, you will return it using a pointer to an int. sortArray The next function is a sort function. You will pass in the array and its length and then sort it using one of the sorts covered in the module on searching and sorting. The array should be sorted in ascending order (smallest at first location, largest at last location). displayArray The fourth function is a display function. You will pass the array to the function along with its length. Then you will display the values, five per line. You should use the setw operator to align your numbers in neat columns. Again, you are to do all your memory references using pointer dereferencing. Search The fifth function performs a binary search. You will pass in the array and its length to the function along with a value. It should return true if the value is in the array, false otherwise. You are to use a binary search for this function. You can make this recursive if you would like, but it is not required. getInteger This function is like getSize, but asks the user for a number between 1 and 99, validates it, and returns it to the calling program. Program Requirements You need to use constants where appropriate and comment all functions. You should free up the memory from your array, using delete properly when you are done. Program Suggestion Your main should have this basic structure: getSize createArray sortArray displayArray loop three times getInteger search output if found or not Define your program with stub functions, then code and test functions in this order: 1. getSize 2. createArray 3. displayArray 4. sortArray 5. search You could design a single function that is passed a range (min to max), asks the user for a number in that range, validates it, and returns it to the calling program. This could then be used for getSize and getInteger.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started