CSC 161 Schedule Readings Labs & Projects Homework Deadlines Resources

Loopy Conditions

Summary
You will write two programs using loops, conditionals, or both.
Objectives
Reinforce the basic operations and control structures of the C programming language.

Problem 1: Calendar Rendering

The terminal utility cal prints a simple calendar to the terminal window. In this exercise, you will implement a less flexible version of the utility.

Write a program calendar.c that begins with an initialized variable for the month (an integer in the range 1…12) and the first day of the week in the month (an integer in the range 0…6 where 0 corresponds to Sunday, 1 to Monday, and so on); the program should print the appropriate one month calendar, as in the following examples.

Example 1

Su  M  T  W Th  F  S
    1  2  3  4  5  6 
 7  8  9 10 11 12 13 
14 15 16 17 18 19 20 
21 22 23 24 25 26 27 
28 29 30 
Month 11 (November) with the first day 1 (Monday).

Example 2

Su  M  T  W Th  F  S
                1  2 
 3  4  5  6  7  8  9 
10 11 12 13 14 15 16 
17 18 19 20 21 22 23 
24 25 26 27 28 29 30 
31 
Month 3 (March) with the first day 5 (Friday).

Problem 2: Monte Carlo Simulation

A Monte Carlo method uses random values to model a system and/or estimate values. The technique is used widely in science and engineering, finance, and even law/policy.

Random dots are placed on the quadrant of a square with a
                      circle inscribed in it
Wikimedia/Nicoguaro (CC-BY)

A simple and common example of a Monte Carlo simulation can be used to estimate the value of π, which is widely known as the ratio of the area of a circle's circumference to its diameter. Less prevalent (but equally true) is the relationship between the areas of a circle and a square it is inscribed within.

With Ac = πr2 and As = (2r)2 = 4r2, the ratio of these two quantities is given by Ac / As = (πr2) / (4r2) = π / 4.

The Monte Carlo method for estimating π picks several random points within a unit square and counts the number of these sample points that also lie within the unit circle. As indicated by the formula above, four times this ratio gives a reasonable estimate of π when the number of samples is large. This fact is demonstrated in the animation at right.

Write a program montecarlopi.c that uses this method to calculate and print an estimate of π that is accurate to three decimal digits.

./montecarlopi
3.1416

Note that you will need to use the C standard library function rand to generate random integers between 0 and RAND_MAX. To consult the manual page for details, you can run the following terminal command.

man 3 rand

You should use the predefined, system-dependent value RAND_MAX (defined in stdlib.h)to convert these numbers to real values between 0 and 1.

General Notes

Your testing should cover an appropriate range of "input" values and your program's output should make it easy to assess correctness.

Grading

In addition to the general grading guidelines for evaluation, the assignment is worth 25 points.

Submit files calendar.c, montecarlopi.c, references.txt, and tests.txt.

A five point penalty will apply to any submission that includes personally identifying information anywhere other than the references/honesty file.