CS 36 FORTRAN Programming
Week 5
Assignment 4
1) p308n02
input:
m = 739, n = 12
Read at least first half of chapter 6 - Programming with Functions
Changes to Schedule
-
Assignment 4 assigned this week
-
Test 2 will be on April 6th (covers chapters 4 and 5)
Hints for Assignment 3
1) p246n14 - smallestLargest
-
you must choose some value for an "end-of-data sentinel", e.g., -999
-
read the first number into integer variable n
-
since n is both the smallest and the largest number read so far, assign
n to smallest and largest and assign 1 to positionSmallest and positionLargest.
-
now set up a loop that repeats until the end-of-data value is read
-
you need a variable, say count, that keeps track of how many values have
been read in
-
inside the loop compare the value just read to the previous smallest and
largest values and revise these variables as needed
2) p246n17 - statistics
-
as in last problem, you need to choose some value to use as an "end-of-data
sentinel".
-
you need a counter, say n
-
you need two accumulators, say sumX and sumX2, to add up the x values and
to add up their squares
-
loop until the end-of-data value is read
-
inside the loop add x to the sumX and add x**2 to the sumX2
3) p246n12 - Fibonacci
-
declare two integer variables, say f1 and f2, that will be used to store
two consecutive Fibonacci numbers
-
initialize f1 and f2 to 1
-
set up a loop that continues as long as f2 < 5000
-
calculate the ratio of f1 to f2
-
display f1, f2, and the ratio
-
now figure out how to move f1 and f2 up one number in the sequence.
I.e., the new f1 gets f2's old value, and the new f2 becomes the sum of
the old f1 and f2. (Hint: it is easier if you use a temp variable
to save one of the f's before assigning it a new value.) [For people
who enjoy a challenge: See if you can do it without using a temp variable.]