Google Answers Logo
View Question
 
Q: C++ Program ( Answered 5 out of 5 stars,   4 Comments )
Question  
Subject: C++ Program
Category: Computers > Programming
Asked by: shock1-ga
List Price: $10.00
Posted: 26 Feb 2005 17:16 PST
Expires: 28 Mar 2005 17:16 PST
Question ID: 481552
I need to write a C++ program tocalculate how much soda a dieter can
drink without dying.

Specifications:

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.

This is a sample of what the program output should look like:

Enter the dieter's name: Ed Parrish
Enter the target weight of the dieter in pounds: 175
Enter the weight of the mouse in grams: 30
Enter the lethal dose for the mouse in grams: 100

For dieter: Ed Parrish
Dieter's target weight: 175 pounds
Lethal dose in grams of sweetener is: 264594
Lethal number of cans of pop: 755983

Additional Specifications:

1. Assume that diet soda contains one-tenth of 1% artificial
sweetener. You may want to express the percentage as the double value
0.001.
2. Use a variable declaration with the modifier const to give a name
to all numbers. Do not use magic numbers.
3. If you weigh a full can of soda and subtract the weight of an empty
can you will see that the weight of the liquid is about 350 grams.
4. Assume that the lethal dose for a mouse is directly proportional to
a lethal dose for a human being.
5. The program must accept the weight of a person in pounds and the
weight of a mouse in grams. Note that 1 lb. = 453.59 grams. A mouse
usually weighs between 15 and 40 grams.
6. The output must be like the sample output shown above with similar
labels for the values reported.
7. Note that you will need to use getline() for input of the dieter's name. 

Thanks.
Answer  
Subject: Re: C++ Program
Answered By: leapinglizard-ga on 26 Feb 2005 18:05 PST
Rated:5 out of 5 stars
 
Dear shock1,

Here you go.



#include<stdlib.h>
#include<stdio.h>
#include<math.h>

#define BUFLEN 8192

int main() {
    const double sweetener_proportion = 0.001;
    const double soda_grams = 350.0;
    const double pound_grams = 453.59;

    char dieter_name[BUFLEN];
    double dieter_pounds, dieter_grams, mouse_grams, mouse_lethal_grams;
    double dieter_lethal_grams, dieter_lethal_cans;

    printf("Enter the dieter's name: ");
    fgets(dieter_name, BUFLEN, stdin);
    printf("Enter the target weight of the dieter in pounds: ");
    scanf("%lf", &dieter_pounds);
    printf("Enter the weight of the mouse in grams: ");
    scanf("%lf", &mouse_grams);
    printf("Enter the lethal dose for the mouse in grams: ");
    scanf("%lf", &mouse_lethal_grams);

    dieter_grams = dieter_pounds * pound_grams;
    dieter_lethal_grams = dieter_grams / mouse_grams * mouse_lethal_grams;
    dieter_lethal_cans = dieter_lethal_grams /
                         (soda_grams * sweetener_proportion);

    printf("For dieter: %s\n", dieter_name);
    printf("Dieter's target weight: %d pounds\n", (int) dieter_pounds);
    printf("Lethal dose in grams of sweetener is: %d\n",
            (int) round(dieter_lethal_grams));
    printf("Lethal number of cans of pop: %d\n",
            (int) round(dieter_lethal_cans));
}



If you have any trouble compiling or executing this program, let me
know so that I have the opportunity to fully meet your needs before
you assign a rating.


Regards,

leapinglizard
shock1-ga rated this answer:5 out of 5 stars and gave an additional tip of: $1.00
Awesome! Program ran perfectly, thanks for your help.

Comments  
Subject: Re: C++ Program
From: eliteskillsdotcom-ga on 26 Feb 2005 17:25 PST
 
Sounds like a homework assignment.

You should do it yourself. I highly doubt anyone will write it for you
for $10 especially with all the specifications.
Subject: Re: C++ Program
From: leapinglizard-ga on 26 Feb 2005 18:07 PST
 
It goes without saying that no one should attempt to pass off a
Researcher's work as his own. However, I see no objection if someone
requests help with an exercise for purposes of self-study or exam
preparation.

leapinglizard
Subject: Re: C++ Program
From: leapinglizard-ga on 27 Feb 2005 03:23 PST
 
Thank you for the rating and tip.

leapinglizard
Subject: Re: C++ Program
From: jeffz_2002-ga on 28 Feb 2005 14:24 PST
 
The program provided was more of a C style, not C++.  Below is
something that is a more modern C++ style.  Explanations for all of
the improvements can be found in Scott Meyers' "Effective C++"

// summary of differences:
// use #include <xxx>, not #include <xxx.h>
// use library code, especially <string> for strings!  Don't use char
arrays unless required.
// use std::cin and std::cout, not printf, scanf, etc ... the code's
cleaner, as shown below.
// main() should return an int

// Note I haven't compiled the code below, but it gives the idea.

#include <iostream>
#include <string> 

using std::string;
using std::cin;
using std::cout;
using std::endl;


int main() {
    const double sweetener_proportion = 0.001;
    const double soda_grams = 350.0;
    const double pound_grams = 453.59;

    std::string dieter_name;

    double dieter_pounds, dieter_grams, mouse_grams, mouse_lethal_grams;
    double dieter_lethal_grams, dieter_lethal_cans;

    cout << "Enter the dieter's name: ";
    cin >> dieter_name;

    cout << "Enter the target weight of the dieter in pounds: ";
    cin >> dieter_pounds;

    // ... etc

    dieter_grams = dieter_pounds * pound_grams;
    dieter_lethal_grams = dieter_grams / mouse_grams * mouse_lethal_grams;
    dieter_lethal_cans = dieter_lethal_grams /
                         (soda_grams * sweetener_proportion);

    cout << "For dieter: " << dieter_name << endl;
    cout << "Dieter's target weight: " <<
static_cast<int>(dieter_pounds) << " pounds\n";
    // etc

    return 0;
}


HTH,
Jeff

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy