WEEK04
May 31 and June 2
This page contains lectures, examples and assignments for week 4
of the class "Introduction to Scientific Programming".
Lectures:
Examples:
-
arrow.cpp,
a program which uses a FOR statement to follow for 20 seconds the path of an arrow
shot into the air; a BREAK statement terminates the loop if the arrow hits
the ground before 20 seconds have elapsed.
-
checkerboard.cpp,
uses a pair of nested FOR statements to print the locations on a chessboard
and on a checkerboard.
-
deck.cpp,
uses a pair of nested FOR statements to print the cards in a deck.
-
gates_plusplus.cpp,
is a revised version of the "gates.cpp" program which uses as many
increment and decrement operators as possible.
-
grades.txt,
a text file containing values that can be added up by the sum_eof program
using a command like sum_eof < grades.txt.
-
leapyear.cpp,
a program which uses a FOR statement to print the years between 1900 and 2000
which were leap years.
-
lunch.cpp,
a program which uses a WHILE statement to read lunch bills.
By using a counter variable, the program only repeats the
loop exactly seven times, and then totals the bills.
-
sqroot_check.cpp,
a program for estimating the square root of a number,
using a WHILE statement, and using a counter to avoid
infinite loops.
-
sqroot_do_while.cpp,
a program for estimating the square root of a number,
using a DO...WHILE statement.
-
sqroot_while.cpp,
a program for estimating the square root of a number,
using a WHILE statement.
-
sum_100.cpp,
a program which sums the integers from 100 to 199 using a FOR loop.
-
sum_counter.cpp,
a program which must sum an unspecified number of real numbers.
The first data item entered by the user must be exactly the number
of values to be summed.
-
sum_eof.cpp,
a program which must sum an unspecified number of real numbers.
If it is used interactively, the user terminates the data with CTRL-D.
If it reads numbers from a file, it detects the end of file automatically.
-
sum_minus.cpp,
a program which must sum an unspecified number of real numbers.
It assumes that the last number entered is a special value of -1,
which is not added to the total, but simply signals the end of data.
-
three_heads.cpp,
a program in which we are flipping 3 coins over and over.
Each flip costs 25 cents, we win each time we get three heads,
and we want to play until we win 10 times. The program uses a
WHILE statement, and a "success" counter to keep track of how
many wins we have achieved.
-
trig_table.cpp,
a program which uses a FOR statement to print out the sine and cosine
of angles between 0 and 90 degrees, with 5 degree increments.
-
truck_for.cpp,
a program which uses a FOR statement to find the maximum brick load
a truck can take over a bridge that has a weight limit.
-
twelve.cpp,
a program which uses a FOR statement to print out which integers
between 1 and 12 are divisors of 12.
Assignments:
-
In class exercise #4, on Tuesday: Compute the Fibonacci Sequence.
-
Program Homework #2 is due on Thursday. Please mail your program and output
to Detelina, or turn in printed copies.
-
Program Homework #3, to be assigned on Thursday.
You can go up one level to
the ISC course page.
Last revised on 01 June 2011.