Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am in C++ E sRTExtracr.docto: 2 OpenOffice Writer File Edit View Insert Format

ID: 3573431 • Letter: I

Question

I am in C++

E sRTExtracr.docto: 2 OpenOffice Writer File Edit View Insert Format Table Tools Window Help 150% ose Preview write a program that prompts for the colors of Band 1, Band 2, and Band 3, and then displays the resistance in kilo-ohms. Include a helper TABLE 7.6 Color Codes for Resistors? value as Multiplier Color Value as Digit Black Brown 10 102 Red 103 Orange Yellow 104 Green 10S Blue 10 Violet 10 108 Gray 109 White 2.Adapted from Sears and Zemansky's University Physics, 10th Ed. by Hugh D. Young and Roger A. Freedman, Addison-Wesley, 2000, p. 807. Search Windows A 4x 2/5/2016 50

Explanation / Answer

//main.c

#include <stdio.h>
#include <string.h>
#include <math.h>

int search(char **list, int size, char *target);

int main()
{
char *list[10] = {
"black", "brown", "red", "orange", "yellow",
"green", "blue", "violet", "gray", "white"
}; // list, in order
  
char another;
do
{
printf("Enter the colors of the resistor's three bands, beginning with "
"the band nearest the end. Type the colors in lower case letters "
"only, NO CAPS. ");
  
char band1[7], band2[7], band3[7];
  
printf("Band 1 => ");
scanf("%s", band1);
  
printf("Band 2 => ");
scanf("%s", band2);
  
printf("Band 3 => ");
scanf("%s", band3);

int 10, band1);
int two = search(list, 10, band2);
int three = search(list, 10, band3);

if(one > -1 && two > -1 && three > -1)
{
int resistance = 10 * one;
resistance = resistance + two;
resistance = resistance * pow(10, three) / 1000;
printf("Resistance value: %d kilo-ohms ", resistance);
} //endif all matched
else if (one < 0) // band 1 doesn't match
printf("Invalid color: %s ", band1);
else if (two < 0) // band 2 doesn't match
printf("Invalid color: %s ", band2);
else // three < 0 aka band 3 doesn't match
printf("Invalid color: %s ", band3);
  
printf("Do you want to decode another resistor? "
"=> ");
scanf(" %c", &another);
} while (another != 'n');

return 0;
} // main();

int search(char **list, int size, char *target)
{
int i;
for (i = 0; i < size; i++)
if (strcmp(list[i],target) == 0)
return i;
return -1;
} // search();