65.0k 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.

1 Answer

3 votes

Answer:

int reverseDrivers ,parkedDrivers ,slowDrivers, safeDrivers ,speeders;

int speed; // variable declaration

if(speed<0) // checking condition

reverseDrivers++; // increment the value by 1

else if (speed<1) // checking condition

parkedDrivers++;// increment the value by 1

else if (speed<40) // checking condition

slowDrivers++;// increment the value by 1

else if(speed<=65) // checking condition

safeDrivers ++;// increment the value by 1

else

speeders++;// increment the value by 1

Step-by-step explanation:

Following are the description of statement

  • Declared a variable reverseDrivers ,parkedDrivers, slowDrivers ,safeDrivers speeders and speed of integer types. These variables increments its value by 1 according to the condition of the speed variable.
  • Speed variable check the condition of speed.
  • if(speed<0) it increment the value by 1 in the "reverseDrivers" variable.
  • if (speed<1) it increment the value by 1 in the "parkedDrivers" variable.
  • if (speed<40) it increment the value by 1 in the "slowDrivers" variable.
  • if(speed<=65) it increment the value by 1 in the "safeDrivers " variable.
  • if all the if block condition fails then control moves to the else block and increment the value by 1 in the "speeders " variable.

User Moosa
by
5.9k points