0% found this document useful (0 votes)
103 views13 pages

CS 1313 Midterm Exam A Solutions

This document contains the solutions to a midterm exam for a CS 1313 course at the University of Oklahoma. It provides answers to short answer questions testing knowledge of hardware devices, differences between compilers/assemblers, and Fortran naming rules. It also gives the output of sample Fortran programs testing basic variable usage, expressions, and logical operators.

Uploaded by

Ahmad Abba
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
103 views13 pages

CS 1313 Midterm Exam A Solutions

This document contains the solutions to a midterm exam for a CS 1313 course at the University of Oklahoma. It provides answers to short answer questions testing knowledge of hardware devices, differences between compilers/assemblers, and Fortran naming rules. It also gives the output of sample Fortran programs testing basic variable usage, expressions, and logical operators.

Uploaded by

Ahmad Abba
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

12/5/2017 Midterm Exam A Solutions, CS 1313 010 Spring 2000, University of Oklahoma, Norman

CS 1313 010 Spring 2000


Midterm Exam A Solutions

Part I: Short Answer Questions

1. Name 2 of each of the following kinds of hardware devices. (2 points for each)
a. input:
Some possible answers:
keyboard, mouse, joystick, scanner
b. output:
Some possible answers:
monitor, printer, speakers
c. secondary storage:
Some possible answers:
hard disk, floppy disk, CDROM, CD-RW, DVD, DVD-RAM, Zip disk, Jaz disk, Superdisk,
magnetic tape, paper tape, punch card

2. Describe the difference(s) between the following. Your answers should be in the form ``Roses are red, but
violets are blue.'' (4 points for each)
Answers:
a. The difference(s) between a compiler and an intepreter:
Answer: a compiler converts an entire program from a high level language into a machine language
executable all at once, before running, but an interpreter reads and performs one high level
command at a time.
b. The difference(s) between a compiler and an assembler:
Answer: a compiler converts a source code in a high level language into a machine language
executable, but an assembler converts a source code in a low-level, CPU-specific assembly
language into a machine language executable.

3. Name 3 properties of a Fortran 90 symbolic name. (2 points for each)


Answers:
a. A Fortran 90 symbolic name can be made of only letters, digits and underscores.
b. A Fortran 90 symbolic name must start with a letter.
c. A Fortran 90 symbolic name can be at most 31 characters long.

4. When is it acceptable to visit a CS1313 instructor or TA in their office? (2 points for each)
Answers:
a. During their scheduled office hours.
b. With an appointment made 24 hours in advance.

Part II: Program Output


Give the output of the following programs. (Or mark NO OUTPUT if the program won't compile or doesn't
output anything.) You don't need to include leading or trailing blanks. For REAL values, assume outputs have 4
digits to the right of the decimal point by default.

1. Basic (2 points each)

http://cs1313.ou.edu/CS1313_2000Spring/midterma_soln.html 1/13
12/5/2017 Midterm Exam A Solutions, CS 1313 010 Spring 2000, University of Oklahoma, Norman

a. PROGRAM meloveprog
IMPLICIT NONE
PRINT *, "I love programming!"
END PROGRAM meloveprog

Answer: I love programming!


% cat meloveprog.f90
PROGRAM meloveprog
IMPLICIT NONE
PRINT *, "I love programming!"
END PROGRAM meloveprog
% f90 -o meloveprog meloveprog.f90
% meloveprog
I love programming!

b. PROGRAM mevarassn
IMPLICIT NONE
INTEGER :: woop
woop = 10
PRINT *, 'woop = ', woop
END PROGRAM mevarassn

Answer: woop = 10
% cat mevarassn.f90
PROGRAM mevarassn
IMPLICIT NONE
INTEGER :: woop
woop = 10
PRINT *, 'woop = ', woop
END PROGRAM mevarassn
% f90 -o mevarassn mevarassn.f90
% mevarassn
woop = 10

c. PROGRAM mevarchg
IMPLICIT NONE
REAL :: wawa = 12.0
wawa = 16
PRINT *, 'wawa = ', wawa
END PROGRAM mevarchg

Answer: wawa = 16.0000


Note: when an INTEGER literal constant is assigned to a REAL variable, the constant is first converted
to REAL.
% cat mevarchg.f90
PROGRAM mevarchg
IMPLICIT NONE
REAL :: wawa = 12.0
wawa = 16
PRINT *, 'wawa = ', wawa
END PROGRAM mevarchg
% f90 -o mevarchg mevarchg.f90

