Question: The table below shows the normal boiling points of several substances. Write a program that prompts the user for the observed boiling point of a substance in Celsius degrees and identifies the substance if the observed boiling point is within 5% of the expected boiling point. If the data input is more than 5% higher or lower than any of the boiling points in the table, the program should output the message “Substance unknown.” Your program should define and call a function “within_x_percent” that takes as parameters a reference value ref, a data value data, and a percentage value x and returns 1 meaning true if data is within x percent of ref –that is,
(ref – x% * ref) < data < (ref + x% * ref). Otherwise “within_x_percent would return zero, meaning false. For example, the call “within_x_percent(357, 323, 10) would return true, since 10% of 357 is 35.7, and 323 falls between 321.3 and 392.7.
Table:
Substance / Normal boiling point (celcius)
Water / 100
Mercury / 357
Copper / 1187
Silver / 2193
Gold / 2660
Code:
#include <stdio.h>
#define WATER_BPT 100
#define MERCURY_BPT 357
#define COPPER_BPT 1187
#define GOLD_BPT 2660
#define SILVER_BPT 2193
int within_x_percent(double ref, double data, double x);
int
main(void)
{
double data, x;
printf("Please enter the boiling point> ");
scanf("lf", &data);
x = .05;
if(within_x_percent(WATER_BPT, data, x))
printf("The substance is water.");
else if(within_x_percent(MERCURY_BPT, data, x))
printf("The substance is mercury.");
else if(within_x_percent(COPPER_BPT, data, x))
printf("The substance is copper.");
else if(within_x_percent(SILVER_BPT, data, x))
printf("The substance is silver.");
else if(within_x_percent(GOLD_BPT, data, x))
printf("The substance is gold.");
else
printf("The substance is unknown.");
return(0);
}
int within_x_percent(double ref, double data, double x)
{
return(if(ref-x*ref<=data && data<=ref+x*ref));
}
I keep getting this error:
homework2.c: In function âwithin_x_percentâ:
homework2.c:33:8: error: expected expression before âifâ
Thanks for any help!"