[問題] 利用迴圈求最大值最小值?
各位大大好
小弟不才
最近剛學fortran
修課時,老師出了一個作業
是要利用計數迴圈去輸入多個任意的實數數值(迴圈數=數值個數)
然後再找出這些數值中的最大最小值
Write a Fortran program that will read an arbitrary number of real values and
find the maximum and minimum of the input data.
Prompt the user for the number of data to be entered and use a do loop
(iterative loop) to read the input data.
以上是作業內容
老師說不能用陣列求(因為還沒教.....)
目前只想到這樣寫....
program hw5
implicit none
integer :: i,n
real :: x,max_x=0,min_x=0
write(*,*) "Enter number of real values:"
read(*,*) n
if (n < 2) then
write(*,*) 'At least 2 values must be entered.'
else
do i=1,n,1
write(*,*) "Enter a real value:"
read(*,*) x
if(x>max_x) max_x=x
if(x<min_x) min_x=x
end if
end do
end if
write(*,*) "最大值為",max_x,"最小值為",min_x
stop
end
我知道是錯的,可是我想不出來該如何修正...
可以請各位高手們幫我看一下嗎><
感謝!!
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 140.112.63.141
※ 文章網址: http://www.ptt.cc/bbs/Fortran/M.1412766319.A.96A.html
推
10/08 23:06, , 1F
10/08 23:06, 1F
→
10/08 23:08, , 2F
10/08 23:08, 2F
→
10/08 23:46, , 3F
10/08 23:46, 3F
推
10/09 09:14, , 4F
10/09 09:14, 4F
→
10/10 00:10, , 5F
10/10 00:10, 5F
推
10/10 01:08, , 6F
10/10 01:08, 6F
→
10/10 10:56, , 7F
10/10 10:56, 7F
推
10/10 12:11, , 8F
10/10 12:11, 8F
推
10/11 17:22, , 9F
10/11 17:22, 9F
推
10/12 18:47, , 10F
10/12 18:47, 10F
→
10/12 19:43, , 11F
10/12 19:43, 11F
推
10/15 23:09, , 12F
10/15 23:09, 12F
→
10/19 19:18, , 13F
10/19 19:18, 13F
Fortran 近期熱門文章
PTT數位生活區 即時熱門文章