You are on page 1of 2

Lab 3: Simple Arithmetic

Objectives:

Introduce C Programming Data Types and Operators

Process:
Creating a New Folder for lab 3

Following the technique you practiced last week, create a new project named lab 3 in your cpre185labs folder on the U: drive
Problem 1: Mysterious Output

Download the following example source code. Compile and run the program. Using the material that we reviewed in lecture this week, describe the reason behind each of the incorrect program outputs. In each case, a unique error has been made when using the printf() display function. Pay close attention these mistakes are often pitfalls in later labs! For your lab report, show a corrected program and explain the erroneous output.
http://www.ece.iastate.edu/~daniels/lab3/lab3.c

Problem 2: Simple Arithmetic

Create a short program that performs the following arithmetic operations and displays the calculation and results to the screen. All calculations should be performed and assigned to an appropriate variable of the specified type. In other words, the arithmetic problem int = 1+2 would be performed by the following piece of code:
int answer; answer = 1 + 2;

Write a single program (lab3-2.c) to perform all of the following arithmetic operations. Be sure to pay special attention to the sample output format. All doubles must be displayed to two decimal places, and ints should be outputted as whole numbers.
a. b. c. d. e. f. g. h. i. j. k. l. m. n. int = 6427 + 1725 int = (6971 * 3925) 95 double = 79 + 12 / 5 double = 3640.0 / 107.9 int = (22 / 3) * 3 int = 22 / (3 * 3) double = 22 / (3 * 3) double = 22 / 3 * 3 double = (22.0 / 3) * 3.0 int = 22.0 / (3.0 * 3.0) double = 22.0 / 3.0 * 3.0 Calculate the area of a circle with circumference 23.567 Convert 14 feet to meters. Convert 76 degrees Fahrenheit to Centigrade.

Sample Program Output:

1 + 2 = 3 45.00 + 2.00 = 47.00 1 ft = 0.30 m

After completion, review the output of your program paying particular attention to arithmetic problems (a.) through (k.). Are the displayed results as you expect in every instance? Capture the output into a text file using the > trick on the command line. For each case where the output differs from the correct arithmetic result, explain the C program output. Include the output, and an explanation in your lab report.
Problem 3: Wiimote

Download http://www.ece.iastate.edu/~daniels/lab3/lab3-3.c Compile lab3-3.c to an executable named mag. (you may need to add -lm to the gcc command line) Copy over wiiwrap to your directory. Run ./wiiwrap /A |./mag Compare magnitudes when the wiimote is at rest, while you are moving it, and while one lab partner drops the wiimote into the others hands from a distance. Explain the source code and how the expression computed in the printf line could be useful in interpreting wiimote outputs.
Ending the Lab Session

Be sure to share your code and output files with your partner. Be sure to logoff before leaving your workstation in the lab!
Hints
1 ft = 0.3048 m TC = (TF - 32) / 1.8

You might also like