In: Computer Science
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;
}
//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: