CSC 161 Grinnell College Spring, 2010
 
Imperative Problem Solving and Data Structures
 

Laboratory Exercise on Input and Output in C

Goals

This laboratory exercise provides practice with reading data, computing, and printing within a C program.

Steps for this Lab

  1. Consider the program quarts-1.c from the annotated program in today's reading.

    1. Change the printf, so that quarts and liters are printed to 1 decimal-place accuracy.
    2. Run the revised program twice, entering 2.44 and 2.47 as the value of quarts. In printing, are these values rounded, or is the value truncated (ignoring the hundreds' digit)?
    3. Change the printing, so that quarts is printed in one printf statement and liters in a second printf statement.
  2. Write a program that reads a person's weight in pounds and computes (and prints) the weight in grams (1 pound avoirdupois = approximately 453.59 grams).

    Run your program with a few test cases. What values might you test (ideally, they should exercise the program's capabilities but be easy for you as programmer to check)?

  3. Each of the following programs reads two numbers using scanf, using different format variations.

    Click on each program, save it, compile it, and run it with the suggested input variations. For each test case, explain what values are read and why the scanf assigns the given values to the variables.

  4. Write a program to read a person's height in feet and inches and print the person's height in centimeters (1 inch = approximately 2.54 centimeters). The output of the program should present an equation of the form:

       5 feet 6.2 inches = 168.15 centimeters
    

    That is, the number of feet should be given as an integer, the number of inches to 1 decimal place, and the number of centimeters to 2 decimal places. One space should separate each number from text or the equal sign.

  5. Write a program that reads the radius of a circle and prints the circle's area and circumference in the format illustrated below:

         radius    area     circumference
           2.5    19.63         15.71
    

    That is, the radius, area, and circumference should appear under headings, the radius should be printed to 1 decimal place, and the area and circumference to 2 decimal places.

  6. Write a program that reads the coefficients a, b, c of a quadratic equation: a x2 + b x + c = 0, and prints the roots of the equation to two decimal places.

    Notes:

  7. Do King, Programming Project 3.1 (page 50).
    Hint: Check the comments for %5.3d format on page 40.

  8. Do King, Programming Project 3.2 (page 50).


This document is available on the World Wide Web as

     http://www.cs.grinnell.edu/~walker/courses/161.sp10/labs/lab-i-o.shtml

created 13 May 1998
last revised 9 January 2010
Valid HTML 4.01! Valid CSS!
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu.