Quiz 5 The point of this quiz is to force you to gain a little experience using
ID: 3736501 • Letter: Q
Question
Quiz 5
The point of this quiz is to force you to gain a little experience using the debugger. Your instructor will specify a due date for this take-home quiz in class.
Preparation
The various gdb commands will not be covered in class, but there are links to a number of gdb tutorials on the course web page (This one makes a good starting point, for example). For most debugging tasks, only about 6-7 different commands are typically used, so it doesn't take a lot of effort to achieve a basic level of proficiency with the debugger.
The Quiz
Type in a copy of the program given below. Type it in exactly as given. Any changes may lead to incorrect answers to the quiz questions. Compile the program with the -g debugging option turned on. You may name the program whatever you wish. For example, if you put the program code in a file called quiz5.cpp, the command to compile and link the program would look like this:
To run the resulting executable program in the debugger, type:
As given here, the program has no compile time errors, compile time warnings, nor run time errors.
Using the debugger, answer the questions given below. In order to answer the questions, you will need to set breakpoints in the program, single step through the executing program, examine values of variables and change the values of variables.
In order to insure that your answers match the correct answers, this program must be run on turing/hopper. Answers obtained running Cygwin or Linux on your machine at home will be different.
The program requires user input. You'll know you've reached this point in the debugger when you hit return and don't get a debugger prompt (gdb). Type in the desired input at this point and hit enter. Be very careful about the input values. An incorrect input value will lead to completely different answers.
Print out a copy of the program you entered. Write the answers to the questions on a separate sheet of paper. Turn them in during class on the due date.
The Program
The Questions
Using the debugger, answer the following questions. The order of the questions is important. The seed values are based on a sequence of dates (10/2/2015, 10/3/2015, etc.). Be sure to use the seed values given or your answers will differ greatly from the correct answers.
Run the program (single step) and enter a seed value of 1022015. After calling the shuffle() function, what is the value of array[8]?
Restart the program and run it with a seed value of 1032015. After calling the fill() function but before calling the shuffle() function, what is the value of array[13]?
After calling the shuffle() function, where has the value that was in array[13] been moved to? (Hint: You can print the contents of the entire array in the main() function by typing the gdb command print array.)
Restart the program and run it with a seed value of 1042015. Step into the fill() function. Inside the loop, when i == 14, what is the value of index after assignment? (i.e., after executing the line index = rand() % size;)
Restart the program and run it with a seed value of 1052015. Step into the fill() function. Set a watchpoint on b[29] (you can use the gdb command watch to do this). Continue the execution of the program. When it stops, what is the value of b[29]? Continue the execution of the program. When it stops again, what is the value of b[29]? Delete the watchpoint using the delete command.
Restart the program and run it with a seed value of 1062015. After the call to fill() and before the call to shuffle() replace the value in array[26] with a -1. (Use the gdb command set variable to do this.) Verify that your change is in place. After calling shuffle() where is the -1?
Restart the program and run it with a seed value of 1072015. Step into the shuffle() function. What are the values computed for first - ar and second - ar when j == 7? (The values of first and second are addresses.)
Restart the program and run it with a seed value of 1082015. Step into the shuffle() function. Set a watch on ar[5]. Continue the execution. When the value in ar[5] changes, what is the value of j?
Explanation / Answer
#include #include using std::cout; using std::cin; using std::flush; const int ARRAYSIZE = 30; void fill(int ar[], int size); void shuffle(int ar[], int size); int main(void) { int array[ARRAYSIZE] = {0}; // Clear out array int seed; coutRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.