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

een 4827 813Nie Problem 2 (35 points) You get a job working for an electrician w

ID: 3791077 • Letter: E

Question

een 4827 813Nie Problem 2 (35 points) You get a job working for an electrician who uses resistors intensively in his circuits. He wants you to create a computer program that can help him translate the color code printed on the resistors into its value in Ohm (2) based on the chart below. Typically, there are 4 color bands on the resistor: 3 color bands for resistor value and the 4 one is for the tolerance value. Your boss only care about the value of the resistor (only the first 3 color bands) Tolerance band First colour band colour band Silver multiply by 0.01 Gold multiply by 0.1 Black multiply by 1 Orange 3 Brown multiply by 10 Orange 3 Yellow 4 Yellow 4 Red multiply by 100 Orange multiply by 1,000 Blue 6 Yellow multiply by 10,000 n multiply by 100,000 Blue multiply by 1,000,000 Figure 1: Resistor Color Code Source: http: www bbc co uk schools gesebitesize design images el resistor colours gif To read the figure above is o The first color band represents the 10 digit o The second color band represents the unit digit o The third color band is the multiplier For example,

Explanation / Answer

main.c

#include <stdio.h>
#include <stdlib.h>

/*
* Function takes color code and integer choice for 3rd band only
* Returns the double value with which we need to multiple
* Gives - 1 in case of error, if color code is not valid
*/
double col_to_multiple(char color, int choice) {
// Silver
   if((color=='S') && (choice == 0)) {
return 0.01;
}
// Gold
   if((color=='G') && (choice == 3)) {
return 0.1;
}
// Black
   if((color=='B') && (choice == 1)) {
return 1;
}
// brown
   if((color=='B') && (choice == 2)) {
return 10;
}
// Red
   if((color=='R') && (choice == 0)) {
return 100;
}
// Orange
   if((color=='O') && (choice == 0)) {
return 1000;
}
// yellow
   if((color=='Y') && (choice == 0)) {
return 10000;
}
// Green
   if((color=='G') && (choice == 1)) {
return 100000;
}
// Blue
   if((color=='B') && (choice == 3)) {
return 1000000;
}   
return -1;
}

/*
* Function takes color code and integer choice for 1st and 2nd band only
* Returns the integer value which should be kept on 1st and 2nd place
* Gives - 1 in case of error, if color code is not valid
*/
int col_to_num(char color, int choice) {
// Black
   if((color=='B') && (choice == 1)) {
return 0;
}
// Brown
   if((color=='B') && (choice == 2)) {
return 1;
}
// Red
   if((color=='R') && (choice == 0)) {
return 2;
}
// Orange
   if((color=='O') && (choice == 0)) {
return 3;
}
// Yellow
   if((color=='Y') && (choice == 0)) {
return 4;
}
// Green
   if((color=='G') && (choice == 1)) {
return 5;
}
// blue
   if((color=='B') && (choice == 3)) {
return 6;
}   
// Velvet
   if((color=='V') && (choice == 0)) {
return 7;
}
// Grey
   if((color=='G') && (choice == 2)) {
return 8;
}
// White
   if((color=='W') && (choice == 0)) {
return 9;
}
return -1;
}

/**
* Driver function to test the color codes in resistorcolor.txt file
*/
int main()
{
// input and output file pointer
FILE *inptr;
FILE *outptr;
inptr = fopen("C:\Users\ykgupta\Documents\df\SD esistorcolor.txt","r");
outptr = fopen("C:\Users\ykgupta\Documents\df\SD esistorvalue.txt","w");

// variables for storing color codes and int value
char color1, color2, color3;
int val1, val2, val3;

// in case if there is some issue in opening files
if((inptr == NULL) || (outptr == NULL))
{
printf("Error!");   
exit(1);   
}
  
// Keep scanning till any input is left
while(fscanf(inptr, "%c %d %c %d %c %d%*c", &color1, &val1, &color2, &val2, &color3, &val3) != EOF) {
// get first and second digit
int first = col_to_num(toupper(color1), val1);
int second = col_to_num(toupper(color2), val2);

// get value to be multiplied with
double multiply = col_to_multiple(toupper(color3), val3);

// check if colors are valid.
if(first == -1) {
fprintf(outptr,"%s", "Wrong 1st color band ");
continue;
}
if(second == -1) {
fprintf(outptr,"%s", "Wrong 2nd color band ");
continue;
}
if(multiply == -1) {
fprintf(outptr,"%s", "Wrong 3rd color band ");
continue;
}
fprintf(outptr,"%.2f Ohm ", (first*10 + second)*multiply);
}
printf("Program completed Successfully");

fclose(inptr);
fclose(outptr);

return 0;
}

resistorcolor.txt:

Y 0 V 0 R 0
R 0 G 1 B 0
B 1 b 2 y 0
S 0 R 1 S 1
O 0 w 0 g 3
O 0 M 0 G 1
B 2 R 0 B 1
W 0 G 3 B 1
B 3 G 2 B 1
R 0 R 0 O 0

Output: resistorValue.txt

4700.00 Ohm
Wrong 3rd color band
10000.00 Ohm
Wrong 1st color band
3.90 Ohm
Wrong 2nd color band
12.00 Ohm
Wrong 2nd color band
68.00 Ohm
22000.00 Ohm


please change the location of files according to your directory structure.