Implement a function that is given 2 positive integers representing 2 angles of a triangle. If the triangle is right, isosceles, or both, the function should return 1, 2 or 3, respectively. Otherwise it should return 0.
im actually just started learning C language and cant move on cause i cant find an answer to this problem. would like a reference to see where are my mistakes.
dont need to print it, only the function. without using loops only if statements.
int TriangleType (unsigned num1, unsigned num2){
int result;
if(num1 == 90 || num2 == 90 || num1 num2 == 90){
result = 1;
} else if(num1 == num2 || 180 - (num1 num2) == num1 || 180 - (num1 num2) == num2) {
result = 2;
} else if((num1 == 90 || num2 == 90 || num1 num2 == 90) && (num1 == num2 || 180 - (num1 num2) == num1 || 180 - (num1 num2) == num2)){
result = 3;
} else if (num1 num2 >= 180){
result = -1;
} else {
result = 0;
}
return result;
}
thats the start so u can see what is the direction... maybe got mistakes even here at the start =)
CodePudding user response:
Correct logic is:
int TriangleType(unsigned num1, unsigned num2)
{
int result;
if (num1 == 90 || num2 == 90 || num1 num2 == 90) {
if (num1 == num2 || 180 - (num1 num2) == num1 || 180 - (num1 num2) == num2)
result = 3;
else
result = 1;
} else if (num1 == num2 || 180 - (num1 num2) == num1 || 180 - (num1 num2) == num2) {
result = 2;
} else if (num1 num2 >= 180) {
result = -1;
} else {
result = 0;
}
return result;
}
In your program there is a mistake in logic in the first if statement. You are asking whether the triangle is right and if so you evaluate is as one. But in reality it can be right and isosceles at the same time, but it's checked just in third if statement to which you won't get since you return one. So you have to check the condition in that first statement.
if (num1 == 90 || num2 == 90 || num1 num2 == 90) {
if (num1 == num2 || 180 - (num1 num2) == num1 || 180 - (num1 num2) == num2)
result = 3;
else
result = 1;
}
Then it should work correctly.
Good practice for the future is use function for any for loop, condition, etc. so you do not need to copy you code. It is much more efficient to write just one function, more readable for the other programmers and if there is a mistake, you will need to fix it at just one place and not multiple ones.
I would write it as:
int is_isosceles(unsigned num1, unsigned num2)
{
if (num1 == num2 || 180 - (num1 num2) == num1 || 180 - (num1 num2) == num2)
return 1;
else
return 0;
}
int TriangleType(unsigned num1, unsigned num2)
{
int result;
if (num1 == 90 || num2 == 90 || num1 num2 == 90) {
if (is_isosceles(num1, num2))
result = 3;
else
result = 1;
} else if (is_isosceles(num1, num2)) {
result = 2;
} else if (num1 num2 >= 180) {
result = -1;
} else {
result = 0;
}
return result;
}
Condition functions can be of type bool, but include library:
#include <stdbool.h>
CodePudding user response:
Let me try and sketch a solution without giving you the complete code...
int TriangleType (unsigned num1, unsigned num2){
int right = 0;
int isosceles = 0;
int result = 0;
if( /* conditions for right angle */ ){
right = 1;
}
if( /* conditions for isosceles */ ) {
isosceles = 1;
}
if ( right == 1 && isosceles == 1 ) {
result = 3;
} else if ( right == 1) {
result = 1;
} else if ( isosceles == 1 ) {
/* ... */
} /*...*/
return result;
}
It could be made shorter but since you are just starting out this is probably best.
CodePudding user response:
int TriangleType (unsigned num1, unsigned num2)
{
unsigned num3 = 180 - (num1 num2);
unsigned int result = 0;
if (num1 == 90 || num2 == 90 || num3 == 90)
result = 1;
if ((num1==num2) || (num2 == num3) || (num1 == num3))
result = 2;
return result;
}