Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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);

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

  1. Run the program (single step) and enter a seed value of 2142021 After calling the shuffle() function, what is the value of array[8]?

  2. Restart the program and run it with a seed value of 2152021 After calling the fill() function but before calling the shuffle() function, what is the value of array[13]?

  3. After calling the shuffle() function, where has the value that was in array[14] been moved to? (Hint: You can print the contents of the entire array in the main() function by typing the gdb command print array.)

  4. Restart the program and run it with a seed value of 2152021. 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;)

  5. Restart the program and run it with a seed value of 2162021 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.

  6. Restart the program and run it with a seed value of 2172021 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?

  7. Restart the program and run it with a seed value of 2182021 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.)

  8. Restart the program and run it with a seed value of 2192021. 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?

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

DB2 Universal Database V7.1 Application Development Certification Guide

Authors: Steve Sanyal, David Martineau, Kevin Gashyna, Michael Kyprianou

1st Edition

ISBN: 0130913677, 978-0130913678

More Books

Students also viewed these Databases questions

Question

What did they do? What did they say?

Answered: 1 week ago