CSE 1540.03

Week 4.2.2 January 29, 2014

Problems

#1A.

Write a program to input a sequence of positive real numbers and compute the average.

program oneA

implicit none

integer count

real sum, x

sum = 0.0

count = 0

do

print*, "Enter the next number (end with neg. num.)"

read*, x

if (x .lt. 0.0) exit

sum = sum + x

count = count + 1

end do

print*, "Average = ", sum/count

end

test data:

general case: 7.5 2 12.6 4.3 -1

boundary case #1: 2.5 -1

boundary case #2: -1

#1B

Write a program to input a sequence of real numbers and compute the average.

program oneB

implicit none

integer count

real sum, x

sum = 0.0

count = 0

do

print*, "Enter the next number (end with 1e-38)"

read*, x

if (x .eq. 1e-38) exit

sum = sum + x

count = count + 1

end do

print*, "Average = ", sum/count

end

test data:

general case: 7.5 2 -12.6 4.3 1e-38

boundary case #1: 2.5 1e-38

boundary case #2: 1e-38

problem with boundary case #2: divide by zero

if (count .eq. 0) then

print*, "list of numbers is empty"

else

print*, "Average = ", sum/count

end if

#2A

Write a program to input a sequence of positive real numbers and find the largest.

program twoA

implicit none

real max, x

max = -1.0

do

print*, "Enter the next number (end with neg. num.)"

read*, x

if (x .lt. 0.0) exit

if (x .gt. max) then

max = x

end if

end do

print*, "Largest value = ", max

end

test data:

general case: 7.5 2 12.6 4.3 -1

boundary case #1: 2.5 -1

boundary case #2: -1

#2B

Write a program to input a sequence of real numbers and find the largest.

program twoB

implicit none

real max, x

print*, "Enter the first number (end with 1e-38)"

read*, max

if (max .eq. 1e-38) then

print*, "list of numbers is empty"

else

do

print*, "Enter the next number (end with 1e-38)"

read*, x

if (x .eq. 1e-38) exit

if (x .gt. max) then

max = x

end if

end do

print*, "Largest value = ", max

end if

end

test data:

general case: 7.5 2 -12.6 4.3 1e-38

boundary case #1: 2.5 1e-38

boundary case #2: 1e-38

#2C

Write a program to input a sequence of real numbers and find the largest. (using hardware/OS sensor for end-of-data)

program twoC

implicit none

integer ios

real max, x

print*, "Enter the first number (end with ctrl-d)"

read(*,*,IOSTAT=ios) max

if (ios .ne. 0) then

print*, "list of numbers is empty"

else

do

print*, "Enter the next number (end with ctrl-d)"

read(*,*,IOSTAT=ios) x

if (ios .ne. 0) exit

if (x .gt. max) then

max = x

end if

end do

print*, "Largest value = ", max

end if

end

test data:

general case: 7.5 2 12.6 4.3 <ctrl-d>

boundary case #1: 2.5 <ctrl-d>

boundary case #2: <ctrl-d>

#2D

Write a program to input a number n, followed by a sequence of n real numbers and find the largest.

program twoD

implicit none

integer i, n

real max, x

print*, "Enter the number of values to process"

read*, n

if (n .le. 0) then

print*, "list of numbers is empty"

else

print*, "Enter the first number"

read*, max

do i = 2, n

print*, "Enter the next number"

read*, x

if (x .gt. max) then

max = x

end if

end do

print*, "Largest value = ", max

end if

end

test data:

general case: 4 7.5 2 -12.6 4.3

boundary case #1: 1 2.5

boundary case #2: 0

boundary case #3: -1

#5.

Summing Series

Google: "Taylor Series" (see Wikipedia entry)

Consider the following series:

Sketch of a program to evaluate series

1

term 1

sum term

do

term − ( term × / )

if ( |term| < ) exit

sum sum + term

end do

program sum

implicit none

real sum, term, x

integer i

print*, "Enter x..."

read*, x

i = 1

term = 1.e0

sum = term

do

term = -(term*x/i)

if (abs(term) .lt. 1.e-3) exit

sum = sum + term

i = i+1

end do

print*, "e^-x = ", sum

end

CSE 1540 Week 4.2.2 – January 29, 2014 page 4 of 4