Rewrite the following expressions as C++ assignment statements that use the conditional operator. (Assume that all variables are declared properly.)   //a.   if (x > y)     z = x - y;   else     z = y - x;     //b.   if (hours >= 40.0)     pay = 40 * 7.50 + 1.5 * 7.5 * (hours - 40);   else     pay = hours * 7.50;     //c.   if (points >= 60)     str = "Pass";   else     str = "Fail";

C++ for Engineers and Scientists
4th Edition
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Bronson, Gary J.
Chapter5: Repetition Statements
Section: Chapter Questions
Problem 7PP
icon
Related questions
Question
100%

Rewrite the following expressions as C++ assignment statements that use the conditional operator. (Assume that all variables are declared properly.)

 

//a.

  if (x > y)

    z = x - y;

  else

    z = y - x;

 

 

//b.

  if (hours >= 40.0)

    pay = 40 * 7.50 + 1.5 * 7.5 * (hours - 40);

  else

    pay = hours * 7.50;

 

 

//c.

  if (points >= 60)

    str = "Pass";

  else

    str = "Fail";

Expert Solution
Step 1

The syntax of the conditional operator is as follows:

Expression1 ? Expression2 : Expression3

If Expression1 is true, then Expression2 is evaluated else Expression3 is evaluated.

a. Write the assignment statement for given statements as follows:

z = ( x>y ? x-y : y-x);

trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps

Blurred answer
Knowledge Booster
Algebraic Expressions
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr