Question
Update your Lab-1-Part-1 tool to now be one main looping program with two resistor identification functions: one for determining the color-code of a given resistor
Update your Lab-1-Part-1 tool to now be one main looping program with two resistor identification functions: one for determining the color-code of a given resistor value, and one to provide the resistor value when given a color-code.
1. Open Code::Blocks and start a new console application project. Copy in the contents from Lab 1 Part 1 to use as a starting point.
2. Add the following function prototypes to the top of your file in addition to Lab 1s prototypes: a. void getColorBands(char*, char*, char*, char*); b. void calcResistance(char, char, char, char);
3. The source code for each of your functions should be written below your main() function. Optionally, if you would like to get more practice with libraries (Part 2), you could create a Resistance Library.
4. Update the prompt(void) function: a. This function should display the Color-Code Scheme displayed in Table 1 then ask the user if they would like to convert a color-code to a resistance value or determine the color-code from a resistance value. NOTE: Table 1 has two more rows and one more column than in Lab 1. To save you time, the print statements for Table 1 have been provided to you in Appendix-A
1. Description of getColorBands (char*, char*, char*, char*) function: a. This function is to be designed to collect user input of 4 characters, aligning with the character-tocolor scheme shown in Table 1. b. Instead of returning anything, this function takes 4 pointers to characters as parameters. Obtaining these characters by reference means this function can return all 4 values of user input in one function call. Make sure to error check that the user did not input an invalid color-character.
2. Description of calcResistance (char, char, char, char) function: a. This function should take, as parameters, 4 characters that describe the color-code of a resistor. Based on these colors, the function should determine the value of the resistor and print that to the screen. b. HINT: Consider using a switch-case statement in your decryption.
3. Description of getResistorColors (int) function (this is a repeat from Lab 1): a. This function should take, as a parameter, an integer value of a resistor. For the sake of simplicity, we will ignore the tolerance (4th band) and fractional multipliers of Gold and Silver in the 3rd band in this step, so the integer should only be between 1-99M ohms. Make sure to error check this before proceeding. Assuming a valid integer, determine what the 3-band colors would be for this integer, using a leading-0 as necessary. b. HINT: Consider making a getIntBetween() function that sets a min and maximum value to retrieve values between and returns a valid integer. c. HINT: Consider determining what multiplier is being used before determining what digits are present in the first two bands. You can accomplish this by counting the number of times the number can be divided by 10 before it itself is less than 100 (a two-digit number).
4. Your main() function should do the following: a. Call the prompt() function to request user input on decryption technique. b. Collect user input for which method of conversion desired c. Perform the requested task d. Ask the user if they would like to try another resistor i. If yes, loop back to step A EGR 226 Structured Laboratory Activities 4 Copyright 201 Padnos College of Engineering and Computing ii. If no, exit the program
5. Error Checking: All points of user input should be carefully error checked to ensure there is no chance for invalid, malicious input. For example, if the program is expecting an integer between 1-999, entering a negative number or a number too large should fail and ask them to try again. Likewise, entering a character when an integer is expected should not break the program.
Original Code for finding color code of a given resistor value:
void prompt(void); void calcResistorColors(int val);
int main() { int loop=1; while(loop==1){ int first; char y; prompt(); printf("Please enter a resistor value (1-99MM): "); scanf("%d", &first); if(first<=0 || first>=99000000){ printf("Invalid code ");} else{ calcResistorColors(first);}
} } void prompt(){ printf("---------------Resistor Codes--------------- "); printf("|Character| Color | Band 1 & 2 | Band 3 | "); printf("| K | Black | 0 |*1 | "); printf("| N | Brown | 1 |*10 | "); printf("| R | Red | 2 |*100 | "); printf("| O | Orange| 3 |*1,000 | "); printf("| Y | Yellow| 4 |*10,000 | "); printf("| G | Green | 5 |*100,000 | "); printf("| B | Blue | 6 |*1,000,000 | "); printf("| V | Violet| 7 |*10,000,000 | "); printf("| E | Grey | 8 | | "); printf("| W | White | 9 | | "); printf("| D | Gold | | | "); printf("| S | Silver| | | "); printf("--------------------------------------------- "); } void calcResistorColors(int val){ int multip,u,x,band1,band2,band3; multip=log10(val); u=pow(10,multip); x=pow(10,multip-1); band1= val/u; band2=(val/x)-band1*10;
if(band1==0) printf("Black-"); if(band1==1) printf("Brown-"); if(band1==2) printf("Red-"); if(band1==3) printf("Orange-"); if(band1==4) printf("Yellow-"); if(band1==5) printf("Green-"); if(band1==6) printf("Blue-"); if(band1==7) printf("Violet-"); if(band1==8) printf("Grey-"); if(band1==9) printf("White-");
if(band2==0) printf("Black-"); if(band2==1) printf("Brown-"); if(band2==2) printf("Red-"); if(band2==3) printf("Orange-"); if(band2==4) printf("Yellow-"); if(band2==5) printf("Green-"); if(band2==6) printf("Blue-"); if(band2==7) printf("Violet-"); if(band2==8) printf("Grey-"); if(band2==9) printf("White-");
if(multip==0) printf("Black "); if(multip==1) printf("Brown "); if(multip==2) printf("Red "); if(multip==3) printf("Orange "); if(multip==4) printf("Yellow "); if(multip==5) printf("Green "); if(multip==6) printf("Blue "); if(multip==7) printf("Violet "); }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started