207k views
0 votes
Write a statement that increments (adds 1 to) one and only one of these five variables: reverseDrivers parkedDrivers slowDrivers safeDrivers speeders. The variable speed determines which of the five is incremented as follows: The statement increments reverseDrivers if speed is less than 0, increments parkedDrivers if speed is less than 1, increments slowDrivers if speed is less than 40, increments safeDrivers if speed is less than or equal to 65, and otherwise increments speeders.

User Ssindelar
by
4.7k points

1 Answer

1 vote

Answer:

The c++ coding for this scenario is given below.

if( speed < 0 )

reverseDrivers = reverseDrivers + 1;

else if( speed >= 0 && speed < 1 )

parkedDrivers = parkedDrivers + 1;

else if( speed >= 1 && speed < 40 )

slowDrivers = slowDrivers + 1;

else if( speed >= 40 && speed < 65 )

safeDrivers = safeDrivers + 1;

else

speeders = speeders + 1;

Step-by-step explanation:

First, five integer variables are declared and initialized.

int reverseDrivers = 0, parkedDrivers = 0, slowDrivers = 0, safeDrivers = 0, speeders = 0;

All the above variables need to be incremented by 1 based on the given condition, hence, these should have some original value.

Another variable speed of data type integer is declared.

int speed;

The variable speed is initialized to any value. User input is not taken for speed since it is not mentioned in the question.

Based on the value of the variable speed, one of the five variables, declared in the beginning, is incremented by 1.

The c++ program to implement the above scenario is as follows.

#include <iostream>

using namespace std;

int main() {

int reverseDrivers = 0, parkedDrivers = 0, slowDrivers = 0, safeDrivers = 0, speeders = 0;

int speed = 34;

if( speed < 0 )

{

reverseDrivers = reverseDrivers + 1;

cout<<" reverseDrivers " <<endl;

}

else if( speed >= 0 && speed < 1 )

{

parkedDrivers = parkedDrivers + 1;

cout<<" parkedDrivers "<<endl;

}

else if( speed >= 1 && speed < 40 )

{

slowDrivers = slowDrivers + 1;

cout<<" slowDrivers "<<endl;

}

else if( speed >= 40 && speed < 65 )

{

safeDrivers = safeDrivers + 1;

cout<<" safeDrivers "<<endl;

}

else

{

speeders = speeders + 1;

cout<<" speeders "<<endl;

}

return 0;

}

The program only outputs the variable which is incremented based on the value of the variable speed.

User OverToasty
by
5.3k points