NO

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

CommonTater

  • Guest
Re: Can you tell me whats wrong with this code
« Reply #15 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.



CommonTater

  • Guest
Re: Can you tell me whats wrong with this code
« Reply #16 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.
 
« Last Edit: January 10, 2013, 11:38:54 PM by CommonTater »

boral

  • Guest
Re: Can you tell me whats wrong with this code
« Reply #17 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.
« Last Edit: February 06, 2013, 06:23:17 PM by boral »

tpekar

  • Guest
Re: Can you tell me whats wrong with this code
« Reply #18 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.

Tino

  • Guest
Re: Can you tell me whats wrong with this code
« Reply #19 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 :)