Please read the pdf carefully.
CSCI 201 – Computer Science 1
Homework 2
Objectives. The objectives of this assignment are:
1. Learn how to track the number of conditionals evaluated when a program runs.
2. Learn to employ the divide and conquer strategy to reduce the number of conditionals evaluated.
3. Learn to construct a complex, nested, if-else statement in C++.
Question 1. Due on Monday January 22.
Recall that in Lab 2, we created a program that maps a numeric wavelength into a color of the
visible spectrum. In this question, we will create an improved version of the program that has the
following additional features.
1. Deal with all possible inputs. If the wavelength is lesser than 400.0, the program will print
the message “wavelength too small”.If the wavelength is greater than 700.0, the program will
print the message “wavelength too large”.
2. Report the number of comparisons that were performed. 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.
Please input a visible wavelength: 795.6
Your wavelength is too large.
You evaluated 8 conditional expressions.
Carry out the following tasks:
1. Designing a solution. Figure 1 partially desribes the logic structure of the program. (Note that
the first condition checks if the input is less than the lower bound, i.e., 400.) Modify the flowchart
structure from Figure 1 to initialize a counter, and increment the counter before each conditional.
Extend the modified flowchart to cover all the cases, and test it using Raptor. (Note that the last
condition will be used to check if the input is greater than the upper bound, i.e., 700.)
2. Implementing the solution. Write a C++ program that implements your design, using the nested
if-else statement.
What to turn in: Create a folder HW2Q1 in CourseFiles and upload the following:
1. The Design. Raptor flowchart.
2. Script file. Start a script session. Display the program source (.cpp file). Compile the program,
1
Min