Reputation: 1
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 =)
Upvotes: 0
Views: 304
Reputation: 600
int TriangleType (unsigned num1, unsigned num2)
{
//Calculate the third angle and keep in num3
unsigned num3 = 180 - (num1 + num2);
//We declare a variable "result" and initialize it with 0
//This way if none of our if conditions match, we would simply return 0 by "return result;".
unsigned int result = 0;
if (num1 == 90 || num2 == 90 || num3 == 90) //if any of our angles are 90-degree
result += 1; //Add 1 to result (which is 0 at this point)
if ((num1==num2) || (num2 == num3) || (num1 == num3)) //if any two angles are same i.e. isosceles
result += 2; //Add 2 to result (which is 1 if previous if condition was true otherwise its 0) )
/*
At this point :
1) If it was neither right-angled or isoscles
both the if statements will be false and result will remain 0
2) If it was right-angled we did "result += 1" i.e. "result = result + 1"
So, In case first "if" condition is true, result is 1 otherwise its 0
3) If it was isoscles we did "result += 2" i.e. "result = result + 2"
So, second "if" condition is true, result becomes result + 2 ie
if result was 0 it becomes 2 or if it was 1 it becomes 3.
*/
return result;
}
Upvotes: 1
Reputation: 1657
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.
Upvotes: 0
Reputation: 63
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>
Upvotes: 0