[問題] 利用迴圈求最大值最小值?

看板Fortran作者 (crossstep)時間9年前 (2014/10/08 19:05), 編輯推噓7(706)
留言13則, 7人參與, 最新討論串1/1
各位大大好 小弟不才 最近剛學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
只用看的,沒看出什麼問題。唯一的就是初始max,min設的
10/08 23:06, 1F

10/08 23:08, , 2F
沒有太好。建議min=10^16, max = -10^16 之類的極端值
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
教好快 我這一班才到if
10/10 01:08, 6F

10/10 10:56, , 7F
嗨樓上 你也是修蔡老師的fortran?
10/10 10:56, 7F

10/10 12:11, , 8F
蔡老師只有在工科系有開fortran吧
10/10 12:11, 8F

10/11 17:22, , 9F
我是化工系的fortran
10/11 17:22, 9F

10/12 18:47, , 10F
是不是多一個end if...
10/12 18:47, 10F

10/12 19:43, , 11F
對 後來我這邊都重寫了 有發現到
10/12 19:43, 11F

10/15 23:09, , 12F
hi同班同學XD 難怪我看到問題覺得頗眼熟~
10/15 23:09, 12F

10/19 19:18, , 13F
嗨樓上你好!
10/19 19:18, 13F
文章代碼(AID): #1KDHflbg (Fortran)