I m trying to build a 2*2 array but my program asks for 5 inputs. Why?












0















I couldn't zero down where i have missed in the input statement. I have build it to acquire four values as input but it goes one more.



manipulating the array entries to check if the fifth value is stored. Basic things



#include <stdio.h>

int main()
{
int i,j,a[2][2];
for(i=1;i<=2;i++)
{
for(j=1;j<=2;j++)
scanf("%dt",&a[i][j]);
}

printf("n%dt%dn%dt%d", a[1][1],a[1][2],a[2][1],a[2][2]);
}









share|improve this question

























  • When you declare an array with two entries, like a[2], the only valid indexes are 0 and 1. But the loops and the printf are using indexes 1 and 2. One solution is to make the array bigger int a[3][3].

    – user3386109
    Jan 2 at 9:26






  • 1





    In C the array starts at 0 not 1, so the declared array a[2][2] contains a[0][0] upto a[1][1].

    – Tom Kuschel
    Jan 2 at 9:28













  • a[1][2] and a[2][1] and a[2][2] do not exist. Your array has 4 elements: a[0][0], a[0][1], a[1][0], and a[1][1].

    – pmg
    Jan 2 at 9:28













  • thanks for the advice... but It doesn't seem to work that way.. it is what we define in the for loop as i am able to get the results as expected. When i printed, a[0][0]..a[1][1], all values except a[1][1] had zeroes..

    – F.Fred
    Jan 2 at 10:03













  • Read How to debug small programs.

    – Basile Starynkevitch
    Jan 2 at 10:12
















0















I couldn't zero down where i have missed in the input statement. I have build it to acquire four values as input but it goes one more.



manipulating the array entries to check if the fifth value is stored. Basic things



#include <stdio.h>

int main()
{
int i,j,a[2][2];
for(i=1;i<=2;i++)
{
for(j=1;j<=2;j++)
scanf("%dt",&a[i][j]);
}

printf("n%dt%dn%dt%d", a[1][1],a[1][2],a[2][1],a[2][2]);
}









share|improve this question

























  • When you declare an array with two entries, like a[2], the only valid indexes are 0 and 1. But the loops and the printf are using indexes 1 and 2. One solution is to make the array bigger int a[3][3].

    – user3386109
    Jan 2 at 9:26






  • 1





    In C the array starts at 0 not 1, so the declared array a[2][2] contains a[0][0] upto a[1][1].

    – Tom Kuschel
    Jan 2 at 9:28













  • a[1][2] and a[2][1] and a[2][2] do not exist. Your array has 4 elements: a[0][0], a[0][1], a[1][0], and a[1][1].

    – pmg
    Jan 2 at 9:28













  • thanks for the advice... but It doesn't seem to work that way.. it is what we define in the for loop as i am able to get the results as expected. When i printed, a[0][0]..a[1][1], all values except a[1][1] had zeroes..

    – F.Fred
    Jan 2 at 10:03













  • Read How to debug small programs.

    – Basile Starynkevitch
    Jan 2 at 10:12














0












0








0








I couldn't zero down where i have missed in the input statement. I have build it to acquire four values as input but it goes one more.



manipulating the array entries to check if the fifth value is stored. Basic things



#include <stdio.h>

int main()
{
int i,j,a[2][2];
for(i=1;i<=2;i++)
{
for(j=1;j<=2;j++)
scanf("%dt",&a[i][j]);
}

printf("n%dt%dn%dt%d", a[1][1],a[1][2],a[2][1],a[2][2]);
}









share|improve this question
















I couldn't zero down where i have missed in the input statement. I have build it to acquire four values as input but it goes one more.



manipulating the array entries to check if the fifth value is stored. Basic things



#include <stdio.h>

int main()
{
int i,j,a[2][2];
for(i=1;i<=2;i++)
{
for(j=1;j<=2;j++)
scanf("%dt",&a[i][j]);
}

printf("n%dt%dn%dt%d", a[1][1],a[1][2],a[2][1],a[2][2]);
}






