0

Happy New Year.

I'm working on a C++ project, which goes something like this:

for(s=1; s<=n; s++){
    for (k=2; k<=n; k++) {

        den[k]       =   0;
        den[k]       =   sqrt((abs(a[1][1][x]))*(abs(a[1][1][x])) + (abs(a[k][1][x]))*(abs(a[k][1][x])));

....Some magic happens here

    }
}

What I cannot figure out is how to make the a[y][y][x] array add one to the third cell (i.e. it becomes a[y][y][x+1]) every time the inner loop takes place.

So, for example, let's say n = 3.

Then after the inner for loops takes place once, then add 1 to x. After it takes place again, add 1 again to x. Then the outer loop will take place, and the inner loop will start again; I want to just add another +1 again to the x. So in total, I want to add six 1's (since the inner loop will run 6 times- one for each time).

Thanks in advance.

3
  • 1
    With your code, you can remove the first for and put int s=n, because it will have the same effect. I'm not sure what you want to do, but i wanted to point this out. Commented Jan 3, 2014 at 23:26
  • 1
    This does not pertain to your question, but you don't need the abs() call. Multiplying real number by itself is always positive. Commented Jan 3, 2014 at 23:28
  • Thanks for the comment Paul, but I don't think I can do that since n is a constant integer chosen in the beginning, and s is a variable that has various uses in the array; plus, the restrictions in each loop are going to change. Thanks BWG too, but the original problem requires me to take the absolute value since there will be complex numbers interfering somewhere. Commented Jan 3, 2014 at 23:59

3 Answers 3

1

I'm not 100% sure what you're asking, but I think you're looking for this:

int xDelta = 0;

for(s=1; s<=n; s++){
    for (k=2; k<=n; k++, xDelta++) {

        den[k]       =   0;
        den[k]       =   sqrt((abs(a[1][1][x + xDelta]))*(abs(a[1][1][x + xDelta])) + (abs(a[k][1][x + xDelta]))*(abs(a[k][1][x + xDelta])));

....Some magic happens here

    }
}

Or if you're looking to actually modify the x variable itself...

for(s=1; s<=n; s++){
    for (k=2; k<=n; k++, x++) {
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot Armen, David and Emanuele. All of your ways work and now I can continue smoothly my project. It's quite a complex program since there's a lot of stuff inside the loops, and working on it for 6 hours straight just got my head stuck in a simple problem that I was thinking about for a while... prolly need a break. Thanks again. P.S. Sorry but I can't choose all 3 as the best answers
1

You can use the command

++x;

to increment the value of the variable x.

Comments

1

it looks easy or maybe I didn't get the

for(s=1; s<=n; s++){
int myX = x;
    for (k=2; k<=n; k++) {

        den[k]       =   0;
        den[k]       =   sqrt((abs(a[1][1][x]))*(abs(a[1][1][myX])) + (abs(a[k][1][myX]))*(abs(a[k][1][myX])));

....Some magic happens here
        myX++;
    }
}

Comments

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.