Question: Indentation not good validation should use loops, not if statement -- get valid input, not go with some default value should use constants for getSize

Indentation not good

validation should use loops, not if statement -- get valid input, not go with some default value

should use constants for getSize

not using a sort you wrote

---------------------------------------------------------------------------------------------------------------------------

#include

#include // Header file for random numbers srand() and rand()

#include // Header file for std::time() to get system time

#include // sort the array

// Function to get the size of the array if size is not in the range between

// 10 and 20 create it to of size 20

int getSize()

{

int n;

std::cout << "Please enter integer between 10 and 20 for size of array: ";

std::cin >> n;

// check if size is not in range of 10 and 20

if (n >= 10 && n <= 20) return n;

else

{

std::cout << "Using 20 as default size, as size is not between 10 and 20. ";

//if size is not between 10 and 20 then return 20 as default size

return 20;

}

}

// 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);

}

// 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!