if else statements

edited October 2017 in Programming Questions

Suppose x and y are variables of type double that have already been declared in your program and contain meaningful values. write a code fragment that sets y to the absolute value of x without calling the predefined absolute value function(i.e. abs function)

Tagged:

Answers

  • edited October 2017

    Okay. Great homework problem. Have you tried to solve it yet? can we see your attempt?

    Obviously you will need a conditional statement. Do you understand what condition you should check? What code should execute if that condition is true? What code should execute if that condition is not true?

    This is literally two lines of code. Please put some effort in. No one should just do it for you. What would you learn? Either put the effort in and learn or FAIL.

    And this is coming from the guy who is MOST likely to just give you the answer.

  • edited October 2017

    sorry, I forgot to attach the one that I did,

    so I have this one accomplished but not sure if it is right.

    double x, y; 
    
    if (x > 0.0) {
      y = -x;
    } else {
      y = x;
    }
    

    the task is to use only if else statements.

  • What does the > symbol mean?

  • Ooh that was suppose to be lesser than (<). Is may code that close to being correct though?

  • Test it!

    Give X a value (after line 1) and print y (after line 7). See if it does what you expect...

  • Is this solved?

    Then please press Yes to This answered my question...

Sign In or Register to comment.