Fortran Need to do a multitask program in Fortran 95

Hi everyone, I am new programming in Fortran and Inhave done about three or four programs that solves different problems in trigonometry, one
is the basic one it gives you hypotenuse upon two sides, the other finds the theta angle by giving hypotenuse and opposite side, other find hypotenuse by giving theta and adjascent side, and so on.
I want to create a program that the user could find all the answers depending the info he has, all in one program, that the program could ask what is the info available and based on that the program could solve those, is it possible?

Thanks in advance
 
Last edited by a moderator:

anorlunda

Mentor
Insights Author
Gold Member
6,847
3,770
I moved this thread from homework to programming.

Of course it's possible. The devil is in the details.

We are not allowed to do your work for you. Show us your attempt at a solution, and we may be able to spot errors or offer suggestions.

I'll start you with a hint. Write down the requirements of what this program should do. That's the first step.
 

berkeman

Mentor
55,114
5,338
is it possible?
I'll start you with a hint. Write down the requirements of what this program should do. That's the first step.
To add to the hint by @anorlunda -- It can be helpful to get in the habit of starting off designing such programs either with a block diagram sketch, or with some "pseudocode". Which one you choose depends on the type of problem you are wanting to code up. I often use some pseudocode to help me organize my thoughts and be sure that I'm including all of the tasks I want to get done in the code. Have a look at the Wikipedia summary of pseudocode to see if you start to see ways that you can think about the code you are about to write:

https://en.wikipedia.org/wiki/Pseudocode

:smile:
 

rcgldr

Homework Helper
8,580
473
There is insufficient information. Knowing two sides or one side and one angle is not enough to determine a triangle. Perhaps, this is a right triangle, in which case the input is two sides and an angle (90 degrees) (SAS = side angle side), or two angles (90 degrees and user input) and the side between the two angles (ASA = angle side angle). The program should prompt the use for which case is to be solved.
 
I moved this thread from homework to programming.

Of course it's possible. The devil is in the details.

We are not allowed to do your work for you. Show us your attempt at a solution, and we may be able to spot errors or offer suggestions.

I'll start you with a hint. Write down the requirements of what this program should do. That's the first step.
Good

As I've said before I have some programs done, I want to place them in one single program:

I am reading that the Nested could be an option, by using the IF instruction.

So first that I need is that user could give to program what's the info he have, he knows opposite side? Hypotenuse? Theta angle?
To add to the hint by @anorlunda -- It can be helpful to get in the habit of starting off designing such programs either with a block diagram sketch, or with some "pseudocode". Which one you choose depends on the type of problem you are wanting to code up. I often use some pseudocode to help me organize my thoughts and be sure that I'm including all of the tasks I want to get done in the code. Have a look at the Wikipedia summary of pseudocode to see if you start to see ways that you can think about the code you are about to write:

https://en.wikipedia.org/wiki/Pseudocode

:smile:
Hi, oh well yes I have done already the flowchart but I have no idea how to start to apply the actions in Fortran
There is insufficient information. Knowing two sides or one side and one angle is not enough to determine a triangle. Perhaps, this is a right triangle, in which case the input is two sides and an angle (90 degrees) (SAS = side angle side), or two angles (90 degrees and user input) and the side between the two angles (ASA = angle side angle). The program should prompt the use for which case is to be solved.
Well yes in fact is about a right triangle
 

berkeman

Mentor
55,114
5,338
Hi, oh well yes I have done already the flowchart but I have no idea how to start to apply the actions in Fortran
Can you use the Upload button to attach a JPEG or PDF copy of your flowchart? Try to ensure it doesn't post sideways or really dark. Thanks.
 
I have started with this sample:
Fortran:
real :: a. !adjascent
real :: b ! opposite
real :: c ! hypotenuse
real :: B ! Theta

print *, enter the input to valuate: ( a,b ), (c,B), (B,b)

if (a,b) then
"First program to be running"
else if (c,B) then
"Second program"
if (B,b) then
"Third program"

end if
What I don't know is how to tell the program to print, since all the if input will bring a different result
 
Here's an sketch of my flowchart
 

Attachments

Last edited:

berkeman

Mentor
55,114
5,338
In the lower right of the Edit/Reply window, there is an UPLOAD button. Click that and browse to the JPEG or PDF file on your computer.
 
32,523
4,243
I have started with this sample:
Fortran:
real :: a. !adjascent
real :: b ! opposite
real :: c ! hypotenuse
real :: B ! Theta

print *, enter the input to valuate: ( a,b ), (c,B), (B,b)

if (a,b) then
"First program to be running"
else if (c,B) then
"Second program"
if (B,b) then
"Third program"

end if
What I don't know is how to tell the program to print, since all the if input will bring a different result
In order of severity, here are some comments:
You're making the same mistake you did in your other program, the one doing calculations with complex numbers. You print a prompt to the user, then you have an if block that uses uninitialized variables. Before you use any variable, it needs to get a value, either by input (using a read statement) or assignment.

