Fortran: Find a triangle's side, plus angles

In summary, the program calculates the length of c, the angle between the two sides of the triangle, and the angle in degree of the triangle.
  • #1
Const@ntine
285
18
The statement:

In every ABC triangle, the laws of sine (a/sinA = b/sinB = c/sinC) & cosine (c2 = a2 + b2 -2*a*b*cosC) are valid, where a, b & c are the sides opposite to A, B & C respectively.

Write a program that calculates and prints on the computer's screen, the length of c, and also the unit of the angles A & B of an ABC triangle. The lengths of a & b, plus the unit of C (in radians) shall be given by the keyboard.

The attempt at a solution

Here's what I have so far:

PROGRAM

REAL :: a, b, c,angle_in_degree_A, angle_in_degree_B, angle_in_degree_C, angle_in_radian_A, angle_in_radian_B, angle_in_radian_C

REAL :: pi = 3.14159265

PRINT*, 'ENTER THE FIRST SIDE OF THE TRIANGLE'
READ*, a
PRINT*, 'FIRST SIDE (a) = ', a

PRINT*, 'ENTER THE SECOND SIDE OF THE TRIANGLE'
READ*, b
PRINT*, 'SECOND SIDE (b) = ', b

PRINT*, 'ENTER THE ANGLE BETWEEN THE TWO SIDES'
READ*, angle_in_degree_C
PRINT*, 'ANGLE IN DEGREE C = ', angle_in_degree_C
angle_in_radian_C = (angle_in_degree_C*pi)/180.0
PRINT*, 'ANGLE IN RADIAN C = ', angle_in_radian_C

c = SQRT (a**2 + b**2 - 2.*a*b*COS(angle_in_radian_C))
PRINT*, 'THIRD SIDE OF TRIANGLE =', c

angle_in_degree_A = (angle_in_radian_A)*180.0/pi
angle_in_radian_A = ASIN(SIN(angle_in_radian_C)*a/c)
PRINT*, 'ANGLE IN DEGREE A = ',angle_in_degree_A

END

We were given this assignment on Wednesday for our programming class, and it's due tomorrow. Problem is, the system here is a bit, well, weird, and I know little to nothing about Fortran. I was studying C++ (yes, two languages in one semester that starts in Novemeber and ends in January, amongst the other 6 subjects with a 600pg+ syllabus-and everything is mandatory), so I could really use the help.

I managed to take a look at the book and with the internet's help, I came up with a way to find the third side of the triangle. My problem now is that I can't find the angles. I checked and saw that the arcsin command is ASIN, but when I put it in my editor, the words become red, and while it compiles, I don't get the desired result.

I could really use some help here. But I warn you, I don't know anything beyond the basic commands, so simplify it as much as you can. I wasn't even planning on taking the subject, but here, things work differently. You get into a department of the University, and you attend mandatory classes that have been assigned to you by the department itself, you can't choose. So, as someone who never "played around" with programming before, it's a bit of hurdle for me to learn Fortran & C++ in ~4 months, amongst the other 6 subjects that go with the Physics Department acceptance.

Any kind of help is appreciated.
 
Physics news on Phys.org
  • #2
Darthkostis said:
The statement:

In every ABC triangle, the laws of sine (a/sinA = b/sinB = c/sinC) & cosine (c2 = a2 + b2 -2*a*b*cosC) are valid, where a, b & c are the sides opposite to A, B & C respectively.

Write a program that calculates and prints on the computer's screen, the length of c, and also the unit of the angles A & B of an ABC triangle. The lengths of a & b, plus the unit of C (in radians) shall be given by the keyboard.

The attempt at a solution

Here's what I have so far:
Fortran:
PROGRAM

REAL :: a, b, c,angle_in_degree_A, angle_in_degree_B, angle_in_degree_C, angle_in_radian_A, angle_in_radian_B, angle_in_radian_C

REAL :: pi = 3.14159265

PRINT*, 'ENTER THE FIRST SIDE OF THE TRIANGLE'
READ*, a
PRINT*, 'FIRST SIDE (a) = ', a

PRINT*, 'ENTER THE SECOND SIDE OF THE TRIANGLE'
READ*, b
PRINT*, 'SECOND SIDE (b) = ', b

PRINT*, 'ENTER THE ANGLE BETWEEN THE TWO SIDES'
READ*, angle_in_degree_C
PRINT*, 'ANGLE IN DEGREE C = ', angle_in_degree_C
angle_in_radian_C = (angle_in_degree_C*pi)/180.0
PRINT*, 'ANGLE IN RADIAN C = ', angle_in_radian_C

