+1(978)310-4246 credencewriters@gmail.com
Select Page

Description

Write a

n interactive

program to prompt the user

to enter the colors of bands 1 through 4 of a

resistor. Allow the user to enter in the

colors using words that are either upper or lower case,

and use functions provided by the header to work around this input (check out

strcasecmp

and

strncasecmp

for

an amazingly easy way to do it)

.

example:

Enter the colors of the resistorÃ¢â‚¬â„¢s three bands, staring with the one on

the left.

Band 1:

green

Band 2:

black

Band 3:

yellow

Band 4:

blank

Resistance value: 500 kilo

ohms.

Tolerance: 20%, or 400 kilo

ohms to 600 kilo

ohms variance

Do you want to decode another resister?

Y

Band 1:

BLUE

Band 2:

Grey

Band 3:

brown

Band 4:

Green

Resistance value: 680 ohms.

Tolera

nce: 0.5%, or 676.6 ohms to 683.4 ohms variance.

Do you want to decode another resister?

N

Lab Assignment 9
C Programming Spring 2021
Recommended Submission Time: Wednesday, April 7, 2021 at 7 PM
Due no later than: Wednesday, April 21, 2021 at 7 PM
Purpose of this Assignment
Ã¢â‚¬Â¢
Ã¢â‚¬Â¢
Ã¢â‚¬Â¢
Ã¢â‚¬Â¢
work with strings
search for a matching string
create a flexible user interface that handles errors when they occur
Prerequisites
1. Read through Chapter 8 in the textbook before attempting to complete this lab.
Background
A resister is an electronic circuit device designed to have
a specific resistance value between the two ends of the
device.
Resistance values are expressed in ohms ( ÃŽÂ© ), kilo-ohms (
Resistors are marked using a color-coding that signifies their resistance values.
We will focus on the inexpensive resistors (see the diagram below) which have three bands on
the left side. The first two bands represent digits, between 0 and 9, and the third band is a
power-of-ten multiplier. Thus, if the first band is blue and the second is grey, using the table
below, we see that is 68. Brown says to multiply that value by 10, resulting in a final value of
680.
If there is no tolerance band, which in this example is blank and therefore isnÃ¢â‚¬â„¢t specified, the
default tolerance is 20%. Also, if the cell in the tolerance column is blank, as it is for orange,
yellow, and white, it is also classified as a default tolerance. You can see a web-based program
that does this (which you can use to confirm your program is working correctly) at
http://resistor.cherryjourney.pt
Problem Specification
Write an interactive program to prompt the user to enter the colors of bands 1 through 4 of a
resistor. Allow the user to enter in the colors using words that are either upper or lower case,
and use functions provided by the header to work around this input (check out
strcasecmp and strncasecmp for an amazingly easy way to do it).
Here’s a sample of how the program might work:
Enter the colors of the resistorÃ¢â‚¬â„¢s three bands, staring with the one on
the left.
Band 1: green
Band 2: black
Band 3: yellow
Band 4: blank
Resistance value: 500 kilo-ohms.
Tolerance: 20%, or 400 kilo-ohms to 600 kilo-ohms variance
Do you want to decode another resister? Y
Band 1: BLUE
Band 2: Grey
Band 3: brown
Band 4: Green
Resistance value: 680 ohms.
Tolerance: 0.5%, or 676.6 ohms to 683.4 ohms variance.
Do you want to decode another resister? N
Use the following global constant for comparing your color codes:
const char COLOR_CODES [10] [7] = {
“black”, “brown”, “red”, “orange”,
“yellow”, “green”, “blue”, “violet”,
“grey”,
“white” };
NOTE:
You can put this in a header file if you want (makes sense, since you need it in several places),
however, since it’s a character array and not a symbolic constant, we need to do the following:
a) Define a symbolic constant called IN_MAIN at the top of your main() function, after the stdin.h
and stdlib.h headers, but before the other includes.
b) In your header file, use preprocessor IF statements to check if you’re in main or if you’re in
another file.
#ifdef MAIN
const char COLOR_CODES [10] [7] = {
#else
extern char COLOR_CODES [10] [7];
#endif
“black”, “brown”, “red”, “orange”,
“yellow”, “green”, “blue”, “violet”,
“grey”,
“white” };
1. Write a function that traverses COLOR_CODES and displays the colors on the screen. This is
to help the user who enters an invalid color code when prompted.
2. Write a function
char *input_color_code( char *prompt );
to prompt for a single string containing a color and confirm, upon entry of each string, it is a valid
color code. The prompt is just going to be “Band 1: “, “Band 2: “, etc.
That is, if the user enters an invalid color code, advise them of their mistake in this function and
allow them to renter it, such as in the following example:
Enter the colors of the resistorÃ¢â‚¬â„¢s three bands, staring with the one on the left.
Band 1: breen
I’m sorry, but “breen” is not a valid color code.
Valid color codes are
black brown red orange yellow green blue violet grey white
Band 1: green
Band 2: Ã¢â‚¬Â¦
3. Use a function with the prototype below to return band1, band2, band3, and band4 are valid
colors. As long as they’re invalid, continue to prompt for them to be re-entered until they are
proper valid colors.
void input_band_colors( char band1[ ], char band2[ ], char band3[ ], char band4[ ] );
4. Create a function to calculate the resistance value, such as
int calculate_resistance( char band1[ ], char band2[ ], char band3[ ], char band4[ ] );
5. Create a function to calculate the tolerance
void calculate_tolerances( int resistance, double tolerance,
double *low, double *high );
6. Create a function to print out the results.
You can also create as many other functions as you want.
You can alter the function prototypes if necessary, but it should only be done along with an
explanation of why you needed/wanted to make the change