Need to do a multitask program in Fortran 95

In summary, the user inputs the information needed to solve a triangle: two sides, an angle, and the hypotenuse. The program then prompts for which side of the triangle to solve.
  • #1
Ottodomus
19
7
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:
Technology news on Phys.org
  • #2
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.
 
  • Like
Likes berkeman and Ottodomus
  • #3
Ottodomus said:
is it possible?
anorlunda said:
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:
 
  • Like
Likes Ottodomus and Klystron
  • #4
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.
 
  • #5
anorlunda said:
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?
berkeman said:
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
rcgldr said:
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
 
  • #6
Ottodomus said:
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.
 
  • #7
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
 
  • #8
Here's an sketch of my flowchart
 

Attachments

  • IMG_4821.JPG
    IMG_4821.JPG
    23.9 KB · Views: 357
Last edited:
  • #9
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.
 
  • Like
Likes Ottodomus
  • #10
Ottodomus said:
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 website 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.
 
  • #11
Mark44 said:
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 website 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
 
  • #12
Mark44 said:
I would advise you to either get a book on Fortran programming or find a website 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.

Ottodomus said:
That's exactly what I am doing,
That was not at all evident in either the code you posted or in your flow chart.
Ottodomus said:
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
 
  • Like
Likes Ottodomus
  • #13
Mark44 said:
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...
 
  • #14
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).

Ottodomus said:
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.
 
  • Like
Likes Ottodomus
  • #15
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 length 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 length 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 length of the adjacent side is ', a
    print *, ' The length of the oposite side is ', b
    end if

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

Thanks!

Otto
 
  • #17
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]
 

1. What is a multitask program in Fortran 95?

A multitask program in Fortran 95 refers to a program that is able to perform multiple tasks simultaneously or in parallel. This is achieved through the use of threading or parallel processing, allowing for more efficient use of computer resources and faster execution of tasks.

2. How do I create a multitask program in Fortran 95?

To create a multitask program in Fortran 95, you will need to use the threading features provided by the language. This can be done through the use of the "THREAD" keyword when declaring a subroutine or function, and using the "THREADPRIVATE" keyword to declare variables that will be private to each thread.

3. What are the advantages of using a multitask program in Fortran 95?

One of the main advantages of using a multitask program in Fortran 95 is the ability to improve performance and efficiency by distributing tasks among multiple threads. This can result in faster execution times for complex programs and allow for the use of larger data sets.

4. Are there any limitations to using a multitask program in Fortran 95?

One limitation of using a multitask program in Fortran 95 is that it requires careful programming and management of threads to avoid potential issues such as race conditions or deadlocks. Additionally, not all algorithms or programs may benefit from parallel processing, so it is important to determine if it is necessary for your specific task.

5. Are there any resources available to help with creating a multitask program in Fortran 95?

Yes, there are a variety of resources available, including tutorials, forums, and documentation, to help with creating a multitask program in Fortran 95. It is also recommended to consult with experienced Fortran programmers or attend workshops or courses on parallel programming to gain a better understanding of the concepts and best practices.

Similar threads

  • Programming and Computer Science
Replies
16
Views
1K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
Replies
2
Views
6K
  • Programming and Computer Science
3
Replies
75
Views
15K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
4
Views
3K
  • STEM Academic Advising
Replies
12
Views
1K
Back
Top