Fortran: Find a triangle's side, plus angles

Click For Summary

Discussion Overview

The discussion revolves around writing a Fortran program to calculate the length of the third side and the angles of a triangle given two sides and an angle. The scope includes programming challenges, mathematical reasoning, and conceptual understanding of trigonometric laws.

Discussion Character

  • Technical explanation
  • Homework-related
  • Debate/contested

Main Points Raised

  • One participant presents a program that uses the laws of sine and cosine to calculate the third side and angles of a triangle, but struggles with finding the angles.
  • Another participant points out that the variable for angle A is used before it is defined, suggesting a change in the order of calculations.
  • There is a discussion about simplifying variable names for clarity.
  • Some participants express sympathy for the original poster's situation of learning two programming languages simultaneously without prior experience.
  • One participant successfully implements a suggested change and reports that it worked well.
  • Concerns are raised about the overwhelming workload of multiple subjects alongside programming, with no clear solutions offered.

Areas of Agreement / Disagreement

Participants generally agree on the challenges of the programming assignment and the difficulties of managing multiple subjects. However, there is no consensus on the best approach to streamline the program or handle the workload effectively.

Contextual Notes

There are unresolved issues regarding the proper sequence of calculations in the program and the handling of undefined variables. The discussion also reflects the participants' varying levels of programming experience and understanding of the subject matter.

Who May Find This Useful

Students learning programming in Fortran, particularly those dealing with mathematical applications in physics or engineering, may find this discussion relevant.

Const@ntine
Messages
285
Reaction score
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
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.
 
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!
 
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.
 
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.
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 12 ·
Replies
12
Views
4K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 9 ·
Replies
9
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K