http://cs1313.ou.edu/CS1313_2000Spring/midterma_soln.html 2/13
12/5/2017 Midterm Exam A Solutions, CS 1313 010 Spring 2000, University of Oklahoma, Norman

% mevarchg
wawa = 16.0000

d. PROGRAM meimpl
INTEGER :: boohoo = 5
booho = boohoo + 1
PRINT *, "boohoo = ", boohoo
END PROGRAM meimpl

Answer: boohoo = 5
% cat meimpl.f90
PROGRAM meimpl
INTEGER :: boohoo = 5
booho = boohoo + 1
PRINT *, "boohoo = ", boohoo
END PROGRAM meimpl
% f90 -o meimpl meimpl.f90
% meimpl
boohoo = 5

Notice that this program is missing the IMPLICIT NONE statement, and that in the assignment
statement the variable name is misspelled on the left hand side of the equal sign, so booho is
implicitly declared as a REAL and is assigned boohoo + 1 (which is 6), and therefore boohoo is
unchanged at 5.

2. Intermediate (4 points each)

a. PROGRAM meexpr1
IMPLICIT NONE
INTEGER :: a = 5, b = 10, c = 15
REAL :: z
z = a + b + c
PRINT *, z
END PROGRAM meexpr1

Answer: 30.0000
Breakdown:
z=a+b +c
z = 5 + 10 + 15
z = 15 + 15
z= 30
z = 30.0000
Notice that z is REAL, but a, b and c are INTEGER. So, the additions are performed as INTEGER
operations, but the final result is converted to REAL before being assigned to the REAL variable z.
% cat meexpr1.f90
PROGRAM meexpr1
IMPLICIT NONE
INTEGER :: a = 5, b = 10, c = 15
REAL :: z
z = a + b + c
PRINT *, z
END PROGRAM meexpr1
http://cs1313.ou.edu/CS1313_2000Spring/midterma_soln.html 3/13
12/5/2017 Midterm Exam A Solutions, CS 1313 010 Spring 2000, University of Oklahoma, Norman

% f90 -o meexpr1 meexpr1.f90


% meexpr1
30.0000

b. PROGRAM meexpr3
IMPLICIT NONE
INTEGER :: a = 2, b = 3, c = 4
REAL :: z
z = a + b / c
PRINT *, z
END PROGRAM meexpr3

Answer: 2.00000
% cat meexpr3.f90
PROGRAM meexpr3
IMPLICIT NONE
INTEGER :: a = 2, b = 3, c = 4
REAL :: z
z = a + b / c
PRINT *, z
END PROGRAM meexpr3
% f90 -o meexpr3 meexpr3.f90
% meexpr3
2.00000

Notice that the division is of two INTEGER variables, and INTEGER division truncates, and the
precedence order puts division before addition.

c. PROGRAM meexpr5
IMPLICIT NONE
LOGICAL :: p = .TRUE., q = .FALSE., r
r = (p .AND. q) .OR. (p .EQV. q)
PRINT *, r
END PROGRAM meexpr5

Answer: F
% cat meexpr5.f90
PROGRAM meexpr5
IMPLICIT NONE
LOGICAL :: p = .TRUE., q = .FALSE., r
r = (p .AND. q) .OR. (p .EQV. q)
PRINT *, r
END PROGRAM meexpr5
% f90 -o meexpr5 meexpr5.f90
% meexpr5
F

3. Advanced

a. Give the output for the given input. (3 points each)


PROGRAM meif
IMPLICIT NONE

http://cs1313.ou.edu/CS1313_2000Spring/midterma_soln.html 4/13
12/5/2017 Midterm Exam A Solutions, CS 1313 010 Spring 2000, University of Oklahoma, Norman

INTEGER :: input1, input2


READ *, input1, input2
PRINT *, "Lookie!"
IF (input1 < input2) THEN
PRINT *, "The first is less!"
ELSE IF (input1 > input2) THEN
PRINT *, "The first is greater!"
ELSE
PRINT *, "Huh?"
END IF
PRINT *, "Get lost!"
END PROGRAM meif

a. 2, 3
Answer:
Lookie!
The first is less!
Get lost!

b. 22, 22
Answer:
Lookie!
Huh?
Get lost!

c. 5, 4
Answer:
Lookie!
The first is greater!
Get lost!

