I am a total beginner in c . I was given an assignment to check if a triangle can be formed with the input of 3 angles by the user, and the type of triangle (e.g. oblique, scalene etc.) has to be printed to the screen as well. I am required to do this in 2 parts and attempted to do the first part in the main while declaring the second part as a function.
However, it seems I could not use the same variables used in the main script in the function without declaring it again. Is there some way to achieve this?
I googled it and learnt of global variables, however, is it really applicable in this case as the value of the variables are user input and hence cannot be performed beyond the main script and other functions?
This is my code:
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main(){
double ang1,ang2,ang3;
printf("This program determines if a triangle can be formed with an input of 3 angles.\n");
printf("Enter the first angle: ");
scanf("%lf",&ang1);
printf("\nEnter the second angle: ");
scanf("%lf",&ang2);
printf("\nEnter the third angle: ");
scanf("%lf",&ang3);
if (ang1<=0.000000 || ang2<=0.000000 || ang3<=0.000000){//handles any angles below or equal to 0 degrees
printf("\nError 404: Angle(s) are not above 0 degrees.\nPlease restart the program again\n");
}
else{
if (ang1+ang2+ang3==180.000000){ //checks if angles add up to 180 degrees
printf("\nA triangle can be formed by this three angles.\n");
}
else{
printf("A triangle cannot be formed by this three angles.\n");
}
}
task2();
}
task2(){ //In a new file
double ang1,ang2,ang3;
printf("This program determines if a triangle can be formed with an input of 3 angles.\n");
printf("Enter the first angle: ");
scanf("%lf",&ang1);
printf("\nEnter the second angle: ");
scanf("%lf",&ang2);
printf("\nEnter the third angle: ");
scanf("%lf",&ang3);
if (ang1<=0.000000 || ang2<=0.000000 || ang3<=0.000000){//handles any angles below or equal to 0 degrees
printf("\nError 404: Angle(s) are not above 0 degrees.\nPlease restart the program again\n");
}
else if (ang1+ang2+ang3==180.000000){ //checks if angles add up to 180 degrees
printf("\nA triangle can be formed by this three angles.");
if (ang1==ang2==ang3){ //checks for equilateral triangle
printf("\nThis is an equilateral triangle.");
}
if (ang1==ang2 || ang2==ang3 || ang1==ang3){ //checks for isoceles triangle
printf("\nThis is an isoceles triangle.");
}
if (ang1!=ang2 || ang2!=ang3 || ang3!=ang1){ // chacks for scalene triangle
printf("\nThis is a scalene triangle.");
}
if (ang1==90.000000 || ang2==90.000000 || ang3==90.000000){ //checks for right angles
printf("\nThis is a right angle.");
}
else{
printf("\nThis is an oblique triangle."); //when no angles are 90 degrees
}
if (ang1<90.000000 && ang2<90.000000&& ang3<90.000000){ //checks if all angles are acute
printf("\nThis is an acute triangle.");
}
if (ang1>90.000000 || ang2>90.000000 || ang3>90.000000){ //checks if one angle is obtuse
printf("\nThis is an obtuse triangle.");
}
}
else {
printf("A triangle cannot be formed by this three angles.\n");
}
}