Translate these nested C++ if statements into if/else if statements. Deal with a
ID: 3782505 • Letter: T
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 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 <iostream>
#include <string>
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;
}
}
Explanation / Answer
if else is not working for these types of condition.. So i am using Else if ladder condition
#include <iostream>
#include <string>
#include<stdlib.h>
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;
if (wavelength < 400)
{
i++;
//Inform user of invalid input
cout << ":: Your wavelength is too small ::" << endl;
cout <<"No of Comparisions :"<< i << endl;
//return 0;
}
else if(wavelength > 700)
{
//Inform user of invalid input
cout << ":: Your wavelength is too big ::" << endl;
cout <<"No of Comparisions :" << i << endl;
}
else
{
//if else is not working for these types of condition.. So i am using Else if ladder condition
if (wavelength>=400 && wavelength<= 445)
{
i++;
//Print wavelength output color
cout << "Your wavelength is Violet" << endl;
//Print number of comparisons performed
cout <<"No of Comparisions :"<< i << endl;
//return 0;
}
else if (wavelength>445 && wavelength<=475)
{
i++;
//Print wavelength output color
cout << "Your wavelength is Indigo" << endl;
//Print number of comparisons performed
cout <<"No of Comparisions :"<< i << endl;
//return 0;
}
else if (wavelength>475 && wavelength<= 510)
{
i++;
//Print wavelength output color
cout << "Your wavelength is Blue" << endl;
cout <<"No of Comparisions :"<< i << endl;
//return 0;
}
else if (wavelength>510 && wavelength <= 570)
{
i++;
//Print wavelength output color
cout << "Your wavelength is Green" << endl;
cout <<"No of Comparisions :"<< i << endl;
//return 0;
}
else if (wavelength>570 && wavelength <= 590)
{
i++;
//Print wavelength output color
cout << "Your wavelength is Yellow" << endl;
cout <<"No of Comparisions :"<< i << endl;
//return 0;
}
else if (wavelength>590 && wavelength <=650)
{
i++;
//Print wavelength output color
cout << "Your wavelength is Orange" << endl;
cout <<"No of Comparisions :"<< i << endl;
//return 0;
}
else if (wavelength>650 && wavelength<=700)
{
i++;
//Print wavelength output color
cout << "Your wavelength is Red" << endl;
cout <<"No of Comparisions :"<< i << endl;
//return 0;
}
}
system("pause");
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.