Homework 2: Working with Arrays & Linked Lists

Due: Tuesday, February 20, 8:59:59pm


Instructions: In each of the three questions below, you are asked to write a short C++ progam. Place the programs in files called q1.cpp, q2.cpp and q3.cpp. On GL, in the script command, compile each program and run each program under valgrind. You should have one typescript file for the three programs. Then, copy the four files q1.cpp, q2.cpp, q3.cpp and typescript to the hw2 subdirectory in your submission directory.



Question 1

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:

  1. a pointer to the array of ints to be searched;
  2. the number of values in the array, as an int;
  3. 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 <iostream> 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\n"; 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 <iostream> 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; }