Answered step by step
Verified Expert Solution
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 ifelse if statements in
One of the common uses of the ifelse 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
onedimensional 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 and The color conversions are:
Violet ; Indigo ; Blue ; Green ; Yellow
; Orange ; Red
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:
dotsdotsdotsdotsdotsdotsdots
The lowest possible input value is and the highest possible value is Each of the in
between numbers such as serves as a "divider"; put together, they partition the entire
range into subranges.
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 If
the number is smaller than the divider, we print the answer "Violet"; if not we continue with the
next divider.
Sample executiongood input:
Please input a wavelength between and :
Your wavelength corresponds to the color Blue
Thankyou for using our program!
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started