Register to reply

Fortran 90 Description in program how to continue it)

by DoremiCSD
Tags: 90description, continue, fortran, program
Share this thread:
DoremiCSD
#1
Feb21-12, 12:36 PM
P: 10
1. The problem statement, all variables and given/known data
Write a program in Fortran 90 (not fortran 77) that reads from the user 4 integers a, b, c, d. The program should first check if applicable inequality
a ≤ b, c ≤ d.
If not then it should asking again numbers from the user until they have given numbers satisfying these inequalities. Once your program reads these numbers should

1. to print the squares of integers k such that is
a ≤ k ≤ b.
2. To print the product of all integers r is such that
c ≤ r ≤ d.



2. Relevant equations
its says that the program should asking the user to give numbers until satisfying a<=b and c<=d. This will be done with loop?( i dont know how to do this and i stucked).Still how i can find the squares of integers k and the product of all integers r?( i want only an description of how to do it to finish the program)



3. The attempt at a solution
program sub

implicit none

integers :: a, b, c, d

print *, "Give me an integer a:"
read *, a

print *, "Give me an integer b:"
read *, b

print *, "Give me an integer c:"
read *, c

print *, "Give me an integer d:"
read *, d

if(a .ge. b) then
print *, "Invalid."
stop
endif

if(c .ge. d) then
print *, "Invalid."
stop
endif
Phys.Org News Partner Science news on Phys.org
Suddenly, the sun is eerily quiet: Where did the sunspots go?
'Moral victories' might spare you from losing again
Mammoth and mastodon behavior was less roam, more stay at home
jedishrfu
#2
Feb21-12, 12:51 PM
P: 2,758
Here's a simple structure in pseudo code:

notOK=0

while(notOK<1) {
read a,b,c,d
if((a>b) or (c>d)) print "ERROR: either a>=b or c>=d: Please re-enter your four numbers"
else notOK=1
}

// step 2
for(k=a; k<=b; k++) {
print "k:"+k+" and k^2: " +(k*k)
}

// set 3 use a loop like the one above within another loop like the one above
DoremiCSD
#3
Feb21-12, 02:45 PM
P: 10
program Arithmetical

implicit none

integers :: a, b, c, d

PRINT *, "Give me an integer a:"
READ *, a

PRINT *, "Give me an integer b:"
READ *, b

PRINT *, "Give me an integer c:"
READ *, c

PRINT *, "Give me an integer d:"
READ *, d

IF(a .ge. b) THEN
PRINT *, "Invalid."
STOP
ENDIF

IF(c .ge. d) then
PRINT *, "Invalid."
STOP
ENDIF

notOK = 0
DO
notOK = notOK + 1
IF( (a>b) or (c>d) ) EXIT
PRINT *, "ERROR: either a>=b or c>=d: Please re-enter your four numbers"
ELSE
notOK = 1
ENDIF
END DO

FOR(k=a, k<=b, k++)
PRINT "k:"+k+" and k^2: " +(k*k)

END PROGRAM



I used this in silverfrost but cant run it. I belive that i have something wrong .Can anyone correct me wrongs?

jedishrfu
#4
Feb21-12, 02:47 PM
P: 2,758
Fortran 90 Description in program how to continue it)

you dont want to stop you want to loop until the a,b,c,d fit the limits the problem defined.

as I read your program it does the following:

asks for a reads a
asks for b reads b
asks for c reads c
asks for d reads d

tests a>b if so STOPS
tests c>d if so STOPS

so use of the notOK does nothing

instead you need to place your reads in a while (...) do end loop:


isOK=0

while(isOK<2) do

isOK=0 // reset the condition counter to ZERO
asks for a reads a
asks for b reads b
asks for c reads c
asks for d reads d

if(a.le.b) isOK=isOK+1 // we add ONE because the 1st condition was met
if(c.le.d) isOK=isOK+1 // we add ONE again because the 2nd condition was met

enddo // the loop ends when isOK==2

...when your program gets here then a<=b and c<=d and you can do steps 2 and 3...
jedishrfu
#5
Feb21-12, 03:00 PM
P: 2,758
for the squaring loop use:

