CSE 1540.03
Week 5.1
January 30, 2012
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 1e38)"
read*, x
if (x .eq. 1e38) 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
1e38
boundary case #1:
2.5
1e38
boundary case #2:
1e38
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
CSE 1540
Week 5.1 – January 30, 2012
page 1 of 4
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
#2A
Write a program to input a sequence of positive real numbers and find
the largest.
This is the end of the preview.
Sign up
to
access the rest of the document.
 Winter '12
 HOFBAUER
 boundary case

Click to edit the document details