Introduction
In this lab, you will learn how to solve a right-angled triangle using trigonometric calculations in C programming. The lab covers the essential steps required to determine the missing sides and angles of a right-angled triangle, given a set of known values. You will start by learning how to read and define the known sides and angles of the triangle, then use trigonometric ratios to calculate the unknown values, and finally, print the missing information. This lab will provide you with a solid understanding of applying trigonometric principles in a practical programming context.
Read Known Sides/Angles
In this step, we will learn how to read and define known sides and angles for a right-angled triangle in C programming. Understanding how to input and store triangle information is crucial for performing trigonometric calculations.
First, let's create a new C file to define our triangle structure and input method:
// ~/project/triangle_solver.c
#include <stdio.h>
#include <math.h>
// Define a structure to represent a right-angled triangle
struct RightTriangle {
double side_a; // Adjacent side
double side_b; // Opposite side
double side_c; // Hypotenuse
double angle_A; // Angle opposite to side a
double angle_B; // Angle opposite to side b
double angle_C; // Right angle (90 degrees)
};
int main() {
struct RightTriangle triangle;
// Input known sides or angles
printf("Enter known side/angle values (enter 0 if unknown):\n");
printf("Side a length: ");
scanf("%lf", &triangle.side_a);
printf("Side b length: ");
scanf("%lf", &triangle.side_b);
printf("Side c (hypotenuse) length: ");
scanf("%lf", &triangle.side_c);
printf("Angle A (in degrees): ");
scanf("%lf", &triangle.angle_A);
printf("Angle B (in degrees): ");
scanf("%lf", &triangle.angle_B);
return 0;
}
Let's compile and run the program to test our input method:
gcc ~/project/triangle_solver.c -o ~/project/triangle_solver -lm
~/project/triangle_solver
Example output:
Enter known side/angle values (enter 0 if unknown):
Side a length: 3
Side b length: 4
Side c (hypotenuse) length: 5
Angle A (in degrees): 36.87
Angle B (in degrees): 53.13
Understanding the Code
- We define a
RightTrianglestructure to store all possible triangle measurements. - The structure includes three sides (a, b, c) and three angles (A, B, C).
- We use
scanf()to allow users to input known values. - Users can enter 0 for unknown values, which we'll use in subsequent steps to calculate missing information.
Input Considerations
- Side lengths can be any positive real number
- Angles are in degrees
- For a right-angled triangle, one angle is always 90 degrees
- You need at least two known values to solve the triangle completely
Use Trigonometric Ratios to Find Unknowns
In this step, we will extend our triangle solver program to calculate unknown sides and angles using trigonometric ratios. We'll update the previous code to include functions for solving right-angled triangles.
Let's modify our triangle_solver.c file to add calculation functions:
// ~/project/triangle_solver.c
#include <stdio.h>
#include <math.h>
#define PI 3.14159265358979323846
struct RightTriangle {
double side_a; // Adjacent side
double side_b; // Opposite side
double side_c; // Hypotenuse
double angle_A; // Angle opposite to side a
double angle_B; // Angle opposite to side b
double angle_C; // Right angle (90 degrees)
};
// Function to convert degrees to radians
double to_radians(double degrees) {
return degrees * (PI / 180.0);
}
// Function to convert radians to degrees
double to_degrees(double radians) {
return radians * (180.0 / PI);
}
// Calculate missing sides using trigonometric ratios
void solve_triangle(struct RightTriangle *triangle) {
// If hypotenuse and one side are known, calculate the third side
if (triangle->side_c > 0 && triangle->side_a > 0 && triangle->side_b == 0) {
triangle->side_b = sqrt(triangle->side_c * triangle->side_c - triangle->side_a * triangle->side_a);
}
// If two sides are known, calculate angles
if (triangle->side_a > 0 && triangle->side_c > 0) {
triangle->angle_A = to_degrees(asin(triangle->side_a / triangle->side_c));
triangle->angle_B = 90.0 - triangle->angle_A;
}
// If two sides are known, calculate hypotenuse
if (triangle->side_a > 0 && triangle->side_b > 0 && triangle->side_c == 0) {
triangle->side_c = sqrt(triangle->side_a * triangle->side_a + triangle->side_b * triangle->side_b);
}
}
// Print triangle information
void print_triangle_info(struct RightTriangle *triangle) {
printf("\nTriangle Information:\n");
printf("Side a: %.2f\n", triangle->side_a);
printf("Side b: %.2f\n", triangle->side_b);
printf("Side c (Hypotenuse): %.2f\n", triangle->side_c);
printf("Angle A: %.2f degrees\n", triangle->angle_A);
printf("Angle B: %.2f degrees\n", triangle->angle_B);
printf("Angle C: 90.00 degrees\n");
}
int main() {
struct RightTriangle triangle = {0}; // Initialize all values to 0
// Input known sides or angles
printf("Enter known side/angle values (enter 0 if unknown):\n");
printf("Side a length: ");
scanf("%lf", &triangle.side_a);
printf("Side b length: ");
scanf("%lf", &triangle.side_b);
printf("Side c (hypotenuse) length: ");
scanf("%lf", &triangle.side_c);
printf("Angle A (in degrees): ");
scanf("%lf", &triangle.angle_A);
printf("Angle B (in degrees): ");
scanf("%lf", &triangle.angle_B);
// Solve for unknown values
solve_triangle(&triangle);
// Print results
print_triangle_info(&triangle);
return 0;
}
Now, let's compile and run the updated program:
gcc ~/project/triangle_solver.c -o ~/project/triangle_solver -lm
~/project/triangle_solver
Example input and output:
Enter known side/angle values (enter 0 if unknown):
Side a length: 3
Side b length: 4
Side c (hypotenuse) length: 0
Angle A (in degrees): 0
Angle B (in degrees): 0
Triangle Information:
Side a: 3.00
Side b: 4.00
Side c (Hypotenuse): 5.00
Angle A: 36.87 degrees
Angle B: 53.13 degrees
Angle C: 90.00 degrees
Understanding Trigonometric Calculations
solve_triangle()uses trigonometric ratios to calculate missing values:- Pythagorean theorem for side calculations
asin()for angle calculations
to_radians()andto_degrees()help convert between degrees and radians- The program can solve the triangle with various input combinations
Key Trigonometric Relationships
- Sine: opposite / hypotenuse
- Cosine: adjacent / hypotenuse
- Tangent: opposite / adjacent
- Pythagorean theorem: a² + b² = c²
Print the Missing Values
In this final step, we'll enhance our triangle solver program to provide more detailed output and handle different input scenarios. We'll modify the code to print missing values and add error checking.
Update the triangle_solver.c file with the following improvements:
// ~/project/triangle_solver.c
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#define PI 3.14159265358979323846
#define EPSILON 0.0001 // Small value for floating-point comparisons
struct RightTriangle {
double side_a;
double side_b;
double side_c;
double angle_A;
double angle_B;
double angle_C;
};
// Previous functions (to_radians, to_degrees, solve_triangle) remain the same
// Enhanced print function with missing value detection
void print_triangle_info(struct RightTriangle *triangle) {
printf("\nTriangle Calculation Results:\n");
printf("------------------------------\n");
// Print sides
printf("Sides:\n");
if (triangle->side_a > 0) {
printf(" Side a: %.2f\n", triangle->side_a);
} else {
printf(" Side a: MISSING (Unable to calculate)\n");
}
if (triangle->side_b > 0) {
printf(" Side b: %.2f\n", triangle->side_b);
} else {
printf(" Side b: MISSING (Unable to calculate)\n");
}
if (triangle->side_c > 0) {
printf(" Side c (Hypotenuse): %.2f\n", triangle->side_c);
} else {
printf(" Side c (Hypotenuse): MISSING (Unable to calculate)\n");
}
// Print angles
printf("Angles:\n");
if (triangle->angle_A > 0) {
printf(" Angle A: %.2f degrees\n", triangle->angle_A);
} else {
printf(" Angle A: MISSING (Unable to calculate)\n");
}
if (triangle->angle_B > 0) {
printf(" Angle B: %.2f degrees\n", triangle->angle_B);
} else {
printf(" Angle B: MISSING (Unable to calculate)\n");
}
// Angle C is always 90 degrees in a right-angled triangle
printf(" Angle C: 90.00 degrees (Right Angle)\n");
}
// Input validation function
int validate_input(struct RightTriangle *triangle) {
int known_values = 0;
// Count known values
if (triangle->side_a > 0) known_values++;
if (triangle->side_b > 0) known_values++;
if (triangle->side_c > 0) known_values++;
if (triangle->angle_A > 0) known_values++;
if (triangle->angle_B > 0) known_values++;
// Need at least two known values to solve the triangle
if (known_values < 2) {
printf("Error: Insufficient information to solve the triangle.\n");
printf("You need to provide at least two known values.\n");
return 0;
}
return 1;
}
int main() {
struct RightTriangle triangle = {0};
// Input known sides or angles
printf("Right-Angled Triangle Solver\n");
printf("Enter known values (enter 0 if unknown):\n");
printf("Side a length: ");
scanf("%lf", &triangle.side_a);
printf("Side b length: ");
scanf("%lf", &triangle.side_b);
printf("Side c (hypotenuse) length: ");
scanf("%lf", &triangle.side_c);
printf("Angle A (in degrees): ");
scanf("%lf", &triangle.angle_A);
printf("Angle B (in degrees): ");
scanf("%lf", &triangle.angle_B);
// Validate input
if (!validate_input(&triangle)) {
return 1;
}
// Solve for unknown values
solve_triangle(&triangle);
// Print results
print_triangle_info(&triangle);
return 0;
}
Compile and run the program:
gcc ~/project/triangle_solver.c -o ~/project/triangle_solver -lm
~/project/triangle_solver
Example input and output:
Right-Angled Triangle Solver
Enter known values (enter 0 if unknown):
Side a length: 3
Side b length: 4
Side c (hypotenuse) length: 0
Angle A (in degrees): 0
Angle B (in degrees): 0
Triangle Calculation Results:
------------------------------
Sides:
Side a: 3.00
Side b: 4.00
Side c (Hypotenuse): 5.00
Angles:
Angle A: 36.87 degrees
Angle B: 53.13 degrees
Angle C: 90.00 degrees (Right Angle)
Key Improvements
- Enhanced
print_triangle_info()function to show missing values - Added
validate_input()to check for sufficient triangle information - Improved error handling and user feedback
- Consistent formatting for output
Learning Outcomes
- Understand how to handle missing triangle values
- Learn to validate geometric calculations
- Practice advanced C programming techniques
Summary
In this lab, we learned how to read and define known sides and angles for a right-angled triangle in C programming. We created a RightTriangle structure to store all possible triangle measurements, including three sides (a, b, c) and three angles (A, B, C). We then used scanf() to allow users to input known values, with the option to enter 0 for unknown values. This step is crucial for performing trigonometric calculations to find the missing values in the next part of the lab.
After defining the input method, the next step is to use trigonometric ratios to calculate the unknown sides or angles based on the known information. The final step is to print the missing values, providing the user with a complete solution to the right-angled triangle problem.



