如何在Fortran中找到统计模式
我正在尝试编写一个程序来查找整数数组的平均值、中位数和众数,但在查找众数时遇到一些复杂情况。以下是我迄今为止编写的代码。
首先,程序将提示用户输入要输入的整数数量的值,然后请求输入该整数数量。然后将整数按升序排序,并找到平均值和中位数。
我遇到的问题是当我尝试获取模式时。我能够计算重复值出现的次数。通过找到出现次数最多的值,我们就能够找到众数。但我不确定如何做到这一点。 Fortran 中是否有任何内在函数可以计算输入值的出现次数以及出现次数最多的值?
PROGRAM STATISTICS
!Created by : Rethnaraj Rambabu
IMPLICIT NONE
REAL, DIMENSION(:), ALLOCATABLE:: VAL
REAL TEMP, MEDIAN
REAL EVEN, MEAN, SUM, FMODE
INTEGER N, I,J
WRITE(*,*)' WHAT IS THE VALUE FOR N? '
READ(*,*) N
ALLOCATE(VAL(N))
WRITE(*,*) 'ENTER THE NUMBERS'
OPEN(1,FILE='FILE.TXT')
READ(1,*)(VAL(I),I=1,N)
CLOSE(1)
WRITE(*,*) VAL
!/---FOR SORTING----/!
DO I=1,N-1
DO J=1,N-1
IF(VAL(J) > VAL(J+1)) THEN
TEMP=VAL(J)
VAL(J)=VAL(J+1)
VAL(J+1)=TEMP
END IF
END DO
END DO
WRITE(*,*) VAL
!/-----MEDIAN----/!
IF ((N/2*2) /= N) THEN
MEDIAN=VAL((N+1)/2)
ELSE IF ((N/2*2) == N) THEN
EVEN= (VAL(N/2)+VAL((N+2)/2))
MEDIAN=EVEN/2
END IF
WRITE(*,*)'MEDIAN=', MEDIAN
!/----MEAN----/
SUM=0
DO I=1,N
SUM=SUM+VAL(I)
END DO
MEAN=SUM/N
WRITE(*,*)'MEAN=', MEAN
!/------MODE----/
FMODE=1
DO I=1,N-1
IF (VAL(I) == VAL(I+1)) THEN
FMODE=FMODE+1
END IF
END DO
WRITE(*,*)FMODE
END PROGRAM
FILE.TXT
包含
10 8 1 9 8 9 9 7 5 9 3 5 6
I'm trying to write a program to find the mean, median, mode of an integer array but am having some complications in finding the mode. The following is the code that I've written so far.
First, the program will prompt user to enter a value for the number of integers that will be entered followed by request to enter that number of integers. The integers are then sorted in ascending order and the mean and median are found.
The problem I am having is when I try to get the mode. I am able to count the number of occurrence of a repetitive value. By finding the value with highest occurrence, we'll be able to find Mode. But I am unsure how to do this. Is there any intrinsic function in Fortran to calculate number of occurrence of input values and the value with highest occurrence?
PROGRAM STATISTICS
!Created by : Rethnaraj Rambabu
IMPLICIT NONE
REAL, DIMENSION(:), ALLOCATABLE:: VAL
REAL TEMP, MEDIAN
REAL EVEN, MEAN, SUM, FMODE
INTEGER N, I,J
WRITE(*,*)' WHAT IS THE VALUE FOR N? '
READ(*,*) N
ALLOCATE(VAL(N))
WRITE(*,*) 'ENTER THE NUMBERS'
OPEN(1,FILE='FILE.TXT')
READ(1,*)(VAL(I),I=1,N)
CLOSE(1)
WRITE(*,*) VAL
!/---FOR SORTING----/!
DO I=1,N-1
DO J=1,N-1
IF(VAL(J) > VAL(J+1)) THEN
TEMP=VAL(J)
VAL(J)=VAL(J+1)
VAL(J+1)=TEMP
END IF
END DO
END DO
WRITE(*,*) VAL
!/-----MEDIAN----/!
IF ((N/2*2) /= N) THEN
MEDIAN=VAL((N+1)/2)
ELSE IF ((N/2*2) == N) THEN
EVEN= (VAL(N/2)+VAL((N+2)/2))
MEDIAN=EVEN/2
END IF
WRITE(*,*)'MEDIAN=', MEDIAN
!/----MEAN----/
SUM=0
DO I=1,N
SUM=SUM+VAL(I)
END DO
MEAN=SUM/N
WRITE(*,*)'MEAN=', MEAN
!/------MODE----/
FMODE=1
DO I=1,N-1
IF (VAL(I) == VAL(I+1)) THEN
FMODE=FMODE+1
END IF
END DO
WRITE(*,*)FMODE
END PROGRAM
The FILE.TXT
contains
10 8 1 9 8 9 9 7 5 9 3 5 6
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,没有。您必须手动计算众数。
以下代码应该可以工作(在排序数组上):
说明:
我们将迄今为止最好的模式保存在
FMODE
变量中,并将FMODE
的计数保存在COUNT
变量。当我们单步遍历数组时,我们会在CURRENTCOUNT
变量中计算出等于我们现在看到的命中数。如果我们查看的下一项与上一项相同,我们只需增加
CURRENTCOUNT
即可。如果不同,那么我们需要重置CURRENTCOUNT
,因为我们现在将计算下一个元素的重复次数。在重置
CURRENTCOUNT
之前,我们会检查它是否大于之前的最佳结果,如果是,我们会覆盖之前的最佳结果(FMODE
和COUNT< /code> 变量)并获得新的最佳结果(无论
VAL(I)
和CURRENTCOUNT
处的结果),然后再继续。这种重置不会在循环结束时发生,因此我在末尾插入了另一个检查,以防最频繁的元素恰好是循环的最后一个元素。在这种情况下,我们会覆盖
FMODE
,就像我们在循环中所做的那样。No, there is not. You'll have to calculate the mode by hand.
The following code should work (on a sorted array):
Explanation:
We keep the best-so-far mode in the
FMODE
variable, and the count of theFMODE
in theCOUNT
variable. As we step through the array we count the number of hits that are equal to what we are looking at now, in theCURRENTCOUNT
variable.If the next item we look at is equal to the previous, we simply increment the
CURRENTCOUNT
. If it's different, then we need to reset theCURRENTCOUNT
, because we will now count the number of duplications of the next element.Before we reset the
CURRENTCOUNT
we check if it's bigger than the previous best result, and if it is, we overwrite the previous best result (theFMODE
andCOUNT
variables) with the new best results (whatever is atVAL(I)
andCURRENTCOUNT
), before we continue.This reset doesn't happen at the end of the loop, so I inserted another check at the end in case the most frequent element happens to be the final element of the loop. In that case we overwrite
FMODE
, like we would have done in the loop.它有点长,您可能可以删除可选参数,但这里提供了一个示例 here 。他们使用此处实现的快速排序算法。
或者,您可以使用
这不需要任何排序。
It is a bit lengthy, you could probably get rid of the optional argument, but there is an example provided here. They use the quick sort algorithm as implemented here.
Alternatively, you could use
This doesn't require any sorting.