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
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
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.
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.
-
Comments on Program Format, Comments, Readability, etc.
(Points not given, but points can be deducted.) -
[8 points] Calendar
- [1 point] Prints day of the week header
- [1 point] Prints first day in the appropriate column
- [2 points] Aligns numbers with day of the week
- [1 point] Prints correct number of days for given month
- [1 point] Ends with exactly one newline
-
[2 points] Uses
ifandcasestatements appropriately
-
[7 points] Monte Carlo Estimation
- [1 point] Repeatedly takes samples
- [2 points] Randomly samples locations within the unit square
- [1 point] Determine whether samples lie within unit circle
- [1 point] Tallies samples within unit circle
- [1 point] Calculates π estimate
- [1 point] Prints exactly four decimal digits of π estimate
-
[10 points] Testing
- [2 points] Test plan enumerates the ranges of problem circumstances
- [2 points] Test plan lists test cases to be considered with expected outcomes
- [4 points] Transcript records actual test runs
- [2 points] Summary statement explains why the programs are correct
A five point penalty will apply to any submission that includes personally identifying information anywhere other than the references/honesty file.
