CSC 161  Grinnell College  Spring, 2009 
Imperative Problem Solving and Data Structures  
This laboratory exercise provides practice with basic elements of the C programming language. Specifically, the lab emphasizes conditional (if) and looping (for, while) constructs.
Use loops to print each of the following patterns:
6 5 4 3 2 1 0 1 2 3 6 8 10 12 14 16 18 Hello! Hello! Hello! Hello! Hello! Hello! Hello!! Hello!!! Hello!!!! Hello!!!!! * ** *** **** ***** ****** ******* ******** ********* * ** *** **** ***** ****** ******* ******** *********
Write a program that prints a table in which each row contains a number N together with its square, square root, cube, and cube root. The table should include 1 ≤ N ≤ 50. Label each of the columns in the table appropriately.
Experiment with each of the following code segments in a program, and explain why the code probably does not do what the programming intended. (In each case, assume j and sum have type int.)
for (j = 1; j <= 10; j++); printf ("%4d", j); sum = 0; for (j = 1; j <= 10; j++) { sum = sum + j; j = j + 1; }
An integer is prime if it is greater than 1 and if it is divisible only by 1 and by itself. Thus 7 and 23 are prime, while 22 and 30 are not (both 22 and 30 are divisible by 2).
Write a program that reads a number N and determines whether or not N is prime.
(Optional) Write a program that will find the first 100 prime numbers.
In program darts.c in today's reading, C's random function was used to help simulate the throwing of a dart. A similar approach can be used for many simulations. (For example, to simulate tossing a fair coin, one could generate a random number between 0 and 1 and then test if the number were less than 1/2. If so, consider a "head" is tossed; if not, consider the result a "tail".)
Consider the following situation: A couple decides that they want to raise at least one boy and one girl. They decide to keep having children until they have one child of each gender, and then stop having children. Assume that having a boy or a girl is equally likely, and assume that the gender of one child has no influence on the gender of the next.
Write a program that simulates the family size for 20 couples and prints the results in a table:
Couple Boys Girls Total 1 2 3 ... 20
Suppose you charge $497.60 on a credit card. The company will require you to make a minimum payment each month, say $15.00. They will also charge interest at the end of each month, say at a monthly rate of 1.5% of the outstanding balance. The balance for the next month is computed by the formula:
new balance = old balance + interest  paymentThis problem asks you to investigate the "cost" of making only the minimum monthly payment.
Write a program that reads the amount borrowed, the monthly interest rate, and the constant monthly payment. Have the program print a labeled table showing the month number and the balance at the beginning of that month (the balance at the beginning of month 1 is the amount borrowed). Continue printing until a payment would cause the balance to drop below zero. Also print the final payment necessary to close the loan, the total amount made in payments, and the "cost" of the loan (total payments  loan).
Computing the Greatest Common Divisor (Euclidean Algorithm):
[The following is an edited and abridged version of Section 4.1 from
Problems for Computer Solutions Using FORTRAN by Henry M. Walker,
Winthrop Publishers, 1980 and is used with permission of the copyright holder.]
Let N and M be two positive integers. The greatest common divisor of N and M, denoted gcd (M, N), is defined to be the positive integer D such that
For example, 2 = gcd (6, 8); 4 = gcd (4, 12); 1 = gcd (8, 9); 6 = gcd (66, 24).
Algorithm: The algorithm proceeds by long division  keeping track of subsequent remainders:
This process continues until we find a remainder R_{i+1} which is 0. Then R_{i} = gcd (M, N).
Example:
Thus, 6 = gcd (66, 24).
Write a C program that reads two positive integers from the keyboard and computes and prints their greatest common divisor  using the Euclidean Algorithm.
This document is available on the World Wide Web as
http://www.walker.cs.grinnell.edu/courses/161.sp09/labloopsc.html
created 31 August 1998 last revised 3 February 2009 

For more information, please contact Henry M. Walker at walker@cs.grinnell.edu. 