Schedule‎ > ‎

Quiz3Prep

Quiz 3 Prep  

 

1.      What is the purpose of this statement: srand(time(0));    (7.1.3)

 

2.       Write code to generate 5 random numbers and print them out.  (7.1.3)


3.      What does the following code print?  (6.2)

int myArray[] = {12, 30, 7, 8, 15, 99,23};

for (int i = 3; i < 5;  i++)  {

                       cout <<  myArray[i];

}


4.       What is the purpose of writing Functions?  (7.2)

 

 

5.       Identify the following by drawing arrows to the appropriate piece of code below (9.1.1)

a.      return type, 

b.      function name,

c.       parameter,

d.      return statement,

e.      function call,

f.   assignment of the returned value

int add(int a, int b) {

    int sum = a + b;

    return sum;

}

int main()  {

               int sum = add(32, 10);

}

6.      Write a function prototype of printArray which will print out all elements of an integer array and returns the sum of the elements of the array.  The parameters for printArray will be an integer array and its size. (8.2.2  and 8.1.4)

 

7.      What is the purpose of a function prototype?  Where do you put it in your code? (8.2.2)

 

8.      Write the definition of your function printArray which will print out all elements of an integer array and returns the sum of the elements of the array. (8.1.4)

 

 

 

 

9.       Write a statement that will call your printArray Function.  Assume that you have already declared an array myArray which contains 5 integers.  You will want to assign the return value from printArray to a variable.  (8.1.4)

 

 

10.    If I want to actually put the hat on a human, and not just on a human copy:  I need to define my putHatOn function like which of the following?   (circle all that apply)  (8.3 and 8c Slides)

a)  int putHatOn(int color, Student myStudent);

b)  int putHatOn(int color, Student& myStudent);

c)  int putHatOn(int color,  Student* myStudent);

note:  Student is a made-up type for example purposes only

 

11.   Write a swap function that uses pass by value to swap two integers.  After the swap function definition,  write a one line statement of how you would call this function.  (8.3)

 

 

 

12.   Write a swap function that uses reference parameters to swap two integers.  After the swap function definition,  write a one line statement of how you would call this function.  (8.3)

 

 

 

13.   Write a swap function that uses pointer parameters to swap two integers.  After the swap function definition,  write a one line statement of how you would call this function. (8c Slides)

 

 

 

Comments