创建给定数组的频率数组时出现问题

发布于 2024-10-31 21:00:28 字数 966 浏览 2 评论 0原文

我正在尝试在 Fortran 95 上创建给定数组的频率数组。例如,如果我有一个数组 (\1 2 4 2 4 2 5),则频率数组应该是每个项目出现的次数; (\1 3 2 3 2 3 1)。因此,因为原始数组中只有 5 个中的 1 个,所以新数组中的最后一个条目是 1,并且因为原始数组中有 3 个 2 中的 3 个,所以新数组中的相应条目是 2。

下面是示例我有代码,但我不断收到错误。我想知道是否有人愿意为我提供一些指导和帮助,帮助我解决我可能做错的事情。我们将非常感激。

我没有包含生成原始数组的代码部分,因为我很确定它是正确的,所以这里只是频率数组的子例程。此外,原始数组在此子例程之外按升序排序。也许我没有正确传递原始数组 num(i) ?

INTEGER, DIMENSION(100)::num(100)
    INTEGER, DIMENSION(100)::freq(100)
    INTEGER:: i=0, k=0, numinteger, count=0

CALL FreqArray(num, numinteger,freq)


SUBROUTINE FreqArray(num, numinteger, freq)

INTEGER, INTENT(IN):: num(100), numinteger
INTEGER, INTENT(OUT):: freq(100)

DO i=1,9
    count=0
    DO k=1, numinteger
        IF (num(k)==i)THEN
            count=count+1
        END IF
    END DO
    freq(i)=count
END DO

PRINT*, "Frequency of Digits"
PRINT*, " "
WRITE(*,'(1X,A,T35,A)'),"Digit","Frequency"
WRITE(*,'(1X,I1,T35,I1)'),num(i),freq(i)


END SUBROUTINE

非常感谢您抽出时间。

I am trying to create a frequency array of a given array on Fortran 95. For instance if I have an array (\1 2 4 2 4 2 5), the frequency array should be the number of times each item appears; (\1 3 2 3 2 3 1). So because there are only 1 of 5s in the original array, the last entry in the new array is 1 and because there are 3 of 2s in the original array, the corresponding entries on the new array is a 2.

Below is a sample of the code I have, but I keep getting errors. I was wondering if anyone would be willing to give me some guidance and help on what I could be doing wrong. It would be very much appreciated.

I haven't included the part of the code that generates my original array because I'm pretty sure it is correct so here is just the subroutine for the frequency array. Also the original array was sorted in ascending order outside this subroutine. Perhaps I didn't pass the original array, num(i) correctly??

INTEGER, DIMENSION(100)::num(100)
    INTEGER, DIMENSION(100)::freq(100)
    INTEGER:: i=0, k=0, numinteger, count=0

CALL FreqArray(num, numinteger,freq)


SUBROUTINE FreqArray(num, numinteger, freq)

INTEGER, INTENT(IN):: num(100), numinteger
INTEGER, INTENT(OUT):: freq(100)

DO i=1,9
    count=0
    DO k=1, numinteger
        IF (num(k)==i)THEN
            count=count+1
        END IF
    END DO
    freq(i)=count
END DO

PRINT*, "Frequency of Digits"
PRINT*, " "
WRITE(*,'(1X,A,T35,A)'),"Digit","Frequency"
WRITE(*,'(1X,I1,T35,I1)'),num(i),freq(i)


END SUBROUTINE

Thanks so much for your time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

香草可樂 2024-11-07 21:00:28

我猜想“(9)”会覆盖“DIMENSION(100)”,使“freq”成为长度为9的数组。因此,对于第三个参数,实际参数的长度为9,而虚拟参数的长度为100。这会导致你的错误消息“实际参数...小于虚拟大小”。

其他建议:您可以使用声明“...,dimension (:) :: num”使子例程更加通用。然后使用“size”内在函数来确定 num 的大小。 freq 可以固定为 9,因为总是有 9 位数字。

I guess that the "(9)" is overriding the "DIMENSION(100)" making "freq" an array of length 9. Thus for the third argument the actual argument is length 9, while the dummy is length 100. Which causes your error message "actual argument ... is smaller than dummy size".

Other suggestions: you could make the subroutine more general using declarations "..., dimension (:) :: num". Then use the "size" intrinsic to determine the size of the num. freq could be fixed to 9 since there are always 9 digits.

静若繁花 2024-11-07 21:00:28

关于你的显示问题,我怀疑你打算循环一下

WRITE(*,'(1X,I1,T35,I1)'),num(i),freq(i)

With regards to your display issue, I suspect that you meant to have a loop around

WRITE(*,'(1X,I1,T35,I1)'),num(i),freq(i)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文