QUESTION IMAGE
Question
activity 2.11.1 conditional expression: print negative or positive.
drag and drop the code statement that assigns condstr with a conditional expression that evaluates to string negative if userval is less than 0, and non - negative otherwise.
ex: if the input is -9, then the output is:
-9 is negative.
note: only one code statement on the left will be used in the final solution.
how to use this tool
unused
(userval < 0)?
egative\ :
on - negative\;
condstr = (userval < 0)?
egative\ :
on - negative\;
condstr = (userval < 0)?
on - negative\ :
egative\;
condstr = (userval > 0)?
egative\ :
on - negative\;
main.cpp
#include <iostream>
#include <string>
using namespace std;
int main() {
string condstr;
int userval;
cin >> userval;
cout << userval << \ is \ << condstr << \.\ << endl;
return 0;
}
The task requires a conditional (ternary) operator statement that assigns "negative" to condStr when userVal < 0, and "non-negative" otherwise.
- The ternary operator syntax is
condition ? value_if_true : value_if_false. - We need to assign the result to
condStr, so the statement must start withcondStr =. - The condition is
userVal < 0, which returns "negative" when true, and "non-negative" when false.
Snap & solve any problem in the app
Get step-by-step solutions on Sovi AI
Photo-based solutions with guided steps
Explore more problems and detailed explanations
condStr = (userVal < 0) ? "negative" : "non-negative";
This statement should be placed in the blank space between cin >> userVal; and cout << userVal << " is " << condStr << "." << endl; in the main.cpp code.