Can anyone know anything about fortran? If i give them a section of fortran can you tell me what the code will be?

SNiPeRX

Senior member
Apr 24, 2000
755
0
0
ISUM = 0
I = 1
20 IF (I .GT. 4) GO TO 30
ISUM = ISUM + I
I = I + 1
GO TO 20
30 WRITE (*,*) ISUM

Anyone?
 

KingNothing

Diamond Member
Apr 6, 2002
7,141
1
0
I think variable names that start with i are integers, so...this is my best guess in C:

int isum = 0;

for (int i = 1; i <= 4; i++)
{
isum++;
}
printf("%d", isum);
 

Descartes

Lifer
Oct 10, 1999
13,968
2
0
Originally posted by: KingNothing
I think variable names that start with i are integers, so...this is my best guess in C:

int isum = 0;

for (int i = 1; i <= 4; i++)
{
isum++;
}
printf("%d", isum);

That's not what the Fortran code does.
 

KingNothing

Diamond Member
Apr 6, 2002
7,141
1
0
Originally posted by: Descartes
Originally posted by: KingNothing
I think variable names that start with i are integers, so...this is my best guess in C:

int isum = 0;

for (int i = 1; i <= 4; i++)
{
isum += i;
}
printf("%d", isum);

That's not what the Fortran code does.

Then fix my code.Those 1's and i's look awfully similar, so is the code in this post correct?
 

SNiPeRX

Senior member
Apr 24, 2000
755
0
0
One the screen it should put something... im just not sure what...
maybe 0 I think it skips everything, because

20 if (1>=4) Go to 30

By going to 30 you get
Write(*,*) ISUM

THat means on the screen is should say "0" right?
 

Descartes

Lifer
Oct 10, 1999
13,968
2
0
Originally posted by: KingNothing
Originally posted by: Descartes
Originally posted by: KingNothing
I think variable names that start with i are integers, so...this is my best guess in C:

int isum = 0;

for (int i = 1; i <= 4; i++)
{
isum += i;
}
printf("%d", isum);

That's not what the Fortran code does.

Then fix my code.Those 1's and i's look awfully similar, so is the code in this post correct?

Yes, that's now correct. I did something similar:

#include <stdio.h>

int main(void)
{
int isum = 0;
int i = 0;

while (++i <= 4)
isum += i;

printf("%d\n", isum);
}

Yours is more inline with the original Fortran though.
 

SNiPeRX

Senior member
Apr 24, 2000
755
0
0
Thats the C++ i dont think i need that, i just need to know what the output would be from the following section.
 

Descartes

Lifer
Oct 10, 1999
13,968
2
0
Originally posted by: SNiPeRX
Thats the C++ i dont think i need that, i just need to know what the output would be from the following section.

rgwalt is correct, it's 10.
 

SNiPeRX

Senior member
Apr 24, 2000
755
0
0
can anyone explain that to me, im learning fortran as you can see for the first time, and im lost, i understand the basics i think, but...
 

Ness

Diamond Member
Jul 10, 2002
5,407
2
0
Originally posted by: KingNothing
I think variable names that start with i are integers, so...this is my best guess in C:

int isum = 0;

for (int i = 1; i <= 4; i++)
{
isum++;
}
printf("%d", isum);


I thought that was correct too :-\

OP said it wasn't, but he also said "Can anyone know anything about fortran? "
 

KingNothing

Diamond Member
Apr 6, 2002
7,141
1
0
ISUM = 0 <-- declare integer variable isum, set it to 0
I = 1 <-- declare integer variable i, set it to 1
20 IF (I .GT. 4) GO TO 30 <-- label this line "20": if the variable i is greater than 4, jump to the line labeled 30
ISUM = ISUM + I <-- add one to the value of isum, this line only executes if i is less than or equal to 4
I = I + 1 <-- add one to the value of i, when i reaches a value of 5 the if statement will jump to the line labeled 30
GO TO 20
30 WRITE (*,*) ISUM <-- dunno what the (*,*) means, but this line prints the value of isum to the screen I think
 

Ness

Diamond Member
Jul 10, 2002
5,407
2
0
Originally posted by: Descartes
Originally posted by: SNiPeRX
Thats the C++ i dont think i need that, i just need to know what the output would be from the following section.

rgwalt is correct, it's 10.


Why isn't it 5? I'm not hassling, I just wanna know.
 

rgwalt

Diamond Member
Apr 22, 2000
7,393
0
0
Originally posted by: KingNothing
ISUM = 0 <-- declare integer variable isum, set it to 0
I = 1 <-- declare integer variable i, set it to 1
20 IF (I .GT. 4) GO TO 30 <-- label this line "20": if the variable i is greater than 4, jump to the line labeled 30
ISUM = ISUM + I <-- add one to the value of isum, this line only executes if i is less than or equal to 4
I = I + 1 <-- add one to the value of i, when i reaches a value of 5 the if statement will jump to the line labeled 30
GO TO 20
30 WRITE (*,*) ISUM <-- dunno what the (*,*) means, but this line prints the value of isum to the screen I think

The (*,*) specifies that you will use the default output format and default output unit (the screen). Also, this piece of code does not type declare any of the variables. The computer will automatically type declare them (probably as integers) if the program does not include the IMPLICIT NONE statement.

Does this make sense SNiPeRX? Fortran is a really easy language to learn, but the logic can get confusing. I see that you are using Fortran 90/95 which allows GO TO statements. In older versions of Fortran, program flow control was handled by DO and IF statements.

Ryan

 

rgwalt

Diamond Member
Apr 22, 2000
7,393
0
0
Originally posted by: SNiPeRX
One the screen it should put something... im just not sure what...
maybe 0 I think it skips everything, because

20 if (1>=4) Go to 30

By going to 30 you get
Write(*,*) ISUM

THat means on the screen is should say "0" right?

The code says:

20 IF (I .GT. 4) GO TO 30

What you think is a 1 is actually an i. The sequence goes:

sum = 0
i =1
sum = 1
i = 2
loop
sum = 3
i = 3
loop
sum = 6
i = 4
loop
sum = 10
i = 5
loop
go to 30

ISUM = 10

Ryan