CSCE 150A (Fall 2009) Homework 3

Assigned Monday, October 12
Due Wednesday, October 21
Total points: 100


When you hand in your results from this homework, you should submit the following:

  1. The source code of your programs, in text files, named as specified below in Problems 3–5.
  2. An optional text file called README that contains any notes about your programs that you want the graders to know.
  3. A single .pdf file with your writeup of the results for all homework problems. Only pdf will be accepted, and you should only submit one pdf file, with the name username.pdf, where username is your username on cse. We will not accept any hard copy submissions of your homework!
  4. Every problem requres a writeup in this homework. For Problems 3–5, you need to decompose each problem just like you did in the previous homework.

Put your name in all your files, and submit everything by the due date and time using the web-based handin program.

On this homework, you must work on your own and submit your own results written in your own words.


  1. (15 pts) Consider the following variables:
    int a = 10, b = 5, c = 15, flag = 0;
    
    Evaluate the following expressions given the values above. Indicate which parts of each expression (if any) are not evaluated due to short-circuiting.
    1. a == c – b || !flag
    2. c != –c || flag && c <= (b * a)
    3. !(a % c) && b >= 5
    4. (a > 10) || c < a + b
    5. !!a == 10 && c >= 15 || b

  2. (15 pts) Convert each of the following from English statements to proper C statements.
    1. x is less than y and y is less than z
    2. x is greater than y and x is less than or equal to z
    3. x is false and y is not equal to z, or z is not equal to 20
    4. x is equal to y or x is equal to 24
    5. x is less than or equal to y and y is less than or equal to z

  3. (15 pts) Write a program in file gas.c that reports the contents of a compressed gas cylinder based on the first letter of the cylinder's color. The program input is a character representing the observed color of the cylinder: 'Y' or 'y' for yellow, 'O' or 'o' for orange, and so on. Cylinder colors and associated contents are as follows:

    orangeammonia
    browncarbon monoxide
    yellowhydrogen
    greenoxygen

    Your program should respond to input of a letter other than the first letters of the given colors with the message:
    Contents unknown
    

  4. (25 pts) A music store needs a program to implement its music teacher's discount policy. The program in file music.c is to prompt the user to enter the purchase total and to indicate whether the purchaser is a teacher. The store plans to give each customer a printed receipt, so your program is to create a nicely formatted file called receipt.txt. Music teachers receive a 10% discount on their sheet music purchases unless the purchase total is $100 or higher. in that case, the discount is 12%. The discount calculation occurs before addition of the 7% sales tax. Your program should use functions to display instructions to the user and to compute the discount, if any. Here is a sample output, for a teacher.
    Total purchases$122.00
    Teacher's discount (12%)14.64
    Discounted total107.36
    Sales tax (7%)7.52
    Total114.88

    Here is a sample output, for a nonteacher.
    Total purchases$24.90
    Sales tax (7%)1.74
    Total26.64

  5. (30 pts) Write a program in file bread.c to control a bread machine. Allow the user to input the type of bread as 'W' for white and 'S' for sweet. Ask the user if the loaf size is double and if the baking is manual. The following table details the time chart for the machine for each bread type. Display a statement for each step. If the loaf size is double, increase the baking time by 50 percent. If baking is manual, stop after the loaf-shaping cycle and instruct the user to remove the dough for manual baking. Use functions to display instructions to the user and to compute the baking time.

    OperationWhite BreadSweet Bread
    Primary kneading15 mins20 mins
    Primary rising60 mins60 mins
    Secondary kneading18 mins33 mins
    Secondary rising20 mins30 mins
    Loaf shaping2 seconds2 seconds
    Final rising75 mins75 mins
    Baking45 mins35 mins
    Cooling30 mins30 mins