% cat meif.f90
PROGRAM meif
IMPLICIT NONE
INTEGER :: input1, input2
READ *, input1, input2
PRINT *, "Lookie!"
IF (input1 < input2) THEN
PRINT *, "The first is less!"
ELSE IF (input1 > input2) THEN
PRINT *, "The first is greater!"
ELSE
PRINT *, "Huh?"
END IF
PRINT *, "Get lost!"
END PROGRAM meif
% f90 -o meif meif.f90
% meif
2, 3
Lookie!
The first is less!
Get lost!
% meif
22, 22
Lookie!
Huh?
Get lost!
% meif
5, 4
Lookie!

http://cs1313.ou.edu/CS1313_2000Spring/midterma_soln.html 5/13
12/5/2017 Midterm Exam A Solutions, CS 1313 010 Spring 2000, University of Oklahoma, Norman

The first is greater!


Get lost!

b. (16 points)
PROGRAM medonest
IMPLICIT NONE
INTEGER :: i, j, aggregate = 0
DO j = 1, 2
DO i = 1, 3
aggregate = aggregate + (j * i)
END DO
END DO
PRINT *, aggregate
END PROGRAM medonest

Answer: 18
% cat medonest.f90
PROGRAM medonest
IMPLICIT NONE
INTEGER :: i, j, aggregate = 0
DO j = 1, 2
DO i = 1, 3
aggregate = aggregate + (j * i)
END DO
END DO
PRINT *, aggregate
END PROGRAM medonest
% f90 -o medonest medonest.f90
% medonest
18

c. (10 points)

PROGRAM mearraya
IMPLICIT NONE
INTEGER,DIMENSION(1:6) :: a = (/1, 1, 2, 3, 5, 8/), b
INTEGER :: i
DO i = 1, 6
b(i) = a(i) * 2
END DO
PRINT *, (b(i), i = 1, 6)
END PROGRAM mearraya

Answer: 2 2 4 6 10 16
% cat mearraya.f90
PROGRAM mearraya
IMPLICIT NONE
INTEGER,DIMENSION(1:6) :: a = (/1, 1, 2, 3, 5, 8/), b
INTEGER :: i
DO i = 1, 6
b(i) = a(i) * 2
END DO
PRINT *, (b(i), i = 1, 6)
END PROGRAM mearraya
% f90 -o mearraya mearraya.f90
% mearraya
2 2 4 6 10 16

http://cs1313.ou.edu/CS1313_2000Spring/midterma_soln.html 6/13
12/5/2017 Midterm Exam A Solutions, CS 1313 010 Spring 2000, University of Oklahoma, Norman

Part III: Fill in the Blank

Fill in the (lined) missing parts of the program in order to produce the output shown. Write each answer on the
appropriate line. Write only on the blank lines and nowhere else.

1. (9 points)
% cat mefbdoa.f90
PROGRAM mefbdoa
IMPLICIT NONE
INTEGER :: i
DO i = 1, 5

IF ( ___________________________ ) THEN

PRINT *, "1st gear!"

ELSE IF ( ___________________________ ) THEN

PRINT *, "2nd gear!"

ELSE IF ( ___________________________ ) THEN

PRINT *, "3rd gear!"


ELSE
PRINT *, "Faster!"
END IF
END DO
END PROGRAM mefbdoa
% f90 -o mefbdoa mefbdoa.f90
% mefbdoa
1st gear!
2nd gear!
3rd gear!
Faster!
Faster!

Answer:
% cat mefbdoa.f90
PROGRAM mefbdoa
IMPLICIT NONE
INTEGER :: i
DO i = 1, 5
IF (i == 1) THEN
PRINT *, "1st gear!"
ELSE IF (i == 2) THEN
PRINT *, "2nd gear!"
ELSE IF (i == 3) THEN
PRINT *, "3rd gear!"
ELSE
PRINT *, "Faster!"
END IF
END DO
END PROGRAM mefbdoa
% f90 -o mefbdoa mefbdoa.f90
% mefbdoa
1st gear!
2nd gear!
3rd gear!
http://cs1313.ou.edu/CS1313_2000Spring/midterma_soln.html 7/13
12/5/2017 Midterm Exam A Solutions, CS 1313 010 Spring 2000, University of Oklahoma, Norman

Faster!
Faster!

2. (5 points)
% cat mefbarraya.f90
PROGRAM mefbarraya
IMPLICIT NONE
INTEGER :: i

