# Simple calculation not working, need help

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 09-23-2004
Summonerur
Simple calculation not working, need help
I am trying to get a program to calculate the side of a triangle. I know this is a simple code, im a first year student. So bare with me :cool:

http://rchevys.com/ffxi/underRATEDGames/test.txt

For some reason, when it gets to the output for the calculation it just puts 0 instead of the value it should be desplaying. what am i doing wrong here?
• 09-23-2004
Salem
> printf("C = %d", C);
Use %f for floats and doubles, not %d
• 09-23-2004
Summonerur
I changed that but the answer is still coming out to "0"
• 09-23-2004
Salem
Betcha didn't change all your scanf() calls to use the correct conversion either...
• 09-23-2004
Summonerur
I changed it to :
(still no good)

Code:

```#include <stdio.h> #include <math.h> #include <windows.h> double A,B,C,calculation; int selection; main() {         printf("Calcute the side of a traingle...a^2 + b^2 = c^2\n");         printf("Which sides do you have?\n");         printf("(1) a , b\n(2) b , c\n(3) a , c\n");         scanf("%d", &selection);                 if (selection==1)                 {                         printf("Please enter the two sides:\n");                         printf("A: ");                         scanf("%f", &A);                         printf("B: ");                         scanf("%f", &B);                         calculation = A*A-B*B;                         C = sqrt(calculation);                         printf("C = %f", C);                 }                 if (selection==2)                 {                         printf("Please enter the two sides:\n");                         printf("B: ");                         scanf("%f", &B);                         printf("C: ");                         scanf("%f", &C);                         calculation = B*B-C*C;                         A = sqrt(calculation);                         printf("A = %f", A);                 }                 if (selection==3)                 {                         printf("Please enter the two sides:\n");                         printf("A: ");                         scanf("%f", &A);                         printf("C: ");                         scanf("%f", &C);                         calculation = C*C-A*A;                         B = sqrt(calculation);                         printf("B = %f", B);                 }         Sleep (50000);         return 0; }```
• 09-23-2004
Dave_Sinkula
>Betcha didn't change all your scanf() calls to use the correct conversion either...
Code:

```double A,B,C,calculation; /* ... */ scanf("%f", &A);```
Nope. Use "%lf" for double with scanf.
• 09-23-2004
Summonerur
Code:

```printf("A: "); scanf("%1f", &A); printf("B: "); scanf("%1f", &B); calculation = A*A-B*B; C = sqrt(calculation); printf("C = %f", C);```
I tried that with still no luck. Still giving me 0.0000
I also tried puting printf("C = %1f", C);
Same result
• 09-23-2004
JarJarBinks
Quote:

printf("A: ");
scanf("%f", &A);
printf("B: ");
scanf("%f", &B);
calculation = A*A-B*B;
C = sqrt(calculation);
printf("C = %f", C);

maybe its just me but the formula for finding sides on right
triangles is :
(Pathagerean Therom.. SP?)
A*A + B*B = C*C

(pay close attention to the + )

EDIT : i was in a rush so i didnt check your other formula's
on there, but i suggest you check them if there not giving you the
• 09-23-2004
Summonerur
Woops, that was normally a +. I accidently copied and pasted over it when i was trying something else. but thats not the problem.
• 09-23-2004
Dave_Sinkula
Quote:

Originally Posted by Summonerur
Code:

```printf("A: "); scanf("%lf", &A); /* 'ell' -- not 'one' */ printf("B: "); scanf("%lf", &B); /* 'ell' -- not 'one' */ calculation = A*A-B*B; C = sqrt(calculation); printf("C = %f", C);```
I also tried puting printf("C = %1f", C);

No need.
Code:

`printf("C = %f", C);`
• 09-23-2004
Summonerur
ahhh :) That did the trick. One last question.

How do I get the printf() to output the answer without rounding off?
• 09-23-2004
Dave_Sinkula
Quote:

Originally Posted by Summonerur
How do I get the printf() to output the answer without rounding off?

Could you post an expected input and output to demonstrate exactly what you mean? For example,
Quote:

Calcute the side of a traingle...a^2 + b^2 = c^2
Which sides do you have?
(1) a , b
(2) b , c
(3) a , c
3
A: 3
C: 4
B = 2.645751

• 09-23-2004
Summonerur
yeah, something like that. where the square root will end up as a very large number. It seems to be rounding it off at random places at times.

http://rchevys.com/ffxi/underRATEDGames/test.txt

here is my code again, it has been updated a bit.
• 09-23-2004
quzah
Quote:

It seems to be rounding it off at random places at times.
Welcome to the world of floating point numbers.

Quzah.
• 09-23-2004
Summonerur
Quote:

Originally Posted by quzah
Welcome to the world of floating point numbers.

Quzah.

Wow! Thanks for the help, I know everything I need to know now.
:rolleyes:

Do you actually enjoy sitting here bashing on people who are trying to learn things? I have been looking at previous threads to maybe learn some things off of other peoples questions and I noticed all you do is post sarcastic comments that have absolutely no educational value.

I am sorry if we are not all as smart as you are or fortunate enough to have had or have professors who know what they are teaching. /bow
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last