Lab: Loops

While we used recursion to do repetition in Scheme, the primary mechanism for repetition in a C program is a new construct called a “loop”.

The goal of this lab is to introduce loops in C programming and increase familiarity with different types of loops.

A. Printing a Table

Earlier this semester, you saw a program (quarts.c.), that converted quarts to liters. Write a program that prints a table listing the conversions from one to twelve quarts into liters.

Use a careful print statement to keep proper spacing in the table.

Example Output:

Table of quart and liter equivalents
Quarts           Liters
   1             0.9463
   2             1.8927 
   3             2.8390 
   4             3.7853 
   5             4.7317 
   6             5.6780 
   7             6.6243 
   8             7.5707 
   9             8.5170 
  10             9.4633 
  11            10.4097 
  12            11.3560 

Exercises

Driver: The student closer to the whiteboard.

  1. In your first version of the program, implement the loop using a for construct.
  2. In your second version of the program, implement the loop using a while construct.

B. Approximating $e$

The mathematical constant \(e\) is calculated by an infinite series

\[e = 1 + 1/1! + 1/2! + 1/3! + ...\]

We can approximate the value of \(e\) with a partial sum:

\[e = 1 + 1/1! + 1/2! + 1/3! + ... + 1/n!\]

Exercises

Driver: The student farther to the whiteboard.

  1. Write a program that approximates $e$ by asking the user to input a value of $n$ to determine the number of terms to include. We haven’t discussed the scanf function in class, but readings have shown how to use it to ask the user for input. The following code fragment will ask a user to input an integer value:
    printf("Please enter a number:");
    int n;
    scanf("%d", &n);
    
  2. Modify your program so that it instead prompts the user for a small (floating-point) number (think 0.001). Then the program should continue adding terms until the current term becomes less than the user-prompted value. Just like for printf, you can use the %f format specifier to tell scanf to read a floating point value.
  3. Test your code using a testing transcript. In order to do this, first make a plan of values that you are going to test. (i.e. values for n in part a, and the approximation you’d expect to see. Note that this means you need to print out the approximated value, if you didn’t already do that.) Then use the technique at transcripts.html to record your testing. Verify your transcript looks how you expect.

Note: You should not include any header files to help you complete this program (such as one with math features). Instead, use loops!

C. Largest Number

Consider this program output:

Enter a number: 60
Enter a number: 38.3
Enter a number: 4.89
Enter a number: 100.62
Enter a number: 0

The largest number entered was 100.62 

Exercises

Driver: The student closer to the whiteboard.

  1. Write a program that finds the largest in a series of numbers entered by the user. The program must prompt the user to enter numbers one by one. When the user enters 0 or a negative number, the program must display the largest nonnegative number entered.

Can you write this program in as few lines as possible? i.e. don’t repeat the same statement more than once.