3.16 Conditional expressions

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Create a conditional expression that evaluates to string "negative" if userVal is less than 0, and "non-negative" otherwise. Example output when userVal = -9 for the below sample program: -9 is negative. #include <string> using namespace std; int main() { string condStr; int userVal; userVal = -9; condStr = /* Your solution goes here */; cout << userVal << " is " << condStr << "." << endl; return 0; }

(userVal >= 0 ) ? "non-negative" : "negative";

Convert each if-else statement to a single assignment statement using a conditional expression, using parentheses around the condition. Enter "Not possible" if appropriate. .. if (x < 20) { y = x; } else { y = 20; } y = (x < 20) __________________

? x : 20;

Convert each if-else statement to a single assignment statement using a conditional expression, using parentheses around the condition. Enter "Not possible" if appropriate. .. if (x < 0) { y = -x; } else { x = x; } ________________________

Not possible

Using a conditional expression, write a statement that increments numUsers if updateDirection is 1, otherwise decrements numUsers. Ex: if numUsers is 8 and updateDirection is 1, numUsers becomes 9; if updateDirection is 0, numUsers becomes 7. Hint: Start with "numUsers = ...". #include <iostream> using namespace std; int main() { int numUsers; int updateDirection; numUsers = 8; updateDirection = 1; /* Your solution goes here */ cout << "New value is: " << numUsers << endl; return 0; }

numUsers = (updateDirection ==1)?(++numUsers):(--numUsers);

Convert each if-else statement to a single assignment statement using a conditional expression, using parentheses around the condition. Enter "Not possible" if appropriate. .. if (x < 0) { x = -x; } else { x = x; } __________________________

x = (x < 0) ? -x : x;

Convert each if-else statement to a single assignment statement using a conditional expression, using parentheses around the condition. Enter "Not possible" if appropriate. .. if (x > 50) { y = 50; } else { y = x; } y = (_____________________) ? 50 : x;

x > 50

Convert each if-else statement to a single assignment statement using a conditional expression, using parentheses around the condition. Enter "Not possible" if appropriate. .. if (x < 100) { y = 0; } else { y = x; } _________________________________

y = (x < 100) ? 0 : x;


Kaugnay na mga set ng pag-aaral

Trigger 9: Acquired brain injury and neuroinflammation

View Set

chapter 6s statistical process control

View Set

Chapter 1- Basic Concepts of Strategic Management

View Set