I am trying to compare an user inputed resistance and tolerance, and find the cl
ID: 3696490 • Letter: I
Question
I am trying to compare an user inputed resistance and tolerance, and find the closest value from the standard EIA values which i already created in arrays. After determining the standard EIA value, i want to input that information back to the user. The information should include the Closest standard resistance, and the 4 color bands needed to make that resistance. My idea was to use the resistance to color function that has been tested to work, and then send those colors into the new function StandardRes, im which i could test band 1 + band 2 's value through the inputed tolerance range. Once it goes through the for loop from the tolerance, I thought it would set equal to one of the nominal values in the array , then i would multiply band 3 to the end of the new value. C++
using namespace std; //introduces namespace std
//EIA Standard array element sizes per tolerance class
const int E12 = 12; // 10% tolerance
const int E24 = 24; // 5% tolerance
const int E6 = 6; // 20% tolerance
const int nominalE12values[E12]={10, 12, 15, 18, 22, 27, 33, 39, 47, 56, 68, 82}; // 10% tolerance
const int nominalE24values[E24]={10,11,12, 13, 15, 16, 18, 20, 22, 24, 27, 30,
33, 36, 39, 43, 47, 51, 56, 62, 68, 75, 82, 91} ; // 5% tolerance
const int nominalE6values[E6]={10, 15, 22, 33, 47, 68}; // 20% tolerance
class Resistance
{
private:
double resValuee;
int tolValueee;
string cband1,cband2,cband3,cband4;
public:
int colorToResistancee(string,string,string,string,double&,int&); //member functions
int StandardRes(string&,string&,string&,string&,double&,int&);
int ResistanceToColor(double,int, string &, string &, string &, string &);
void maxminResistancee(double&,double&);
int main()
else if(answer=="b")
{
cout<<"Please enter the desired resistance value in ohms: ";
cin >> rr;
cout<< endl;
cout<<"Please enter the desired tolerance as a percentage (5, 10, or 20): ";
cin >> tt;
resistor.ResistanceToColor(rr,tt,bA,bB,bC,bD);
resistor.StandardRes(bA,bB,bC,bD,rr,tt);
cout<< endl;
cout << "Closest Standard Resistance: " << fixed << setprecision(0) << rr << " ohms " << endl;
cout<<"Color Band A: "<< bA << endl;
cout<<"Color Band B: "<< bB << endl;
cout<<"Color Band C: "<< bC << endl;
cout<<"Color Band D: "<< bD << endl;
}
int Resistance::StandardRes(string &b1 ,string &b2, string &b3, string &b4, double &resR, int &tolR)
{
cband1 = b1;
cband2 = b2;
cband3 = b3;
cband4 = b4;
if ( cband2 == "black" ) resValuee = 0;
if ( cband2 == "brown" ) resValuee = 1;
if ( cband2 == "red" ) resValuee = 2;
if ( cband2 == "orange" ) resValuee = 3;
if ( cband2 == "yellow" ) resValuee = 4;
if ( cband2 == "green" ) resValuee = 5;
if ( cband2 == "blue" ) resValuee = 6;
if ( cband2 == "violet" ) resValuee = 7;
if ( cband2 == "gray" ) resValuee = 8;
if ( cband2 == "white" ) resValuee = 9;
else return 2;
if ( cband1 == "black" );
if ( cband1 == "brown" ) resValuee += 10;
if ( cband1 == "red" ) resValuee += 20;
if ( cband1 == "orange" ) resValuee += 30;
if ( cband1 == "yellow" ) resValuee += 40;
if ( cband1 == "green" ) resValuee += 50;
if ( cband1 == "blue" ) resValuee += 60;
if ( cband1 == "violet" ) resValuee += 70;
if ( cband1 == "gray" ) resValuee += 80;
if ( cband1 == "white" ) resValuee += 90;
else return 1;
if ( cband4 == "gold" )
{
tolValueee = 5;
for(int i=1 ; i<25 ; i++)
{
if(resValuee == nominalE24values[i])
{
resValuee=nominalE24values[i];
}
if(resValuee == nominalE24values[i-1])
{
resValuee=nominalE24values[i-1];
}
if(resValuee>nominalE24values[i-1] && resValuee<nominalE24values[i])
{
resValuee=nominalE24values[i-1];
}
}
}
if ( cband4 == "silver" )
{
tolValueee = 10;
for(int i=1 ; i<13 ; i++)
{
if(resValuee == nominalE12values[i])
{
resValuee=nominalE12values[i];
}
if(resValuee == nominalE12values[i-1])
{
resValuee=nominalE12values[i-1];
}
if(resValuee>nominalE12values[i-1] && resValuee<nominalE12values[i])
{
resValuee=nominalE12values[i-1];
}
}
}
if ( cband4 == "none" )
{
tolValueee = 20;
for(int i=1 ; i<7 ; i++)
{
if(resValuee == nominalE6values[i])
{
resValuee=nominalE6values[i];
}
if(resValuee == nominalE6values[i-1])
{
resValuee=nominalE6values[i-1];
}
if(resValuee>nominalE6values[i-1] && resValuee<nominalE6values[i])
{
resValuee=nominalE6values[i-1];
}
}
}
else return 3;
if ( cband3 == "black" );
if ( cband3 == "brown" ) resValuee *= 10;
if ( cband3 == "red" ) resValuee *= 100;
if ( cband3 == "orange" ) resValuee *= 1000;
if ( cband3 == "yellow" ) resValuee *= 10000;
if ( cband3 == "green" ) resValuee *= 100000;
if ( cband3 == "blue" ) resValuee *= 1000000;
if ( cband3 == "violet" ) resValuee *= 10000000;
if ( cband3 == "gray" ) resValuee *= 100000000;
if ( cband3 == "white" ) resValuee *= 1000000000;
if ( cband3 == "silver" ) resValuee *= 0.1;
if ( cband3 == "gold" ) resValuee *= 0.01;
resR = resValuee;
tolR = tolValueee;
return 0;
}
here is an example of the test output that i should recieve:
Example 3 (assuming that the user already selected Option 2): Please enter the desired resistance value in ohms: 280000 Please enter the desired tolerance as a percentage (5, 10, or 20): 5 Results: Closest standard value = 270000 ohms Color band A: red Color band B: violet Color band C: yellow Color band C: gold Example 4 (assuming that the user already selected Option 2): Please enter the desired resistance value in ohms: 200000000 Please enter the desired tolerance as a percentage (5, 10, or 20): 5 Results: Invalid inputExplanation / Answer
Solution:
Add blocks for main method, and declare necessary variables which are used in this progrm. And then try it once again. Hope, we will get Correct output.
Here, I corrected some part of the program.
Program:
#include <iostream>
#include <string>
using namespace std;
//introduces namespace std
//EIA Standard array element sizes per tolerance class
const int E12 = 12; // 10% tolerance
const int E24 = 24; // 5% tolerance
const int E6 = 6; // 20% tolerance
const int nominalE12values[E12]={10, 12, 15, 18, 22, 27, 33, 39, 47, 56, 68, 82}; // 10% tolerance
const int nominalE24values[E24]={10,11,12, 13, 15, 16, 18, 20, 22, 24, 27, 30,33, 36, 39, 43, 47, 51, 56, 62, 68, 75, 82, 91} ; // 5% tolerance
const int nominalE6values[E6]={10, 15, 22, 33, 47, 68}; // 20% tolerance
class Resistance
{
private:
double resValuee;
int tolValueee;
string cband1, cband2, cband3, cband4;
public:
int colorToResistancee(string,string,string,string,double&,int&); //member functions
int StandardRes(string&,string&,string&,string&,double&,int&);
int ResistanceToColor(double,int, string &, string &, string &, string &);
void maxminResistancee(double&,double&);
int main()
{
int rr, tt;
cout<<"Please enter the desired resistance value in ohms: ";
cin >> rr;
cout<< endl;
cout<<"Please enter the desired tolerance as a percentage (5, 10, or 20): ";
cin >> tt;
resistor.ResistanceToColor(rr,tt,bA,bB,bC,bD);
resistor.StandardRes(bA,bB,bC,bD,rr,tt);
cout<< endl;
cout << "Closest Standard Resistance: " << fixed << setprecision(0) << rr << " ohms " << endl;
cout<<"Color Band A: "<< bA << endl;
cout<<"Color Band B: "<< bB << endl;
cout<<"Color Band C: "<< bC << endl;
cout<<"Color Band D: "<< bD << endl;
}
int Resistance::StandardRes(string &b1 ,string &b2, string &b3, string &b4, double &resR, int &tolR)
{
cband1 = b1;
cband2 = b2;
cband3 = b3;
cband4 = b4;
if ( cband2 == "black" ) resValuee = 0;
if ( cband2 == "brown" ) resValuee = 1;
if ( cband2 == "red" ) resValuee = 2;
if ( cband2 == "orange" ) resValuee = 3;
if ( cband2 == "yellow" ) resValuee = 4;
if ( cband2 == "green" ) resValuee = 5;
if ( cband2 == "blue" ) resValuee = 6;
if ( cband2 == "violet" ) resValuee = 7;
if ( cband2 == "gray" ) resValuee = 8;
if ( cband2 == "white" ) resValuee = 9;
else return 2;
if ( cband1 == "black" );
if ( cband1 == "brown" ) resValuee += 10;
if ( cband1 == "red" ) resValuee += 20;
if ( cband1 == "orange" ) resValuee += 30;
if ( cband1 == "yellow" ) resValuee += 40;
if ( cband1 == "green" ) resValuee += 50;
if ( cband1 == "blue" ) resValuee += 60;
if ( cband1 == "violet" ) resValuee += 70;
if ( cband1 == "gray" ) resValuee += 80;
if ( cband1 == "white" ) resValuee += 90;
else return 1;
if ( cband4 == "gold" )
{
tolValueee = 5;
for(int i=1 ; i<25 ; i++)
{
if(resValuee == nominalE24values[i])
{
resValuee=nominalE24values[i];
}
if(resValuee == nominalE24values[i-1])
{
resValuee=nominalE24values[i-1];
}
if(resValuee>nominalE24values[i-1] && resValuee<nominalE24values[i])
{
resValuee=nominalE24values[i-1];
}
}
}
if ( cband4 == "silver" )
{
tolValueee = 10;
for(int i=1 ; i<13 ; i++)
{
if(resValuee == nominalE12values[i])
{
resValuee=nominalE12values[i];
}
if(resValuee == nominalE12values[i-1])
{
resValuee=nominalE12values[i-1];
}
if(resValuee>nominalE12values[i-1] && resValuee<nominalE12values[i])
{
resValuee=nominalE12values[i-1];
}
}
}
if ( cband4 == "none" )
{
tolValueee = 20;
for(int i=1 ; i<7 ; i++)
{
if(resValuee == nominalE6values[i])
{
resValuee=nominalE6values[i];
}
if(resValuee == nominalE6values[i-1])
{
resValuee=nominalE6values[i-1];
}
if(resValuee>nominalE6values[i-1] && resValuee<nominalE6values[i])
{
resValuee=nominalE6values[i-1];
}
}
}
else return 3;
if ( cband3 == "black" );
if ( cband3 == "brown" ) resValuee *= 10;
if ( cband3 == "red" ) resValuee *= 100;
if ( cband3 == "orange" ) resValuee *= 1000;
if ( cband3 == "yellow" ) resValuee *= 10000;
if ( cband3 == "green" ) resValuee *= 100000;
if ( cband3 == "blue" ) resValuee *= 1000000;
if ( cband3 == "violet" ) resValuee *= 10000000;
if ( cband3 == "gray" ) resValuee *= 100000000;
if ( cband3 == "white" ) resValuee *= 1000000000;
if ( cband3 == "silver" ) resValuee *= 0.1;
if ( cband3 == "gold" ) resValuee *= 0.01;
resR = resValuee;
tolR = tolValueee;
return 0;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.