Classwork 3: Input & Output
Objectives
practice using printf() and scanf()
Assignment
Write a program that asks the user for the user's name and the user's height in inches. Your program then replies with the user's name and height in centimeters. A sample run of your program might look like this: (The user's response is in orange.)PT[203]% gcc -Wall height.c PT[204]% ./a.out What is your name? Lancelot How tall are you in inches? 78 Hello, Lancelot. You are 198.120000 centimeters tall. PT[205]%
Notes
- Please name your C program height.c
- You can use any of the programs we have seen in class as a starting point. These are on the topics page. You can download these or cut-and-paste into nano. If you download, the file will usually be saved in your downloads directory. Use the Unix mv command to move and rename the file. Recall that gcc requires that the name of C programs end with the .c extension. The programs on the class web site have names that end with .txt (so the web browser will display them).
- Think about how many variables you might want for this program. What should their types be? int, double, ...?
- If you have extra time, consider the following
embellishments:
- Most people do not need to see their height with 6 digits after the decimal point (especially in centimeters). In printf(), you can use modifiers with %f to specify the precision. For example %8.3f, says to print a total of 8 characters (including the decimal point) and use 3 digits after the decimal point.
- Play around with the precision until you have a format that looks good to you.
- Suppose that you want to round the height to the nearest centimeter. E.g., you want to have 198.120000 reported as 198 and 198.621000 reported as 199. How can you do this?
What to submit
Use the script command to record yourself compiling your program and running your program 3 times. Use exit to terminate the recording. Then submit your program and typescript file:
submit cs104_chang cw03 height.c typescript
Note: If you do not complete the program, you should submit whatever you have accomplished. (Do record yourself compiling the program with script.) The classwork will be closed out from further submission at the end of class, but you may improve your score by 10% if you submit a better version by next class. As described in the syllabus, only people who have submitted classwork in class can submit an improved version.
To submit an improved version of your classwork after class, use finish as the project name in place of cw03:
submit cs104_chang finish height.c typescript