NO

Author Topic: Can you tell me whats wrong with this code  (Read 9354 times)

thudson

  • Guest
Can you tell me whats wrong with this code
« on: January 10, 2013, 01:40:41 AM »
#include <stdio.h>

#include <math.h>

int main(void)

{

   double leg a;
   double leg b;
   double hypotenuse;

   printf("Enter leg a: ");
   
   scanf("%lf", &leg a);

   printf("Enter leg b: ");

   scanf("%lf", &leg b);

   hypotenuse = sqrt(a*a + b*b);

   printf("The hypotenuse of a right triangle with leg a of %f
   and leg b of %f is %f.\n", leg a, leg b, hypotenuse);

   return(0);
}

Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 772
Re: Can you tell me whats wrong with this code
« Reply #1 on: January 10, 2013, 01:51:52 AM »
Seriously?

Don't know what books/resources you used to learn C, but you simply can't have any blanks/spaces in variable names...

- A variable name can only consist of the letters 'a'..'z', 'A'..'Z', the underscore '_' and the numbers '0'..'9'
- A variable name can not start with a number
- A variable name can not be identical to a reserved word

hth,

Ralf

CommonTater

  • Guest
Re: Can you tell me whats wrong with this code
« Reply #2 on: January 10, 2013, 02:42:43 AM »
Seriously?

Don't know what books/resources you used to learn C, but you simply can't have any blanks/spaces in variable names...

- A variable name can only consist of the letters 'a'..'z', 'A'..'Z', the underscore '_' and the numbers '0'..'9'
- A variable name can not start with a number
- A variable name can not be identical to a reserved word

hth,

Ralf

Also, his calculation is likely to give him wrong answers even after he fixes the variable names...
 
hypoetneuse = sqrt(a*a + b*b)  presents undefined behaviour that can be evaluated in several different ways... 
((a * a) + b) * b 
or
a * ((a+b) * b)
etc. 
 
He needs to make sure the order of evaluation is under his control as in... sqrt((a*a) + (b*b)) ... so that as it works it's way out of the parenthese he has only one number on each side of each mathematical operation.
 
Plus, if he doesn't want answers like 3.345678  he's going to have to improve his final printf() call by adding field sizes like %.3f
 
« Last Edit: January 10, 2013, 02:54:04 AM by CommonTater »

thudson

  • Guest
Re: Can you tell me whats wrong with this code
« Reply #3 on: January 10, 2013, 03:47:57 AM »
Thanks guys for your help. I just started my course in school two weeks ago so this is all new to me. I fixed the variable issues and the formula issue, I still have errors

Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 772
Re: Can you tell me whats wrong with this code
« Reply #4 on: January 10, 2013, 06:40:59 AM »
Thanks guys for your help. I just started my course in school two weeks ago so this is all new to me. I fixed the variable issues and the formula issue, I still have errors
You need to realize that nobody around here has neither a working crystal ball or Vulcan mind reading capabilities...

That means we can only help you with those things you describe, "I still have errors" doesn't tell us what your issues are.

The variable names was the obvious thing that immediately stood out in the code you posted, something that you should have been taught within the first hour or two of your course.

So post your current version and be more specific as to what error you think you get stuck at and we can help you with the next step. You also need to be aware that we won't do your homework for you. Ask specific questions and you will get specific answers...

Ralf

Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 772
Re: Can you tell me whats wrong with this code
« Reply #5 on: January 10, 2013, 06:47:55 AM »
Also, his calculation is likely to give him wrong answers even after he fixes the variable names...
<snip>
Plus, if he doesn't want answers like 3.345678  he's going to have to improve his final printf() call by adding field sizes like %.3f
The variable name error was so blatant that I thought it was best to take it one step at a time, with the formatting issue IMHO of the least importance. If (s)he doesn't understand how to properly name variables (and functions!), (s)he won't ever get that far that this matters...

Now let's wait and see if/when (s)he posts some more information at what error (s)he gets stuck now...

Ralf

CommonTater

  • Guest
Re: Can you tell me whats wrong with this code
« Reply #6 on: January 10, 2013, 09:20:13 AM »
Thanks guys for your help. I just started my course in school two weeks ago so this is all new to me. I fixed the variable issues and the formula issue, I still have errors

Ok... What errors? 

If you open your project in POIDE and go to the main menu ... Project -> Zip files ...  Then attach the resulting zip file of of your project to a message, we can have a look at it for you. 

Offline jj2007

  • Member
  • *
  • Posts: 536
Re: Can you tell me whats wrong with this code
« Reply #7 on: January 10, 2013, 10:52:22 AM »
hypoetneuse = sqrt(a*a + b*b)  presents undefined behaviour that can be evaluated in several different ways... 
((a * a) + b) * b 
or
a * ((a+b) * b)
etc. 

Why that, tater?

Quote
multiplication, division, and modulus have higher precedence than binary addition and subtraction operators

CommonTater

  • Guest
Re: Can you tell me whats wrong with this code
« Reply #8 on: January 10, 2013, 11:36:40 AM »
hypoetneuse = sqrt(a*a + b*b)  presents undefined behaviour that can be evaluated in several different ways... 
((a * a) + b) * b 
or
a * ((a+b) * b)
etc. 

Why that, tater?

Quote
multiplication, division, and modulus have higher precedence than binary addition and subtraction operators

The most reliable math in C always has a single number on each side of the operator ...  A + B  .... When you allow multiples you end up with the possibility that it may evaluate your equation differently than you expect... Order of evaluation is not always left to right...

A + B * C ...  if A =  1, B = 2 and C = 3  could be parsed as  :
 
1 + (2 * 3) = 1 + 6 = 7
 
By writing parenthetical code you force it to evaluate brackets first always working with one number on each side of each operator... giving you control over order of evaluation and precidence.
 
(A + B) * C = (1 + 2) * 3 = 3 * 3 = 9
 
 
« Last Edit: January 10, 2013, 11:50:18 AM by CommonTater »

Offline jj2007

  • Member
  • *
  • Posts: 536
Re: Can you tell me whats wrong with this code
« Reply #9 on: January 10, 2013, 01:43:28 PM »
A + B * C ...  if A =  1, B = 2 and C = 3  could be parsed as  :
 
1 + (2 * 3) = 1 + 6 = 7

It will be parsed as 1 + 6 = 7

That's what operator precedence was designed for, and that's why there will be no "undefined behaviour" in the OP's respective code.

CommonTater

  • Guest
Re: Can you tell me whats wrong with this code
« Reply #10 on: January 10, 2013, 05:49:46 PM »
A + B * C ...  if A =  1, B = 2 and C = 3  could be parsed as  :
 
1 + (2 * 3) = 1 + 6 = 7

It will be parsed as 1 + 6 = 7

That's what operator precedence was designed for, and that's why there will be no "undefined behaviour" in the OP's respective code.

AND... by standard math, working left to right that would be the wrong answer... Hense the need for parentheses to control the calculation.
 
Tell me something, honestly JJ ... have you ever started with a blank editor and written a medium or large sized C program from the ground up?
« Last Edit: January 10, 2013, 05:54:55 PM by CommonTater »

aardvajk

  • Guest
Re: Can you tell me whats wrong with this code
« Reply #11 on: January 10, 2013, 06:59:23 PM »
If Pelles C or any other compiler that claimed it could handle standard C ever produced 9 for 1 + 2 * 3 it would be a laughing stock. Likewise if it produced anything but a2 + b2 for a*a + b*b.

Quote
Tell me something, honestly JJ ... have you ever started with a blank editor and written a medium or large sized C program from the ground up?
I think it's your knowledge of C basics thats shaky. You didn't know how scopes worked, and now you don't know how its math works. I'd be very concerned if an assembly specialist knew 'my' languages rules better than I did.

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
Re: Can you tell me whats wrong with this code
« Reply #12 on: January 10, 2013, 07:02:14 PM »
Tater and JJ,
you are saying the same thing, but don'seems to understand reciprocally.
From Tater said he is saying that if you want A+B executed before you have to put parenthesis.
JJ consider that who wrote the formula is well aware of expression precedence and layed it up it conseguently.
Anyway operator precedence is in C standard and cannot violate precedences.
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

Offline jj2007

  • Member
  • *
  • Posts: 536
Re: Can you tell me whats wrong with this code
« Reply #13 on: January 10, 2013, 07:56:40 PM »
Tell me something, honestly JJ ... have you ever started with a blank editor and written a medium or large sized C program from the ground up?

Tater, that's an interesting idea! Do you think at the end of the process I would be sufficiently confused to accept your "standard math", i.e.
hypoetneuse = sqrt(a*a + b*b)  presents undefined behaviour
?

Difficult to imagine, honestly. I learned 50 years ago that sqrt(a*a + b*b) is exactly defined, following operator precedence rules, as sqrt((a*a) + (b*b)) or sqrt(a2 + b2), and such early lessons tend to be sticky...

But maybe you can produce a snippet that compiles with Pelle's C and produces, as you suggested, randomly different results as shown below?
... can be evaluated in several different ways... 
((a * a) + b) * b 
or
a * ((a+b) * b)
etc.

CommonTater

  • Guest
Re: Can you tell me whats wrong with this code
« Reply #14 on: January 10, 2013, 11:15:14 PM »
I learned 50 years ago

Interesting ... especially since C has only been around for 40 or so years.

http://plan9.bell-labs.com/who/dmr/chist.html

Quote
Tater, that's an interesting idea! Do you think at the end of the process I would be sufficiently confused to accept your "standard math", i.e.

Look... I don't play pissing contests.  You've already admitted you've never written C code... that should tell everyone where you're coming from.  Do you even use Pelles C?
« Last Edit: January 10, 2013, 11:17:29 PM by CommonTater »