Reputation: 27
I am working on a program in C that will take the input of a temperature from a user and return a substance and boiling point if it is within 3% of that substance's BP.
I have currently run into 2 issues that I can't seem to solve.
int find_substance(double value)
seems to skip and not print some iterations. I have it set to print the index to make sure the indexes are chosen properly.
I checked each value on the table and the only ones that don't print are -78.5, -35.5, and 3,280.00.So what could cause an error like this?
I have checked the arrays to make sure there weren't any errors I missed and still have yet to find what is causing the problem.
#include <stdio.h>
#include <stdlib.h>
const char* name[] = {"Carbon dioxide", "Ammonia", "Wax","Water", "Olive Oil", "Mercury", "Sulfur", "Talc", "Silver", "Copper", "Gold", "Iron", "Silicon"};
const double temp[] = {-78.5, -35.5, 45, 100.7, 300.00, 356.9, 444.6, 1,500.00, 2,212.00, 2,562.00, 2,700.00, 2,862.00, 3,280.00};
int is_substance_within_x_percent(double temp, double value);
int find_substance(double value);
int main()
{
double obs_temp;
printf("Enter the temperature: ");
scanf("%lf", &obs_temp);
double value = obs_temp;
int index = find_substance(value);
}
int is_substance_within_x_percent(double temp, double value)
{
temp = abs(temp);
if((value>=temp - (0.03 * temp)) && (value <=temp + (0.03 * temp))){
return 1;
}
else
return 0;
}
int find_substance(double value)
{
int index = 0;
int i;
for(i=0;i<13;i++)
{
if(is_substance_within_x_percent(temp[i], value) == 1){
printf("index: %d", i);
break;
}
}
return i;
if (is_substance_within_x_percent(temp[i], value)== -1){
printf("No substance was found.\n");
return 0;
}
}
Upvotes: 0
Views: 155
Reputation: 27
In the string temp[]
change the commas for numbers in the thousands to straight numbers as well as remove any extra zeros since the array is type double
, making them unnecessary (ex. 2,562.00 >>> 2562).
Also in is_substance_within_x_percent
: add value = fabs(value)
and change temp = abs(temp)
into temp = fabs(temp)
since both of these variables are double and you want the absolute value of each before calculating.
Upvotes: 1