my code is this #include #include #include #include #define NUM_COL 13 struct re
ID: 3839675 • Letter: M
Question
my code is this
#include
#include
#include
#include
#define NUM_COL 13
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)
};
struct resist_value RESIST[NUM_COL]={{"black" ,0 ,-1},{ "brown" ,1 ,1},{ "red" ,2 ,2},
{ "orange",3 ,-1},
{ "yellow",4 ,-1},
{ "green" ,5 ,0.5},
{ "blue" ,6 ,0.25},
{ "violet",7 ,0.1},
{ "grey" ,8 ,-1},
{ "white" ,9 ,-1},
{ "gold" ,-1 ,5},
{ "silver" ,-1 ,10},
{ "none" ,-1 ,20}
};
main()
{
int i,j;
float x[4],p;
char c[4][10],m;
struct resist_value r1;
do
{
for(i=0;i<4;i++)
x[i]=-2;
printf("Enter the colour of the resistor's four bands beginning with the band nearest the end. Type the colors in lower letters only. No CAPS. ");
for(i=0;i<4;i++)
{
printf("Band %d=>",i+1);
fflush(stdin);
scanf('"%s",c[i]);
}
for(i=0;i<4;i++)
{
for(j=0;j {
if(strcmp(c[i],RESIST[j].COL)==0)
{
if(i<3)
x[i]=RESIST[j].VAL;
else
x[i]=RESIST[j].TOL;
}
if(strcmp(c[i],"")==0)
{
if(i<3)
x[i]=RESIST[12].VAL;
else
x[i]=RESIST[12].TOL;
}
}
}
if(x[0]<0 || x[1]<0 || x[2]<0 || x[3]<0)
{
if(x[0]<0 || x[1]<0 || x[2]<0)
printf("Invalid color(s) in either bands 1,2 or 3(resistor value):");
if(x[0]<0)
printf("%s",c[0]);
if(x[1]<0)
printf(" %s",c[1]);
if(x[2]<0)
printf(" %s",c[2]);
if(x[3]<0)
printf(" Invalid color for band 4(tolerance):%s",c[3]);
goto CHECK;
}
p=pow(10,x[2]);
printf(" Resistance value: %.0f ohms",(x[0]*10+x[1])*p);
printf(" Tolerance: %.2f %%",x[3]);
CHECK: printf(" Do you want to decode another resistor(Y/N)? ");
fflush(stdin);
printf("=>");
scanf(" %c",&m);
}while(m!='N' && m!='n');
}
however the situation i have is my resistance output is a single number for example "500000 ohms" however i want to add a space every 3rd digit when it is printed eg (500 000 or 2 400 000), how would i go about modifying my code to do that?
Explanation / Answer
Following code should do the job. Note that it is C code, and I have added another function for the required functionality. To compile use : gcc <filename> -lm
-lm is to add header file <math.h> that is required for power function.
#include <stdio.h>
#include <string.h>
#include <math.h>
#define NUM_COL 13
void printResistanceValue( float value ){
printf("%.0f ",value);
char str[ 1000 ];
long long int val = (long long int)value;
sprintf( str, "%lld", val );
int length = strlen( str );
printf(" Resistance value:");
int remainder = length%3;
int i = 0;
if( remainder != 0 ){ printf(" ");}
for( ; i < remainder; i++ ){
printf("%c",str[i]);
}
while( i < length ){
printf(" %c", str[i] );
printf("%c", str[i+1] );
printf("%c", str[i+2] );
i = i + 3;
}
printf(" ohms ");
}
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)
};
struct resist_value RESIST[NUM_COL]={
{"black" ,0 ,-1},{ "brown" ,1 ,1},{ "red" ,2 ,2},
{ "orange",3 ,-1},
{ "yellow",4 ,-1},
{ "green" ,5 ,0.5},
{ "blue" ,6 ,0.25},
{ "violet",7 ,0.1},
{ "grey" ,8 ,-1},
{ "white" ,9 ,-1},
{ "gold" ,-1 ,5},
{ "silver" ,-1 ,10},
{ "none" ,-1 ,20}
};
int main()
{
int i,j;
float x[4],p;
char c[4][10],m;
struct resist_value r1;
do
{
for(i=0;i<4;i++)
x[i]=-2;
printf("Enter the colour of the resistor's four bands beginning with the band nearest the end. Type the colors in lower letters only. No CAPS. ");
for(i=0;i<4;i++){
printf("Band %d=>",i+1);
fflush(stdin);
scanf("%s",c[i]);
}
for(i=0;i<4;i++){
for(j=0;j< 13; j++){
if(strcmp(c[i],RESIST[j].COL)==0){
if(i<3){
x[i]=RESIST[j].VAL;}
else{
x[i]=RESIST[j].TOL;
}
}
if(strcmp(c[i],"")==0){
if(i<3){
x[i]=RESIST[12].VAL;}
else{
x[i]=RESIST[12].TOL;}
}
}
}
if(x[0]<0 || x[1]<0 || x[2]<0 || x[3]<0)
{
if(x[0]<0 || x[1]<0 || x[2]<0)
printf("Invalid color(s) in either bands 1,2 or 3(resistor value):");
if(x[0]<0)
printf("%s",c[0]);
if(x[1]<0)
printf(" %s",c[1]);
if(x[2]<0)
printf(" %s",c[2]);
if(x[3]<0)
printf(" Invalid color for band 4(tolerance):%s",c[3]);
goto CHECK;
}
p= pow(10.0,x[2]);
float resistanceValue = (x[0]*10+x[1])*p;
printResistanceValue( resistanceValue );
printf(" Tolerance: %.2f %%",x[3]);
CHECK: printf(" Do you want to decode another resistor(Y/N)? ");
fflush(stdin);
printf("=>");
scanf(" %c",&m);
}while(m!='N' && m!='n');
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.