Loops
The goal of this lab is to introduce loops in C programming and increase familiarity with different types of loops.
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
-
In your first version of the program, implement the loop
using a
forconstruct. -
In your second version of the program, implement the loop
using a
whileconstruct.
-
In your first version of the program, implement the loop
using a
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!
- Write a program that approximates e by asking the user to input a value of n to determine the number of terms to include.
- 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.
- 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!
Largest Number
-
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:
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
Can you write this program in as few lines as possible? i.e. don't repeat the same statement more than once.
Lab Report 3 Submission
Submit your work for all three problems from this lab. This should incude 3 .c files, and your testing transcript from problem 2. In the case of problems 1 and 2, make sure both versions are present (a and b), though if you'd like you can have one of the two versions commented out. Before submitting, review the Guidelines for submitting work. Make sure you have:
- Identified all authors who contributed to the program
- Included an Academic Honesty Statement at the top of each program file.
- Documented each function (except for main).
- Included comments within main to describe what you are doing.
Note: there is no autograder for this program.