Reputation:
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");
}
}
Upvotes: 0
Views: 322
Reputation: 111
As other answers have said, pass the angles as parameters. To add to that, I see a few problems:
if (ang1==ang2 || ang2==ang3 || ang1==ang3){ //checks for isoceles triangle
printf("\nThis is an isoceles triangle.");
}
This should be an else if. If it's plain old if
, equilateral triangles will be declared equilateral and isosceles.
if (ang1!=ang2 || ang2!=ang3 || ang3!=ang1){ // chacks for scalene triangle
printf("\nThis is a scalene triangle.");
}
In this snippet, each OR || should become AND &&. It's not just one pair that must be inequal, it's all of them. The other option is just to use (ang1!=ang2!=ang3)
.
Everything else looks good to me. Good luck!
Upvotes: 1
Reputation: 3067
You could pass the 3 values as parameters to the 2nd function. Define it like so:
void task2 (double ang1, double ang2, double ang3){
...
...
}
and call it on the first one like so:
task2(ang1, ang2, ang3);
Then you wouldn't have to ask for them twice, as you're doing now.
Also, it is unadvisable to use global variables unless you really need them (Are global variables bad?)
Upvotes: 1
Reputation: 148890
Just past the variables as parameters to task2
function.
void task2(double ang1, double ang2, double ang3);
int main (){
...
task2(ang1, ang2, ang3);
}
void task2(double ang1, double ang2, double ang3) {
...
}
Upvotes: 3
Reputation: 8982
Pass your angles to the called function as parameters:
void task2(double ang1, double ang2, double ang3) {
// check what's necessary here
}
int main() {
double ang1, ang2, ang3;
// read user input
// check whether angles make a triangle
task2(ang1, ang2, ang3);
}
Upvotes: 5