Pelles C forum

C language => Beginner questions => Topic started by: thudson on January 10, 2013, 01:40:41 AM

Title: Can you tell me whats wrong with this code
Post by: thudson 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);
}
Title: Re: Can you tell me whats wrong with this code
Post by: Bitbeisser 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
Title: Re: Can you tell me whats wrong with this code
Post by: CommonTater 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
 
Title: Re: Can you tell me whats wrong with this code
Post by: thudson 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
Title: Re: Can you tell me whats wrong with this code
Post by: Bitbeisser 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
Title: Re: Can you tell me whats wrong with this code
Post by: Bitbeisser 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
Title: Re: Can you tell me whats wrong with this code
Post by: CommonTater 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. 
Title: Re: Can you tell me whats wrong with this code
Post by: jj2007 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 (http://www.alc.amadeus.com/content/public/alw/skillsoft/cbtlib/116697/116703/eng/thin/transcript.html)
Title: Re: Can you tell me whats wrong with this code
Post by: CommonTater 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 (http://www.alc.amadeus.com/content/public/alw/skillsoft/cbtlib/116697/116703/eng/thin/transcript.html)

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
 
 
Title: Re: Can you tell me whats wrong with this code
Post by: jj2007 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.
Title: Re: Can you tell me whats wrong with this code
Post by: CommonTater 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?
Title: Re: Can you tell me whats wrong with this code
Post by: aardvajk 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.
Title: Re: Can you tell me whats wrong with this code
Post by: frankie 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.
Title: Re: Can you tell me whats wrong with this code
Post by: jj2007 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.
Title: Re: Can you tell me whats wrong with this code
Post by: CommonTater 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 (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?
Title: Re: Can you tell me whats wrong with this code
Post by: CommonTater on January 10, 2013, 11:21:49 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.

I understand that Frankie .... I usually write parenthetic code to safeguard against compiler decisions and optimizations that may, on occasion, hand me the wrong answer... As I'm sure you know there is no harm in this even if I am only reinforcing C's native order of evaluations. 

What is harmful is developing a lazy habit of counting on things that may or may not work as expected, when you can easily control them...  i.e. a few brackets never hurt anyone.


Title: Re: Can you tell me whats wrong with this code
Post by: CommonTater on January 10, 2013, 11:26:30 PM
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.

Good lord man...
 
I had a Pelles C specific question about the behaviour of the stack when creating inline scopes... Are you seriously going to tell me that you knew the stack handling changed with optimization settings in Pelles C when nobody else seemed to know that either. In fact, the concensus was that stack optimizations should be on all the time.

Really ... go find something else to do.
 
Title: Re: Can you tell me whats wrong with this code
Post by: boral on February 06, 2013, 03:05:46 PM
I have modified the code to this and I think it is giving correct answers.

#include <stdio.h>

#include <math.h>

int main(void)

{

   double lega;
   double legb;
   double hypotenuse;

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

   printf("Enter legb: ");

   scanf("%lf", &legb);

   hypotenuse = sqrt(lega*lega + legb*legb);

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

   return(0);
}

Note: Please be sure that the contents of printf are in one line. A lot of error pops up just for breaking the printf contents in 2 lines. Don't know why .
Also note that there is no variable as a or b, the variables are lega and legb.
Title: Re: Can you tell me whats wrong with this code
Post by: tpekar on February 06, 2013, 05:49:47 PM
Another thing to think about.  I have found in C (as well as other languages) compound mathematical evaluations (such as a*b+c*d or more complex) can be problematical because you cannot control how the compiler stores intermediate results (is a*b stored as an integer or a double or a float, etc.?).  I have had to on occasion break down the calculation so I could control how the intermediate results are represented.
Title: Re: Can you tell me whats wrong with this code
Post by: Tino on February 12, 2013, 06:08:41 AM
Hi Forum :)

Maybe this link help (if) to parenthese expressions or even break them down to muplie lines.

http://cboard.cprogramming.com/c-programming/144668-pelles-c-dev-cplusplus-giving-different-output-same-code.html

Have fun :)