Type in a copy of the program given below. Type it in exactly as given. Any chan
ID: 3804003 • Letter: T
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 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 <cstdlib>
#include <iostream>
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 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.)
b. 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 <cstdlib>
#include <iostream>
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;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.