APS105 Lab 1

Objective

The objective of this lab is to give you practice writing C programs that use some arithmetic operators.

Part 1 - Compiling and Running

Download the file money.c. It contains a program that prompts the user for the price of an item, and then tells the user the price they entered, as well as the sale price. The sale price of an item is calculated as 85% of the item price.

Begin by compiling this program with the following command:
gcc -o money money.c
The -o lets us specify the output filename for the executable file produced by the compiler. If we do not include -o followed by a filename, we'll end up with a file called a.out, which isn't exactly the most descriptive filename in the world.

To run the program you just compiled, type ./money at the shell.

Please be sure you understand how this program works before continuing. Run the program several times with different sample inputs, and verify that the program does what you expect.

For this part of the lab, there are three questions. Place your discussion of each question in a file named lab1Part1.txt. You are not submitting any code for this part.

(1) Run the program with item price 2.10. (This represents an item that costs $2.10.) What is the sale price of the item?

(2) Using a calculator, calculate 85% of 2.10. You will get a result with three decimal places. If you properly round this value to two decimal places, what result do you get? It will be different from the result you got for question (1).

(3) Examining money.c, you'll see that two decimal digits are printed in each printf statement. printf is supposed to be rounding to two decimal places. Yet, it did not properly do this as we saw in question (1). Continue to increase the number of decimal digits printed for the sale price until you see the problem. You will notice that the sale price for 2.10 is not stored exactly inside the computer, but is only a (very close) approximation. How many decimal digits are required to see this?

Part 2 - Rewriting the Program

If you use the above program in a store, you're going to be off by one cent on some sales whose prices cannot be stored exactly. In general, using float or double variables for monetary values is dangerous, because we often cannot store real numbers exactly. What is typically done instead is to use integers, which we know can never have any rounding error.

Rewrite money.c so that it uses only integer values. There should be no float or double values or variables anywhere in your program. How can we possibly do this, given that values like $2.10 look like they have a fractional component?

What we will do is ask the user to provide the price of the item as a number of cents. For example, rather than typing 2.10, they will type 210 (for 210 cents). As another example, rather than 115.0 for $115, they will type 11500.

Once the user provides the price of the item in this way, your program should calculate the sale price in cents. (The sale price is 85% of the item price.) For example, for an input of 210, the sale price in cents is 179. To do this, you may use the following approach: multiply the item price by 85, then add 50, then divide by 100. Your program should then print the regular price of the item in dollars and cents, followed by the sale price in dollars and cents. As examples, here are four independent program runs.

Price of item? 210
Regular price of item: $2.10
Sale price of item: $1.79

Price of item? 357
Regular price of item: $3.57
Sale price of item: $3.03

Price of item? 1
Regular price of item: $0.01
Sale price of item: $0.01

Price of item? 10000
Regular price of item: $100.00
Sale price of item: $85.00

Be careful: we always want the number of cents to be two digits. If the number of cents is not two digits, we want to include a leading 0 in the output. Appendix B of the book includes information on how to do this with printf format specifiers.

Note: use exactly the same prompts that are used in the program runs above. Do not add any other prompts, and do not ask for any other input.

Submitting your Lab

When you have completed the lab, use the command submitaps105s 1 lab1Part1.txt lab1Part2.c to submit your files. You can also submit the files individually after you complete each part of the lab. Make sure you name your files exactly as stated (including lower/upper case letters). You may check the status of your submission using the command submitaps105s -l 1, where -l is a hyphen followed by the letter 'ell'.