c = SQRT (a**2 + b**2 - 2.*a*b*COS(angle_in_radian_C))
PRINT*, 'THIRD SIDE OF TRIANGLE =', c

angle_in_degree_A = (angle_in_radian_A)*180.0/pi
angle_in_radian_A = ASIN(SIN(angle_in_radian_C)*a/c)
PRINT*, 'ANGLE IN DEGREE A = ',angle_in_degree_A

END
Your program so far is pretty straightforward: prompt for input values for sides a and b and angle C. It then calculates side c, using the Law of Cosines. So far, so good.

At the end, you have a problem, because angle_in_radian_A is undefined, but you use it to assign a value to angle_in_degree_A. You need to switch those two lines. Also, your variable names are more verbose than necessary (IMO). I would use C_deg and C_rad, for the measure of angle C in degrees and radians, respectively. In the very last line you're using the Law of Sines to calculate angle A.
Darthkostis said:
We were given this assignment on Wednesday for our programming class, and it's due tomorrow. Problem is, the system here is a bit, well, weird, and I know little to nothing about Fortran. I was studying C++ (yes, two languages in one semester that starts in Novemeber and ends in January, amongst the other 6 subjects with a 600pg+ syllabus-and everything is mandatory), so I could really use the help.

I managed to take a look at the book and with the internet's help, I came up with a way to find the third side of the triangle. My problem now is that I can't find the angles. I checked and saw that the arcsin command is ASIN, but when I put it in my editor, the words become red, and while it compiles, I don't get the desired result.

I could really use some help here. But I warn you, I don't know anything beyond the basic commands, so simplify it as much as you can. I wasn't even planning on taking the subject, but here, things work differently. You get into a department of the University, and you attend mandatory classes that have been assigned to you by the department itself, you can't choose. So, as someone who never "played around" with programming before, it's a bit of hurdle for me to learn Fortran & C++ in ~4 months, amongst the other 6 subjects that go with the Physics Department acceptance.

Any kind of help is appreciated.
 
  • #3
D'oh, of course! Thanks a ton, I tried it and it worked great! But yeah, it'd also be a nice idea to streamline it a bit as well.

Thanks for the help!
 
  • #4
Also, I sympathize your plight in having to take two programming languages at the same time! That has to be tough, especially with no prior background in programming.
 
  • #5
Mark44 said:
Also, I sympathize your plight in having to take two programming languages at the same time! That has to be tough, especially with no prior background in programming.

Yeah, it sure is. I mean, there's only one month left and the only thing I've managed to do is go through half the syllabus of Physics, with contains 14 Chapters. Calculus, Chemistry and Analytical Geometry/Linear Algebra I haven't even touched! How's someone supposed to get through all these in ~4 months, with the books arriving in Novemeber (the semester officially starts in october and the finals start in January)? Especially when each one has a 600pg syllabus (plus programming). I was interested in programming, but I was going to start it by my own. Hell, essentially that's what I'm doing anyway, it's not like they're competent here. If it wasn't for this place, it'd have been a lost cause.
 

1. What is Fortran?

Fortran is a programming language commonly used in scientific and engineering applications. It was developed in the 1950s and is known for its efficient handling of mathematical calculations.

2. How can Fortran be used to find a triangle's side and angles?

Fortran has built-in functions and mathematical operators that can be used to calculate a triangle's side and angles. By using the Pythagorean theorem and trigonometric functions, the length of a side and the measure of an angle can be determined.

3. Can Fortran find the side and angles of any triangle?

Yes, as long as the necessary information is provided (e.g. length of two sides or measure of two angles), Fortran can be used to find the missing side and angles of any triangle. However, for a non-right triangle, additional information such as the Law of Sines or Cosines may be needed.

4. Are there any limitations to using Fortran for triangle calculations?

Fortran is a powerful and versatile programming language, but it is not specifically designed for geometry or trigonometry. Therefore, it may require more complex coding and additional input from the user to accurately calculate a triangle's side and angles.

5. Is Fortran the only programming language that can find a triangle's side and angles?

No, there are many other programming languages that can perform similar calculations. However, Fortran is often preferred in scientific and engineering fields due to its efficient handling of mathematical operations.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • General Math
Replies
1
Views
730
  • Precalculus Mathematics Homework Help
Replies
9
Views
2K
  • Math POTW for Secondary and High School Students
Replies
1
Views
944
  • Engineering and Comp Sci Homework Help
Replies
13
Views
2K
Replies
2
Views
1K
  • General Math
Replies
1
Views
736
Replies
2
Views
827
  • Precalculus Mathematics Homework Help
Replies
19
Views
2K
  • Precalculus Mathematics Homework Help
Replies
5
Views
2K
Back
Top