Write a C program that prompts for the colors of Band 1, Band 2, Band 3 and Band
ID: 3839617 • Letter: W
Question
Write a C program that prompts for the colors of Band 1, Band 2, Band 3 and Band 4, and then displays the resistance in ohms and the tolerance. Here is a sample run of the C program:
the resistor and tollerance storage is shown as follows
NOTE: the tollerance must be included in the output, and the Coloured bands must be defined as shown
A resistor is an electric circuit device component and it has a specific resistance value, where the resistance values are expressed in ohms (S). Resistons 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 one) (Band two) (Band three) (Band fou 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, if the first band is green, the second is black, the third is orange and the fourth is red, the resistor has a value of 50x102 with a tolerance of 2%. Color Value as digit Value as multiplier Tolerance (Bands one and two) (Band three) (Band four) Orange 10 10 Silver 10 No color 200Explanation / Answer
The program is given below:
#include<stdio.h>
#include<string.h>
#include<string.h>
#include<math.h>
#define NUM_COL 13
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)
};
struct resist_value RESIST[NUM_COL]={{"black" ,0 ,-1},{ "brown" ,1 ,1},{ "red" ,2 ,2},
{ "orange",3 ,-1},
{ "yellow",4 ,-1},
{ "green" ,5 ,0.5},
{ "blue" ,6 ,0.25},
{ "violet",7 ,0.1},
{ "grey" ,8 ,-1},
{ "white" ,9 ,-1},
{ "Gold" ,-1 ,5},
{ "Silver" ,-1 ,10},
{ "None" ,-1 ,20}
};
void print(int n)
{
int l=0,i,a[100],j=0;
for(i=n;i>=1;i=i/10)
l++;
for(i=0;i<l;i++,n=n/10)
a[l-1-i]=n%10;
for(i=0;i<l;i++)
{
if(j==3)
{
printf(" ");
j=0;
}
printf("%d",a[i]);
j++;
}
}
main()
{
int i,j;
float x[4],p;
char c[4][10],m;
struct resist_value r1;
do
{
for(i=0;i<4;i++)
x[i]=-2;
printf("Enter the colour of the resistor's four bands beginning with the band nearest the end. Type the colors in lower letters only. No CAPS. ");
for(i=0;i<4;i++)
{
printf("Band %d=>",i+1);
fflush(stdin);
gets(c[i]);
}
for(i=0;i<4;i++)
{
for(j=0;j<NUM_COL;j++)
{
if(strcmp(c[i],RESIST[j].COL)==0)
{
if(i<3)
x[i]=RESIST[j].VAL;
else
x[i]=RESIST[j].TOL;
}
if(strcmp(c[i],"")==0)
{
if(i<3)
x[i]=RESIST[12].VAL;
else
x[i]=RESIST[12].TOL;
}
}
}
if(x[0]<0 || x[1]<0 || x[2]<0 || x[3]<0)
{
if(x[0]<0 || x[1]<0 || x[2]<0)
printf("Invalid color(s) in either bands 1,2 or 3(resistor value):");
if(x[0]<0)
printf("%s",c[0]);
if(x[1]<0)
printf(" %s",c[1]);
if(x[2]<0)
printf(" %s",c[2]);
if(x[3]<0)
printf(" Invalid color for band 4(tolerance):%s",c[3]);
goto CHECK;
}
p=pow(10,x[2]);
printf(" Resistance value: ");
print((x[0]*10+x[1])*p);
printf(" ohms");
printf(" Tolerance: %.2f %%",x[3]);
CHECK: printf(" Do you want to decode another resistor(Y/N)? ");
fflush(stdin);
printf("=>");
scanf(" %c",&m);
}while(m!='N' && m!='n');
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.