Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Objectives: Become familiar with designing a solution that requires a series of condition checks. Learn how to translate such a design into code using the

Objectives: 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++.
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 Violet 445;445 Indigo 475;475 Blue 510;510 Green 570;570 Yellow
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:
400dots445dots475dots510dots570dots590dots650dots700
The lowest possible input value is 400.0 and the highest possible value is 700.0 Each of the in-
between 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!
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Pro SQL Server Administration

Authors: Peter Carter

1st Edition

1484207106, 9781484207109

More Books

Students also viewed these Databases questions