Sovi.AI - AI Math Tutor

Scan to solve math questions

QUESTION IMAGE

activity 2.11.1 conditional expression: print negative or positive. dra…

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;
}

Explanation:

Brief Explanations

The task requires a conditional (ternary) operator statement that assigns "negative" to condStr when userVal < 0, and "non-negative" otherwise.

  1. The ternary operator syntax is condition ? value_if_true : value_if_false.
  2. We need to assign the result to condStr, so the statement must start with condStr = .
  3. The condition is userVal < 0, which returns "negative" when true, and "non-negative" when false.

Answer:

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.