CS 2073-001,   Exam 2, Fall 2005

Directions: Fill in answers on the pages below. Don't spend too much time on any one problem.
Points for each problem: 1-15, 2-20, 3-25, 4-15, 5-25.


  1. Simple Arrays: Fill in the rest of the C program below, so that it will use a loop to add the square of each number in the array and then print the sum. (The program should be complete with all necessary declarations.)


  2. Arrays as parameters: Fill in the rest of the C program below by writing a function printp that will take the array p as an actual parameter and will print the elements of p, on one line, with a comma but no space after each number except for the last one, and with a newline at the end. (No comma at the end.) Your program should print the following when it runs:
    2,3,5,7,11,13,17

  3. Gambling at Roulette: In the American version of Roulette, if you bet $1 on a single number, then if your number doesn't come up, they take your money. If your number does come up, then they add $35 to your $1, and you have $36 left. Your number comes up 1/38 of the time, that is, with probability 1/38 = 0.0263158. (In this game there are the 36 numbers 1 through 36 to bet on, plus 0 and 00. A rotating ball will fall into a slot labeled with one of these numbers. The game would be exactly fair if the probability of winning was 1/36 = 0.0277778. The difference is the house edge.)

    You are to write a program that simulates betting repeatedly on a single number. You start with $1000 (and we'll drop the $-sign). Just initialize a variable such as amount to 1000. You bet 20 at a time, inside a loop. If you lose, your amount of money drops by 20. If you win, your amount of money goes up by 20*36 = 720. The loop continues until one of two things happens: either your money drops below 20 (actually to 0, since everything here occurs in increments of 20), so you can't bet any more, or your money goes up to 2000 or above, so that you have doubled your stake, and you quit happy.

    It's easy to simulate something randomly happening with probability 1/38: just produce a random double uniformly distributed between 0 and 1. If this random double is less than 1/38, you win, and otherwise you lose.

    Write your program into the framework below. I have inserted the random number generator that we have often used, along with its initialization.

    You should count the number of bets needed until you either at least double your money or lose all your money, and print this number. Because of the way this is set up, when the program ends, you will either have 0 amount, or if you double your money, you may have considerably more than 2000 when you quit. Thus you should also print the final value of amount.

  4. Macros: Consider the program below that uses a preprocessor macro, along with the ? : version of an if-else

    1. Could the macro MAX be used to find the maximum of two doubles? (Give more than just a yes or no answer.)

       

       

       

    2. Give below a macro MIN that will find the minimum of two numbers.

       

       

       

    3. Give what will be substituted in place of MAX(a+6, b+7) in the program above.

       

       

       

       

       

       

       

       

       

       

       

       

  5. Pointers, and 2-dimensional arrays: Consider the program below, which defines a 4-by-6 2-dimensional array, with various numbers in it. The function search searches the whole array for a specific number, in the case below the number 24. This number is found in row 2 and column 4, as is shown by the printout. The function returns a 1 if the value is found, and a 0 if the value is not found. For this problem, you are to add to this program: specifically, you are to add two additional pointer parameters to the function search. These parameters will allow you to get two numbers out of the search function and back into the main function, namely the row and column numbers of the where the value being searched for is to be found, if it is in the array. You will have to make changes to the prototype, to the main function, and to the search function. Show all these changes on the listing below.