INTEGER,DIMENSION(0:4) :: a = ______________________________________

INTEGER,DIMENSION(0:4) :: b
DO i = 0, 4
b(i) = a(i) * 2
END DO
PRINT *, (b(i), i = 0, 4)
END PROGRAM mefbarraya
% f90 -o mefbarraya mefbarraya.f90
% mefbarraya
2 4 8 16 32

% cat mefbarraya.f90
PROGRAM mefbarraya
IMPLICIT NONE
INTEGER :: i
INTEGER,DIMENSION(0:4) :: a = (/ (2 ** i, i = 0, 4) /)
INTEGER,DIMENSION(0:4) :: b
DO i = 0, 4
b(i) = a(i) * 2
END DO
PRINT *, (b(i), i = 0, 4)
END PROGRAM mefbarraya
% f90 -o mefbarraya mefbarraya.f90
% mefbarraya
2 4 8 16 32

OR you could do:


% cat mefbarraya2.f90
PROGRAM mefbarraya2
IMPLICIT NONE
INTEGER :: i
INTEGER,DIMENSION(0:4) :: a = (/ 1, 2, 4, 8, 16 /)
INTEGER,DIMENSION(0:4) :: b
DO i = 0, 4
b(i) = a(i) * 2
END DO
PRINT *, (b(i), i = 0, 4)
END PROGRAM mefbarraya2
% f90 -o mefbarraya2 mefbarraya2.f90
% mefbarraya2
2 4 8 16 32

3. (16 points)
% cat mefbtempa.f90
PROGRAM mefbtempa
IMPLICIT NONE

INTEGER,PARAMETER :: lb = ________________ , ub = ________________

REAL,DIMENSION(lb:ub) :: hi_temp_deg_F, hi_temp_deg_C


INTEGER :: year
http://cs1313.ou.edu/CS1313_2000Spring/midterma_soln.html 8/13
12/5/2017 Midterm Exam A Solutions, CS 1313 010 Spring 2000, University of Oklahoma, Norman

PRINT *, "What were Norman's high temperatures in degrees Fahrenheit"


PRINT *, " for the years ", lb, " through ", ub, "?"

READ *, ____________________________________________________________

DO year = lb, ub
hi_temp_deg_C(year) = &
& (hi_temp_deg_F(year) - 32.0) * (5.0 / 9.0)
END DO
PRINT *, "Norman's high temperatures in degrees Celsius"
PRINT *, " for the years ", lb, " through ", ub, " were:"

PRINT *, ____________________________________________________________

END PROGRAM mefbtempa


% f90 -o mefbtempa mefbtempa.f90
% mefbtempa
What were Norman's high temperatures in degrees Fahrenheit
for the years 1995 through 1999 ?
105, 106, 107, 108, 109
Norman's high temperatures in degrees Celsius
for the years 1995 through 1999 were:
40.55556 41.11111 41.66667 42.22223 42.77778

Answer:
% cat mefbtempa.f90
PROGRAM mefbtempa
IMPLICIT NONE
INTEGER,PARAMETER :: lb = 1995, ub = 1999
REAL,DIMENSION(lb:ub) :: hi_temp_deg_F, hi_temp_deg_C
INTEGER :: year
PRINT *, "What were Norman's high temperatures in degrees Fahrenheit"
PRINT *, " for the years ", lb, " through ", ub, "?"
READ *, (hi_temp_deg_F(year), year = lb, ub)
DO year = lb, ub
hi_temp_deg_C(year) = &
& (hi_temp_deg_F(year) - 32.0) * (5.0 / 9.0)
END DO
PRINT *, "Norman's high temperatures in degrees Celsius"
PRINT *, " for the years ", lb, " through ", ub, " were:"
PRINT *, (hi_temp_deg_C(year), year = lb, ub)
END PROGRAM mefbtempa
% f90 -o mefbtempa mefbtempa.f90
% mefbtempa
What were Norman's high temperatures in degrees Fahrenheit
for the years 1995 through 1999 ?
105, 106, 107, 108, 109
Norman's high temperatures in degrees Celsius
for the years 1995 through 1999 were:
40.5556 41.1111 41.6667 42.2223 42.7778

OR you could have:


