[問題] 讀取檔案數據並求最大最小值

看板Fortran作者 (GanHuaWang)時間5年前 (2018/11/02 12:08), 5年前編輯推噓1(107)
留言8則, 2人參與, 5年前最新討論串1/1
大家好,最近學了fortran程式,還不是很熟,這邊有個小問題要請教大家,問題如下 Write a Fortran that will read an arbitrary number of non-negative real values and find the maximum and minimum of the input data. To test your program, input the following data from a data file (x.txt) and use IOSTAT in read command to determine the number of data points to be processed. Print the number of data points and the maximum and minimum you find. 97,28,66,87,15,95,45,67,76,65,73,88,69,90,63,25,47,70,63,99,61,79,10,92,36,89 前面讀數據的部分我已經解決,問題是如何讀進來後如何用Max和min函數來找最大最小值@@ 助教有打一些提示(?)教我們怎麼找 https://imgur.com/a/OdZqTLX https://imgur.com/a/VC6vPdo 可是這是從螢幕手動輸入的寫法,換成數據我就不太會了,= =還希望版上大神可以幫幫 期中快要爆炸的小弟Orz -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 140.112.25.45 ※ 文章網址: https://www.ptt.cc/bbs/Fortran/M.1541131690.A.CED.html

11/02 13:23, 5年前 , 1F
read(*,*) x 改成 read(UNIT=file_unit,*) x 就可以了吧?
11/02 13:23, 1F

11/02 13:24, 5年前 , 2F
如果是讀進來存到陣列 就用MAXVAL、MINVAL找陣列的最大、
11/02 13:24, 2F

11/02 13:24, 5年前 , 3F
小值
11/02 13:24, 3F
https://imgur.com/a/6DDAtBm 附上小弟的程式圖(用的是cygwing64),不過執行後會跑出如下圖 https://imgur.com/a/LzKRnvS 不知道問題在哪裡@@ ※ 編輯: ganhua (140.112.241.23), 11/02/2018 22:10:58

11/03 13:57, 5年前 , 4F
試試看"read(30,10...) data"的迴圈結束之後先關掉檔案
11/03 13:57, 4F

11/03 13:57, 5年前 , 5F
"do while(x>0)" 之前再打開檔案一次
11/03 13:57, 5F

11/03 13:57, 5年前 , 6F
還有data本身就是複數 不需要加s
11/03 13:57, 6F

11/03 16:08, 5年前 , 7F
因為第一個do loop已經read到檔案底了,重頭read要在do
11/03 16:08, 7F

11/03 16:08, 5年前 , 8F
while前先rewind(30)
11/03 16:08, 8F
了解,我過幾天試試看,先準備其他科期中QQ ※ 編輯: ganhua (140.112.241.23), 11/04/2018 09:24:37
文章代碼(AID): #1Rsysgpj (Fortran)
文章代碼(AID): #1Rsysgpj (Fortran)