Question: Skeleton for Assignment 2 // dynalloc parms: array size, pointer to array which is defined in main but modified by function when it executes the

 Skeleton for Assignment 2 // dynalloc parms: array size, pointer toarray which is defined in main but modified by function when it

Skeleton for Assignment 2

// dynalloc parms: array size, pointer to array which is defined in main but modified by function when it executes the // definition with new. Return value: none.

void dynAlloc(int, int*&);

// insertionSort parms (or bubbleSort parms): pointer to array to sort, array size, pointer to sorted array. // Return value: number of iterations.

int insertionSort(int *, int, int *);

// linearSearch parms: pointer to array to search, array size, target value, number of iterations. Number of // iterations defined in main but modified by search. Return value: element # of found element, or -1 if not found.

int linearSearch(int *, int, int, int &); // binarySearch parms: same as with linearSearch int binarySearch(int *, int, int, int &); main

numElements=100; int * unsortedArrayPtr, * sortedArrayPtr;

dynAlloc(numElements, unsortedArrayPtr) for(i=0;i

unsortedArrayPtr[i]=rand() % 100+1 cout ... unsorted array

dynAlloc(numelements, sortedArrayPtr) numIter=insertionSort(unsortedArrayPtr, numElements, sortedArrayPtr) cout ... sorted array cin ... a target value while (!= -1)

return

// dont forget to clean up

// new not xeqd until execution time

wasFound=linearSearch(sortedArrayPtr, numElements, targetValue, numIter) cout ... results wasFound=binarySearch(sortedArrayPtr, numElements, targetValue, numIter) cout ... results

cin ... a target value

dynAlloc(int numelements, int * &ptrtothearray) ...

ptrtothearray=new int[numelements]

int insertionSort(int *toSort, numElements, int*sortedResult) ...

sortedResult[i]=toSort[i]; ...

// or use a global // a ptr can function as an array name it can be subscripted // refer to the rules // get space for array to sort // populate array to sort

// get array to hold sorted result

if (sortedResult[i]

int linearSearch(int *toSearch, numElements, targetValue, numIterations) ...

if(toSearch[i] = targetValue) ...

int binarySearch(int *toSearch, numElements, targetValue, numIterations) ...

if(toSearch[i] = targetValue) ...

In assignment 2, you'll have main use dynAlloc to get space for the two arrays, one array at a time, and two new functions which both search the array for a given value. You'll also reuse one of your sort functions from assignment 1. You'll have a main which will now look like: invoke dynAlloc to get space for the unsorted array populate the unsorted array with random integers display the unsorted array invoke dynAlloc to get space for the sorted array invoke a sort function display the sorted array get an integer from the user at the console loop while the user integer is non-negative invoke linearSearch display the result(s) invoke binarySearch display the result(s) get another integer from the user exit Function dynAlloc The dynamic allocation function has two arguments - the number of elements to allocate, and a pointer to the allocated array. Use 100 as the number of elements. The pointer is defined in main and passed by reference so that when the function allocates the array, the pointer can be passed back to main. The function uses the "new" operand to allocate the array. Functions linearSearch and binarySearch

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!