Question: Translate these nested C++ if statements into if/else if statements. Deal with all possible inputs. If the wavelength is lesser than 400 the program will

Translate these nested C++ if statements into if/else if statements.

Deal with all possible inputs.

If the wavelength is lesser than 400 the program will print the message "wavelength too small." If the wavelength is greater than 700 the program will print the message "wavelength too large".

In order to do that, define a global counter variable at the beginning of the program. Increment this variable immediately before each condition that is checked.

With these changes the sample executions would look something like this:

Please input a visible wavelength: 495.6

Your wavelength corresponds to the color Blue.

You evaluated 4 conditional expressions.

Your wavelength corresponds to the color Blue.

You evaluated 4 conditional expressions.

Please input a visible wavelength: 795.6

Your wavelength is too large.

You evaluated 8 conditional expressions.

I have the working C++ code with nested if statements but need to write using if/else is statements.

Code :

#include #include

using namespace std; //Declare global variable for comparisons int i = 0;

int main() { //Declare variable for user input int wavelength; //Prompt user for wavelength input cout << "Please enter a wavelength between 400 and 700 :" ; //Store user input in wavelength variable cin >> wavelength; i++; if (wavelength < 400) { //Inform user of invalid input cout << ":: Your wavelength is too small ::" << endl; cout <<"No of Comparisions :"<< i << endl; return 0; } i++; if (wavelength>=400 && wavelength<= 445) { //Print wavelength output color cout << "Your wavelength is Violet" << endl; //Print number of comparisons performed cout <<"No of Comparisions :"<< i << endl; return 0; } i++; if (wavelength>445 && wavelength<=475) { //Print wavelength output color cout << "Your wavelength is Indigo" << endl; //Print number of comparisons performed cout <<"No of Comparisions :"<< i << endl; return 0; } i++; if (wavelength>475 && wavelength<= 510) { //Print wavelength output color cout << "Your wavelength is Blue" << endl; cout <<"No of Comparisions :"<< i << endl; return 0; } i++; if (wavelength>510 && wavelength <= 570) { //Print wavelength output color cout << "Your wavelength is Green" << endl; cout <<"No of Comparisions :"<< i << endl; return 0; } i++; if (wavelength>570 && wavelength <= 590) { //Print wavelength output color cout << "Your wavelength is Yellow" << endl; cout <<"No of Comparisions :"<< i << endl; return 0; } i++; if (wavelength>590 && wavelength <=650) { //Print wavelength output color cout << "Your wavelength is Orange" << endl; cout <<"No of Comparisions :"<< i << endl; return 0; } i++; if (wavelength>650 && wavelength<=700) { //Print wavelength output color cout << "Your wavelength is Red" << endl; cout <<"No of Comparisions :"<< i << endl; return 0; } else { //Inform user of invalid input cout << ":: Your wavelength is too big ::" << endl; cout <<"No of Comparisions :" << i << endl; } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!