Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

#1
October 13th, 2012, 10:41 AM
 Lan07
Registered User

Join Date: Oct 2012
Posts: 10
Time spent in forums: 8 h 16 m 23 sec
Reputation 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
October 13th, 2012, 11:30 AM
 bdb
Contributing User

Join Date: Aug 2012
Posts: 156
Time spent in forums: 1 Week 15 h 48 m 11 sec
Reputation Power: 32
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]).

Code:
`int ar[3][3];`

#3
October 13th, 2012, 11:31 AM
 salem
Contributed User

Join Date: Jun 2005
Posts: 3,905
Time spent in forums: 2 Months 3 Weeks 4 Days 1 h 2 m 3 sec
Reputation Power: 1774
> 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

#4
October 13th, 2012, 11:50 AM
 Lan07
Registered User

Join Date: Oct 2012
Posts: 10
Time spent in forums: 8 h 16 m 23 sec
Reputation 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.

#5
October 13th, 2012, 12:11 PM
 bdb
Contributing User

Join Date: Aug 2012
Posts: 156
Time spent in forums: 1 Week 15 h 48 m 11 sec
Reputation Power: 32
> 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).

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.

#6
October 13th, 2012, 12:47 PM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 3,458
Time spent in forums: 1 Month 2 Weeks 4 Days 6 h 26 m 43 sec
Reputation Power: 403
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!

 Viewing: Dev Shed Forums > Programming Languages > C Programming > Array input problem