1

I know this is a basic question, but I'm a Python user very new to C++

I need to compute a function (cosmology.bias_eff) using a for loop for which the input parameter (Mass_min) needs to be multiplied by an array (M_ratio).

I am making a mistake here by using two for loops.

vector<double> M_ratio = {1.0,0.9,1.1,1.05,1.1,1.15,1.2,1.25,1.2};
double Mass_min = 2e13;
double Delta0 = 200.0;

for (redshift=0.0;redshift<1.7;redshift=redshift+0.2)
{   
    for (size_t i=0; i<M_ratio.size(); i++)
    {
        double Delta = cosmology.DeltaR(Delta0, redshift);

        double bias_eff = cosmology.bias_eff(M_ratio[i]*Mass_min,redshift,Delta);

        cout << bias_eff <<"," << endl;
    }
}

What I want is, for each redshift in the for loop, I want Mass_min to be multiplied by M_ratio,

i.e. for redshift = 0.0, Mass_min = Mass_min*1.0, for redshift = 0.2, Mass_min = 0.9*Mass_min and so on.

1 Answer 1

7

You need only a single loop:

vector<double> M_ratio = {1.0,0.9,1.1,1.05,1.1,1.15,1.2,1.25,1.2};
double redshift;
int i;
for (redshift=0.0,i=0; i<M_ratio.size(); i++,redshift+=0.2) {
  double Delta = cosmology.DeltaR(Delta0, redshift);
  double bias_eff = cosmology.bias_eff(M_ratio[i]*Mass_min,redshift,Delta);
  cout << bias_eff <<"," << endl;
}

On each iteration, redshift in incremented by 0.2, at the same time i is incremented by one. Inside the loop the needed computation takes place.

Sign up to request clarification or add additional context in comments.

1 Comment

but shouldn't it be for (double redshift = 0.0, i=0;) instead of int ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.