Can someone please explain this program

I found this from a ebook. cannot take an idea whats it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
using namespace std;

int main()
{
    float goal[4];
    goal[0]=0.9;
    goal[1]=0.75;
    goal[2]=0.5;
    goal[0]=0.9;
    
    float weight, target;
    
    cout << "Enter Current Weight : ";
    cin >> weight;
    cout << "Enter goal weight : ";
    cin >> target;
    cout << "\n";
    
    for (int i=0; i<4; i++)
    {
        float loss = (weight - target)* goal[i];
        cout << "Goal : " << i << " : ";
        cout << weight - loss << "\n";
        }
        return 0;
    }
Last edited on
closed account (o3hC5Di1)
Hi there,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
using namespace std;

int main()
{
    float goal[4];  //array of 4 floating point numbers
    goal[0]=0.9;
    goal[1]=0.75;
    goal[2]=0.5;
    goal[0]=0.9;  //sure that this shouldn't be goal[3] = 0.25?
    
    float weight, target; 
    
    cout << "Enter Current Weight : ";  //get data from the user and store it in relevant variables
    cin >> weight;   //for instance 20
    cout << "Enter goal weight : ";
    cin >> target; //for instance 10
    cout << "\n";
    
    for (int i=0; i<4; i++)  //traversing the array, ie do the following for every element in goal[]
    {
        float loss = (weight - target)* goal[i];  // ie 20-10=10*0.5 = 5 (calculate how much is to be lost per goal
        cout << "Goal : " << i << " : ";  //print goal, ie 0.5
        cout << weight - loss << "\n";  //print weight - loss, ie 20-5 = 15
        }
        return 0;
    }


It basically asks the user for an initial weight and a target weight, then it displays 4 stages of loosing weight calculated according to the values in the goal[] array.

Hope that helps.

All the best,
NwN
Can you give me a same exact question to this, then it will help me to try and better understand
Last edited on
closed account (o3hC5Di1)
Hi there,

Sorry I don't understand what you mean by:

prabhanuka wrote:
Can you give me a same exact question to this


Could you clarify that please?

All the best,
NwN
Actually I need a question to try. Like this.
Last edited on
closed account (o3hC5Di1)
Do you mean like this:

Design a program that will ask the user for his current weight and his target weight after weight loss.
Then display the target weight for every 4 phases of the weight-loss program, knowing that at the first phase a loss of 10% is expected, 25% at the second phase, 50% at the third, ... ?

All the best,
NwN
I think now I got it. Thanks alot
Topic archived. No new replies allowed.