Having problems with a loop

B

Brian

void generation_loop()
{
int generation = 1;
double x = 0.25;
double y = 0.50;
double z = 0.25;
double AA, AB, BB;
double p = (y/(y+z))/2;
AA = pow(p,2);
AB = 2*p*(1-p);
BB = pow(1-p,2);
double p2 = (AB /(AB+BB))/2;


for (generation = 1; generation <= 30; generation++)
{
cout << "Generation " << generation << " ";
cout << x << " "<< y << z << endl;
}
}
I've been pulling my hair out on this. For each time a generation has
passed I want a new value computed. The prevous value should be saved
in x,y,z and then recomputed with the new values.
 
B

Brian

void generation_loop()
{
int generation = 1;
double x = 0.25;
double y = 0.50;
double z = 0.25;
double AA, AB, BB;
double p = (y/(y+z))/2;
AA = pow(p,2);
AB = 2*p*(1-p);
BB = pow(1-p,2);
double p2 = (AB /(AB+BB))/2;


for (generation = 1; generation <= 30; generation++)
{
cout << "Generation " << generation << " ";
cout << AA << " "<< AB << BB << endl;
x = AA;
y = AB;
z = BB;
}
}

The updated problem. maybe a little more clear.
 
B

Brian

void generation_loop()
{
int generation = 1;
double x = 0.25;
double y = 0.50;
double z = 0.25;
double AA, AB, BB;
double p = (y/(y+z))/2;
AA = pow(p,2);
AB = 2*p*(1-p);
BB = pow(1-p,2);
double p2 = (AB /(AB+BB))/2;


for (generation = 1; generation <= 30; generation++)
{
cout << "Generation " << generation << " ";
cout << AA << " "<< AB << BB << endl;
x = AA;
y = AB;
z = BB;
}
}

The updated problem. maybe a little more clear.
 
S

Scott McPhillips [MVP]

Brian said:
void generation_loop()
{
int generation = 1;
double x = 0.25;
double y = 0.50;
double z = 0.25;
double AA, AB, BB;
double p = (y/(y+z))/2;
AA = pow(p,2);
AB = 2*p*(1-p);
BB = pow(1-p,2);
double p2 = (AB /(AB+BB))/2;


for (generation = 1; generation <= 30; generation++)
{
cout << "Generation " << generation << " ";
cout << AA << " "<< AB << BB << endl;
x = AA;
y = AB;
z = BB;
}
}

The updated problem. maybe a little more clear.

If you are expecting AA, AB, and BB to change each time around the loop
then the computation of them has to be inside the loop.
 
M

Marco Wahl

Brian said:
void generation_loop()
{
int generation = 1;
double x = 0.25;
double y = 0.50;
double z = 0.25;
double AA, AB, BB;
double p = (y/(y+z))/2;
AA = pow(p,2);
AB = 2*p*(1-p);
BB = pow(1-p,2);
double p2 = (AB /(AB+BB))/2;


for (generation = 1; generation <= 30; generation++)
{
cout << "Generation " << generation << " ";
cout << x << " "<< y << z << endl;
}
}
I've been pulling my hair out on this. For each time a generation has
passed I want a new value computed. The prevous value should be saved
in x,y,z and then recomputed with the new values.

Do you want to do something like the following?

[[[
#include <iostream>
using namespace std;

double calculateNextGeneration(double recent)
{
return 0.5 * recent; // Arbitrary rule to get some new value.
}

int main()
{
int generation = 1;
double someParameter = 0.42;
double AA = 42.0;
double lastAA = AA;
for (generation = 1; generation <= 30; generation++)
{
cout << "Generation " << generation << " ";
cout << AA << endl;
if(generation > 1)
cout << "BTW: The last generation was: " << lastAA << "." << endl;
lastAA = AA;
AA = calculateNextGeneration(AA);
}
return 0;
}
]]]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top