c for-loop






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 2 at 9:24









Mat

166k29316347




166k29316347










asked Jan 2 at 9:21









F.FredF.Fred

62




62













  • When you declare an array with two entries, like a[2], the only valid indexes are 0 and 1. But the loops and the printf are using indexes 1 and 2. One solution is to make the array bigger int a[3][3].

    – user3386109
    Jan 2 at 9:26






  • 1





    In C the array starts at 0 not 1, so the declared array a[2][2] contains a[0][0] upto a[1][1].

    – Tom Kuschel
    Jan 2 at 9:28













  • a[1][2] and a[2][1] and a[2][2] do not exist. Your array has 4 elements: a[0][0], a[0][1], a[1][0], and a[1][1].

    – pmg
    Jan 2 at 9:28













  • thanks for the advice... but It doesn't seem to work that way.. it is what we define in the for loop as i am able to get the results as expected. When i printed, a[0][0]..a[1][1], all values except a[1][1] had zeroes..

    – F.Fred
    Jan 2 at 10:03













  • Read How to debug small programs.

    – Basile Starynkevitch
    Jan 2 at 10:12



















  • When you declare an array with two entries, like a[2], the only valid indexes are 0 and 1. But the loops and the printf are using indexes 1 and 2. One solution is to make the array bigger int a[3][3].

    – user3386109
    Jan 2 at 9:26






  • 1





    In C the array starts at 0 not 1, so the declared array a[2][2] contains a[0][0] upto a[1][1].

    – Tom Kuschel
    Jan 2 at 9:28













  • a[1][2] and a[2][1] and a[2][2] do not exist. Your array has 4 elements: a[0][0], a[0][1], a[1][0], and a[1][1].

    – pmg
    Jan 2 at 9:28













  • thanks for the advice... but It doesn't seem to work that way.. it is what we define in the for loop as i am able to get the results as expected. When i printed, a[0][0]..a[1][1], all values except a[1][1] had zeroes..

    – F.Fred
    Jan 2 at 10:03













  • Read How to debug small programs.

    – Basile Starynkevitch
    Jan 2 at 10:12

















When you declare an array with two entries, like a[2], the only valid indexes are 0 and 1. But the loops and the printf are using indexes 1 and 2. One solution is to make the array bigger int a[3][3].

– user3386109
Jan 2 at 9:26





When you declare an array with two entries, like a[2], the only valid indexes are 0 and 1. But the loops and the printf are using indexes 1 and 2. One solution is to make the array bigger int a[3][3].

– user3386109
Jan 2 at 9:26




1




1





In C the array starts at 0 not 1, so the declared array a[2][2] contains a[0][0] upto a[1][1].

– Tom Kuschel
Jan 2 at 9:28







In C the array starts at 0 not 1, so the declared array a[2][2] contains a[0][0] upto a[1][1].

– Tom Kuschel
Jan 2 at 9:28















a[1][2] and a[2][1] and a[2][2] do not exist. Your array has 4 elements: a[0][0], a[0][1], a[1][0], and a[1][1].

– pmg
Jan 2 at 9:28







a[1][2] and a[2][1] and a[2][2] do not exist. Your array has 4 elements: a[0][0], a[0][1], a[1][0], and a[1][1].

– pmg
Jan 2 at 9:28















thanks for the advice... but It doesn't seem to work that way.. it is what we define in the for loop as i am able to get the results as expected. When i printed, a[0][0]..a[1][1], all values except a[1][1] had zeroes..

– F.Fred
Jan 2 at 10:03







thanks for the advice... but It doesn't seem to work that way.. it is what we define in the for loop as i am able to get the results as expected. When i printed, a[0][0]..a[1][1], all values except a[1][1] had zeroes..

– F.Fred
Jan 2 at 10:03















Read How to debug small programs.

– Basile Starynkevitch
Jan 2 at 10:12





Read How to debug small programs.