% cat mefbtempa2.f90
PROGRAM mefbtempa2
IMPLICIT NONE
INTEGER,PARAMETER :: lb = 1995, ub = 1999
REAL,DIMENSION(lb:ub) :: hi_temp_deg_F, hi_temp_deg_C
INTEGER :: year
PRINT *, "What were Norman's high temperatures in degrees Fahrenheit"
PRINT *, " for the years ", lb, " through ", ub, "?"
READ *, hi_temp_deg_F
DO year = lb, ub

http://cs1313.ou.edu/CS1313_2000Spring/midterma_soln.html 9/13
12/5/2017 Midterm Exam A Solutions, CS 1313 010 Spring 2000, University of Oklahoma, Norman

hi_temp_deg_C(year) = &
& (hi_temp_deg_F(year) - 32.0) * (5.0 / 9.0)
END DO
PRINT *, "Norman's high temperatures in degrees Celsius"
PRINT *, " for the years ", lb, " through ", ub, " were:"
PRINT *, hi_temp_deg_C
END PROGRAM mefbtempa2
% f90 -o mefbtempa2 mefbtempa2.f90
% mefbtempa2
What were Norman's high temperatures in degrees Fahrenheit
for the years 1995 through 1999 ?
105, 106, 107, 108, 109
Norman's high temperatures in degrees Celsius
for the years 1995 through 1999 were:
40.55556 41.11111 41.66667 42.22223 42.77778

Part IV: Writing a Program


Write the program described below.