Next, your if conditions make no sense. For example "if (a, b)" is not valid Fortran, as far as I remember. It's valid C/C++, but it does not do what you seem to be wanting the code to do.

Finally, your prompt (the print statement) would be just about impossible for anyone but you to decipher. Is a person supposed to enter 6 numbers? To make it simple for a person running the program, a prompt should ask the user to enter only one value. If your program needs ttwo values, use a separate prompt for each one.

I would advise you to either get a book on Fortran programming or find a web site that presents the basic ideas of programming in this language. Either way, take a look at actual programs that compile and run, and produce reasonable results.
 
In order of severity, here are some comments:
You're making the same mistake you did in your other program, the one doing calculations with complex numbers. You print a prompt to the user, then you have an if block that uses uninitialized variables. Before you use any variable, it needs to get a value, either by input (using a read statement) or assignment.

Next, your if conditions make no sense. For example "if (a, b)" is not valid Fortran, as far as I remember. It's valid C/C++, but it does not do what you seem to be wanting the code to do.

Finally, your prompt (the print statement) would be just about impossible for anyone but you to decipher. Is a person supposed to enter 6 numbers? To make it simple for a person running the program, a prompt should ask the user to enter only one value. If your program needs ttwo values, use a separate prompt for each one.

I would advise you to either get a book on Fortran programming or find a web site that presents the basic ideas of programming in this language. Either way, take a look at actual programs that compile and run, and produce reasonable results.
That's exactly what I am doing, in fact I have about 6 or 7 sucessful programs made in Fortran 95, here a copy of one of them:

Code:
! New Quadratic formula calculator
program new_quad_solve
    implicit none
    real :: a, b, c
    real :: disc, root1, root2

    print *, ' Enter values for a, b and c by commas or spaces '
    read *, a, b, c
   
    disc = b**2 - 4*a*c    ! discriminant

    if ( disc < 0 ) then
      print *, ' Roots are imaginary '
    else
      root1 = (b*sqrt(disc))/(2*a)
      root2 = (-b*sqrt(disc))/(2*a)
      print *, ' Roots are ', root1, ' and ', root2
    end if

end program new_quad_solve
 
32,523
4,243
I would advise you to either get a book on Fortran programming or find a web site that presents the basic ideas of programming in this language. Either way, take a look at actual programs that compile and run, and produce reasonable results.
That's exactly what I am doing,
That was not at all evident in either the code you posted or in your flow chart.
Fortran:
print *, enter the input to valuate: ( a,b ), (c,B), (B,b)

if (a,b) then
"First program to be running"
Your quadratic equation solver if much better. Notice that the action is controlled by if (disc < 0), where the value of the inequality determines which branch gets executed. Your example in this thread, "if (a, b)", doesn't do this.

If you want the program to do one operation among a number of choices, display a menu that the user can choose from, something like this:
Fortran:
print *, '1 - Solve a quadratic equation'
print *, '2 - Calculate the hypotenuse of a right triangle, given the two legs'
print *, '3 - Calculate the adjacent side of a right triangle, given the hypotenuse and the other leg'
print *, 'Enter your choice'
read *, choice

if (choice == 1) then
!    Prompt the user for the coefficents a, b, and c
!   Solve for the variable
!   Print the results
else if (choice == 2) then
!   Prompt the user for the known legs of the triangle
!   Calculate the hypotenuse
!   Print the results
else if (choice == 3) then
!   Similar to above
end if
 
That was not at all evident in either the code you posted or in your flow chart.

Your quadratic equation solver if much better. Notice that the action is controlled by if (disc < 0), where the value of the inequality determines which branch gets executed. Your example in this thread, "if (a, b)", doesn't do this.

If you want the program to do one operation among a number of choices, display a menu that the user can choose from, something like this:
Fortran:
print *, '1 - Solve a quadratic equation'
print *, '2 - Calculate the hypotenuse of a right triangle, given the two legs'
print *, '3 - Calculate the adjacent side of a right triangle, given the hypotenuse and the other leg'
print *, 'Enter your choice'
read *, choice

if (choice == 1) then
!    Prompt the user for the coefficents a, b, and c
!   Solve for the variable
!   Print the results
else if (choice == 2) then
!   Prompt the user for the known legs of the triangle
!   Calculate the hypotenuse
!   Print the results
else if (choice == 3) then
!   Similar to above
end if
Very nice! Will try that....
 
32,523
4,243
I should add that your flow chart isn't helpful at all. Much of it is too small to read, and what I can read doesn't help with actually writing the code. Also, flowcharts are pretty much passe. Very few computer science textbooks use flowcharts any longer -- most of them present the algorithms in pseudocode, which is only loosely defined. Here's a link to some examples -- http://www.unf.edu/~broggio/cop2221/2221pseu.htm.