– Basile Starynkevitch
Jan 2 at 10:12












2 Answers
2






active

oldest

votes


















2














#include <stdio.h>


int main()
{
int i,j,a[2][2];
for(i=0;i<2;i++) /*go through rows - arrays in c go [0- (n-1)]*/
{
for(j=0;j<2;j++)/*go through col */
scanf("%d",&a[i][j]); /*remove t- now will scan 4 values only */
}

printf("n%dt%dn%dt%d", a[0][0],a[0][1],a[1][0],a[1][1]);
return 0;
}





share|improve this answer
























  • Thanks a lot.. the tab was trouble some 't'. I have one more q. when i added one more matrix with the 't' say b[2][2] in the same program(int i,j,m,n,a[2][2],b[2][2], then the fifth value that got entered after a[1][1] appeared as the first value in the b[0][0]. Where was it stored then?(should i specify the code?)

    – F.Fred
    Jan 2 at 9:49





















4














You have declared this



int a[2][2];


which has four items, a[0][0], a[0][1], a[1][0] and a[1][1].



However, you are starting your indexing at 1 and going up to 2, so are stepping out of bounds which is undefined behaviour.
Anything can then happen.



Change your loops i.e.:



   for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
// as you were


to index from 0.



You also need to consider your printf statement, since that oversteps too.






share|improve this answer


























  • True... Added an edit to point out that needs thought too.

    – doctorlove
    Jan 2 at 9:36











  • Thanks for the help,. when i tried with my original program, i mean for(i=1;i<2;i++), i still get the results with a comment as such " *** stack smashing detected ***: ./a.out terminated Aborted (core dumped)".. Why is that so? should i understand something more?

    – F.Fred
    Jan 2 at 9:59











  • When you tried what with your original program? Have you sorted the printf statement? If you have a core dump, you can load it in the debugger to see what went wrong where. Concentrate on making sure you don't try to access [2] in a - for either index though.

    – doctorlove
    Jan 2 at 10:13











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',
autoActivateHeartbeat: false,
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%2f54003846%2fi-m-trying-to-build-a-22-array-but-my-program-asks-for-5-inputs-why%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














#include <stdio.h>


int main()
{
int i,j,a[2][2];
for(i=0;i<2;i++) /*go through rows - arrays in c go [0- (n-1)]*/
{
for(j=0;j<2;j++)/*go through col */
scanf("%d",&a[i][j]); /*remove t- now will scan 4 values only */
}

printf("n%dt%dn%dt%d", a[0][0],a[0][1],a[1][0],a[1][1]);
return 0;
}





share|improve this answer
























  • Thanks a lot.. the tab was trouble some 't'. I have one more q. when i added one more matrix with the 't' say b[2][2] in the same program(int i,j,m,n,a[2][2],b[2][2], then the fifth value that got entered after a[1][1] appeared as the first value in the b[0][0]. Where was it stored then?(should i specify the code?)

    – F.Fred
    Jan 2 at 9:49


















2














#include <stdio.h>


int main()
{
int i,j,a[2][2];
for(i=0;i<2;i++) /*go through rows - arrays in c go [0- (n-1)]*/
{
for(j=0;j<2;j++)/*go through col */
scanf("%d",&a[i][j]); /*remove t- now will scan 4 values only */
}

printf("n%dt%dn%dt%d", a[0][0],a[0][1],a[1][0],a[1][1]);
return 0;
}





share|improve this answer
























  • Thanks a lot.. the tab was trouble some 't'. I have one more q. when i added one more matrix with the 't' say b[2][2] in the same program(int i,j,m,n,a[2][2],b[2][2], then the fifth value that got entered after a[1][1] appeared as the first value in the b[0][0]. Where was it stored then?(should i specify the code?)

    – F.Fred
    Jan 2 at 9:49
















2












2








2







#include <stdio.h>


int main()
{
int i,j,a[2][2];
for(i=0;i<2;i++) /*go through rows - arrays in c go [0- (n-1)]*/
{
for(j=0;j<2;j++)/*go through col */
scanf("%d",&a[i][j]); /*remove t- now will scan 4 values only */
}

printf("n%dt%dn%dt%d", a[0][0],a[0][1],a[1][0],a[1][1]);
return 0;
}





share|improve this answer













#include <stdio.h>


int main()
{
int i,j,a[2][2];
for(i=0;i<2;i++) /*go through rows - arrays in c go [0- (n-1)]*/
{
for(j=0;j<2;j++)/*go through col */
scanf("%d",&a[i][j]); /*remove t- now will scan 4 values only */
}

printf("n%dt%dn%dt%d", a[0][0],a[0][1],a[1][0],a[1][1]);
return 0;
}






share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 2 at 9:44









H.cohenH.cohen

47018




47018













  • Thanks a lot.. the tab was trouble some 't'. I have one more q. when i added one more matrix with the 't' say b[2][2] in the same program(int i,j,m,n,a[2][2],b[2][2], then the fifth value that got entered after a[1][1] appeared as the first value in the b[0][0]. Where was it stored then?(should i specify the code?)

    – F.Fred
    Jan 2 at 9:49





















  • Thanks a lot.. the tab was trouble some 't'. I have one more q. when i added one more matrix with the 't' say b[2][2] in the same program(int i,j,m,n,a[2][2],b[2][2], then the fifth value that got entered after a[1][1] appeared as the first value in the b[0][0]. Where was it stored then?(should i specify the code?)

    – F.Fred
    Jan 2 at 9:49



















Thanks a lot.. the tab was trouble some 't'. I have one more q. when i added one more matrix with the 't' say b[2][2] in the same program(int i,j,m,n,a[2][2],b[2][2], then the fifth value that got entered after a[1][1] appeared as the first value in the b[0][0]. Where was it stored then?(should i specify the code?)

– F.Fred
Jan 2 at 9:49







Thanks a lot.. the tab was trouble some 't'. I have one more q. when i added one more matrix with the 't' say b[2][2] in the same program(int i,j,m,n,a[2][2],b[2][2], then the fifth value that got entered after a[1][1] appeared as the first value in the b[0][0]. Where was it stored then?(should i specify the code?)

– F.Fred
Jan 2 at 9:49















4














You have declared this



int a[2][2];


which has four items, a[0][0], a[0][1], a[1][0] and a[1][1].



However, you are starting your indexing at 1 and going up to 2, so are stepping out of bounds which is undefined behaviour.
Anything can then happen.



Change your loops i.e.:



   for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
// as you were


to index from 0.



You also need to consider your printf statement, since that oversteps too.






share|improve this answer


























  • True... Added an edit to point out that needs thought too.

    – doctorlove
    Jan 2 at 9:36











  • Thanks for the help,. when i tried with my original program, i mean for(i=1;i<2;i++), i still get the results with a comment as such " *** stack smashing detected ***: ./a.out terminated Aborted (core dumped)".. Why is that so? should i understand something more?

    – F.Fred
    Jan 2 at 9:59











  • When you tried what with your original program? Have you sorted the printf statement? If you have a core dump, you can load it in the debugger to see what went wrong where. Concentrate on making sure you don't try to access [2] in a - for either index though.

    – doctorlove
    Jan 2 at 10:13
















4














You have declared this



int a[2][2];


which has four items, a[0][0], a[0][1], a[1][0] and a[1][1].



However, you are starting your indexing at 1 and going up to 2, so are stepping out of bounds which is undefined behaviour.
Anything can then happen.



Change your loops i.e.:



   for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
// as you were


to index from 0.



You also need to consider your printf statement, since that oversteps too.






share|improve this answer


























  • True... Added an edit to point out that needs thought too.

    – doctorlove
    Jan 2 at 9:36











  • Thanks for the help,. when i tried with my original program, i mean for(i=1;i<2;i++), i still get the results with a comment as such " *** stack smashing detected ***: ./a.out terminated Aborted (core dumped)".. Why is that so? should i understand something more?

    – F.Fred
    Jan 2 at 9:59











  • When you tried what with your original program? Have you sorted the printf statement? If you have a core dump, you can load it in the debugger to see what went wrong where. Concentrate on making sure you don't try to access [2] in a - for either index though.

    – doctorlove
    Jan 2 at 10:13














4












4








4







You have declared this



int a[2][2];


which has four items, a[0][0], a[0][1], a[1][0] and a[1][1].



However, you are starting your indexing at 1 and going up to 2, so are stepping out of bounds which is undefined behaviour.
Anything can then happen.



Change your loops i.e.:



   for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
// as you were


to index from 0.



You also need to consider your printf statement, since that oversteps too.






share|improve this answer















You have declared this



int a[2][2];


which has four items, a[0][0], a[0][1], a[1][0] and a[1][1].



However, you are starting your indexing at 1 and going up to 2, so are stepping out of bounds which is undefined behaviour.
Anything can then happen.



Change your loops i.e.:



   for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
// as you were


to index from 0.



You also need to consider your printf statement, since that oversteps too.







share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 2 at 10:11

























answered Jan 2 at 9:31









doctorlovedoctorlove

14.8k23051




14.8k23051













  • True... Added an edit to point out that needs thought too.

    – doctorlove
    Jan 2 at 9:36











  • Thanks for the help,. when i tried with my original program, i mean for(i=1;i<2;i++), i still get the results with a comment as such " *** stack smashing detected ***: ./a.out terminated Aborted (core dumped)".. Why is that so? should i understand something more?

    – F.Fred
    Jan 2 at 9:59











  • When you tried what with your original program? Have you sorted the printf statement? If you have a core dump, you can load it in the debugger to see what went wrong where. Concentrate on making sure you don't try to access [2] in a - for either index though.

    – doctorlove
    Jan 2 at 10:13



















  • True... Added an edit to point out that needs thought too.

    – doctorlove
    Jan 2 at 9:36











  • Thanks for the help,. when i tried with my original program, i mean for(i=1;i<2;i++), i still get the results with a comment as such " *** stack smashing detected ***: ./a.out terminated Aborted (core dumped)".. Why is that so? should i understand something more?

    – F.Fred
    Jan 2 at 9:59











  • When you tried what with your original program? Have you sorted the printf statement? If you have a core dump, you can load it in the debugger to see what went wrong where. Concentrate on making sure you don't try to access [2] in a - for either index though.

    – doctorlove
    Jan 2 at 10:13

















True... Added an edit to point out that needs thought too.

– doctorlove
Jan 2 at 9:36





True... Added an edit to point out that needs thought too.

– doctorlove
Jan 2 at 9:36













Thanks for the help,. when i tried with my original program, i mean for(i=1;i<2;i++), i still get the results with a comment as such " *** stack smashing detected ***: ./a.out terminated Aborted (core dumped)".. Why is that so? should i understand something more?

– F.Fred
Jan 2 at 9:59





Thanks for the help,. when i tried with my original program, i mean for(i=1;i<2;i++), i still get the results with a comment as such " *** stack smashing detected ***: ./a.out terminated Aborted (core dumped)".. Why is that so? should i understand something more?

– F.Fred
Jan 2 at 9:59













When you tried what with your original program? Have you sorted the printf statement? If you have a core dump, you can load it in the debugger to see what went wrong where. Concentrate on making sure you don't try to access [2] in a - for either index though.

– doctorlove
Jan 2 at 10:13





When you tried what with your original program? Have you sorted the printf statement? If you have a core dump, you can load it in the debugger to see what went wrong where. Concentrate on making sure you don't try to access [2] in a - for either index though.

– doctorlove
Jan 2 at 10:13


















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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54003846%2fi-m-trying-to-build-a-22-array-but-my-program-asks-for-5-inputs-why%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

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas