Need Help with a code

Discussion in 'Code Snippets and Tutorials' started by rarehunternick, Feb 20, 2009.

Thread Status:
Not open for further replies.
  1. rarehunternick

    rarehunternick Level II

    Joined:
    Mar 18, 2007
    Messages:
    327
    Likes Received:
    0
    Ok...here's the deal. I am struggling on a code and its part of my homework so i was gonna post it into the homework section, but then I feel that i wouldn't have a great deal of response. So, maybe i could get a better response here. Its basic C programming.

    I kind of understand the problem. I know their is a if, else...but the problem I am having is trying to get to do what I want. Here, I'll show a simple version of what I have so far...

    Thats all I have so far. Its not complete yet. I know that I have to brute force the if/else area but i have trouble trying to do the function. Can anyone help?
     
  2. rarehunternick

    rarehunternick Level II

    Joined:
    Mar 18, 2007
    Messages:
    327
    Likes Received:
    0
    any ideas anyone? I need to get this code done before Tuesday. So, please help me.
     
  3. Zer0

    Zer0 Level IV

    Joined:
    Mar 2, 2008
    Messages:
    3,037
    Likes Received:
    180
    Location:
    Home sweet home
  4. rarehunternick

    rarehunternick Level II

    Joined:
    Mar 18, 2007
    Messages:
    327
    Likes Received:
    0
    I understand how to write functions. I just don't know what to do with all the reference numbers running through the equations
     
  5. Zer0

    Zer0 Level IV

    Joined:
    Mar 2, 2008
    Messages:
    3,037
    Likes Received:
    180
    Location:
    Home sweet home
    Ok, first of all, the function definition should be:
    Code (Text):
    1. bool within_x_percent(int ref, int data, double x)
    Then the function would be:
    Code (Text):
    1. if( (ref - x*ref)<= data || data <= (ref + x*ref) )
    2. return true;
    3. else
    4. return false;
     
  6. zav75

    zav75 Level I

    Joined:
    Feb 28, 2007
    Messages:
    76
    Likes Received:
    6
    Location:
    Canada, Province of Québec
    It may be late but here a full solution.

    Code (Text):
    1. #include <stdio.h>
    2.  
    3. /* Prototype */
    4. bool within_x_percent(double SubtanceA, double SubtancesB, double percentage);
    5.  
    6.  
    7. // Constants
    8. const double Water = 100.0;
    9. const double Mercury = 357.0;
    10. const double Copper = 1187.0;
    11. const double Silver = 2193.0;
    12. const double Gold = 2660.0;
    13.  
    14.  
    15. int main()
    16. {
    17.     /* declared variables */
    18.     double subtanceA=0,percentage =0;
    19.         int subtanceB=0.0;
    20.     /* Prompt the user */
    21.     //we get the subtanece
    22.     printf("Enter subtance  : ");  
    23.     scanf("%d", &subtanceB);
    24.     //here the tolerance
    25.     printf("Enter percentage : ");
    26.     scanf("%d", &percentage);
    27.  
    28.     //here an example how to use it
    29.     if(within_x_percent(Water,subtanceB,10 ))
    30.         printf("the subtance is water ! : ");
    31.  
    32.     return 0;
    33. }
    34.  
    35. bool within_x_percent(double SubtanceA, double SubtancesB, double percentage)
    36. {
    37.     double range = SubtanceA  * (percentage/100) ;
    38.     double topRange = SubtanceA + range ;
    39.     double bottomRange = SubtanceA  - range ;
    40.     if(SubtancesB >= bottomRange && SubtancesB <= topRange )
    41.     {
    42.         return true;
    43.     }
    44.     else
    45.         return false;
    46. }
     
Thread Status:
Not open for further replies.