Hi everyone. Im from Puerto Rico, and Im studying Computing Science. Im actually taking my first course of programming, and it is called "Introduction to computer programming", and we are programming with C++. At this point almost everything has been theory, but now we are starting with practice, and I really have a lot of problems with the algorithm design.

For example here is one of the Problems I have to solve:

A government research lab has conluded that an artificial sweetener commonly
used in diet soda pop will cause death in laboratory mice. A friend of yours is desperate
to lose weight but cannot give up soda pop. Your friend wants to know how much diet
soda pop 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 sweetener needed to kill a mouse, the
weight of a mouse, and the weight of the dieter. To ensure 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 1/10th 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 a double value 0.001. Your program should allow
the calculation to be repeated as often as the user wishes.

I have already done some things, like this:

#include <iostream>
using nameplate std;
int main()
{
	double amount_of_sweetener_mouse, weight_of_mouse, weight_of_dieter, amount_of_sweetener, 
	int amount_of_soda
	const int sweetener_in_soda = 0.001

	cout << "Enter the sweetener needed to kill a mouse.\n";
	cin >> amount_of_sweetener_mouse;
	cout << "Now enter the weight of the mouse.\n";
	cin >> weight_of_mouse;
	cout << "Enter the weight at which the dieter will stop dieting.\n";
	cin >> weight_of_dieter;

	amount_of_sweetener = (amount_of_sweetener_mouse / weight_of_mouse) * weight_of_dieter

Now, I do not know how I can put how to find how much soda does the dieter needs in order to die...

I do not actually want you guys to do the homework for me, I just want some help because I really want to learn how to program and Im starting and I have a lot of doubts.

Thanks :D

Recommended Answers

All 12 Replies

Someone?

Someone?

Please repost with code tags. Looks like at least part of your program got cut off.

[code]

// paste code here

[/code]

I'll look into it, give me a little time. And I don't think it got cut off..he didn't finish

#include <iostream>

using namespace std;

int main()
{
    char ReRunProgram;
	double amount_of_sweetener_mouse, weight_of_mouse, weight_of_dieter, amount_of_sweetener_to_kill; 
	double amount_of_soda_to_kill;
	const float sweetener_in_soda = 0.001;

	cout << "\nEnter the sweetener needed to kill a mouse.\t";
	cin >> amount_of_sweetener_mouse;
	cout << "\nNow enter the weight of the mouse.\t";
	cin >> weight_of_mouse;
	cout << "\nEnter the weight at which the dieter will stop dieting.\t";
	cin >> weight_of_dieter;

	amount_of_sweetener_to_kill = (amount_of_sweetener_mouse / weight_of_mouse) * weight_of_dieter;
	
	amount_of_soda_to_kill = amount_of_sweetener_to_kill / sweetener_in_soda;
    
    cout << "The amount of sweetner that will kill the dieter is " << amount_of_sweetener_to_kill << ".";
    cout << "\nThis means that after " << amount_of_soda_to_kill << " sodas the dieter will die.";
    cout << "\nDo you wish to run the program again? Y/N:\t";
    cin >> ReRunProgram;
    if (ReRunProgram == 'y')
    {
        main();
    }
    else
    {
        if (ReRunProgram == 'n')
        {
            exit(0);
        }
    }
    
    return 0;
}

There you go!

#include <iostream>

using namespace std;

int main()
{
    char ReRunProgram;
	double amount_of_sweetener_mouse, weight_of_mouse, weight_of_dieter, amount_of_sweetener_to_kill; 
	double amount_of_soda_to_kill;
	const float sweetener_in_soda = 0.001;

	cout << "\nEnter the sweetener needed to kill a mouse.\t";
	cin >> amount_of_sweetener_mouse;
	cout << "\nNow enter the weight of the mouse.\t";
	cin >> weight_of_mouse;
	cout << "\nEnter the weight at which the dieter will stop dieting.\t";
	cin >> weight_of_dieter;

	amount_of_sweetener_to_kill = (amount_of_sweetener_mouse / weight_of_mouse) * weight_of_dieter;
	
	amount_of_soda_to_kill = amount_of_sweetener_to_kill / sweetener_in_soda;
    
    cout << "The amount of sweetner that will kill the dieter is " << amount_of_sweetener_to_kill << ".";
    cout << "\nThis means that after " << amount_of_soda_to_kill << " sodas the dieter will die.";
    cout << "\nDo you wish to run the program again? Y/N:\t";
    cin >> ReRunProgram;
    if (ReRunProgram == 'y')
    {
        main();
    }
    else
    {
        if (ReRunProgram == 'n')
        {
            exit(0);
        }
    }
    
    return 0;
}

There you go!

Seems OK to me. The formulas look right, the results look right. If you want to make it better, you can check for bad input (i.e. user types in something besides 'y' or 'n', or types in 0 or a negative number) and give an appropriate error message, but as far as the assignment goes, it looks like the program fulfills the requirements and calculates/runs correctly.

Thanks mate, thanks a lot for the check. I could have done that, but he can figure that out later using the stuff I provided. I'm not going to do everything for him...

man, you need to use shorter variable names

Thanks a lot!!!

Its ok dont worry, I dont want you guys to do all the work for me, I just actually wanted some help

As far as in the course goes, there are some things that they have not teached us yet like the "char ReRunProgram", but Ill fix it like hes showing us in the basics

Thanks a lot again!!!

To: alexvena

I used his variable names...

To: mrrko

If this topic is solved please mark it as such (to do so just click the mark this topic as solved on the right of "Reply to Thread"); and you are very much welcome! ;)

Also the char is a basic character to get the y or n the user inputs to re run the program or to close it when it's introduced at the end of the code. But up to you, it's not my code, and you could always seem more advanced than you are like I do in my class XD.

I'm only a week into C++, and know what I know, so no problem with showing what you know to your teacher.

Already marked as solved ;)

I have the same problem but in java can someone help.

A government research lab has conluded that an artificial sweetener commonly
used in diet soda pop will cause death in laboratory mice. A friend of yours is desperate
to lose weight but cannot give up soda pop. Your friend wants to know how much diet
soda pop 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 sweetener needed to kill a mouse, the
weight of a mouse, and the weight of the dieter. To ensure 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 1/10th 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 a double value 0.001. Your program should allow
the calculation to be repeated as often as the user wishes.

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.