Question
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
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:
g++ -Wall -g -std=c++11 -o quiz5 quiz5.cpp
To run the resulting executable program in the debugger, type:
gdb quiz5
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
#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;
cout << "Seed value? " << flush;
cin >> seed;
srand(seed);
fill(array, ARRAYSIZE);
shuffle(array, ARRAYSIZE);
return 0;
}
void fill(int b[], int size)
{
int index;
// Place random values at random locations in the array
for(int i = 0; i < 10 * size; i++)
{
index = rand() % size;
b[index] = rand();
}
}
void shuffle(int ar [], int size)
{
int* first, * second;
for(int j = 0; j < 5 * size; j++)
{
// Pick a random pair of positions to swap
first = ar + rand() % size;
second = ar + rand() % size;
// Swap
int temp = *first;
*first = *second;
*second = temp;
}
}
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.
1.
a. 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;)
b. 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.
c. 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?
Understand that you must be on turing/hopper, just need a walkthrough of how you would do it on your system, the output does not need to match i just want to see an example
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started