DO 10 K=A,B,1
print K*K
10 CONTINUE
Mark44
#6
Feb21-12, 03:26 PM
Mentor
P: 21,216
Quote Quote by jedishrfu View Post
for the squaring loop use:

DO 10 K=A,B,1
print K*K
10 CONTINUE
For fortran 90, you'd want to dispense with the line numbers and CONTINUE, and do something like this:
DO K = A, B
   PRINT K * K
END DO
jedishrfu
#7
Feb21-12, 04:17 PM
P: 2,758
Quote Quote by Mark44 View Post
For fortran 90, you'd want to dispense with the line numbers and CONTINUE, and do something like this:
DO K = A, B
   PRINT K * K
END DO
Thanks for the correction, I work mostly in java and haven't done fortran in roughly 40 years or so but I think F90 still accepts the old style otherwise they'd break a lot of programs.
Joffan
#8
Feb21-12, 06:38 PM
P: 361
Quote Quote by DoremiCSD View Post
:
[[code]]
:
I used this in silverfrost but cant run it. I belive that i have something wrong .Can anyone correct me wrongs?
Check your variable declarations, and significant parts of this are not in Fortran. Also, you really need to review how to form loops; googling "fortran 90 loops" will take you to informative material, including Wikibooks

As a code snip, you might use:

do 
    print *, "Give me an integer a:"
    read *, a
    print *, "Give me an integer b, not less than a:"   ! try to AVOID mistakes
    read *, b
    if (b >= a) exit          ! ===> input OK
    print *, "Found b less than a, please re-enter your numbers a & b"
end do
I suggest you just get this working for the inputs before you try the calculations. Then read up on iterated loops as per Mark44's post.
DoremiCSD
#9
Feb22-12, 04:24 AM
P: 10
1. to print the squares of integers k such that is
a ≤ k ≤ b.
2. To print the product of all integers r is such that
c ≤ r ≤ d.
How i can to do the above 2 parts i stucked here.
Joffan
#10
Feb22-12, 09:01 AM
P: 361
1. Read Mark44 above
2. Initialize the product calculation p at 1, then make a loop starting at c, ending at d, and multiply the loop counter into p each time.
DoremiCSD
#11
Feb22-12, 12:46 PM
P: 10
1. The problem statement, all variables and given/known data

Write a program in fortran 90 which reads from the user 4 integers a,b,c and d. The program first

needs to make sure that the following inequalities hold : a <=b <=c <=d.

If not,the program needs to ask again those numbers from the user until the user finally gives

numbers that satisfy the above inequalities.

Once your program reads these numbers , then it will have to :

1) type the squares of the integers k , such as a< = k <= b , and

2)type the product of all the integers r, such as c<= r <= d.

2. Relevant equations

Guys i am the guy with the fortran problem and my program doesnt run i dont know why.. Maybe its wrong written because i am new in programming.Plz if anyone cangive me the right code.Down is my try for the program.....



3. The attempt at a solution

program Arithmetical

implicit none

integer :: a, b, c, d, x, y

print *, "Give me an integer a:"
read *, a

print *, "Give me an integer b:"
read *, b

print *, "Give me an integer c:"
read *, c

print *, "Give me an integer d:"
read *, d

if(a .ge. b) then
print *, "Invalid."
stop
endif

if(c .ge. d) then
print *, "Invalid."
stop
endif

do
print *, "Give me an integer a:"
read *, a
print *, "Give me an integer b, not less than a:"
read *, b
if(a .ge. b) exit
print *, "Found b less than a, please re-enter your numbers a & b"
endif
end do

do
print *, "Give me an integer c:"
read *, c
print *, "Give me an integer d, not less than c:"
read *, d
if(c .ge. d) then
print *, "Found d less than c, please re-enter your numbers a & b"
endif
end do

x = a
y = 0
do while (x <= b)
y = x^2
print y
x = x+1
end do

x = c
y = 0
do while (x <= d)
y = y+x*(x+1)
x = x+1
end do

