Write a simple program for this question: Hint: You could use the following data
ID: 3836885 • Letter: W
Question
Write a simple program for this question:
Hint:
You could use the following data structure for the colour bands in the resistor.
As the resistance computation is involved with colour bands, resistance values and tolerances (illustrated in the Table in Question 2), we can define the data structure with the three members and each member is corresponding to the colour bands , resistance values and tolerances respectively.We can also initialize an array to store the contents of the resistor with respect to the three members.
Here is the data structure, resist_value, and the initialized array, RESIST[11]
By the way, you don't have to use the data structure, You could have your own approach. Please give me the full code for this program. Thanks!
A resistor is an electric circuit device component and it has a specific resistance value, where the resistance values are expressed in ohms (2). Resistors are generally marked with four colored bands that encode their resistance values which are shown in the following figure. First digit Second digit Multiplier olerance (Band une (Band two (Band three) (Band four) The first two bands (Band one and Band two are the first and the second digits. The third band (Band three is a power-of-ten multiplier. The fourth band (Band four indicates the tolerance of the resistor which is the percentage deviation of the desired resistance value, measured at 25 C. The table below shows the meanings of each band color. For example, ifthe first band is green, the second is black, the third is orange and the fourth is red, the resistor has a value of 50x103Q with a tolerance of 2 Color Value as digit Value as multiplier Tolerance Bands one and two) (Band three) Band four) Brown 10 10 10 Orange 4 101 Yellow Green 10P Blue 10 0.25 10 0.1 Violet Grey 100s White 10 Gold 10 Silver I No Color 2200Explanation / Answer
#include<stdio.h>
#include<string.h>
#include<math.h>
struct resist_value { char COL[10]; // COLOR_CODES_TOL for resistor band colours
int VAL; // VAL for resistor band values (for bands one to three)
float TOL; // TOL for resistor tolerance (for band four)
};
// The colour bands, band values and tolerances are defined in the array COLOR[10]
struct resist_value RESIST[13] = {{"black" ,0 ,-1}, // ’-1* means no tolerance is illustrated for the colour
{ "brown" ,1,1},
{"red" ,2,2},
{"orange",3,-1}, // ’-l' means no tolerance is illustrated for the colour
{"yellow",4,-1}, // ’-l' means no tolerance is illustrated for the colour
{"green",5,0.5},
{"blue",6,0.25},
{"violet",7,0.1},
{"grey",8,-1}, // ‘-l’ means no tolerance is illustrated for the colour
{"white",9,-1}, // ‘-1' means no tolerance is illustrated for the colour
{"gold",-1,5}, // '-1' means no tolerance is illustrated for the colour
{"silver" ,-1 ,10}, // '-1' means no tolerance is illustrated for the colour
{ "",-1 ,20} // "" and -1 means no colour band;
};
int main() {
char band1[10], band2[10], band3[10], band4[10];
int b[3] = {0}, valid = 1, i;
double tol;
long res;
char ch;
do {
printf("Enter the colors of the resistor's four bands, beginning with the band nearest the end. Type the colors in lowercase letters only, NO CAPS. ");
printf("Band 1 => ");
scanf("%s", band1);
printf("Band 2 => ");
scanf("%s", band2);
printf("Band 3 => ");
scanf("%s", band3);
printf("Band 4 => ");
scanf("%s", band4);
for (i = 0; i<13; i++)
if (strcmp(RESIST[i].COL, band1) == 0)
b[0] = RESIST[i].VAL;
for (i = 0; i<13; i++)
if (strcmp(RESIST[i].COL, band2) == 0)
b[1] = RESIST[i].VAL;
for (i = 0; i<13; i++)
if (strcmp(RESIST[i].COL, band3) == 0)
b[2] = RESIST[i].VAL;
for (i = 0; i<13; i++)
if (strcmp(RESIST[i].COL, band4) == 0)
tol = RESIST[i].TOL;
res = (b[0]*10 + b[1]) * pow(10, b[2]);
if (b[0] <=0 || b[1] <= 0 || b[2] <= 0) { //chk if res bands are valid
printf("Invalid color(s) in either bands 1, 2 or 3 (resistor value): "); // if invalid print the fixed part of the line
if (b[0] <= 0) //then print the colors
printf("%s ",band1);
if (b[1] <= 0)
printf("%s ",band2);
if (b[2] <= 0)
printf("%s ",band3);
valid = 0;
printf(" ");
}
if (tol <= 0) {
printf("Invalid color for band 4 (tolerance): %s", band4);
valid = 0;
printf(" ");
}
if (valid) {
printf("Reseistance value: %d ohms ",res);
printf("Tolerance value: %lf % ",tol);
}
printf("Do you wane zo decode another resistor (Y/N)? => ");
ch = getchar();
}while(ch == 'y');
}
I hope this code helps you. If you need any assistance, please let me know. I shall try my best to resolve all your issues.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.