just started c++ class.... we use the absolute c++ book

after starting to read this book i decided to buy c++ for dummies and borrowed a book titled object oriented programming with c++ (book is from india written in english)

i still have no idea where to start on these two problems...
can someone dummy them down to basics and concepts... i dont want the code as i would like to learn this instead of just get the coding.

please help me

tatum

2. A government research lab has concluded that an artificial sweetener commonly used in
diet soda will cause death in laboratory mice. A friend of yours is desperate to lose
weight but cannot give up soda. Your friend wants to know how much diet soda it is
possible to drink without dying as a result. Write a program to supply the answer. The
input to the program is the amount of artificial sweetener needed to kill a mouse, the
weight of the mouse, and the weight of the dieter. To ensure the safety of your friend,
be sure the program requests the weight at which the dieter will stop dieting, rather
than the dieter’s current weight. Assume that diet soda contains one-tenth of 1% artificial
sweetener. Use a variable declaration with the modifier const to give a name to this
fraction. You may want to express the percentage as the double value 0.001.

8. The Babylonian algorithm to compute the square root of a number n is as follows:
1. Make a guess at the answer (you can pick n/2 as your initial guess).
2. Compute r = n / guess.
3. Set guess = (guess + r) / 2.
4. Go back to step 2 for as many iterations as necessary. The more steps 2 and 3 are
repeated, the closer guess will become to the square root of n.
Write a program that inputs an integer for n, iterates through the Babylonian algorithm
five times, and outputs the answer as a double to two decimal places. Your answer will
be most accurate for small values of n.

Dani AI

Generated

Short, conceptual walkthrough (no code) for both exercises, focused on the math and common implementation traps.

For the diet-soda problem: convert the mouse result into a per-mass lethal dose, scale that to the dieter's target mass, then convert that lethal sweetener mass into an amount of soda. Key steps in plain terms — keep units consistent (mg vs g vs kg); compute lethal_per_mass = (mouse lethal amount) / (mouse mass); lethal_for_person = lethal_per_mass * (dieter target mass); convert the soda sweetener percentage into a decimal fraction (one-tenth of one percent) and divide the lethal_for_person by that fraction to get soda mass. To report a practical volume, assume soda density near 1 g/mL (or use an explicit density). Typical errors are unit mismatches, zero or negative inputs, and integer truncation when translating to C++; declare the concentration as a named constant (for example, using a const double).

For the Babylonian (Heron) square-root task: the method refines a guess by averaging the guess with n divided by the guess. Pick a sensible initial guess (n/2 for large n, or 1 for n<1), apply the average-update step repeatedly — five iterations per the assignment — and output the resulting floating-point value rounded to two decimal places. Treat n==0 as a special case (result 0) and flag negative n as invalid for a real result. Use double precision to avoid truncation and guard against division by zero.

Quick verification and troubleshooting: test the sqrt routine on perfect squares (e.g., 36) to confirm convergence; test the toxicology calculation with simple, consistent units to catch conversion errors. The two most common implementation traps are unit errors and accidental integer division; addressing those first usually resolves incorrect answers. As noted, setting up the proportional relationship first is the essential insight. ’s aside is noted for levity but not needed for the solutions.

Recommended Answers

All 2 Replies

Basically, you have to turn the description into a set of quations. This has nothing to do with C++, yet.

2. A government research lab has concluded that an artificial sweetener commonly used in diet soda will cause death in laboratory mice. A friend of yours is desperate to lose weight but cannot give up soda. Your friend wants to know how much diet soda it is possible to drink without dying as a result. Write a program to supply the answer. The input to the program is the amount of artificial sweetener needed to kill a mouse, the weight of the mouse, and the weight of the dieter. To ensure the safety of your friend, be sure the program requests the weight at which the dieter will stop dieting, rather than the dieter’s current weight. Assume that diet soda contains one-tenth of 1% artificial sweetener. Use a variable declaration with the modifier const to give a name to this fraction. You may want to express the percentage as the double value 0.001.

Given the inputs described above, these ratios are useful and equivalent: SweetenerFriend / WeightFriend = SweetenerMouse / WeightMouse We know 3 of the 4 items, so rewrite as: SweetenerFriend = (SweetenerMouse / WeightMouse) * WeightFriend This gives you the amount of sweetener that will kill your friend.

Last thing to calculate is amount of soda. Since sweetener is 0.1% of the soda (or 0.001), you need to figure out how much soda is needed to get SweetenerFriend amount of the deadly chemical... This I leave to you...

Once you have the equations, it should be a simple matter to plug them into a program -- that is if you know programming ;) Do what you can and post your attempt and we'll help you clean it up.

I find it humorous that there was a google ad for aspartame resources just under tatumkay's post when I first read it. :lol:

That is all. You may now return to your regularly scheduled programming.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.