Like Tree1Likes
  • 1 Post By Salem

finding boiling point need help!!!

This is a discussion on finding boiling point need help!!! within the C Programming forums, part of the General Programming Boards category; Question: The table below shows the normal boiling points of several substances. Write a program that prompts the user for ...

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    14

    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. #2
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    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. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    14
    I fixed the scanf problem. How can I return true or false without including if inside the return statement? Thanks for the reply!

  4. #4
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    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. #5
    Registered User
    Join Date
    Feb 2013
    Posts
    14
    I don't understand. What should I do?

  6. #6
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    Code:
    return ( ref - x * ref <= data
             &&
             data <= ref + x * ref );

  7. #7
    Registered User
    Join Date
    Feb 2013
    Posts
    14
    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. #8
    Registered User
    Join Date
    Feb 2013
    Posts
    14
    I need help this is very frustrating....thank you for any help

  9. #9
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    Post your revised code, because it works fine for me.

  10. #10
    Registered User
    Join Date
    Feb 2013
    Posts
    14
    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. #11
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    As is, compiles without error and works for me on MinGW and VS10.

  12. #12
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    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)
    Crossfire likes this.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  13. #13
    Registered User
    Join Date
    Dec 2012
    Posts
    289
    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!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 04-27-2011, 04:34 AM
  2. Point to Point Protocol and Bluetooth DUN
    By PSLoh in forum Networking/Device Communication
    Replies: 2
    Last Post: 03-03-2008, 08:44 AM
  3. fixed point / floating point
    By confuted in forum Game Programming
    Replies: 4
    Last Post: 08-13-2002, 01:25 PM
  4. Floating point faster than fixed-point
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 11-08-2001, 10:34 PM
  5. Finding a point within a circle
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 09-23-2001, 08:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21