Classwork 9: While Loops

Thursday, July 25, 2012     


[Previous Classwork] [Next Classwork]


Complete the following:

  1. Create the file cw09.c for a classwork assignment and start your file as normal.
  2. Note you may use functions if you want, it might make the work easier, but you are not required to.
  3. You need to use at least one loop, a do-while or while loop, either is acceptable
  4. You are going to write a small calculator program
  5. Your program is going to keep track of two numbers that the user may perform operations on
  6. The first time the program starts, ask the user for two doubles
  7. Then, inside a loop, you should print out a menu that shows the following:

    Number1: XXXX
    Number2: YYYY
    Input an operation to perform (+, -, *, /, =), select number to modify (1,2), or quit (x): 
    

  8. Where XXXX is the first number the user entered and YYYY is the second number the user entered
  9. After displaying the menu, get input from the user to select the operation to perform
    • If the user types '+', print the result of adding number 1 and number 2
    • If the user types '-', print the result of subtacting number 1 from number 2
    • If the user types '*', print the result of multiplying number 1 and number 2
    • If the user types '/', print the result of dividing number 1 by number 2
    • If the user types '=', print the "EQUAL" if number 1 equals number 2, otherwise print "NOT EQUAL"
    • If the user types '1', ask the user to input a new number to store in number 1
    • If the user types '2', ask the user to input a new number to store in number 2
    • If the user types 'x', exit from the loop
    • For any other input print "UNKNOWN INPUT"
  10. Note that the results of all arithmetic operations should be doubles
  11. After performing the operation, redisplay the menu and wait for user input again
  12. EXTRA CREDIT: Provide an extra menu option '!' that will perform the factorial of the truncated integer value of number 1. So given a number 1 of 6.72, the result of '!' should be (6*5*4*3*2*1) = 720. Note also that 0! should give a result of one.
  13. Once you are sure the program is done and working well, start a script using script
  14. Once the script is running, show you running the program once, selecting each menu option at least once (including the extra credit if implemented). Note you should NOT need to run your program more than once.
  15. Exit from the script, which will save the log typescript
  16. Once you are done, use submit to submit two files, cw09.c and typescript

    linux3[26]% submit cs104 cw09 cw09.c typescript
    

  17. After submitting your files, you can verify your files were submitted by using the submitls command
  18. MAKE SURE YOU LOGOUT AFTER SUBMITTING THE ASSIGNMENT AND BEFORE LEAVING CLASS!