### resistor color code

ok. so i'm doing a school project where i have to make a program that:

1. Asks the user to enter
(a) A minimum resistance value RMIN.
(b) A maximum resistance value RMAX.
2. Computes, and prints to standard output,
(a) The mean (average) resistance value
R =1/2(RMIN + RMAX)
(b) A, B, and C, as described above.
(c) D: The tolerance percentage
TOL = (100*(RMAX - R))/R
3. Creates an image file called resistor.ppm, 200 pixels high, made up
of the following 10 or 11 vertical columns, each 20 pixels wide:
(a) Beige background. (Beige is R=.96, G=.96, B=.86.)
(b) A’s color code.
(c) Beige background.
(d) B’s color code.
(e) Beige background.
(f) C’s color code.
(g) Beige background, repeated twice. This second column creates a
wider gap between C and D.
(h) D’s color code, if it is gold or silver; if not, this last column is omitted.
(i) Beige background, repeated twice again

i have:

#include <iostream>
#include "csufmedia.h"
using namespace std;
int main ()
{
make_canvas(1, 200);

int A, B, C, D;
double tol, Rmin, Rmax, Ravg, R;

cout << "Enter a minimum value: ";
cin >> Rmin;
cout << "Enter a maximum value: ";
cin >> Rmax;
R = (Rmin + Rmax)/(2); //computing the average resistance
cout << "Resistance average = " << R << endl;

cout << "Enter a number 0-9 for A: ";
cin >> A;
cout << "Enter a number 0-9 for B: ";
cin >> B;
cout << "Enter a number 0-9 for C: ";
cin >> C;
cout << "Enter a number for D: ";
cin >> D;
tol = ((100*(Rmax - R))/(R)); //computes the tolerance
cout << "Tolerance = " << tol << endl;

cout << "Minimum resistance: " << Rmin << endl;
cout << "Maximum resistance: " << Rmax << endl;
cout << "A= " << A << endl;
cout << "B= " << B << endl;
cout << "C= " << C << endl;
cout << "D= " << D << endl;

switch(A)
{
case 0:
break;
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
case 6:
break;
case 7:
break;
case 8:
break;
case 9:
break;
default:
cout << "That is not a number 0-9";
}

switch(B)
{
case 0:
break;
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
case 6:
break;
case 7:
break;
case 8:
break;
case 9:
break;
default:
cout << "That is not a number 0-9";
}

switch(C)
{
case 0:
break;
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
case 6:
break;
case 7:
break;
case 8:
break;
case 9:
break;
default:
cout << "That is not a number 0-9";
}

if(tol >= 0 || tol <= 5)
{
}
else
if(tol >= 5.001 || tol <= 10)
{
}
else
if(tol >= 10.001 || tol <= 20)
{
add_column(20, .96, .96, .86); //none so it defaults to background color, beige
}

save_canvas("resistor.ppm");

return 0;
}

and when i check the ppm, the ones where D is supposed to be silver or none/beige turns out gold. could it be something with the if/else?
Firstly, my omnipresent request to please edit your so it uses code tags the <> button on the right

