Homework 1
[Solutions]Due Date
This assignment is due before class on Monday, June 6th. Please refer to the page on electronic submission for more information. If you choose to use a single late day, the deadline becomes extended until 11:59pm on June 7thth (so it extends the deadline by almost 30 hours). After that, each additional late day will extend the deadline an additional 24 hours. Remember that you can use at most 2 late days on this first assignment.Programming
- (40 Points) For the first program, write a quick input/output
program. This program should take as input exactly
three integers (each on a new line) and print out their average
(mean). Also, have the program print out which is largest and which is smallest. As there is not much to code here, the point is to work
on some output. All printed numbers should be formatted to fit
in 6 spaces (no decimals, since these are integers). The average field
should be formatted to always contain 2 digits after the decimal point. The average should be some type of decimal data type.
The output should look as follows:
$gcc -Wall -o average average.c $./average Enter First Number: 2 Enter Second Number: 4 Enter Third Number: 4 Largest Number: 4 Smallest Number: 2 NUM1 NUM2 NUM3 AVR ------------------------- 2 4 4 3.33
So your code should work with floating point numbers as output (float or double) and print out no more than two decimal places. Also, the numbers above are right-justified in the table and separated by tabs. This is all controlled by printf and can be found in the documentation. However, we will cover this on Wednesday (but other parts of this can be completed immediately)
- (60 points) Write a program to find a solution to the following
problem. The strategy is similar to the one used in class 2.
You are planning a gambling trip around Nevada. You will spend one night at each casino you visit and will have to pay for a room. Since you want the trip to get better towards the end, you decide to spend $2 on a room at the first hotel, $3 on the second, $4 at the third and so on (spending one additional dollar at each new destination). Assume that you are bringing no less than $11 on this trip.
Given this is a casino, you also end up spending 1/9th of whatever money you have in your pocket (after paying for the room) at each casino. This assumes you start the trip with some lump sum in your pocket that continually decreases as the trip goes on.
So you plan to get some money, go to a casino, pay for a room, spend 1/9 of what's left, and continue to the next destination. You must spend less than $100 on this trip and stay at fewer than 10 casinos.
Here is the problem: You don't want to gamble at the last casino (you want to relax), so you want to bring exactly enough money such that you have exactly $0 left for gambling at the final destination. This avoids temptation. The goal is to find some amount of money and some number of resorts such that you end up with $0 to spend on gambling at the final resort while obeying the above constraints.
Hints (Possibly more to come)
This is like the problem we solved in class, but much more complex. A key piece is to realize that we want to hunt for two variables rather than one. We want to try all possible combinations of dollar amounts and resorts until we find a combination that works. Think about how to enumerate combinations... we will see a loop structure on Wednesday that will be helpful (although I mentioned a possible structure on Monday).
Beware of integer division. You don't want to be burdened with change. So let's assume that your solution is only valid if you can exactly take 1/9 of your money and spend it on gambling each visit. Before trying to take 1/9 of the remaining money, check to see if it is evenly divisible by 9 (using the % operator).
The program should simply output an answer such as:
$gcc -Wall -o casinotrip casinotrip.c
$./casinotrip
Solution: $xy and n resorts
Grading
Grading will be based on correctness as mentioned in each problem. Read each problem very carefully to ensure that you've completed all that is required. A programmer needs to be able to read a paragraph description and implement the requirements rigorously. (80%)COMMENTS and description will account for 10% of each assignment. You must use comments to describe in a succinct fashion what is happening in your code. Also, name, id, etc should be placed in a comment at the very top of all your files.
Your code must be ANSI compliant. If you follow what the books say, this should not be an issue, but bad habits are known to pop up at this stage. (5%)
The last 5% is based on elegance/efficiency/style. For this project, efficiency won't be taken heavily, but formatting your code as we've been doing will be. Don't write code that completely defies any reasonable formatting convention (even if it is correct). For example, don't do things like:
int main(void) { int x;int y; int z; int c,d;c=5; if (c == 1) { d = 7; c = 4; } return 0; }
You are submitting two files: casinotrip.c and average.c, each with a description of how your code works either in comments or in a separate README file. Don't submit binaries (like a.out). Place your .c files (and perhaps README file as plain text) in a directory on the cunix machine and see the submission instructions