CS 31 Bridge – Summer 08                                                                      Homework 6
 
Name: ______________________   Group: _________________________________    Hours taken: ___
 
You can work in group of 2-3 people. You can talk and discuss among your group. However, write your own answers for the homework. Do NOT copy each other. Make your answers CLEAR and SHORT.
 
True False:

 

An explanation is required.

 

1.     If we want to find the median of 100 scores, we need 100 separate variables to hold the data.

 

2.     With arrays, indices start at any number the programmer chooses to indicate in the definition.

 

3.     The programmer should always use a defined constant in an array declaration.

 

4.     To call a function with an array parameter, write the array argument as the array name followed by empty square brackets, as int  f(a[], 7); (The first argument is an array a, the second is the size.)

 

5.     Consider the array declaration, int x[20];. There is no memory allocated for x[20].

 

6.     Arrays in C++ may have several different types stored in them.

 

7.     A for-loop is a convenient way to step through an array.

 

 

Free Form Questions:

 

1.     Describe the difference in the meaning of the 5 in int x[5]; and the meaning of the 4 in x[4]. What are the meanings of the int, the [5] and the [4]?

 

 

 

2.     In the array declaration

double score[5];

identify the following:

a)     The array name

b)     The base type

c)     The declared size of the array

d)     The smallest and largest index values this array can have.

e)     Give one of  the indexed variables (a.k.a. elements) of this array.

 

3.     Consider the following function definition:

void tripler(int& n)

{

n = 3*n;

}

Given this definition, which of the following are acceptable function calls?

int a[3] = {3,4,5}, number = 2;

a)     tripler(a[2]);

b)     tripler(a[3]);

c)     tripler(a[number]);

d)     tripler(a);

e)     tripler(number);

4.     What is the output of the following code?

 

#include<iostream>

int main()

{

  using namespace std;  

  double a[3] = {1.1, 3.3, 2.2};

  cout << a[0] << " " << a[1] << " "  << a[2] << endl;

  a[1] = a[2];

  cout << a[0] << " " << a[1] << " "  << a[2] << endl;

}

 

5.     What is the problem with this code?

int array[5];

for (int index = 1; index <= 5; index++)

   array[index] = index /2;

 

 

Multiple Choice:

 

There may be more than one correct answer. You must give all correct answers for full credit. An explanation is required.

 

1.     In C++ array indices, that is subscript values, must be

a)     An integer type

b)     Non-negative

c)     Positive

d)     Less than or equal to the declared size of the array

e)     None of these is correct

 

2.     Given the array declaration, int a[20]; The first element is written as:

a)     a[1]

b)     a[0]

c)     a

d)     a[20]

e)     a[19]

 

3.     Given the array declaration, int a[20]; The last (legal) element is written as:

a)     a[2]

b)     a[0]

c)     a

d)     a[20]

e)     a[19]

 

4.     Are the following array initializations correct? If not, why not?

a)     int x[4] = {8, 7, 6, 5, 4};

b)     int x[] = {8, 7, 6, 5, 4};

c)     int x[4] = {8, 7, 6};

d)   const int SIZE =4;

int x[SIZE];

e)   const int SIZE =4;

int x[SIZE-4];