#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    10
    Rep Power
    0

    Array input problem


    Hello everybody i am pretty newbie in c and general on programming so take it easy with me..
    So my question is
    i have here this code
    PHP Code:
    #include <stdio.h>
    #include <stdlib.h>

    main(){
           
    int ar[2][2];
           
    int i,j;
           for (
    i=0;i<=2;i=i+1)
               for (
    j=0;j<=2;j=j+1){
                   
    printf(" i = %d j = %d   ",i,j); 
                   
    scanf("%d",&ar[i][j]);
                   }
           
           for (
    i=0;i<=2;i=i+1){
               
    printf("\n");
               for (
    j=0;j<=2;j=j+1)
                   
    printf("%d for i= %d, j=  %d     ",ar[i][j],i,j);
           }
           
           
           
    printf("\nwrong values are ..  %d   %d   %d",ar[0][2],ar[1][2],ar[2][2]);
                  
    scanf("%d",&i);

    i put in the values 1,2,3 ... to 9 respectivly but it outputs me this h t t p: / / imageshack.us/photo/my-images/840/006503.jpg/

    i didnt know any other way to refer people to this image so i had to put this link
    Thanks.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    156
    Rep Power
    33
    The array (of arrays) defined with
    Code:
    int ar[2][2];
    has 4 elements. Those elements are: ar[0][0], ar[0][1], ar[1][0], and ar[1][1]. You are trying to access elements that do not exist, namely those with 2 as one of the indexes (eg ar[0][2], or ar[2][2]).

    Try defining your array with
    Code:
    int ar[3][3];
  4. #3
  5. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,379
    Rep Power
    1871
    > for (i=0;i<=2;i=i+1)
    The valid subscripts of ar[2][2] are [0][0], [0][1], [1][0] and [1][1]

    You're running off the ends of the array.

    Use <, not <= in all your loops.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    10
    Rep Power
    0
    Really thank you for the help.
    BUT
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    main(){
           int ar[3][3];
           int i,j;
           for (i=1;i<=3;i=i+1)
               for (j=1;j<=3;j=j+1){
                   printf(" i = %d j = %d   ",i,j); 
                   scanf("%d",&ar[i][j]);
                   }
           
           for (i=1;i<=3;i=i+1){
               printf("\n");
               for (j=1;j<=3;j=j+1)
                   printf("%d for i= %d, j=  %d     ",ar[i][j],i,j);
           }
           
           
           printf("\nwrong values are ..  %d   %d   %d",ar[1][3],ar[2][3],ar[3][3]);
                  scanf("%d",&i);
    }
    can you explain me why this one works?(ar[1,2,3,][3] dont exist as you said)
    And on the previous code how the computer got access to the ar[0][2] ar[1][2]..
    and put those specific (wrong) values, was it random values?
    Again thanks for the help.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    156
    Rep Power
    33
    > Can you explain why this one works?
    Accessing elements that do not exist, in C, is Undefined Behavior, meaning the compiler can do anything: it can complain and stop compilation, it can complain but compile anyway. Furthermore the resulting executable (if any) can do anything: it can crash, it can work with apparent errors, or it can work as you would expect (the most difficult UB to catch).

    To answer your question: that one works because you were unlucky.

    I see you changed the loop to start accessing the array elements at index 1 (it was index 0 in your previous post). In C, array indexes start at 0. Your first post was correct in this regard.
  10. #6
  11. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,841
    Rep Power
    480
    How many non-negative integers are there less than 3 ?

    0 1 2

    That's right, 3 of them.

    Less than n where n is a non-negative integer?

    That's right, n of them.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo