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.










share|improve this question


















  • 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] 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










  • @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















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.










share|improve this question


















  • 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] 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










  • @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













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.










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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 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










  • @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




    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] 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










  • @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












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];
. . .





share|improve this answer

















  • 1




    Ironically, now they don't need to work through it on paper...
    – paddy
    Nov 15 at 12:27











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















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

























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];
. . .





share|improve this answer

















  • 1




    Ironically, now they don't need to work through it on paper...
    – paddy
    Nov 15 at 12:27















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];
. . .





share|improve this answer

















  • 1




    Ironically, now they don't need to work through it on paper...
    – paddy
    Nov 15 at 12:27













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];
. . .





share|improve this answer












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];
. . .






share|improve this answer












share|improve this answer



share|improve this answer










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














  • 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


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

How to change which sound is reproduced for terminal bell?

Can I use Tabulator js library in my java Spring + Thymeleaf project?

Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents