Need Help With Assignment

Hi, this is my first time taking C++, I registered for the course late and I missed a lot, trying to get caught up but I'm having difficulties, I spent hours sitting here but i'm stuck. I wouldn't want the solution rather than someone telling me where I went wrong. Here's the question and then my code:

One way to measure the amount of energy that is expended during exercise is to use metabolic equivalents (MET). Here are some METS for various activities:

Running 6 MPH: 10 METS
Basketball: 8 METS
Sleeping: 1 MET

The number of calories burned per minute may be estimated using the following formula:

Calories/Minute = 0.0175 x MET x Weight(Kg)

Write a program that calculates and output the total number of calories burned for a 150-pound person who runs 6 MPH for 30 minutes, play basketball for 30 minutes, and then sleep for 6 hours. One kilogram is equal to 2.2 pounds.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <stdio.h>

int main()
{
int Minutes, Weight, MET;
    float constant=0.0175;
    float Calories;
    
    printf("Enter Weight:");
    scanf("%d",&Weight);
    printf("Enter Running Speed in MET:");
    scanf("%d",&MET);
    printf("Enter the duration time in minutes:");
    scanf("%d",&Minutes);
    
    Calories= constant*MET*(Weight/2.2);
    
    printf("Your total calories burned is:%.2f\n", &Calories);
    

    
    return 0;
}



Here's the second 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 program has no input but does have defined constants for the following items: the amount of artificial sweetener needed to kill a mouse, the weight of the mouse, the starting weight of the dieter, and the desired weight of the dieter. To ensure the safety of your friend, be sure the program uses the weight at which the dieter will stop dieting, rather than the dieter’s current weight, to calculate how much soda pop the dieter can safely drink. You may use any reasonable values for these defined constants. Assume that diet soda contains 1/10th of 1% artificial sweetener. Use another named constant for this fraction. You may want to express the percent as the double value 0.001. (If your program turns out not to use a defined constant, you may remove that defined constant from your program.)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

int main()
{
    const double ArtificialSweetner = 0.001;
    double SweetnerMouse, WeightMouse, StartWeight, DesireWeight;
    int SodaPop;
    printf("Enter Amount of Artificial Sweetner needed to kill a mouse");
    scanf("%f",&SweetnerMouse);
    printf("What is the weight of the mouse?");
    scanf("%d",&WeightMouse);
    printf("What is your desired weight?");
    scanf("%d",&DesireWeight);
    
    StartWeight = (SweetnerMouse/WeightMouse)*DesireWeight;
    SodaPop = (StartWeight/ArtificialSweetner);
    
    printf("Amount of Soda Pop that can kill you is:%.2f",&SodaPop);
    return 0;


   
}
Last edited on
For the first question would I have to use double variable instead of float?

For the second question when I run it, my final answer ends up being like 2320932842042804. I tried changing the format specifier because I don't think my math calculations are wrong.

Would it matter if I don't have the #include <math.h> ?
%d should be used for ints, for both scanf and printf.
%lf should be used to scanf a double.
%f should be used to scanf a float.
%f is used to printf doubles or floats.

printf should just take the variable, not the address of it.

Also SodaPop is an int, so when you divide two doubles, you're bound to get some truncation problems.


More importantly:
You're using C++, you're using C++ headers like <iostream>, so why are you limiting yourself to the fragility of printf and scanf?
C++ has much more safe IO using std::cout and std::cin, although the use of the << operator I'll admit looks kinda weird.

1
2
3
4
5
int     my_int = 3;
double  my_double = 3.14;
float   my_float;
std::cin >> my_float; // get input from console, like scanf.
std::cout << "Hello! " << my_int << " " << my_double  <<" " << my_float << std::endl;



Edit: And inclusion of <math.h> should not matter for what you're doing.
Last edited on
Ganado,

Thanks for your help, I got it fully functional now. I went back and read up more about using the <stdio.h> and <iostream> headers. I never realized the difference in using them with printf and scanf. My professor hasn't used the std::cout/std::cin in my lectures much although he did state cin>>x1 = scanf("%d",&x1).

I joined my class late so I didn't learn much, had to self teach myself everything a bit. But I do have one question, as I google examples and whatnot, they comment frequently. Should I leave comments thorough my code?
Topic archived. No new replies allowed.