I have an issue with the following code I have been writing. I am trying to compare capacitors to standard values on a chart to see if they are in the array I created or not. I am having issues when I enter 0.01, 0.001, 0.0001 etc. With the following code, all of these numbers should be multiplied by a certain factor of 10 to get the number within a range of 10 to 99. When I enter 0.01 for example, it multiplies it by 10,000 instead of 1000 where it should be. If you see any error with the code let me know.
void checkstd_cap (float c)
{
if(c >= .000001 && c <= 10000) // This if statement is used to get the users entered capacitor
value between 10 and 99,
// which is the range in the array for capacitor standard values
{
if(c >= 10000)
c /= 1000;
if(c >= 1000 && c <= 9999)
c /= 100;
if(c >= 100 && c <= 999)
c /= 10;
if(c >= 10 && c <= 99)
c /= 1;
if(c >= 1.0 && c <= 9.999999)
c *= 10;
if(c >= 0.1 && c <= 0.999999)
c *= 100;
if(c >= 0.01 && c <= 0.0999999)
c *= 1000;
if(c >= 0.001 && c <= 0.00999999)
c *= 10000;
if(c >= 0.0001 && c <= 0.000999999)
c *= 100000;
if(c >= 0.00001 && c <= 0.0000999999)
c *= 1000000;
if(c >= 0.000001 && c <= 0.00000999999)
c *= 10000000;
}
int i = 0; // i is initilized to 0
while( i<6 ) /* If i reaches 12, this proves resistor B is not standard
and a warning message is printed */
{
if (fabs(c - STDVC[i]) <= 0.01)
{
printf("Capacitor is a standard value");
break; /* while loop breaks if this statement above is true as this
proves capacitor is a standard value */
}
else
{
i++; // if the capacitor is not standard, i keeps incrementing to 6
}
}
while (i == 6) /* If i reaches 6, this proves the capacitor is not standard
and a warning message is printed */
{
printf("Warning: Capacitor is not a standard value\n");
break;
}
}