SCHEME CODING please note SCHEME and not C or other languages.. Resistors are co
ID: 3579258 • Letter: S
Question
SCHEME CODING please note SCHEME and not C or other languages..
Resistors are common electronic components used to regulate the current in a circuit. Resistance is expressed in Ohms; resistors are color coded using four colored strips. The first three strips represent the base value of the resistor and the fourth strip represents the multiplier. The table below holds the translation key. For example, a resistor with the first three bands red-yellow-black will have a base value of 240. The fourth band is red, which means that the multiplier is 10^2 = 100. Write a program that translates a resistor's color coding to a value. For example: (resistance '(red yellow black red)); Value: 24000Explanation / Answer
#include #include #include void print_codes( void ); /* menu of codes */ double decode_char( char code ); main() { char code1, code2, code3; /* one code per band */ double resistance; double color1, color2, color3; /* decoded values */ int flag; /* Print codes and prompt for user input. */ print_codes(); printf( " Enter three codes. " ); /* Read three character codes. */ code1 = getchar(); code2 = getchar(); code3 = getchar(); /* Decode each character code. */ color1 = decode_char( code1 ); color2 = decode_char( code2 ); color3 = decode_char( code3 ); /* Check whether codes were legal. */ if ( color1 == -999.0 || color2 == -999.0 || color3 == -999.0 ) printf( " Bad code -- cannot compute resistance " ); /* If codes were legal, compute and print resistance in ohms. */ else { resistance = ( 10.0 * color1 + color2 ) * pow( 10.0, color3 ); printf( " Resistance in ohms: %f ", resistance ); } return EXIT_SUCCESS; } /* This function prints a menu of color codes to guide the user in entering input. */ void print_codes( void ) { printf( " The colored bands are coded as follows: " ); printf( "COLOR CODE " ); printf( "----- ---- " ); printf( " Black-------------------> B " ); printf( " Brown-------------------> N " ); printf( " Red---------------------> R " ); printf( " Orange------------------> O " ); printf( " Yellow------------------> Y " ); printf( " Green-------------------> G " ); printf( " Blue--------------------> E " ); printf( " Violet------------------> V " ); printf( " Gray--------------------> A " ); printf( " White-------------------> W " ); } /* This function expects a character (color code) and returns a double precision floating point number as its value. If the code is not legal, it returns a value that signals this fact. */ double decode_char( char code ) { switch ( code ) { case 'B': return 0.0; case 'N': return 1.0; case 'R': return 2.0; case 'O': return 3.0; case 'Y': return 4.0; case 'G': return 5.0; case 'E': return 6.0; case 'V': return 7.0; case 'A': return 8.0; case 'W': return 9.0; default: return -999.0; /* illegal code */ }Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.