Question

In: Computer Science

What does this mean in C++ syntax? (leftover < 0 ? -leftover : 0) and (leftover...

What does this mean in C++ syntax?

(leftover < 0 ? -leftover : 0)

and

(leftover > 0 ? leftover : 0)

Full version:

// rugfit1.cpp - calculates fit of rug to a floor

#include <iostream>
using namespace std;

// utility function to calculate area of a rectangle
double area(double width, double length) {
return width * length;
}

int main() {
  
double floorWidth, floorLength, rugWidth, rugLength,
floorArea, rugArea, leftover;

cout << "enter width and length of floor: ";
cin >> floorWidth >> floorLength;
cout << "enter width and length of rug: ";
cin >> rugWidth >> rugLength;

floorArea = area(floorWidth, floorLength);
rugArea = area(rugWidth, rugLength);
leftover = rugArea - floorArea;

cout << "floor area: " << floorArea << endl;
cout << "rug area: " << rugArea << endl;
cout << "leftover rug area: " <<
(leftover > 0 ? leftover : 0) << endl;
cout << "empty floor area: " <<
(leftover < 0 ? -leftover : 0) << endl;

return 0;
}

Solutions

Expert Solution

//What does this mean in C++ syntax?

//Conditional Operator : In C++, there is a ternary operator called conditional operator that can be used to replace a simple if else statement for simplicity.

// A ternary operator is an operator that requires three expressions in order to use it.

// a binary operator is an operator that requires two expressions inorder to use it (ex +,-,*,/)

//Syntax : condition?to do if true:to doif false

   expression1?expression2:expression3

// Example: in your case

// 1. (leftover < 0 ? -leftover : 0)

// Explanation : here the condition is "if leftover variable is holding a value that is less than 0". The true part is, if the leftover < 0 is true, then give negative of leftover so that it will become a positive number. The false part is, if the leftover < 0 is false, then give 0.

// here this conditional operator is used to decide what to print on the console. Hence, if the leftover is less than 0 then -leftover i.e., thee positive of leftover is printed, otherwise 0 will be printed.

// 2. (leftover > 0 ? leftover : 0)

// Explanation : here the condition part is "if leftover variable is holding a value that is greater than 0". The true part is, if the leftover > 0 is true, give leftover value. The false part is, if the leftover > 0 is false, then give 0.

// same as above example here also the conditional operator is used to decide what to print on the console. Hence, if the leftover variable's value is greater than 0 then print it's value otherwise, print 0.

// Explanation of the given program :

// this program is used to find if a rug can be put on a floor. We calculate the area of the floor and the area of the rug and tell if once put on the floor, the rug will be left over or the floor will be left over.

// Full version:

// rugfit1.cpp - calculates fit of rug to a floor

#include <iostream>

using namespace std;

// utility function to calculate area of a rectangle

double area(double width, double length) {

return width * length;

}

int main() {

double floorWidth, floorLength, rugWidth, rugLength,

floorArea, rugArea, leftover;

cout << "enter width and length of floor: ";

cin >> floorWidth >> floorLength;

cout <<endl<< "enter width and length of rug: ";

cin >> rugWidth >> rugLength;

floorArea = area(floorWidth, floorLength);

rugArea = area(rugWidth, rugLength);

leftover = rugArea - floorArea;

cout <<endl<< "floor area: " << floorArea << endl;

cout << "rug area: " << rugArea << endl;

cout << "leftover rug area: " << (leftover > 0 ? leftover : 0) << endl;

cout << "empty floor area: " << (leftover < 0 ? -leftover : 0) << endl;

return 0;

}

Output:


Related Solutions

in probability, what does {0} mean?
in probability, what does {0} mean?
What does break and continue mean in C++?
What does break and continue mean in C++?
C++ : Find the syntax errors in the following program. For each syntax error, fix it...
C++ : Find the syntax errors in the following program. For each syntax error, fix it and add a comment at the end of the line explaining what the error was. #include <iostream> #include <cmath> using namespace std; int main(){ Double a, b, c; 2=b; cout<<"Enter length of hypotenuse"<<endl; cin>>c>>endl; cout>>"Enter length of a side"<<endl; cin>>a; double intermediate = pow(c, 2)-pow(a, 2); b = sqrt(intermediate); cout<<"Length of other side is:" b<<endline; return 0; }
what is a const in C++? what does const mean in these functions
what is a const in C++?what does const mean in these functions-> " void idkwhattonameit( const name) {} "-> " void idkwhattonameit2( const &name) {} "-> " void idkwhattonameit3( const * name) {} "-> " void idkagain (const unique_ptr < Name > name_uPtr)"
If the narrow sense heritability of a trait is 0, what does this mean? Does this mean a trait is entirely determined by environment?
If the narrow sense heritability of a trait is 0, what does this mean? Does this mean a trait is entirely determined by environment? Explain all the contributions to total phenotypic variance.
What does this and *this mean in c++ And how to use them
What does this and *this mean in c++ And how to use them
What does the term performance mean? Expected performance? b. What does the term risk mean? c....
What does the term performance mean? Expected performance? b. What does the term risk mean? c. Can the risk be completely eliminated? Why? d. In the financial securities market, how can the level of risk be minimized? and. What does variability mean within corporate finance? F. What does the concept of diversification mean within corporate finance? What is its application within a global stock market?
what does this "->" mean in c? when can you use it?
what does this "->" mean in c? when can you use it?
What does it mean when the L1 instruction cache miss rate is 0
What does it mean when the L1 instruction cache miss rate is 0
When Economic Profit = 0, then what does this mean? 1-This means that all firms are...
When Economic Profit = 0, then what does this mean? 1-This means that all firms are operating at a loss. The best thing to do next would be to raise prices to cover the losses. 2-There is no motivation for the firms to leave the industry and there is no motivation for new firms to enter the industry. 3-Everyone run for the exit doors! Everyone in the industry is crazy if they don’t get out of an industry like this...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT