1. ## Pythagoras

C programming:
I want to creat a program that asks the user to input the integer lengths of three sides of a triangle, and if so whether the triangle is a right-angled, acute or obtuse one. i am having some doubts about the if, else if statements. I never seem to know how and in what order to use them (could maybe someone explain this to me please) What am i doing wrong here??

Code:
```#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main(int argc, char *argv[]){
int SideA;
int SideB;
int SideC;           /*sideC = Hypotenuse*/

printf("Type in the integer lengths of 3 sides of a triangle:\n");
scanf("%d %d %d", &SideA, &SideB, &SideC);
printf("%d %d %d\n", &SideA, &SideB, &SideC);

{

if (SideA*SideA + SideB*SideB == SideC*SideC){
printf("This is a right-angled triangle.\n");
}

else if  (SideC*SideC < SideA*SideA + SideB*SideB){
printf("This is an acute-angled triangle.\n");
}

else if(SideC*SideC > SideA*SideA + SideB*SideB){
printf("This is an obtuse-angled triangle.\n");
}

else {
printf("This is not a triangle\n");

}

return 0;

}```

2. Unless I've missed something on my quick look, your code won't compile since it is hasn't matched curly braces properly.

You need to ensure all three lengths exceed zero, and then sort them. The longest side of a triangle is always opposite the largest angle. The shortest side is always opposite the smallest angle.

If the longest length is greater than or equal to the sum of the other two lengths, the set don't correspond to a triangle. You're not checking for that and need to BEFORE anything else.

If the length of the longest side squared equals the sum of lengths of the other two, it is a right angle triangle. If the longest side squared is less than the sum of squares of the other two, the triangle is acute. Otherwise it is obtuse.

You might also want to check for isosceles and equilateral triangles. Note that isosceles triangles may be either acute or obtuse. An equilateral triangle (all three sides equal) is acute by definition.

3. Since you don't know if/else statements clearly here is a quick on-the-spot guide/tutorial:
if and else statements are used when the expected input/output may be different each time and you want your program to act differently depending on the value. The ordering of if/else statements are as the following:
use if for the first expected value or the value to check for first, followed by else if for the second expected value or the value to check for if the first one is false and else at the last when all if/else if fails.

And here is a sample:
Code:
```#include <stdio.h>

int main()
{
int userAge;
printf("Enter your age: ");
scanf("%d", &userAge);
if(userAge < 13)
{
printf("You are a kid!\n");
}
else if(userAge >= 13 && userAge < 18)
{
printf("You are a teen but not an adult.\n");
}
else if(userAge >= 18 && userAge < 20)
{
printf("You are a teen as well as an adult.\n");
}
else
{
printf("You are an adult and not a teen.");
}
getch();
return 0;
}```
And now coming to your code.
Why are you putting the if/else if/else construct inside curly braces? Its not necessary.
Code:
```    {//No need this

if (SideA*SideA + SideB*SideB == SideC*SideC){
printf("This is a right-angled triangle.\n");
}

else if  (SideC*SideC < SideA*SideA + SideB*SideB){
printf("This is an acute-angled triangle.\n");
}

else if(SideC*SideC > SideA*SideA + SideB*SideB){
printf("This is an obtuse-angled triangle.\n");
}

else {
printf("This is not a triangle\n");

}//No need this```
Also, use 4 spaces inside the body of the if/else if/else statements. And you might want to use double/float instead of int so you can enter decimal lengths.

Try this code:
Code:
```#include <stdio.h>

void flush_buf(void); //Function that stops crashing if input is different from variable type

int main()
{
float A, B, C;
printf("Enter side A: ");
scanf("%f", &A);
flush_buf();
printf("Enter side B: ");
scanf("%f", &B);
flush_buf();
printf("Enter side C: ");
scanf("%f", &C);
flush_buf();

printf("\nA: %.2f, B: %.2f, C: %.2f\n", A, B, C);

if((int)A == 0 || (int)B == 0 || (int)C == 0) //Check whether user entered 0/letters
{
printf("\nNot a valid triangle.\n");
}
else if(A*A + B*B == C*C)
{
printf("\nThe triangle is a right-angled triangle.\n");
}
else if(A*A + B*B > C*C)
{
printf("\nThe triangle is an acute-angled triangle.\n");
}
else if(A*A + B*B < C*C)
{
printf("\nThe triangle is an obtuse-angled triangle.\n");
}

getch();
return 0;
}

void flush_buf(void)
{
while(getchar() != '\n');
}```

4. Thx Guys! I figured it out doing just minimal, But as you can see on the picture (added with this post), is that test case number 2 is saying that i am still doing something wrong. The hint is; Does your program still return the same answer if you reorder the input? So what is the problem, what should I change? Thank u guys in advance. Bare with me, I am new to this.. but lovin it. Doing Artificial Intelligence at the University..

Code:
```#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main(){
int A, B, C;

printf("Type in the integer lengths of 3 sides of a triangle:\n");
scanf("%d %d %d", &A, &B, &C);

if((int)A <= 0 || (int)B <= 0 || (int)C <= 0){
printf("This is not a triangle.\n");
}
else if(A*A + B*B == C*C) {
printf("This is a right-angled triangle.\n");
}
else if(A*A + B*B > C*C){
printf("This is an acute-angled triangle.\n");
}
else if (A*A + B*B < C*C){
printf("This is an obtuse-angled triangle.\n");
}

return 0;
}```

5. You should reorder A,B and C - so C be the biggest side.

Also Note that 1,1,5 is not triangle...

6. Originally Posted by vart
Also Note that 1,1,5 is not triangle...
I assume you meant something like (1,1), (5,1), (5,5)? Or (1,1), (2, 1), (5,1)?

Anyway I essentially agree, altough some may argue otherwise and call them degenerate triangles.

Degeneracy (mathematics) - Wikipedia, the free encyclopedia

7. Originally Posted by SirPrattlepod
I assume you meant something like (1,1), (5,1), (5,5)? Or (1,1), (2, 1), (5,1)?
You are mixing 2 different threads. Here input is not coordinates but side lengths

8. Originally Posted by Se7enUp
The hint is; Does your program still return the same answer if you reorder the input? So what is the problem, what should I change?
Read the second sentence of my previous post in this thread.

9. Originally Posted by vart
You are mixing 2 different threads. Here input is not coordinates but side lengths
De gads you're right! Oops

10. ## Figured it out, Thx for replies guys!

Code:
```#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main(){
int A, B, C;
int x;

printf("Type in the integer lengths of 3 sides of a triangle:\n");
scanf("%d %d %d", &A, &B, &C);

if(A > B && A > C){ /*set largest value to C*/
x=A;
A=C;
C=x;}
if(B > A && B > C){
x=B;
B=C;
C=x;}

if(((int)A <= 0 || (int)B <= 0 || (int)C <= 0)||(A+B <= C)||(A+C<=B)||(B+C<=A)){  /*Is this a triangle?*/
printf("This is not a triangle.\n");
}

else if(A*A + B*B == C*C){
printf("This is a right-angled triangle.\n");
}

else if(A*A + B*B > C*C){
printf("This is an acute-angled triangle.\n");
}

else if (A*A + B*B < C*C){
printf("This is an obtuse-angled triangle.\n");
}

return 0;
}```

11. Well done! On it right. Just a few things which you may want to consider

Code:
`if(((int)A <= 0 || (int)B <= 0 || (int)C <= 0)||(A+B <= C)`
No need to type cast those variables here. They are already int and can only return a whole value

Code:
`else if(A*A + B*B == C*C){`
Try start using bracket to isolate the expression and to avoid confusions future when evaluating complex expression. The becomes far much easier avoid run time issues in relation to operator precedence.

ssharish

12. Originally Posted by ssharish2005
Try start using bracket to isolate the expression and to avoid confusions future when evaluating complex expression. The becomes far much easier avoid run time issues in relation to operator precedence.
To the OP: yes, well done!

@ssharish2005: I actually disagree with the part of your message quoted. When I see expressions like n = (a*b) + c; I literally cringe. n = a*b + c is much easier to read in my opinion

13. Well that is your opinion as to how you want to write your expression. I'm not even considering the scenario the OP has has in this thread, consider an other thread on here which i was reading about macro function

Code:
```#define PRODUCT(x)  (x*x)
PRODUCT(i+1);```
What's the answer to it? Altho its much better to write x*x ; but the answer you derive at the end is not what you want. It is not just all about readability, it also about the how the expression get evaluated and you can control that by putting the bracket, because ( has more precedence than any other operator. This is how OPs code would look like if he did us the brackets

Code:
`else if( ( ( A * A ) + ( B * B ) ) == ( C * C ) ) {`
And makes it no hard to read than it was already and it will only can get better in the later stages in writing a quality code and not putting anything in will only yield making it harder and difficult to debug!

ssharish

14. Originally Posted by ssharish2005

Code:
`else if( ( ( A * A ) + ( B * B ) ) == ( C * C ) ) {`
And makes it no hard to read than it was already and it will only can get better in the later stages in writing a quality code and not putting anything in will only yield making it harder and difficult to debug!

ssharish
For me it is a lot harder when the original version

15. Originally Posted by ssharish2005
consider an other thread on here which i was reading about macro function
Code:
```#define PRODUCT(x)  (x*x)
PRODUCT(i+1);```
What's the answer to it? Altho its much better to write x*x ; but the answer you derive at the end is not what you want. It is not just all about readability, it also about the how the expression get evaluated and you can control that by putting the bracket, because ( has more precedence than any other operator.
In other words, in the case of function-style macros, defensively programming for correctness trumps readability.

Originally Posted by ssharish2005
This is how OPs code would look like if he did us the brackets
Code:
`else if( ( ( A * A ) + ( B * B ) ) == ( C * C ) ) {`
And makes it no hard to read than it was already
I disagree. It is far more difficult to read as I have to parse all those nested parentheses. It is equally correct as:
Code:
`else if(A * A + B * B == C * C) {`
which is far more readable, if the reader knows the relative predence of the operators involved, so correctness is not an issue. The pattern of comparing some arithmetic expression is so common that I find it very hard to fathom that anyone past the rank beginner stage would fail to immediately see that the above is equivalent to:
Code:
`else if((A * A + B * B) == (C * C)) {`
Generally, I think that students are taught some version of operator precedence in mathematics that corresponds to the precedence of the similiar operators in C, so likewise I find it difficult to believe that someone might fail to interpret the above as being equivalent to:
Code:
`else if((A * A) + (B * B) == C * C) {`
But still, I grant that it is possible, so I would not object to such use of redundant grouping.

For:
Code:
`n = a * b + c;`
I find it hard to believe that anyone beyond the beginner stage would both be unaware of the relative precedence and also choose to interpret with right to left associativity for * and + instead, so I would object to:
Code:
`n = (a * b) + c;`
but for:
Code:
`n = a + b * c;`
I think this is acceptable, though I wouldn't bother:
Code:
`n = a + (b * c);`
By the way, do you also write:
Code:
`x = y + z;`
as:
Code:
`x = (y + z);`
? After all, a rank beginner could interpret it as:
Code:
`(x = y) + z;`

Popular pages Recent additions