Docsity

Fortran90 Lab: Loop Constructs and Fibonacci Sequence - Prof. Charles Pruett, Lab Reports of Mathematics

Toggle sidebar
bg1
Math 248 Computers and Numerical Algorithms–Pruett
LABORATORY ASSIGNMENT
Programming for Repetition–Loop Constructs
C Write, compile, and run a Fortran90 program that uses a DO-EXIT loop
construct to count from 1 to 100 in integer arithmetic; print out the value of
the counter at each iteration.
C Write, compile, and run a Fortran90 program that uses an incrementing au-
tomatic counter-controlled loop construct to count from 0 to 100 by 2’s in
an integer variable and print out the value of the counter at each iteration.
What is the value of the loop control variable upon exit from the loop?
B Write, compile, and run a Fortran90 program that uses a decrementing
counter-controlled loop construct to count backward from 200 to 100 by
10’s in integer arithmetic and print out the value of the counter at each it-
eration. What is the value of the loop control variable upon exit from the
loop?
B Write, compile, and run a Fortran90 program that counts from 1 to 100 in
integer arithmetic and prints out the value of the counter at every iteration
except 27.
A Enter and compile the program on the reverse side, which reads nand com-
putes the first nFibonacci numbers.
A Run program FIBONACCI using n=30 and describe what happens. Then
run program FIBONACCI using n=50 and describe what happens.
A+ Explain in some detail what went wrong for n=50.
pf2

Partial preview of the text

Download Fortran90 Lab: Loop Constructs and Fibonacci Sequence - Prof. Charles Pruett and more Lab Reports Mathematics in PDF only on Docsity!

Math 248 Computers and Numerical Algorithms–Pruett

LABORATORY ASSIGNMENT

Programming for Repetition–Loop Constructs

C Write, compile, and run a Fortran90 program that uses a DO-EXIT loop construct to count from 1 to 100 in integer arithmetic; print out the value of the counter at each iteration.

C Write, compile, and run a Fortran90 program that uses an incrementing au- tomatic counter-controlled loop construct to count from 0 to 100 by 2’s in an integer variable and print out the value of the counter at each iteration. What is the value of the loop control variable upon exit from the loop?

B Write, compile, and run a Fortran90 program that uses a decrementing counter-controlled loop construct to count backward from 200 to 100 by 10’s in integer arithmetic and print out the value of the counter at each it- eration. What is the value of the loop control variable upon exit from the loop?

B Write, compile, and run a Fortran90 program that counts from 1 to 100 in integer arithmetic and prints out the value of the counter at every iteration except 27.

A Enter and compile the program on the reverse side, which reads n and com- putes the first n Fibonacci numbers.

A Run program FIBONACCI using n = 30 and describe what happens. Then run program FIBONACCI using n = 50 and describe what happens.

A+ Explain in some detail what went wrong for n = 50.

PROGRAM fibonacci ! !%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% ! ! Outputs the first n Fibonacci numbers, where n is ! an integer input value ! ! Author: Dr. Dave Pruett ! JMU Math ! Credits: None ! !%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% ! IMPLICIT NONE INTEGER :: fibo_number, previous1=1, previous2=0, i, n ! ! input the number n ! WRITE (,) ’enter the number n of Fibonacci ’, & ’numbers to print out ’ READ (,) n ! ! write the first two Fibonacci numbers outside the loop ! WRITE (,) 1, previous2! first Fibonacci number WRITE (,) 2, previous1! second Fibonacci number ! ! write the next n-2 Fibonacci numbers ! DO i = 3,n! NOTE: counter i is passive except at exit

fibo_number = previous2 + previous WRITE (,) i, fibo_number previous2 = previous1! shift operations previous1 = fibo_number! CAUTION: order is crucial

END DO

STOP

END PROGRAM fibonacci