Question: art for each case. Turn this in separately in by creating a folder ExtraCreditHW2 within your CourseFiles folder; upload the flowchart and the written analysis.

art for each case. Turn this in separately in by creating a folder ExtraCreditHW2 within your CourseFiles folder; upload the flowchart and the written analysis.

Lab assignment 2: Implementing conditionals using an if/else if statement Due date Monday Jan 23, 2:00 PM.

Objective: Become familiar with designing a solution that requires a series of condition checks. Learn how to translate such a design into code using the if/else if statements in C++.

Note: Question 1 (pre-lab component) has to be completed before you come to the lab.

One of the common uses of the if/else if statement is to determine the range in which a given input value falls. (This problem falls into the category of point location problems; this is a one-dimensional point location problem). In this exercise, we convert a floating point input that specifies a wavelength in nanometers, into the appropriate color of the visible spectrum. The input to the program is a real value that lies between 400.0 and 700.0. The color conversions are: 400 V iolet < 445; 445 Indigo < 475; 475 Blue < 510; 510 Green < 570; 570 Y ellow < 590; 590 Orange < 650; 650 Red < 700.

Assumption: We shall assume for now that the input always falls within this range (the user of the program must take on the responsibility of ensuring that the input is correct, and if the input is outside the range, the program is not expected to give correct answers; see the second sample execution).

The entire range of input values can be represented by the following structure:

400...445...475...510...570...590...650...700

The lowest possible input value is 400.0 and the highest possible value is 700.0 Each of the inbetween numbers (such as 445, 475) serves as a divider; put together, they partition the entire range into 7 sub-ranges.

We can write a conditional statement to achieve this partition. Note that at each step we are comparing the input with one of the dividers, thus narrowing the range within we much we must continue the search.

Strategy. Our strategy is as follows: Compare the input number with the first divider(445). If the number is smaller than the divider, we print the answer Violet; if not we continue with the next divider.

Sample execution(good input):

Please input a wavelength between 400.0 and 700.0: 475.0

Your wavelength corresponds to the color Blue Thank-you for using our program!

Sample execution(input outside the range):

Please input a wavelength between 400.0 and 700.0: 823.0

1

Your wavelength corresponds to the color Red Thank-you for using our program!

input < 445?

< 700

400 <=

input

Output

Violet

Y

N

< 700

445 <=

input

(

input is between 400

and 700)

(

if input is not less than 445, then

input must be between 445 and 700)

(

input between

400

and

445)

Input < 475?

Figure 1: Partial flowchart for the lab depicting conditional to find color of given visible wavelength

(Pre-lab component; to be completed before the lab meeting) Question 1: Using Raptor, complete the partial labeled flowchart for this problem from Figure 1. Each arrow has a label that specifies the possible range of values that the input can have when execution reaches that point. For instance, before the first branch, we have the expression 400 input < 700. On the outgoing arrows, we have the labels 400 input < 445 (which gives us the output Violet) and 445 input < 700 (which deals with all the other possible colors). Simulate your flowchart using Raptor and verify that your design is correct. Take a printout of your flowchart.

Note: Each conditional expression must compare the input value against one divider only.

Question 2: Implement the above flowchart in a C++ program using if/else if (not the nested if-else) statements.

What to submit: Within your starid folder in CourseFiles, create a folder named Lab2. Upload the raptor file, your source code, and a script showing the tests.

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!