Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In the main() function below, we have an array of non-zero integers solicited from the user, in strict increasing numerical order (you can assume the

In the main() function below, we have an array of non-zero integers solicited from the user, in strict increasing numerical order (you can assume the user did not make any mistakes). It then loops soliciting query numbers from the user and reporting where they were found in the array. Your task is to write the function binarySearch, including a proper declarations of the return type and parameter names and types, and a complete, correct implementation. The function must do exactly what the name implies: perform a binary search on the values in the array, looking for and returning the position of the requested value. The three arguments are:

a pointer to the array of ints to be searched;

the number of values in the array, as an int;

the integer value to be searched for;

The returned value is an int indicating the position the value was found at, or -1 if not found. The function must use a loop, and must not use any recursion. That is for the next question! Make sure you correctly handle odd lengths.

Make sure you place the definition at the location marked: "INSERT YOUR binarySearch IMPLEMENTATION HERE", or the compiler will complain.

#include using namespace std ; // // INSERT YOUR binarySearch IMPLEMENTATION HERE // int main() { int val, myNums[1000]; // Yuck--magic number! int pos, cnt = -1; cout << "Enter numbers from smallest to largest, 0 to stop "; do { cin >> myNums[++cnt]; } while (myNums[cnt] != 0); do { cout << "Enter number to search for: "; cin >> val; if (val != 0) { pos = binarySearch(myNums, cnt, val); cout << "binarySearch reported " << pos << endl; } } while (val != 0); return 0 ; }

Question 2

Rewrite your solution to the binarySearch function from Question 1 so that it is recursive. After deciding where to split the array, the function should decide which half the value is in, and then call itself recursively to search just that half, by passing in a pointer to the relevant part of the array, as well as a correct sub-length. The base case for the recursion would be when length == 1, in which case the only possible return values are 0 or -1, depending on whether that single element matches the value being searched for.

(Caution: make sure you make a copy of q1.cpp to q2.cpp before proceeding, or you might clobber your solution to Question 1 by mistake!)

Question 3

In the incomplete source below, we have pre-defined a Node class to be used as elements in a singly-linked list. The code given for the main() function below calls the function getListFromUser, which you will write, that reads in a list of non-zero integers from the user, stopping when they enter a 0 (zero). It then walks the list and prints them out; this is to provide additional help for you to understand how the linked list works.

You must write the complete function definition for getListFromUser. It must loop getting input from the user, until they enter "0" to stop. For all input numbers (except the 0, obviously), the function must dynamically allocate objects of class Node, one per number. The list that you construct must have the numbers in the same order as the user entered them in: most importantly, they cannot be reversed! The function should return a pointer to the first Node in the list, or NULL if the user specified an empty list (i.e., immediately typed "0" as their first input).

#include using namespace std ; class Node { public: int m_data; Node *m_next; }; // // INSERT YOUR getListFromUser IMPLEMENTATION HERE // int main() { Node *head, *curr, *next; head = getListFromUser(); next = head; while (next != NULL) { curr = next; next = curr->m_next; cout << "Next #: " << curr->m_data << endl; delete curr; } return 0; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data And Databases

Authors: Jeff Mapua

1st Edition

1978502257, 978-1978502253

More Books

Students also viewed these Databases questions

Question

What advantages does this tactic offer that other tactics do not?

Answered: 1 week ago