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

use class Program Description: Write a C++ program that begins by giving the use

ID: 3861209 • Letter: U

Question

use class

Program Description: Write a C++ program that begins by giving the user two options: The user can enter the 4 resistor color codes and the program will display the value of the resistance, tolerance, maximum resistance, and minimum resistance. The user can enter a desired resistor value and the program will determine the closest corresponding 4 color codes. Program Requirements: Write and use class Resistance as follows: Data members: include the following Resistance Tolerance 4 color bands Member functions: include the following Converting the color bands to resistance and tolerance Converting the resistance and tolerance to color bands Determine R max and R min given the resistance and tolerance Resistors are not commonly available in all values, but are typically available in standard values. Determine the nearest standard resistance value given the desired resistance and tolerance. The following link provides a table of standard values http://www.resistorguide.com/resistor-values/. Round up if the value is exactly half way between two standard values. Additional member function requirements: Each member function should provide error checking to make sure the inputs are valid Do not use global variables Do not display the results inside the function Give the end user the option to rerun the program and re-enter values Valid/invalid inputs are Option 1: Input colors should be accepted using any combination of upper and lower case Allow input color bands to corresponding to resistance values 0.1 ohm to 100,000,000 ohms (100M) Option 2: Allow input resistance values from 0.1 ohms to 100,000,000 ohms (100M) Only allow tolerances of 1 %, 5%, 10%, or 20% Standard resistor values; Standard resistors are generally available with the following values (multiplied by 10N) from 0.1 to 100 M For example, the overlap ranges for a 10% resistor values: k represents 0.9 - 1.1 k 1.2 k represents 1.08 - 1.32 k 1.5 k represents 1.08 - 1.65 k 1.8 k represents 1.62 - 1.98 k 2.2 k represents 1.98 – 2. 42 k E12 series (tolerance 10%) 0 12 15 18 22 27 33 39 47 56 68 82 E24 series (tolerance 5% and 1%) 10 11 12 13 15 16 18 20 22 24 27 30 33 36 39 43 47 51 56 62 68 75 82 91 E6 series (tolerance 20%) 10 15 22 33 47 68

Explanation / Answer

The solution was quite long, so managed to solve only option 1. Chegg also request you to post 1 question at a time. I tried my best to keep things as simple as possible for you and commented majority of the code!

#include<iostream>
#include<cmath>
#include<cstring>
using namespace std;

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)
};


double decode (char* band1, char* band2, char* band3, char* band4, double & tol) {
   int b[3] = {0}, valid = 1, i;
   double res;
  

   // The colour bands, band values and tolerances are defined in the array COLOR[10]
struct resist_value RESIST[13] = {{"black" ,0   ,-1}, // ’-9* means no tolerance is illustrated for the colour
{ "brown" ,1,1},
{"red" ,2,2},
{"orange",3,-9}, //   ’-9'   means   no   tolerance   is   illustrated   for   the   colour
{"yellow",4,-9}, //   ’-9'   means   no   tolerance   is   illustrated   for   the   colour
{"green",5,0.5},
{"blue",6,0.25},
{"violet",7,0.1},
{"grey",8,-9}, //   ‘-9’   means   no   tolerance   is   illustrated   for   the   colour
{"white",9,-9}, //   ‘-9'   means   no   tolerance   is   illustrated   for   the   colour
{"gold",-1,5}, // '-9' means no tolerance is illustrated for the colour
{"silver" ,-2 ,10}, // '-9' means no tolerance is illustrated for the colour
{"",-9 ,20} // "" and -9 means no colour band;
};


   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] == -9 || b[1] == -9 || b[2] == -9) {
       cout << "Invalid color(s) in either bands 1, 2 or 3 (resistor value): ";
       if (b[0] == -9)
           cout << band1;
       if (b[1] == -9)
           cout << band2;
       if (b[2] == -9)
           cout << band3;
       valid = 0;
   }
   if (tol <= 0) {
       cout << "Invalid color for band 4 (tolerance): " << band4;
       valid = 0;
   }

   if (valid) {
       return res;      
   }
   else return -1;


}


int main() {
   char band1[10], band2[10], band3[10], band4[10];
   double tol;
   double res;
   char ch;
   do {
   cout << "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. ";
   cout << "Band 1 => ";
   cin >> band1;
   cout << "Band 2 => ";
   cin >> band2;
   cout << "Band 3 => ";
   cin >> band3;
   cout << "Band 4 => ";
   cin >> band4;

   res = decode(band1, band2, band3, band4, tol);
   if (res > 0) {
       cout << "Resistance is :" << res << " ohms" << endl;
       cout << "Tolerance is :" << tol << "% "<< endl;
   }
   else
       cout << "Invalid" << endl;
    cout << "Do you wane zo decode another resistor (Y/N)? => ";
   cin >> ch;
   }while(ch == 'y');
}