In the middle you have three diamond shapes. The first has a,b inside it, the second has c,b inside it, and the third has B, b inside it. The diamond shape in flowcharts represents a decision with a true or false answer. "a, b" doesn't lend itself to either true or false.

Going down from the left-most diamond, there is a box that contains ##a^2 + b^2 = \sqrt c##. For one thing, the box shapes are supposed to be used for executable statements, such as print statements, input statements, assignment statements, etc. You apparently intend to do an assignment statement, but your equation is incorrect and it is not an assignment statement, which always has a variable on the left side and some expression on the right side. In your case the assignment statement should be ##c = \sqrt{a^2 + b^2}##, or using Fortran notation, c = sqrt(a**2 + b**2).

Hi, oh well yes I have done already the flowchart but I have no idea how to start to apply the actions in Fortran
The reason for using flowcharts, or better yet, pseudocode, is to get a good headstart on what the code should actually look like.
 
Hello Mark, I have done the code, and its working, but I am getting the following error:
"warning:comparing floating point quantities for equality may give misleading"

would you please like to help me fixing what I am doing wrong?

The Code:

Fortran:
am multitask_trig  ! This program solves three questions on trigonometry

    implicit none
    real :: side_1
    real :: side_2
    real :: h
    real, parameter :: rad_2_deg = 57.29577951308232 ! Fortran does need to convert radians to degrees
    real :: a
    real :: b
    real :: c
    real :: angle
    real,parameter :: deg_2_rad = 0.017453292519943 ! Fortran needs to convert degrees to radians first
    real :: theta
    real :: Opt_1, Opt_2, Opt_3
    real :: choice
    ! Prompt the user for any of the choices
 
    print *, ' Opt 1 Want to have Hypotenuse value and do you know two legs? '
    print *, ' Opt 2 Want to know Theta angle value and do you know opposite side and hypotenuse? '
    print *, ' Opt 3 Want to know Two Legs values and do you know hypotenuse and theta angle? '
    print *, ' Enter your choice: Opt_1, Opt_2, Opt_3 '
    Opt_1 = 1
    Opt_2 = 2
    Opt_3 = 3
    read *, choice
 
    if ( choice == Opt_1 ) then
    print *, ' Enter the value of side_1 '
    read *, side_1
    print *, ' Enter the value of side_2 '
    read *, side_2

    h = sqrt ( side_1 ** 2 + side_2 ** 2 )

    print *, ' The lenght of hypotenuse is: ', h

      else if ( choice == Opt_2 ) then
    print *, ' Enter the value of oposite side '
    read *, a
    print *, ' Enter the value of hypotenuse '
    read *, c

    angle = ASIN ( a / c ) * rad_2_deg ! ASIN is the equivalent of inverse of SIN

    print *, ' Angle is: ', angle

    else if ( choice == Opt_3 ) then
    print *, ' Enter the lenght of the hypotenuse C: '
    read *, c
    print *, ' Enter the angle THETA in degrees '
    read *, theta

    a = c * COS ( theta * deg_2_rad )
    b = c * SIN ( theta * deg_2_rad )

    print *, ' The lenght of the adjacent side is ', a
    print *, ' The lenght of the oposite side is ', b
    end if

end program multitask_trig
 
Last edited by a moderator:
Hi, can someone help me with the above question?

Thanks!

Otto
 
32,523
4,243
Get rid of Opt_1, Opt_2, and Opt_3, and make choice an integer, not a real.
Fortran:
integer:: choice
! Prompt the user for any of the choices
 
print *, ' 1 Want to find Hypotenuse value and do you know two legs? '
print *, ' 2 Want to find Theta angle value and do you know opposite side and hypotenuse? '
print *, ' 3 Want to findTwo Legs values and do you know hypotenuse and theta angle? '
print *, ' Enter your choice: 1, 2, or 3 '
   
read *, choice
 
if ( choice == 1 ) then
    !  Code for calculating hypotenuse
else if (choice == 2) then
    ! Code for finding theta
else
    ! Code for finding the two legs
The reason for your warning is that comparason of floating point values is not exact, but comparison of integer values is exact.

Also, when you use code tags, there is no separate fortran tag. The opening code tag for fortran should look like this:
[code=fortran]

not like this
[code][fortran]

At the end of your code, the closing tag is [/code]
 

Want to reply to this thread?

"Need to do a multitask program in Fortran 95" You must log in or register to reply here.

Physics Forums Values

We Value Quality
• Topics based on mainstream science
• Proper English grammar and spelling
We Value Civility
• Positive and compassionate attitudes
• Patience while debating
We Value Productivity
• Disciplined to remain on-topic
• Recognition of own weaknesses
• Solo and co-op problem solving
Top