With this code & the switches:

 ``123456789101112`` ``````switch(A) { case 0: add_column(20, .96, .96, .86); //beige add_column(20, 0, 0, 0); //black add_column(20, .96, .96, .86); //beige break; case 1: add_column(20, .96, .96, .86); //beige add_column(20, .65, .16, .16); //brown add_column(20, .96, .96, .86); //beige break; ``````

Each case has the colour beige for items 1 & 3, while the other is some other colour.

Can you write a function that takes the other colour as an argument, and carries out the 3 add_column function calls? You can do something similar for the other switches. This will improve the code considerably.

 and when i check the ppm, the ones where D is supposed to be silver or none/beige turns out gold. could it be something with the if/else?

`if(tol >= 0 || tol <= 5)`

Use the && oprator instead of the OR operator.

Am interested to see how you go, can you post your code again after you make the changes? Don't for get the code tags

Edit:

There could be a problem with your if statements, because of the comparison of the double types.

Floating Point (floats & doubles) are represented as binary fractions and not all real numbers can be represented exactly. The problem with this code:

 ``123456`` ``````if(tol >= 0 || tol <= 5) { //code } else if (tol >= 5.001 || tol <= 10) { //code }``````

The `tol <= 5.0` will fail if tol is 5.0, because tol is probably something like 5.0 + 1e-15, so the equality test fails. It is not caught with the next else if clause, because of the `tol >= 5.001`

The answer to al this is to make use of `numeric_limits<double>epsilon`. This is the distance to the next representable number greater than 1.0. It needs to be scaled up for the number being compared. To check for equality, you need to work out the absolute value of the difference between your number and the comparison number. Then compare this to the scaled up epsilon.

Last edited on
awesome thanks! i just changed
 ``12345678910111213141516171819`` ``````if(tol >= 0 || tol <= 5) { add_column(20, 1, .83, 0); //gold add_column(20, .96, .96, .86); //beige add_column(20, .96, .96, .86); //beige } else if(tol >= 5.001 || tol <= 10) { add_column(20, .75, .75, .75); //silver add_column(20, .96, .96, .86); //beige add_column(20, .96, .96, .86); //beige } else if(tol >= 10.001 || tol <= 20) { add_column(20, .96, .96, .86); //none so it defaults to background color, beige add_column(20, .96, .96, .86); //beige }``````

to

 ``12345678910111213141516171819`` ``````if(tol >= 0 && tol <= 5) { add_column(20, 1, .83, 0); //gold add_column(20, .96, .96, .86); //beige add_column(20, .96, .96, .86); //beige } else if(tol >= 5.001 && tol <= 10) { add_column(20, .75, .75, .75); //silver add_column(20, .96, .96, .86); //beige add_column(20, .96, .96, .86); //beige } else if(tol >= 10.001 && tol <= 20) { add_column(20, .96, .96, .86); //none so it defaults to background color, beige add_column(20, .96, .96, .86); //beige }``````

and it works perfect. um, we haven't learned `numeric_limits<double>epsilon` in class yet so that's why i didn't use it and the numbers i used in the if/else where given to me in the instructions

so now my code looks like

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220`` ``````#include #include "csufmedia.h" using namespace std; int main () { make_canvas(1, 200); int A, B, C, D; double tol, Rmin, Rmax, Ravg, R; cout << "Enter a minimum value: "; cin >> Rmin; cout << "Enter a maximum value: "; cin >> Rmax; R = (Rmin + Rmax)/(2); //computing the average resistance cout << "Resistance average = " << R << endl; cout << "Enter a number 0-9 for A: "; cin >> A; cout << "Enter a number 0-9 for B: "; cin >> B; cout << "Enter a number 0-9 for C: "; cin >> C; cout << "Enter a number for D: "; cin >> D; tol = ((100*(Rmax - R))/(R)); //computes the tolerance cout << "Tolerance = " << tol << endl; cout << "Minimum resistance: " << Rmin << endl; cout << "Maximum resistance: " << Rmax << endl; cout << "A= " << A << endl; cout << "B= " << B << endl; cout << "C= " << C << endl; cout << "D= " << D << endl; switch(A) { case 0: add_column(20, .96, .96, .86); //beige add_column(20, 0, 0, 0); //black add_column(20, .96, .96, .86); //beige break; case 1: add_column(20, .96, .96, .86); //beige add_column(20, .65, .16, .16); //brown add_column(20, .96, .96, .86); //beige break; case 2: add_column(20, .96, .96, .86); //beige add_column(20, 1, 0, 0); //red add_column(20, .96, .96, .86); //beige break; case 3: add_column(20, .96, .96, .86); //beige add_column(20, 1, .65, 0); //orange add_column(20, .96, .96, .86); //beige break; case 4: add_column(20, .96, .96, .86); //beige add_column(20, 1, 1, 0); //yellow add_column(20, .96, .96, .86); //beige break; case 5: add_column(20, .96, .96, .86); //beige add_column(20, 0, 1, 0); //green add_column(20, .96, .96, .86); //beige break; case 6: add_column(20, .96, .96, .86); //beige add_column(20, 0, 0, 1); //blue add_column(20, .96, .96, .86); //beige break; case 7: add_column(20, .96, .96, .86); //beige add_column(20, .93, .51, .93); //violet add_column(20, .96, .96, .86); //beige break; case 8: add_column(20, .96, .96, .86); //beige add_column(20, .50, .50, .50); //gray add_column(20, .96, .96, .86); //beige break; case 9: add_column(20, .96, .96, .86); //beige add_column(20, 1, 1, 1); //white add_column(20, .96, .96, .86); //beige break; default: cout << "That is not a number 0-9"; } switch(B) { case 0: add_column(20, 0, 0, 0); //black add_column(20, .96, .96, .86); //beige break; case 1: add_column(20, .65, .16, .16); //brown add_column(20, .96, .96, .86); //beige break; case 2: add_column(20, 1, 0, 0); //red add_column(20, .96, .96, .86); //beige break; case 3: add_column(20, 1, .65, 0); //orange add_column(20, .96, .96, .86); //beige break; case 4: add_column(20, 1, 1, 0); //yellow add_column(20, .96, .96, .86); //beige break; case 5: add_column(20, 0, 1, 0); //green add_column(20, .96, .96, .86); //beige break; case 6: add_column(20, 0, 0, 1); //blue add_column(20, .96, .96, .86); //beige break; case 7: add_column(20, .93, .51, .93); //violet add_column(20, .96, .96, .86); //beige break; case 8: add_column(20, .50, .50, .50); //gray add_column(20, .96, .96, .86); //beige break; case 9: add_column(20, 1, 1, 1); //white add_column(20, .96, .96, .86); //beige break; default: cout << "That is not a number 0-9"; } switch(C) { case 0: add_column(20, 0, 0, 0); //black add_column(20, .96, .96, .86); //beige add_column(20, .96, .96, .86); //beige break; case 1: add_column(20, .65, .16, .16); //brown add_column(20, .96, .96, .86); //beige add_column(20, .96, .96, .86); //beige break; case 2: add_column(20, 1, 0, 0); //red add_column(20, .96, .96, .86); //beige add_column(20, .96, .96, .86); //beige break; case 3: add_column(20, 1, .65, 0); // add_column(20, .96, .96, .86); //beige add_column(20, .96, .96, .86); //beige break; case 4: add_column(20, 1, 1, 0); //yellow add_column(20, .96, .96, .86); //beige add_column(20, .96, .96, .86); //beige break; case 5: add_column(20, 0, 1, 0); //green add_column(20, .96, .96, .86); //beige add_column(20, .96, .96, .86); //beige break; case 6: add_column(20, 0, 0, 1); //blue add_column(20, .96, .96, .86); //beige add_column(20, .96, .96, .86); //beige break; case 7: add_column(20, .93, .51, .93); //violet add_column(20, .96, .96, .86); //beige add_column(20, .96, .96, .86); //beige break; case 8: add_column(20, .50, .50, .50); //gray add_column(20, .96, .96, .86); //beige add_column(20, .96, .96, .86); //beige break; case 9: add_column(20, 1, 1, 1); //white add_column(20, .96, .96, .86); //beige add_column(20, .96, .96, .86); //beige break; default: cout << "That is not a number 0-9"; } if(tol >= 0 && tol <= 5) { add_column(20, 1, .83, 0); //gold add_column(20, .96, .96, .86); //beige add_column(20, .96, .96, .86); //beige } else if(tol >= 5.001 && tol <= 10) { add_column(20, .75, .75, .75); //silver add_column(20, .96, .96, .86); //beige add_column(20, .96, .96, .86); //beige } else if(tol >= 10.001 && tol <= 20) { add_column(20, .96, .96, .86); //none so it defaults to background color, beige add_column(20, .96, .96, .86); //beige } save_canvas("resistor.ppm"); return 0; }``````
 and it works perfect.

Really? How many values did you test it with? Testing doesn't mean trying a few values. To give you an idea, I write code to test my functions, so I can test lots of values, and especially values which have potential to fail, or are near boundaries - in this case values of 5 & 10.

 and the numbers i used in the if/else where given to me in the instructions

That may be so, but you have holes in the logic, because any number between 5.0 and 5.001 & 10.0 and 10.001 "falls through" . That is something that should be fixed.

BTW else if looks like this:
 ``1234567891011121314151617`` ``````if(tol >= 0 && tol <= 5) { add_column(20, 1, .83, 0); //gold add_column(20, .96, .96, .86); //beige add_column(20, .96, .96, .86); //beige } else if(tol >= 5.001 && tol <= 10) { add_column(20, .75, .75, .75); //silver add_column(20, .96, .96, .86); //beige add_column(20, .96, .96, .86); //beige } else if(tol >= 10.001 && tol <= 20) { add_column(20, .96, .96, .86); //none so it defaults to background color, beige add_column(20, .96, .96, .86); //beige } else { //fix errors here }``````

With what I was saying about reducing down the switches:

 ``12345678910111213`` ``````add_column(20, .96, .96, .86); //beige switch(A) { case 0: add_column(20, 0, 0, 0); //black break; case 1: add_column(20, .65, .16, .16); //brown break; //similar for other cases } //end of switch add_column(20, .96, .96, .86); //beige ``````

You could do something similar for the other switches.

I notice you don't do anything with variable D.
@TheIdeasMan:
> BTW else if looks like this:
 ``1234`` ``````if( condition ) statement [ else statement ]``````

@OP: you don't seem to understand what `else' means.
 ``1234`` ``````if( n<42 ) foo(); else //here n>=42, you don't need to recheck that bar();``````

@ne555

Not really my formatting rules.

 ``12345678`` ``````if( condition ) statements else if( condition ) statements else if( condition ) statements ``````

I was proposing:

 ``12345678`` ``````if( condition ) statements else if( condition ) statements else if( condition ) statements else statements ``````

This was because the OP probably wasn't aware of the `else if` clause, and the `else` to catch any other value.
Weren't you suppose to figure out how how to compute the values of A, B, and C instead of inputting them?
well the instructions for the project was a little unclear. i know that if/else should look like

 ``12345678`` ``````if( condition ) statements else if( condition ) statements else if( condition ) statements else statements ``````

but i was only given the table

Tolerance Color R G B
0–5% gold 1 .83 0
5.001–10% silver .75 .75 .75
10.001-20% (none) – – –

i thought of doing
 ``123456789101112131415161718`` ``````if(tol >= 0 && tol <= 5) { add_column(20, 1, .83, 0); //gold add_column(20, .96, .96, .86); //beige add_column(20, .96, .96, .86); //beige } else if(tol >= 5.001 && tol <= 10) { add_column(20, .75, .75, .75); //silver add_column(20, .96, .96, .86); //beige add_column(20, .96, .96, .86); //beige } else { add_column(20, .96, .96, .86); //none so it defaults to background color, beige add_column(20, .96, .96, .86); //beige }``````

but in the table for none the tolerance is 10.001-20%, so....

@TheIdeasMan:
the professor wants us to turn in the
 Input and output, and the corresponding PPM file, for the three examples given earlier.
and when i tried those, it worked.
i'm fairly new at C++ so i didn't realize that i could reduce the switch statements to
 ``1234567891011`` ``````add_column(20, .96, .96, .86); //beige switch(A) { case 0: add_column(20, 0, 0, 0); //black break; case 1: add_column(20, .65, .16, .16); //brown break; } add_column(20, .96, .96, .86); //beige ``````

@killax:
.....i hate you....but love you for pointing that out. now i know where that other equation comes in. crud. that changes the first part then.....

so now new question:
i'm given the equation A.B x 10^C +- D%, i assume that i'll be using the Rmin and Rmax values that the user inputs to figure out A, B, and C. but since the professor hasn't gone over base and exponent. can anyone help or refer me to something that can help me?
Last edited on
To compute exponents, you can use the pow function, which is found in the math library -` #include<cmath> `

HTH
To find C, you must put #include <math.h>
the formula for C is C=floor(log10(R));
i don't know about A and B though.
i got it to where it computes C right but i don't know how to get A and B. so far i've got:
 ``123456789101112131415`` `````` int A, B, C, D, AB; double Rmin, Rmax, R; cout << "Enter a minimum value: "; cin >> Rmin; cout << "Enter a maximum value: "; cin >> Rmax; R = (Rmin + Rmax)/(2); //computing the average resistance cout << "Resistance average = " << R << endl; D = ((100*(Rmax - R))/(R)); //computes the tolerance C=floor(log10(R)); AB=D/pow(10.0, (C-1)); A=(AB/10); B=(AB%10); ``````

but A and B comes out as 0
Last edited on
`AB=D/(10^(C-1));`

I am guessing this is psuedo code. To calculate an exponent use the pow function.

`AB=D/(pow(10,(C-1)));`

The ^ operator is the bitwise XOR operator, hence the answers of zero.

I find it always pays to check out all the functions / operators you are using, so you can understand how they work.

 http://www.cplusplus.com/doc/tutorial/operators/

HTH
i got it!
 ``123456789101112131415`` ``````int A, B, C, D, AB; double Rmin, Rmax, R; cout << "Enter a minimum value: "; cin >> Rmin; cout << "Enter a maximum value: "; cin >> Rmax; R = (Rmin + Rmax)/(2); //computing the average resistance cout << "Resistance average = " << R << endl; D = ((100*(Rmax - R))/(R)); //computes the tolerance C=floor(log10(R)); AB=R/pow(10.0,C-1); A=(AB/10); B=(AB%10); ``````

it works now :p
`#include <math.h> `

This is the C version. The following is the C++ version:

`#include<cmath> `

ok so it works now where A, B, and C is computed (thanks killax for pointing that out) so now it looks like this

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167`` ``````#include #include "csufmedia.h" #include using namespace std; int main () { make_canvas(1, 200); int A, B, C, D, AB; double Rmin, Rmax, R; cout << "Enter a minimum value: "; cin >> Rmin; cout << "Enter a maximum value: "; cin >> Rmax; R = (Rmin + Rmax)/(2); //computing the average resistance cout << "Resistance average = " << R << endl; D = ((100*(Rmax - R))/(R)); //computes the tolerance C=floor(log10(R)); AB=R/pow(10.0,C-1); A=(AB/10); B=(AB%10); cout << "Minimum resistance: " << Rmin << endl; cout << "Maximum resistance: " << Rmax << endl; cout << "A= " << A << endl; cout << "B= " << B << endl; cout << "C= " << C << endl; cout << "D= " << D << endl; add_column(20, .96, .96, .86); //beige switch(A) { case 0: add_column(20, 0, 0, 0); //black break; case 1: add_column(20, .65, .16, .16); //brown break; case 2: add_column(20, 1, 0, 0); //red break; case 3: add_column(20, 1, .65, 0); //orange break; case 4: add_column(20, 1, 1, 0); //yellow break; case 5: add_column(20, 0, 1, 0); //green break; case 6: add_column(20, 0, 0, 1); //blue break; case 7: add_column(20, .93, .51, .93); //violet break; case 8: add_column(20, .50, .50, .50); //gray break; case 9: add_column(20, 1, 1, 1); //white break; default: cout << "That is not a number 0-9"; } add_column(20, .96, .96, .86); //beige switch(B) { case 0: add_column(20, 0, 0, 0); //black break; case 1: add_column(20, .65, .16, .16); //brown break; case 2: add_column(20, 1, 0, 0); //red break; case 3: add_column(20, 1, .65, 0); //orange break; case 4: add_column(20, 1, 1, 0); //yellow break; case 5: add_column(20, 0, 1, 0); //green break; case 6: add_column(20, 0, 0, 1); //blue break; case 7: add_column(20, .93, .51, .93); //violet break; case 8: add_column(20, .50, .50, .50); //gray break; case 9: add_column(20, 1, 1, 1); //white break; default: cout << "That is not a number 0-9"; } add_column(20, .96, .96, .86); //beige switch(C) { case 0: add_column(20, 0, 0, 0); //black break; case 1: add_column(20, .65, .16, .16); //brown break; case 2: add_column(20, 1, 0, 0); //red break; case 3: add_column(20, 1, .65, 0); //orange break; case 4: add_column(20, 1, 1, 0); //yellow break; case 5: add_column(20, 0, 1, 0); //green break; case 6: add_column(20, 0, 0, 1); //blue break; case 7: add_column(20, .93, .51, .93); //violet break; case 8: add_column(20, .50, .50, .50); //gray break; case 9: add_column(20, 1, 1, 1); //white break; default: cout << "That is not a number 0-9"; } add_column(20, .96, .96, .86); //beige add_column(20, .96, .96, .86); //beige if(D >= 0 && D <= 5) { add_column(20, 1, .83, 0); //gold add_column(20, .96, .96, .86); //beige add_column(20, .96, .96, .86); //beige } else if(D >= 5.001 && D <= 10) { add_column(20, .75, .75, .75); //silver add_column(20, .96, .96, .86); //beige add_column(20, .96, .96, .86); //beige } else if(D >= 10.001 && D <= 20) { add_column(20, .96, .96, .86); //none so it defaults to background color, beige add_column(20, .96, .96, .86); //beige } save_canvas("resistor.ppm"); return 0; }``````

thanks for the help. is there anything else that i can improve?
 ```Tolerance Color R G B 0–5% gold 1 .83 0 5.001–10% silver .75 .75 .75 10.001-20% (none) – – –```

Just going back to my earlier comments about the holes in the logic for the output values.

Your professor may have given those numbers, but you could get brownie points for doing this properly & pointing out the problem. The 3 decimal places in those numbers is a clue to the expected precision of your output.

At the moment if variable D is anywhere between 5.0 and 5.001 or 10.0 and 10.001, no action is carried out. Also there is no else clause to catch error values for the value of variable D.

I am not sure that you understand how floating point values work.

They are stored as binary fractions, so that cannot represent all real values. For this reason, comparisons often fail:

 ``1234`` ``````float a = 0.1; // a == 0.09999997 float b = 10 * a; //b == 0.9999997 if (b == 1.0) //false ``````

Changing the type to double does not fix the problem.

To do the equality comparison properly, you need to work out the absolute value of your number less the comparison number. This needs to be compared to a scaled up epsilon value.

`numeric_limits<double>epsilon` is the difference between 1.0 and the next representable value and is about 1.0e-16. This value needs to be multiplied by the number you are dealing with.

The other alternative is to use a user precision ( in your case 0.001). You can follow the algorithm above using this precision rather than the epsilon value, but there can still be problems and you might find yourself having to use the epsilon value as well.

One potential problem is when you get values that are a smidgin less than zero (-1.0e-14 say) as opposed to a smidgin above zero. Note that a calculated double is almost never zero exactly, and it is exceedingly rare that whole numbers calculate exactly. So you need to check that you value is greater than 0.0 - epsilon.

I hope this shown some of the myriad of problems associated with floating point numbers (doubles & floats).
Last edited on
yes i realize that there will be holes in my code. i'm just learning C++ and we haven't gone over `numeric_limits<double>epsilon` in class. what i've been trying to say is that i can't use things in the code that i haven't even learned yet. my code is based off of what we have gone over.
OK, at least change the if conditions, so they form a contiguous range, and the else clause:

 ``123456789101112`` ``````if(D >= 0.0 && D <= 5.0) { } else if(D >= 5.0 && D <= 10.0) { } else if(D >= 10.0 && D <= 20.0) { } else { //catch any other value //error processing here }``````

You could still do what I was saying in my last post, but use you user precision of 0.001, that will catch most of the floating point errors. It's not that hard to do - and could get you brownie points from your professor.
Last edited on
here i am a new member and i have no any idea.... :(
@Troyque (1)

Don't make pointless posts like that, you could easily be reported to admin.

If you have a question, ask it. Or if you have something meaningful to contribute, then do it.

Topic archived. No new replies allowed.