Understanding simple maths

This is a discussion on Understanding simple maths within the C++ Programming forums, part of the General Programming Boards category; Determine the amount of a diet soda unsafe for consumption. This diet soda contains artificial sweetener that kills mice. Program ...

  1. #1
    Registered User FloatingPoint's Avatar
    Join Date
    Jun 2003
    Posts
    191

    Understanding simple maths

    Determine the amount of a diet soda unsafe for consumption. This diet soda contains artificial sweetener that kills mice.

    Program input is:
    1) amount of artificial sweetener fatal to a mouse
    2) weight of the mouse
    3) weight of a dieter.

    The program should ask for the weight at which the dieter will stop dieting and not his current weight.

    The diet soda contains 1/10th of 1% artificial sweetener.

    Use a variable declaration w/ the const modifier to give a name to this fraction.

    Express the percent as the double value 0.001.
    Hi

    I know this is simple maths but I'm having trouble understanding what/how it wants me to exactly do.

    a) What should the amount of sweetener be in? mg? g?
    b) What does this mean The diet soda contains 1/10th of 1% artificial sweetener?
    c) Where/when should the double percent(0.001) used?

    Thanx
    Come cast your shadow over me
    and I'll cast mine all over thee
    Take me away, into the shades
    where there is no light of day

  2. #2
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640

    Re: Understanding simple maths

    Originally posted by FloatingPoint
    a) What should the amount of sweetener be in? mg? g?
    b) What does this mean The diet soda contains 1/10th of 1% artificial sweetener?
    c) Where/when should the double percent(0.001) used?

    Thanx
    a) use whatever the other weights are given in (ie, the wight of the mouse)

    b) 1% == 0.01, 1/10 of 1% == 0.001

    c) (see b)

  3. #3
    Registered User FloatingPoint's Avatar
    Join Date
    Jun 2003
    Posts
    191
    a) The whole question doesn't state the weight of the mouse. It just says that a certain amount (again, not stated) of the artificial sweetener kills mice.

    b) and c) hehe..

    Thanx.
    Come cast your shadow over me
    and I'll cast mine all over thee
    Take me away, into the shades
    where there is no light of day

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    You are supposed to allow the user to enter the amount that killed a mouse, the weight of the mouse that was killed, and the dieter's weight--in other words, those are what are called "variables".

  5. #5
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    b) What does this mean The diet soda contains 1/10th of 1% artificial sweetener?
    I'd assume that there is 0.001 x 12 oz = 0.012 oz of sweetner in a can of soda.*

    You probably want to express your answer in ounces. (If you're in an English measurement "zone". ...Otherwise, I'd use ml.

    Lets say it takes 1 ounce of sweeetner to kill a 1 pound mouse (big mouse!). It would take 100 ounces of sweetner to kill a 100 pound man (small man). That would be 100,000 ounces of soda to kill the man.

    Now, start working on your variables and input/output. Then, figure-out and try-out the math/ratio stuff!

    *A chemist would most likely be referring to weight when components are listed in percent... but for an exercise like this... they probably want liquid-measure.

  6. #6
    Registered User FloatingPoint's Avatar
    Join Date
    Jun 2003
    Posts
    191
    Thanx guys.

    Working on it...
    Come cast your shadow over me
    and I'll cast mine all over thee
    Take me away, into the shades
    where there is no light of day

  7. #7
    Registered User FloatingPoint's Avatar
    Join Date
    Jun 2003
    Posts
    191
    Roughly the all the codes needed are there, but in the process I had to make some assumptions and I hope with that sort of question, it wont be wholly wrong.

    Anyway, I'd like to have this program output some percentage to make it clearer and easier to understand.

    Consider:

    double a, b;

    cin >> a >> b;
    cout << "a over b is " << a/b << " %." << endl;


    Enter these values for example,
    if a = 10, b = 20,

    then the program above would output 0.5%

    I'd like to make the program say 50% instead of 0.5.

    How do I do that?

    Thanx
    Come cast your shadow over me
    and I'll cast mine all over thee
    Take me away, into the shades
    where there is no light of day

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    double num = .526;

    double percent = 100.0 * num;

    //To round to the nearest integer
    int rounded = static_cast<int>(percent + 0.5);

    cout<<rounded<<"%"<<endl;
    Last edited by 7stud; 08-05-2003 at 12:15 PM.

  9. #9
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    Uhhh... The problem doesn't require you to calculate a percentage. The concentration of sweetner is a constant which is GIVEN as 0.1% AND as 0.001 in the problem statement.

    So, all you have to do is this:
    double PercentSweetner = 0.001;
    or,
    const double PercentSweetner = 0.001;

    Your program needs to do some ratio calculations with floats or doubles (so you can use decimals).

    Here's one way to do it:

    Take this equation:
    AmountSweetnerKillsMouse/MouseWeight = AmountSweetnerKillsMan/ManWeight

    Re-arrange this equation to "solve for" AmountOfSweetnerKillsMan = ?
    Do this "manually" with algebra. C++ won't "solve" the equation.

    Your program should calculate AmountOfSweetnerKillsMan and multiply this by the inverse of the concentration (i.e x1000) to get the amount of soda. (Actually, divide by PercentSweetner)

    The OUTPUT of your program is supposed to be "amount of soda" in fluid-ounces or liters.

    [edit]
    Changed float to double
    Last edited by DougDbug; 08-05-2003 at 07:12 PM.

  10. #10
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    Lightbulb Some rambling-random thoughts...

    FloatingPoint,

    Your difficulty seems to be with the "word problem" aspect of this problem. Computer programs usually start with a problem statement or task in words.

    Just like math "word problems", much of the work is in determining what the problem really is and how to solve it. You may not need to know a particular programming language to do this part.

    The second part of the problem is how to write the C++ code... knowing what library functions and language features are avallable, what they do and how to use them, and the general language syntax.

    For a problem like this, you need to figure-out to do it with a calculator before writing a program. Of course, if you do get something wrong in your algorithm or program flow, you can debug it later.

    You should plan your program before you start it. You can use a flow chart, psuedo code, outline, etc. I usually take an outline approach with a bunch of comments... not quite as detailed as real psuedo code.

    Before I write any code, I usually something like this:
    Code:
    int main()
    {
         // SET-UP CONSTANTS (percent sweetner)
    
         // SET-UP (declare) VARIABLES
         //     Amount of sweetner that kills mouse
         //     Amount of sweetner that kills dieter
         //     Mouse Weight
         //     Dieter Weight   
         //          NOTE:  Dieter weight not really needed.  could just put 
         //          the calculation in the cout statement.
    
         // GET USER INPUT
         //     Amount of Sweetner that kills mouse
         //     Mouse Weight
         //     Dieter Weight 
    
    
         // MAKE CALCULATIONS
    
         // DISPLAY RESULTS (amount of sweetner that kills dieter)
    
    return 0;
    }
    Next, I'd add the calculations to the outline
    Code:
    //...
         // MAKE CALCULATIONS
         //    Find ratio of (sweetner that kills mouse) to (mouse weight)
         // Multiply ratio by dieter's weight
         //    NOTE:  If this is done in two steps, we will need a "ratio" variable. 
    //...
    After I had the program planned-out, I'd add a few lines of real code and then test-compile.... Add a few more lines of code and re-compile. I'd test-run the program as soon as there was a cout statement. (I would make a cout statement before I wrote the calculation part of the program.)

  11. #11
    Registered User FloatingPoint's Avatar
    Join Date
    Jun 2003
    Posts
    191
    Thanx a lot for your explaining and the tips too DougDbug

    I guess you're right, I was having problems understanding what the question wants me to do. The picture is much clearer now
    Come cast your shadow over me
    and I'll cast mine all over thee
    Take me away, into the shades
    where there is no light of day

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple message encryption
    By Vicious in forum C++ Programming
    Replies: 10
    Last Post: 11-07-2004, 11:48 PM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  3. Simple simple program
    By Ryback in forum C++ Programming
    Replies: 10
    Last Post: 09-09-2004, 06:48 AM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. Need help with simple DAQ program
    By canada-paul in forum C++ Programming
    Replies: 12
    Last Post: 03-15-2002, 08:52 AM

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