Index variable does not incremenet at array shifting
up vote
-2
down vote
favorite
I want to shift the elements of a 2D array by n-elements to the right. My array has 26 Elements.
This doens't seem to work, and I don't know why.
i = 2;
c = 0;
for (i; i < 26; i++)
{
array[1][i-1] = array[1][c];
c++;
}
But I am overwriting every Element at Index i with the element of the array[0][0] and not with the next elemetn.
The indexvariable c doesn't seem to increment and I don't really know why.
Thankfull for any input.
c
add a comment |
up vote
-2
down vote
favorite
I want to shift the elements of a 2D array by n-elements to the right. My array has 26 Elements.
This doens't seem to work, and I don't know why.
i = 2;
c = 0;
for (i; i < 26; i++)
{
array[1][i-1] = array[1][c];
c++;
}
But I am overwriting every Element at Index i with the element of the array[0][0] and not with the next elemetn.
The indexvariable c doesn't seem to increment and I don't really know why.
Thankfull for any input.
c
3
This is an excellent moment to learn to use a debugger to see what is going on.
– Paul Ogilvie
Nov 15 at 12:02
Please edit your post and provide a Minimal, Complete, and Verifiable example.
– Antti Haapala
Nov 15 at 12:03
and no, you're not overwriting with the element ofarray[0][0]
butarray[1][0]
... that's what your loop does.
– Antti Haapala
Nov 15 at 12:05
You should add more information about the purpose. You could modify the pointer to the first element instead of checking and shifting the whole "2D array".
– Jose
Nov 15 at 12:08
@Jose unless it's not a pointer, and rather an actual array (or in this case, an actual array of arrays). Regardless, this algorithm is dreadful. It looks like pure guessing rather than drawing up some boxes and arrows on a scratch pad, then writing the code to match.
– WhozCraig
Nov 15 at 12:11
add a comment |
up vote
-2
down vote
favorite
up vote
-2
down vote
favorite
I want to shift the elements of a 2D array by n-elements to the right. My array has 26 Elements.
This doens't seem to work, and I don't know why.
i = 2;
c = 0;
for (i; i < 26; i++)
{
array[1][i-1] = array[1][c];
c++;
}
But I am overwriting every Element at Index i with the element of the array[0][0] and not with the next elemetn.
The indexvariable c doesn't seem to increment and I don't really know why.
Thankfull for any input.
c
I want to shift the elements of a 2D array by n-elements to the right. My array has 26 Elements.
This doens't seem to work, and I don't know why.
i = 2;
c = 0;
for (i; i < 26; i++)
{
array[1][i-1] = array[1][c];
c++;
}
But I am overwriting every Element at Index i with the element of the array[0][0] and not with the next elemetn.
The indexvariable c doesn't seem to increment and I don't really know why.
Thankfull for any input.
c
c
asked Nov 15 at 11:57
MathewN
11
11
3
This is an excellent moment to learn to use a debugger to see what is going on.
– Paul Ogilvie
Nov 15 at 12:02
Please edit your post and provide a Minimal, Complete, and Verifiable example.
– Antti Haapala
Nov 15 at 12:03
and no, you're not overwriting with the element ofarray[0][0]
butarray[1][0]
... that's what your loop does.
– Antti Haapala
Nov 15 at 12:05
You should add more information about the purpose. You could modify the pointer to the first element instead of checking and shifting the whole "2D array".
– Jose
Nov 15 at 12:08
@Jose unless it's not a pointer, and rather an actual array (or in this case, an actual array of arrays). Regardless, this algorithm is dreadful. It looks like pure guessing rather than drawing up some boxes and arrows on a scratch pad, then writing the code to match.
– WhozCraig
Nov 15 at 12:11
add a comment |
3
This is an excellent moment to learn to use a debugger to see what is going on.
– Paul Ogilvie
Nov 15 at 12:02
Please edit your post and provide a Minimal, Complete, and Verifiable example.
– Antti Haapala
Nov 15 at 12:03
and no, you're not overwriting with the element ofarray[0][0]
butarray[1][0]
... that's what your loop does.
– Antti Haapala
Nov 15 at 12:05
You should add more information about the purpose. You could modify the pointer to the first element instead of checking and shifting the whole "2D array".
– Jose
Nov 15 at 12:08
@Jose unless it's not a pointer, and rather an actual array (or in this case, an actual array of arrays). Regardless, this algorithm is dreadful. It looks like pure guessing rather than drawing up some boxes and arrows on a scratch pad, then writing the code to match.
– WhozCraig
Nov 15 at 12:11
3
3
This is an excellent moment to learn to use a debugger to see what is going on.
– Paul Ogilvie
Nov 15 at 12:02
This is an excellent moment to learn to use a debugger to see what is going on.
– Paul Ogilvie
Nov 15 at 12:02
Please edit your post and provide a Minimal, Complete, and Verifiable example.
– Antti Haapala
Nov 15 at 12:03
Please edit your post and provide a Minimal, Complete, and Verifiable example.
– Antti Haapala
Nov 15 at 12:03
and no, you're not overwriting with the element of
array[0][0]
but array[1][0]
... that's what your loop does.– Antti Haapala
Nov 15 at 12:05
and no, you're not overwriting with the element of
array[0][0]
but array[1][0]
... that's what your loop does.– Antti Haapala
Nov 15 at 12:05
You should add more information about the purpose. You could modify the pointer to the first element instead of checking and shifting the whole "2D array".
– Jose
Nov 15 at 12:08
You should add more information about the purpose. You could modify the pointer to the first element instead of checking and shifting the whole "2D array".
– Jose
Nov 15 at 12:08
@Jose unless it's not a pointer, and rather an actual array (or in this case, an actual array of arrays). Regardless, this algorithm is dreadful. It looks like pure guessing rather than drawing up some boxes and arrows on a scratch pad, then writing the code to match.
– WhozCraig
Nov 15 at 12:11
@Jose unless it's not a pointer, and rather an actual array (or in this case, an actual array of arrays). Regardless, this algorithm is dreadful. It looks like pure guessing rather than drawing up some boxes and arrows on a scratch pad, then writing the code to match.
– WhozCraig
Nov 15 at 12:11
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
Take a piece of paper and write down what happens step by step.
Like:
i = 2;
c = 0;
array[1][1] = array[1][0];
c=1;
i=3;
array[1][2] = array[1][1];
c=2;
i=4;
array[1][3] = array[1][2];
. . .
As you can see you start by assigning value of element 0 to element 1, then you assign value of element 1 to element 2. Since the value of element 1 is the same as value of element 0, element 2 now holds the same value as element 0 and element 1. So in the end, you actually assigns the value of element 0 to all other elements.
The solution is to start from the other end. That is:
array[1][25] = array[1][24];
array[1][24] = array[1][23];
array[1][23] = array[1][22];
. . .
1
Ironically, now they don't need to work through it on paper...
– paddy
Nov 15 at 12:27
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
Take a piece of paper and write down what happens step by step.
Like:
i = 2;
c = 0;
array[1][1] = array[1][0];
c=1;
i=3;
array[1][2] = array[1][1];
c=2;
i=4;
array[1][3] = array[1][2];
. . .
As you can see you start by assigning value of element 0 to element 1, then you assign value of element 1 to element 2. Since the value of element 1 is the same as value of element 0, element 2 now holds the same value as element 0 and element 1. So in the end, you actually assigns the value of element 0 to all other elements.
The solution is to start from the other end. That is:
array[1][25] = array[1][24];
array[1][24] = array[1][23];
array[1][23] = array[1][22];
. . .
1
Ironically, now they don't need to work through it on paper...
– paddy
Nov 15 at 12:27
add a comment |
up vote
0
down vote
Take a piece of paper and write down what happens step by step.
Like:
i = 2;
c = 0;
array[1][1] = array[1][0];
c=1;
i=3;
array[1][2] = array[1][1];
c=2;
i=4;
array[1][3] = array[1][2];
. . .
As you can see you start by assigning value of element 0 to element 1, then you assign value of element 1 to element 2. Since the value of element 1 is the same as value of element 0, element 2 now holds the same value as element 0 and element 1. So in the end, you actually assigns the value of element 0 to all other elements.
The solution is to start from the other end. That is:
array[1][25] = array[1][24];
array[1][24] = array[1][23];
array[1][23] = array[1][22];
. . .
1
Ironically, now they don't need to work through it on paper...
– paddy
Nov 15 at 12:27
add a comment |
up vote
0
down vote
up vote
0
down vote
Take a piece of paper and write down what happens step by step.
Like:
i = 2;
c = 0;
array[1][1] = array[1][0];
c=1;
i=3;
array[1][2] = array[1][1];
c=2;
i=4;
array[1][3] = array[1][2];
. . .
As you can see you start by assigning value of element 0 to element 1, then you assign value of element 1 to element 2. Since the value of element 1 is the same as value of element 0, element 2 now holds the same value as element 0 and element 1. So in the end, you actually assigns the value of element 0 to all other elements.
The solution is to start from the other end. That is:
array[1][25] = array[1][24];
array[1][24] = array[1][23];
array[1][23] = array[1][22];
. . .
Take a piece of paper and write down what happens step by step.
Like:
i = 2;
c = 0;
array[1][1] = array[1][0];
c=1;
i=3;
array[1][2] = array[1][1];
c=2;
i=4;
array[1][3] = array[1][2];
. . .
As you can see you start by assigning value of element 0 to element 1, then you assign value of element 1 to element 2. Since the value of element 1 is the same as value of element 0, element 2 now holds the same value as element 0 and element 1. So in the end, you actually assigns the value of element 0 to all other elements.
The solution is to start from the other end. That is:
array[1][25] = array[1][24];
array[1][24] = array[1][23];
array[1][23] = array[1][22];
. . .
answered Nov 15 at 12:25
4386427
20.1k31745
20.1k31745
1
Ironically, now they don't need to work through it on paper...
– paddy
Nov 15 at 12:27
add a comment |
1
Ironically, now they don't need to work through it on paper...
– paddy
Nov 15 at 12:27
1
1
Ironically, now they don't need to work through it on paper...
– paddy
Nov 15 at 12:27
Ironically, now they don't need to work through it on paper...
– paddy
Nov 15 at 12:27
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53319004%2findex-variable-does-not-incremenet-at-array-shifting%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
This is an excellent moment to learn to use a debugger to see what is going on.
– Paul Ogilvie
Nov 15 at 12:02
Please edit your post and provide a Minimal, Complete, and Verifiable example.
– Antti Haapala
Nov 15 at 12:03
and no, you're not overwriting with the element of
array[0][0]
butarray[1][0]
... that's what your loop does.– Antti Haapala
Nov 15 at 12:05
You should add more information about the purpose. You could modify the pointer to the first element instead of checking and shifting the whole "2D array".
– Jose
Nov 15 at 12:08
@Jose unless it's not a pointer, and rather an actual array (or in this case, an actual array of arrays). Regardless, this algorithm is dreadful. It looks like pure guessing rather than drawing up some boxes and arrows on a scratch pad, then writing the code to match.
– WhozCraig
Nov 15 at 12:11