::::: Homework Assignment #1 ::::: Due Wednesday, October 7, 2009 by midnight! *** All work has to be done individually! *** 1 written question and 1 programming question inside. Written question #1: Logical expressions Intro: Writing correct logical expressions is an important programming skill. Your program will make decisions based on your flow of logic. So here are some questions on logical expressions. Instructions: Write the following into a C logical expression. The expression should give correct intended results and it should compile without error. (b is a variable of type int.) Hints: You can test your answer on CUNIX using an "if" construct before submitting. a) b is exactly 153. b) b is neither 0 nor 1. c) b is in the range 5 and 10 (including 5 and 10) or in the range 15 to 20 (excluding 15 and 20). Grading: a) 5 points b) 10 points c) 15 points Programming question #1: Vending machine Intro: You want to write software for your vending machine. Here's how the vending machine works. 1) Machine shows the list of items on sale to user. The machine sells three items. It's your machine so you get to choose what you want to sell and how much they cost. For example, here's what my vending machine menu would look like. Menu: (a) apple $1.25 (b) orange $1.65 (c) banana $0.75 Please choose a, b, or c: 2) The user picks one item from the list. If the user does not pick a, b, or c, then program ends with an error message. For example, if the user types d, then the machine says, Sorry, you picked an invalid menu item. then terminates. If the user picks one of a, b, or c then move on to step 3. 3) Machine asks how many the user wants to buy and gets the information. Make sure the user does not input a negative number or zero. If they do, then terminate with an error message. If the user's input is valid, then move on to step 4. 4) Machine shows the total price. For example, Your total price is $6.25. 5) Machine receives payment from the user. The user can pay the exact amount, more than the amount, or less than the amount. Make sure the user does not input a negative number. If they do, terminate with an error message. (Paying $0.00 is not an error. The user paid less than the amount.) If the user pays the exact amount, print the invoice once more and you're done. For example, Here's your item (a), quantity = 5. Thank you! If the user pays more, then give them back the exact change. For example, Here's your item (a), quantity = 5. And here's your change: $0.65. Thank you! If the user pays less, then tell them how much more they have to pay and start again in step 5. For example, Sorry, you need to pay $3.25 more. Special instructions: Use the switch/case construct for step 2. For all other decision-making, you can use if, if/else, or if/else if/else constructs. Hints: The vending machine has five parts. Attack each part separately, starting from the first part. You already know everything there is to know to write this program. It's really simple. Use the printf statements when machine needs to tell the user something. Use the scanf statement when machine needs input from the user. Use the decision-making constructs such as switch/case of if/else. The last part (step 5) has a loop element. You have to start step 5 all over again if the user enters less money. To terminate program, use the "return 0;" statement. For example, if ( payment < 0 ) return 0; // returns out of the main function, which terminates program Grading: Your program will be graded out of 100 with the following breakdown: - Correctness (70 points) The vending machine should work as stated above. And it should compile!!!!! We will not accept any program that does not compile in CUNIX. A program that compiles but is less complete is better than one that does not compile. - Elegance (10 points) Simple code is better than complex code. Sometimes, complex code comes from the programmer not thinking clearly about the problem that the program is trying to solve. Write simple, elegant code. - Format (10 points) Use indentation so that your code is readable. Your code should look neat. - Comments (10 points) Write comments liberally at the top of your code, and also inside the code. Extra credit (10 points): In the last step, keep track of how much the user has already put into the machine, instead of resetting everything and starting over. (Hint: use a separate variable like total_deposit). Then, if the user puts less money in, then ask for more money until total_deposit equals total_cost of items. Of course, the user may put more money than the total_cost either accidentally or because of coin denominations. Then give them back the exact change. What to submit: 1) README file README file is a text file that contains your name, UNI, name and explanation of files that you're submitting, and your comments on your program. 2) Answer to written questions This should go in a separate text file with an extension .txt. 3) Your source code file This is the .c file you'll submit as the programming part of your homework. How to submit: 1) make a directory called homework1 2) copy or move your README, written answer file, source code file to homework1. 3) 'cd homework1' to get into that directory make sure you check that you're in homework1 by issuing a 'pwd' Unix command. 4) Type '~jk2520/submit hwk1' in shell prompt. For example, this is what it'd look like with the CUNIX shell prompt, $ ~jk2520/submit hwk1 The submit script will ask some questions and let you submit homework. You will receive a confirmation email if the homework was submitted properly.