Question: *** USING C++ *** Write a void printArray function, using the following program and steps. Thanks for the help! Consider the following program: #include using

*** USING C++ ***

Write a void printArray function, using the following program and steps. Thanks for the help!

Consider the following program:

#include using namespace std; const int MAXSIZE = 16; static int values[MAXSIZE]; static int i; // bad idea to call a global variable i! void initArray(int[], int); void printArray(int[]);

// This initializes element arr[i] to val for each array element void initArray(int arr[], int val) { for (i=MAXSIZE; i >= 0; i--) arr[i] = val; return; }

// This prints the contents of the argument array, with each element printed as "index: value" on its own line // For example, a 4-element array containing {10,11,12,13} would print as: // 0: 10 // 1: 11 // 2: 12 // 3: 13 void printArray(int arr[]) { ... };

int main() { int dummy; initArray(values,5); int *arr2 = values; values[0]=9; arr2[1]=8; cout << "values is:" << endl; printArray(values); cout << endl << "arr2 is:" << endl; printArray(arr2); return 0; };

As you can see, the above program should initialize each element of the array values to 5, update the 0th element to 9 and then print the entire array out. The arr2 variable should be another array with the same contents, except without the update of the 0th element and an additional update of the 1st element. After understanding the above code, finish writing printArray. Make sure to align columns Run the program and you will get unexpected results. Understand why you get these results and explain in comments (be precise - your grade is based largely on your explanation). If you don't understand what is happening, use ddd to trace through the program while displaying the appropriate variables. Then, fix the code (with a minimal number of changes) to do as described above and run the program. More information (read this after you finish the above, as it won't make sense otherwise): One problem above resulted from a buffer overflow error. This is a common security error since it allows malicious code to access memory locations that they should not have access to. That memory location might have a function parameter, or even machine code for the program that can now be overwritten with malicious code (if the OS allows)!

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!