C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 13th, 2012, 10:41 AM
Lan07 Lan07 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 10 Lan07 User rank is Corporal (100 - 500 Reputation Level)Lan07 User rank is Corporal (100 - 500 Reputation Level)Lan07 User rank is Corporal (100 - 500 Reputation Level)Lan07 User rank is Corporal (100 - 500 Reputation Level) 
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.

Reply With Quote
  #2  
Old October 13th, 2012, 11:30 AM
bdb bdb is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 156 bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level) 
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]).

Try defining your array with
Code:
int ar[3][3];

Reply With Quote
  #3  
Old October 13th, 2012, 11:31 AM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,839 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 19 h 1 m 4 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

Reply With Quote
  #4  
Old October 13th, 2012, 11:50 AM
Lan07 Lan07 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 10 Lan07 User rank is Corporal (100 - 500 Reputation Level)Lan07 User rank is Corporal (100 - 500 Reputation Level)Lan07 User rank is Corporal (100 - 500 Reputation Level)Lan07 User rank is Corporal (100 - 500 Reputation Level) 
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.

Reply With Quote
  #5  
Old October 13th, 2012, 12:11 PM
bdb bdb is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 156 bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level) 
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).

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.

Reply With Quote
  #6  
Old October 13th, 2012, 12:47 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,376 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 12 h 21 m 42 sec
Reputation Power: 383
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!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Array input problem

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

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


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap