CS2023 Assignment 2

Winter 2004

Due Friday, January 30, 10:30 AM, in the assignment bin on E-Level
This assignment will be marked out of 50 points. The worth of each question is given below.
  1. (3 points) Suppose that i is a variable of type int, f is a variable of type float, and d is a variable of type double.
    1. What is the type of the expression i * f /d ?
    2. Explain what conversions take place during the executition of the statment d = i + f;
  2. (6 points) Assume that a program contains the following declarations:

  3. char c = '\1';
    short int s = 2;
    int i = -3;
    long int m = 5;
    float f = 6.5;
    double d = 7.5;

    Give the value and the type of each expression listed below:

    1. c * i
    2. s + m
    3. f / c
    4. d / s
    5. f - d
    6. (int) f
  4. (16 points) Write a program called phone_alpha.c that translates an alphabetic phone number into numeric form:

  5. Enter phone number: CALLATT
    2255288
    where italic case refers to the user's input. In case you don't have a telephone nearby, here are the letters on the keys: 2=ABC, 3=DEF, 4=GHI,5=JKL,6=MNO,7=PRS,8=TUV,9=WXY. If the original phone number contains nonalphabetic characters (digits or punctuation, for example), leave them unchanged:
    Enter phone number: 1-800-COL-LECT
    1-800-265-5328
    You may assume that any letters entered by the user are upper case and do not include the letters Q or Z.
  6. (25 points) "The Curious Incident of the Dog in the Night-time" is the name of a novel by Mark Haddon (Doubleday Canada, 2002) and is about an autistic child who loves math. It contains a discussion of the Monty Hall problem:
  7. "You are on a game show on television. On this game show the idea is to win a car as a prize. The game show host shows you three doors. He says there is a car behind one of the doors and there are goats behind the other two doors. He asks you to pick a door. You pick a door but the door is not opened. Then the game show host opens one of the doors you didn't pick to show a goat (because he knows what is behind the doors). Then he says that you have one final chance to change your mind before the doors are opened and you get a car or a goat. So he asks you if you want to change your mind and pick the other unopened door instead. What should you do?"
    The correct solution to this problem is counter-intuitive. When the question was first posed and solved in 1990, by Marilyn vos Savant, many people wrote letters to say that she was wrong, including mathematicians and scientists with PhDs!

    First try solving the problem using your intuition. Then write a C program called monty_hall.c to simulate the problem, and estimate the probabilities of winning the car by the two decisions: sticking with the first door you pick, or picking the other unopened door. You can assume, without loss of generality, that the contestant always picks the same door at the beginning (say door A). The car is assigned randomly to one of three doors, and the host opens one of the other two doors (if the car is behind door A, the host will open door B or door C with equal probability). Your program must prompt the user for a seed for the random number generator (rand, available in stdlib.h; type man rand for more details) and the number of games played, and then output the number of games won if the contestant always sticks to his/her first pick and the number of games won if the contestant always switches doors. Run the program with several different random number seeds and different numbers of games played to verify that the result is always approximately the same. Comment on the result: did it agree with your own solution to the problem?

Marking Scheme for Programming Questions

For each of questions 3 and 4, 2 marks will be deducted if your program does not compile (using gcc on a Linux/Intel machine) and 2 marks will be deducted if execution of your program does not terminate. Make sure you follow the C Language Coding Standard, format your code clearly, and choose meaningful variable names: these are all worth 4 marks for each of questions 3 and 4.

Deliverables

A hardcopy of your assignment in the bin on E-level, which must include source code of all programs and electronic submission of the programs in questions 3,4: submit two files, which must be called phone_alpha.c and monty_hall.c (lower case only!) to the "Submit your assignment" link on the CS2023 home page.
 
Eric Aubanel, Jan. 21 2004, corrected Jan. 23