end program
Mark44
#12
Feb22-12, 01:04 PM
Mentor
P: 21,216
Mod note: Merged the contents of a new thread started by the OP into the existing thread.
Quote Quote by DoremiCSD View Post
1. The problem statement, all variables and given/known data

Write a program in fortran 90 which reads from the user 4 integers a,b,c and d. The program first

needs to make sure that the following inequalities hold : a <=b <=c <=d.

If not,the program needs to ask again those numbers from the user until the user finally gives

numbers that satisfy the above inequalities.

Once your program reads these numbers , then it will have to :

1) type the squares of the integers k , such as a< = k <= b , and

2)type the product of all the integers r, such as c<= r <= d.
In 1 and 2 above, "such as" is incorrect, as it suggests an example. The problem statement undoubtedly means "such that."
Do you understand what parts 1 and 2 are asking you to do? If not, you will never be able to write the code to implement these parts.

To assess your understanding, if a = 5 and b = 15, what output would you expect from part 1.

Similarly, if c = 3 and d = 12, what output would you expect from part 2?
Quote Quote by DoremiCSD View Post

2. Relevant equations

Guys i am the guy with the fortran problem and my program doesnt run i dont know why.
Be more specific. When you say it doesn't run, we have no idea what that means. Does your code fail to compile? If so, you need to fix the syntax errors. One that I see is '^' used for raising a number to a power. The operator in Fortran is **, not ^.

Does your code compile but not run? If so, there are errors in your logic, so that the program halts before doing anything useful. In your input section, if the user enters invalid values, the program STOPs. It should not be doing that. Your whole input section needs to be in a DO loop of some kind, where the loop keeps running until all of the input values are in the right order.
Quote Quote by DoremiCSD View Post
Maybe its wrong written because i am new in programming.Plz if anyone cangive me the right code.
We DO NOT do your work for you. We'll help, but we aren't going to just give you the code.
Quote Quote by DoremiCSD View Post
Down is my try for the program.....



3. The attempt at a solution
Use a [code] tag just above your code, and a [/code] tag after the end of your code . This makes your code easier for us to read, by preserving your indentation. Please make an effort to do that on code that you show.
Quote Quote by DoremiCSD View Post
program Arithmetical

implicit none

integer :: a, b, c, d, x, y

print *, "Give me an integer a:"
read *, a

print *, "Give me an integer b:"
read *, b

print *, "Give me an integer c:"
read *, c

print *, "Give me an integer d:"
read *, d

if(a .ge. b) then
 print *, "Invalid."
 stop
endif

if(c .ge. d) then
 print *, "Invalid."
 stop
endif

do 
 print *, "Give me an integer a:"
 read *, a
 print *, "Give me an integer b, not less than a:"
 read *, b
 if(a .ge. b) exit
  print *, "Found b less than a, please re-enter your numbers a & b"
 endif
end do

do
 print *, "Give me an integer c:"
 read *, c
 print *, "Give me an integer d, not less than c:"
 read *, d
 if(c .ge. d) then
  print *, "Found d less than c, please re-enter your numbers a & b"
 endif
end do

x = a
y = 0
do while (x <= b)
 y = x^2
 print y
 x = x+1
end do

x = c
y = 0
do while (x <= d)
 y = y+x*(x+1)
 x = x+1
end do

end program
Joffan
#13
Feb22-12, 02:38 PM
P: 361
It looks like you don't appreciate the difference between an inline if and a block if statement:

Inline ("logical") if:
if (<test>) <single-action>
(note, there is no end if for an inline if)

Block if:
if (<test>) then
    <action>
    <action>
    <action>
    <action>
end if
(note that there are further options in a block if using else and else if)

Selective execution

Also you won't really get closer to a solution by just piling every suggested piece of code - with unnecessary additions - into your program. You really have to understand what the code is doing.

And you'd do well to annotate your code with some comments, too.


Register to reply

Related Discussions
Fortran: End Do vs. Continue Programming & Computer Science 3
Fortran Program Won't run Programming & Computer Science 15
Help with a FORTRAN program Programming & Computer Science 0
Help with fortran program Programming & Computer Science 6
Running a Fortran 77 program in a C++ enviorment/program Programming & Computer Science 2