SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,julia Lobur
Chapter:
C++ Basics
Exercise:
Programming Projects
Question:2 | ISBN:9780321531346 | Edition: 7

Question

A government research lab has concluded 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 Programming Projects 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 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 percent as the double value 0.001 . Your program should allow the calculation to be repeated as often as the user wishes.

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

Program code:

//Header file       
#include <iostream>
#include <ctype.h>
#include <conio.h>
#include <math.h>
#include <stdlib.h>
using namespace std;

//Prototypes functions
void content();                          
int computeLab(double, double, double);   

//main function
int main()
{
	//variables declaration
   	const double PERCENT = 0.001;      
   	double sweetener, mouse, dieter;    
   	double numSoda;                    
   	char more;                          

   do
   {
   

    cout << "Amount of artifical sweetener to kill a mouse: ";
    cin >> sweetener;
    cout << "What is the weight of the mouse: ";
    cin >> mouse;
    cout << "What is the weight of the dieter: ";
    cin >> dieter;
      // output with calculation
      numSoda = computeLab(dieter, sweetener, mouse) / 
(sweetener * PERCENT);
      cout << "\nIt is possible to drink up to " 
<< ( ceil(numSoda) - 1 )
           << " of diet soda pop without dying.\n\n";

      cout << "Continue (y/n)? ";
      cin >> more;

   } while (tolower(more) == 'y');
   return EXIT_SUCCESS;
} 





//function definition
int computeLab(double d, double s, double m)
{
	return (d * (s / m));
}

Output:

 

Amount of artifical sweetener to kill a mouse: 1000
What is the weight of the mouse: 10
What is the weight of the dieter: 11

It is possible to drink up to 1099 of diet soda pop without dying.

Continue (y/n)? y
Amount of artifical sweetener to kill a mouse: 500
What is the weight of the mouse: 12
What is the weight of the dieter: 111

It is possible to drink up to 9249 of diet soda pop without dying.

Continue (y/n)? n
0 0

Discussions

Post the discussion to improve the above solution.