Homework 2: Working with Arrays

Due: Thursday, September 20, 8:59:59pm


Instructions: In each of the three questions below, you are asked to write a short C++ progam. Place the programs in files called q1.cpp, q2.cpp and q3.cpp. On GL, in the script command, compile each program and run each program under valgrind. You should have one typescript file for the three programs. Then, copy the four files q1.cpp, q2.cpp, q3.cpp and typescript to the hw2 subdirectory in your submission directory.



Question 1

In the main() function below, we have an array of 10 pointers to objects of class Two. Fill in the rest of the code that has a for loop which allocates memory for each entry of the array and initializes the members part1 and part2 to 0. This program does not have any output.

#include <iostream> using namespace std ; class Two { public: int part1 ; int part2 ; } ; int main() { Two * dp[10] ; // fill in code here return 0 ; }



Question 2

In the main() function below, we use a function called initialize() that allocates memory for an array of int. The size of the array is given by the second parameter to initialize. The array must be initialized so the i th item of the array has value i. (See sample output below.) The first parameter must be passed by reference and modified by the initialize() function to point to the newly allocated array.

Implement the initialize function so it is compatible with the main() function as given. Yes, part of the question here is to figure out the function signature for initialize().

#include <iostream> using namespace std ; // // add your implementation of the initialize() function here // (do not edit any other line of this file) // int main() { int *ptr ; int n = 10 ; initialize(ptr,n) ; for (int i=0 ; i < n ; i++) { cout << "ptr[" << i << "] = " << ptr[i] << endl ; } int sum = 0 ; for (int i=0 ; i < n ; i++) { sum += ptr[i] ; } cout << "Sum = " << sum << endl ; delete [] ptr ; int *ptr2 ; initialize(ptr2,15) ; sum = 0 ; for (int i=0 ; i < 15 ; i++) { sum += ptr2[i] ; } cout << "Sum = " << sum << endl ; delete [] ptr2 ; }

The ouput of the program should be: ptr[0] = 0 ptr[1] = 1 ptr[2] = 2 ptr[3] = 3 ptr[4] = 4 ptr[5] = 5 ptr[6] = 6 ptr[7] = 7 ptr[8] = 8 ptr[9] = 9 Sum = 45 Sum = 105

Question 3 (Adapted from R-3.14 of textbook.)

This question was re-written for clarification. -RC

In the code below, the srand() function from cstdlib is used to set the random seed of a pseudo-random number generator. After that, you can call the rand() function to obtain a "random" number:

r = rand() ; That is, each call to rand() will return an int value that has no readily apparent pattern.

Implement the printRandom() function that repeatedly selects and "removes" a random entry from the array C and then prints out the selected value until all values in the array C have been printed. Each value from the array should be printed exactly once. It is OK to modify the array C (in fact, it is encouraged, to make your code simpler). The first parameter of printRandom() is the array to be printed, the second parameter is the size of the array and the third parameter is the radom seed to be used.

If we ran the main program below, which calls printRandom() three times with different random seeds, we might get output that looks like:

-34 -18 4 13 24 21 11 -42 89 2 -42 2 -34 11 13 4 24 -18 21 89 11 24 21 -18 89 13 2 -42 4 -34

#include <cstdlib> #include <iostream> using namespace std ; void printRandom(int C[], int n, int seed) { srand(seed) ; // fill in your code here // } int main() { int A[10], B[10] ; A[0] = 21 ; A[1] = -34 ; A[2] = 2 ; A[3] = -42 ; A[4] = 89 ; A[5] = 24 ; A[6] = 11 ; A[7] = 4 ; A[8] = 13 ; A[9] = -18 ; for (int i=0 ; i < 10 ; i++) { B[i] = A[i] ; } printRandom(B,10,38173410) ; for (int i=0 ; i < 10 ; i++) { B[i] = A[i] ; } printRandom(B,10,83103131) ; for (int i=0 ; i < 10 ; i++) { B[i] = A[i] ; } printRandom(B,10,77192102) ; return 0 ; }