#include <iostream>
#define LAMBDA_0 5
#define THRESHOLD 2
#define BETA 0.1 // the step
usingnamespace std;
float net (float x) {
/* This is the unknown net function.
In order to run the program and have the
expected behaviour, we can just use the identity
function, i.e. a function which receives 'x' and
returns 'x' */
return x;
}
int main(int argc, charconst *argv[])
{
// the initial value
constfloat L_0 = net (LAMBDA_0);
float lambda = LAMBDA_0;
float L = L_0;
while (L > THRESHOLD) {
cout << "Condition not met" << endl;
lambda = lambda - BETA * L;
cout << "New value of lambda = " << lambda << endl;
L = net (lambda);
cout << "New value of L = " << L << endl;
cout << endl;
}
cout << "Terminated." << endl;
return 0;
}
and this is the corresponding output:
Condition not met
New value of lambda = 4.5
New value of L = 4.5
Condition not met
New value of lambda = 4.05
New value of L = 4.05
Condition not met
New value of lambda = 3.645
New value of L = 3.645
Condition not met
New value of lambda = 3.2805
New value of L = 3.2805
Condition not met
New value of lambda = 2.95245
New value of L = 2.95245
Condition not met
New value of lambda = 2.65721
New value of L = 2.65721
Condition not met
New value of lambda = 2.39148
New value of L = 2.39148
Condition not met
New value of lambda = 2.15234
New value of L = 2.15234
Condition not met
New value of lambda = 1.9371
New value of L = 1.9371
Minomic, you are an absolute life saver. Could you just do me two more favors? Could you simply explain what you did there in simple english because I am gonna have to present this work. Second favor, what's your paypal e-mail??
First of all I defined a net function. I know that the text says that the programmer does not know it, but in order to run the program we must have something we can use. The net function has to mimick the behaviour which is described in the text, so it has to be monotone increasing with the argument. This is the reason why I chose a simple net(x) = x. I could have used other functions too, like net(x) = x+1, net(x) = 2x, and so on.
Then I defined at the beginning of the file three quantities: the initial value of lambda (LAMBDA_0), the THRESHOLD (which is the value I want to reach) and BETA (which can be seen as a 'descending step' towards the threshold).
In the main I then set these initial condition with
1 2
float lambda = LAMBDA_0;
float L = L_0;
Now the while loop: while the condition is not met (which means 'while the value of L is still higher than the THRESHOLD I want to reach'), I calculate a new (lower) value of lambda by subtracting BETA*L. Once I have this new value, I give it to my net function to calculate the new value of L. At the following iteration, this new L is tested to verify whether it is still higher than the threshold or we can stop.
I know, I think so too. But in many years of "forum attendance" (if this is the right way to say it), I learnt that sometimes what students need is something to start from. Hopefully they can become interested in what they see and start reading something on their own.
Anyway, let's be clear: no one can be forced to learn. They can be forced to study, but it is a much sadder thing. And another thing I really believe in is this one: we can't save everyone! :)
This means the following: if programming is something which will be required for one's job, then if you haven't studied it at school you will have to study it later. Otherwise you will be fired. On the other hand, if programming is something you have to do just at school and you don't need it for your life, then it's up to you to choose whether to spend some time on it as a hobby or not.