简体   繁体   English

将FORTRAN循环转换为Java

[英]Converting FORTRAN loop to Java

For an assignment I've been given some stuff to do involving FORTRAN code, but the only issue is that we haven't been taught it yet so I'm not entirely sure what's going on so I've attempted to convert it to Java to try get a grasp on it. 对于一项任务,我已经给了一些涉及FORTRAN代码的东西,但唯一的问题是我们还没有被教过它所以我不完全确定发生了什么,所以我试图将它转换为Java试着抓住它。 The following is the FORTRAN code: 以下是FORTRAN代码:

L1:        DO 20 I = 1, 512
L2:           SUM(I) = 0
L3:           DO 40 J = 1, I
L4: 40         SUM(I) = SUM(I) + 1
L5: 20      CONTINUE

The idea is that L2 and L4 both take a machine cycle and I have to work out how long it takes for the loop to complete. 我们的想法是L2和L4都需要一个机器周期,我必须弄清楚循环完成所需的时间。 The following is my Java which I think is at least reasonably close to working out the value I want: 以下是我的Java,我认为至少可以合理地计算我想要的值:

public static void main(String[] args) {
    int cycles = 0;
    for(int i = 1; i < 512; i++){
        cycles = cycles + 1;
        for(int j = 1; j < i; j++){
            cycles = cycles +1;
        }
    }
    System.out.println(cycles);
}

Does this seem correct? 这看起来是否正确? Any help is appreciated. 任何帮助表示赞赏。 I've thought through it mathematically and got a different answer (although both are close to each other) so I'm not sure which is better. 我已经通过数学思考并得到了不同的答案(虽然两者都很接近)所以我不确定哪个更好。

EDIT: I'd like to make clear that I'm not attempting to port the FORTRAN directly to Java, rather just calculate the cycle time mentioned above by using Java. 编辑:我想明确表示我并没有尝试将FORTRAN直接移植到Java,而只是通过使用Java来计算上面提到的循环时间。

EDIT 2: I'm not trying to create the Array, only calculate the cycles taken during the loops. 编辑2:我不是要创建数组,只计算循环期间的周期。 Because both lines L2 and L4 take a cycle, I've swapped it in the Java ONLY to figure out the cycles taken, not do what the FORTRAN loop does. 因为L2和L4这两行都需要一个循环,所以我只在Java中交换它来计算所采用的循环,而不是像FORTRAN循环那样做。

In the Fortran the statement 在Fortran中的声明

DO 20 I = 1, 512

starts a loop whose end is the line with label 20 . 开始一个循环,其末尾是带有标签20的行。 Similarly, the inner loop ends on the statement labelled 40 . 类似地,内部循环在标记为40的语句上结束。 In modern Fortran this might look like 在现代的Fortran中,这可能看起来像

    DO I = 1, 512
       SUM(I) = 0
       DO J = 1, I
          SUM(I) = SUM(I) + 1
       END DO
    END DO

or even, since Fortran (since Fortran 90) has array statements and, as Duffymo has observed, SUM is an array 或者甚至,因为Fortran(自Fortran 90以来)有数组语句,正如Duffymo所观察到的, SUM是一个数组

       DO I = 1, 512
          SUM(I) = I
       END DO

or, as I would write it, using an array constructor with an implied do loop: 或者,正如我所写,使用带有隐含do循环的数组构造函数:

SUM = [(I,I=1,512)]

The Fortran sets element I of SUM to I . Fortran将SUM元素I设置为I

So, to answer OP's question more directly, the original Fortran code executes line 2 512 times and line 4 1+2+3+4+...+512 times. 因此,为了更直接地回答OP的问题,原始Fortran代码执行第2行512次,第4行1+2+3+4+...+512次。

My opinion is that writing a Java (or indeed any language) program to compute this sum is exactly the sort of thing that students of computer science (or software engineering or ...) should be taught not to do; 我的观点是,编写一个Java(或实际上是任何语言)程序来计算这个总和正是计算机科学(或软件工程或......)的学生应该被教导不要做的事情; there is a well-known closed-form equation for the sum of the first N integers (italicised to clarify what the term you should be Googling for is) and any aspiring software developer ought to know this closed-form. 有一个众所周知的封闭形式的方程式,用于前N个整数总和 (斜体来澄清你应该用谷歌搜索的术语是什么),任何有抱负的软件开发者都应该知道这种封闭形式。 Such an aspirant ought to know this closed-form precisely to be able to figure out how many operations are invoked in loops such as the ones shown without having to write a program to iterate pointlessly. 这样的追求者应该准确地知道这种封闭形式,以便能够弄清楚在循环中调用了多少操作,例如所显示的操作,而无需编写程序来无意义地迭代。

To conclude, OP's Java program would get an F- on any course I taught because it is not an implementation of what ought to have been implemented - a function to calculate the sum of the first N integers. 总而言之,OP的Java程序将在我教过的任何课程上获得F-因为它不是应该实现的实现 - 一个计算前N整数之和的函数。 That F- would be applied irrespective of the correctness or otherwise of the program. 无论程序的正确性或其他方面如何,都将应用F- Since I'm not a teacher that's not much of a threat, but I simply wouldn't hire anyone pretending to be a software engineer without this knowledge. 因为我不是一个没有太大威胁的老师,但我根本不会雇用任何假装自己没有这方面知识的软件工程师。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM