# Thread: finding boiling point need help!!!

1. ## finding boiling point need help!!!

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!"

2. You're missing the % for the conversion specifier in scanf().

You can drop the if(...) from the return statement in your function. The expression itself will evaluate to true or false, i.e. 1 or 0, which can be returned directly.

3. I fixed the scanf problem. How can I return true or false without including if inside the return statement? Thanks for the reply!

4. The logical operators applied to the operands evaluate the expression to a boolean value, not the if. The if() is used to make a decision about whether to execute the consequent statement, of which you have none, based on that boolean value.

5. I don't understand. What should I do?

6. Code:
```return ( ref - x * ref <= data
&&
data <= ref + x * ref );```

7. Ok I did it, but now as I try to execute it... any number I enter for the boiling point it tells me that the substance is unknown. Basically it doesn't do the calculations to deternine the substance.
Here:
Please enter the boiling point> 104.5
The substance is unknown.% ./homework2
Please enter the boiling point> 101
The substance is unknown.%
It should say that the substance is water....I don't understand why doesn't it work? Thanks for the reply!

8. I need help this is very frustrating....thank you for any help

9. Post your revised code, because it works fine for me.

10. 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(int 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(int ref, double data, double x)
{
return(ref-x*ref<=data && data<=ref+x*ref);
}```

11. As is, compiles without error and works for me on MinGW and VS10.

12. Works for me too (Ubuntu gcc)
Code:
```\$ gcc -W -Wall -Wextra foo.c
\$ ./a.out
Please enter the boiling point> 101
The substance is water.\$```
A common newbie mistake is to edit the code, then either
- forget to save, before compiling (so recompiling the old code)
- forgetting to compile, before running (so running the old code)

13. yeah i am guilty of not saving and compiling, but i must say Pelles for C and QT seem to do it for me though when i click run!