In mathemetics, a one-dimensional array (the kind we've been using in class) is often called a vector. The dot
product of two vectors of the same length is the sum of the pairwise products of their elements. That is, for two
vectors of length n

X = (x1, x2, ... , xn)

and

Y = (y1, y2, ... , yn)

(where n is a positive integer), their dot product is defined as

X . Y = x1 . y1 + x2 . y2 + ... + xn . yn

Write a program that

a. inputs an integer representing the length of the two vectors;


b. inputs all of the elements of the first vector;
c. inputs all of the elements of the second vector;
d. calculates and outputs the dot product of the two vectors.

For this exam problem, you are not required to have comments, idiotproofing or introductory output (though you
should prompt the user for input), and you may use numeric literal constants in the body of your program. You
may assume that the maximum possible vector length will be 100, and that the vector elements can be non-
integers.

You'll get substantial partial credit for setting up the problem correctly (e.g., declarations, input, output), so if
you can't write the full program, do as much as you can.

You may attach an extra sheet of notebook paper to this exam or write on the back of this page if you want. (35
points)

Answer:
% cat dotproduct.f90
PROGRAM dotproduct

http://cs1313.ou.edu/CS1313_2000Spring/midterma_soln.html 10/13
12/5/2017 Midterm Exam A Solutions, CS 1313 010 Spring 2000, University of Oklahoma, Norman

IMPLICIT NONE
REAL,DIMENSION(1:100) :: vector1, vector2
REAL :: dotprod = 0
INTEGER :: vector_length, i
PRINT *, "What is the length of the vectors?"
READ *, vector_length
PRINT *, "What are the ", vector_length, " elements of the first vector?"
READ *, (vector1(i), i = 1, vector_length)
PRINT *, "What are the ", vector_length, " elements of the second vector?"
READ *, (vector2(i), i = 1, vector_length)
DO i = 1, vector_length
dotprod = dotprod + vector1(i) * vector2(i)
END DO
PRINT *, "The dot product of the vectors is ", dotprod, "."
END PROGRAM dotproduct
% f90 -o dotproduct dotproduct.f90
% dotproduct
What is the length of the vectors?
5
What are the 5 elements of the first vector?
1 2 3 4 5
What are the 5 elements of the second vector?
2 4 6 8 10
The dot product of the vectors is 110.0000 .
% dotproduct
What is the length of the vectors?
3
What are the 3 elements of the first vector?
1 5 9
What are the 3 elements of the second vector?
2 6 10
The dot product of the vectors is 122.0000 .
% dotproduct
What is the length of the vectors?
7
What are the 7 elements of the first vector?
1 2 3 4 5 6 7
What are the 7 elements of the second vector?
1 2 3 4 5 6 7
The dot product of the vectors is 140.0000 .

Bonus Program
Write the program described below.

The nth Fibonacci number, for any positive integer n, is given as:

/ 1, if n = 1 or n = 2
f(n) = |
\ f(n - 1) + f(n - 2), otherwise

So, the first 7 Fibonacci numbers are:

1, 1, 2, 3, 5, 8, 13

Write a program that takes as its input n (the Fibonacci number that the user wants) and outputs the nth
Fibonacci number.

You may use an array if you want, but you're not required to.
http://cs1313.ou.edu/CS1313_2000Spring/midterma_soln.html 11/13
12/5/2017 Midterm Exam A Solutions, CS 1313 010 Spring 2000, University of Oklahoma, Norman

For this exam problem, you are not required to have comments, idiotproofing or introductory output (though you
should prompt the user for input), and you may use numeric literal constants in the body of your program.

You'll get substantial partial credit for setting up the problem correctly (e.g., declarations, input, output), so if
you can't write the full program, do as much as you can.

You may attach an extra sheet of notebook paper to this exam or write on the back of this page if you want. (25
points)

Answer without an array:


% cat fibonacci.f90
PROGRAM fibonacci
IMPLICIT NONE
INTEGER :: fibnum
INTEGER :: fib_n_minus_2 = 1, fib_n_minus_1 = 1, fib
INTEGER :: i
PRINT *, "Which Fibonacci number should I calculate?"
READ *, fibnum
IF ((fibnum == 1) .OR. (fibnum == 2)) THEN
fib = 1
ELSE
DO i = 3, fibnum
fib = fib_n_minus_1 + fib_n_minus_2
fib_n_minus_2 = fib_n_minus_1
fib_n_minus_1 = fib
END DO
END IF
PRINT *, "Fibonacci number ", fibnum, " is ", fib, "."
END PROGRAM fibonacci
% f90 -o fibonacci fibonacci.f90
% fibonacci
Which Fibonacci number should I calculate?
1
Fibonacci number 1 is 1 .
% fibonacci
Which Fibonacci number should I calculate?
2
Fibonacci number 2 is 1 .
% fibonacci
Which Fibonacci number should I calculate?
3
Fibonacci number 3 is 2 .
% fibonacci
Which Fibonacci number should I calculate?
4
Fibonacci number 4 is 3 .
% fibonacci
Which Fibonacci number should I calculate?
5
Fibonacci number 5 is 5 .
% fibonacci
Which Fibonacci number should I calculate?
6
Fibonacci number 6 is 8 .
% fibonacci
Which Fibonacci number should I calculate?
7
Fibonacci number 7 is 13 .
% fibonacci
Which Fibonacci number should I calculate?
8
Fibonacci number 8 is 21 .

http://cs1313.ou.edu/CS1313_2000Spring/midterma_soln.html 12/13
12/5/2017 Midterm Exam A Solutions, CS 1313 010 Spring 2000, University of Oklahoma, Norman

Answer with an array (works for up to the 25th Fibonacci number):


% cat fibonacci_array.f90
PROGRAM fibonacci_array
IMPLICIT NONE
INTEGER,DIMENSION(1:25) :: fib
INTEGER :: fibnum
INTEGER :: fibprevprev = 0, fibprev = 0
INTEGER :: i
PRINT *, "Which Fibonacci number should I calculate?"
READ *, fibnum
fib(1) = 1
fib(2) = 1
IF (fibnum > 2) THEN
DO i = 3, fibnum
fib(i) = fib(i-1) + fib(i-2)
END DO
END IF
PRINT *, "Fibonacci number ", fibnum, " is ", fib(fibnum), "."
END PROGRAM fibonacci_array
% f90 -o fibonacci_array fibonacci_array.f90
% fibonacci_array
Which Fibonacci number should I calculate?
1
Fibonacci number 1 is 1 .
% fibonacci_array
Which Fibonacci number should I calculate?
2
Fibonacci number 2 is 1 .
% fibonacci_array
Which Fibonacci number should I calculate?
3
Fibonacci number 3 is 2 .
% fibonacci_array
Which Fibonacci number should I calculate?
4
Fibonacci number 4 is 3 .
% fibonacci_array
Which Fibonacci number should I calculate?
5
Fibonacci number 5 is 5 .
% fibonacci_array
Which Fibonacci number should I calculate?
6
Fibonacci number 6 is 8 .
% fibonacci_array
Which Fibonacci number should I calculate?
7
Fibonacci number 7 is 13 .
% fibonacci_array
Which Fibonacci number should I calculate?
8
Fibonacci number 8 is 21 .

Ask for help with this question.

http://cs1313.ou.edu/CS1313_2000Spring/midterma_soln.html 13/13

You might also like