2

I'm sorry this is an extremely easy question for you guys however since I have no one I can talk to about it I thought I should ask it here. I've only been learning C++ for a few days now from a book and I am trying to define matrix elements through two loops for the matrix "Smat" as given in the code below. For each 'k' rows and 'j' columns I want the value "Stemp" to be put into the matrix value "Smat[k][j]". However, when I run the code below the code only uses k = 0 and ignores k = 1, k = 2, etc. (it does manage to go through all the j values thankfully). If someone can tell me where I went wrong I'd really appreciate it. This question is very trivial but unfortunately there's no one I can talk to about it in real life with. Thanks in advance.

unsigned long int j = 1;
unsigned long double M = pow(2, 8);
double T = 1;
double dW;
double dt = T / M;
double Smat[100][256]; // Define rows/cols of Smat
double sigma;
double c = -2;
double mu = 2;
double Stemp = S_0;
double theta = 0.01;

for (int k = 0; k < 100; k++) {
    Stemp = S_0;
    Smat[k][0] = Stemp; // All first rows of Smat start with S_0 value

    for (j = 1; j < M; j++) {
        double nrv = normaldist();
        dW = sqrt(dt)*nrv;
        sigma = sigatm + c /(max(sqrt(T), theta))* log(Stemp / S_0);
        Stemp = Stemp + Stemp*mu*dt + Stemp*sigma*dW;
        Smat[k][j] = Stemp;
        }
}

1 Answer 1

2
for (j; j < M; j++) {

This loop fails to initialize j. On the first outer loop, j still has the initial value of 1, that it was initially assigned; but on the second outer loop, j will still be equal to M, from the last iteration, and the inner loop won't iterate at all.

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

2 Comments

Although I initially misread the code, this is still a problem, so I updated the explanation.
Thanks, I've changed it now. And the code works! Thanks heaps for that!

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.