Go Back   CodingForums.com > :: Computing & Sciences > Computer Programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rating: Thread Rating: 4 votes, 3.25 average.
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-21-2005, 10:21 AM   PM User | #1
seabreeze
New to the CF scene

 
Join Date: Aug 2005
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
seabreeze is an unknown quantity at this point
two dimensional arrays in C

hey y'all!
so i was working on this little program, and got to the point where i have to let the user enter integers from the keyboard to fill in a 3x3 matrix. I declared my 3x3 matrix as a a two dimensional array thus:

int matrix[3][3];

and also

int i, j; //as counters

to fill this array i did:

for(i=0; i<3; i++) //rows of the matrix
{
for(j=0; j<3; j++) //columns of the matrix
{
printf("Enter a number to fill matrix [%d][%d]: ", i, j);
scanf("%d", &matrix[i][j]);

}

}



but the program won't compile!! I get an error message for the line with "scanf" saying "invalid binary operator"
is that an improper way to fill a two dimensional array from the keyboard during run time?
Please somebody help! I'm very confused here
Thanks a lot!
seabreeze is offline   Reply With Quote
Old 08-22-2005, 11:22 AM   PM User | #2
shmoove
Regular Coder

 
Join Date: Dec 2003
Posts: 367
Thanks: 0
Thanked 0 Times in 0 Posts
shmoove is an unknown quantity at this point
Maybe:
Code:
scanf("%d", &(matrix[i][j]));
shmoove

Edit:
Looking at the operator precedence chart it shouldn't matter since '&' has lower precedence that '[]', so that's probably not it.
shmoove is offline   Reply With Quote
Old 08-22-2005, 06:57 PM   PM User | #3
aman
Regular Coder

 
Join Date: Oct 2004
Posts: 230
Thanks: 0
Thanked 0 Times in 0 Posts
aman is an unknown quantity at this point
What compiler and OS are you using? It compiles just fine on MSVC.
aman is offline   Reply With Quote
Old 08-29-2005, 06:39 PM   PM User | #4
dg_rm
New to the CF scene

 
Join Date: Aug 2005
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
dg_rm is an unknown quantity at this point
here are two ways:

printf("Enter a number to fill matrix [%d][%d]");
scanf("%d",&i,&j);

or
printf("Enter a number to fill matrix [%d][%d]: ", i, j);

i think now you won't be confused
dg_rm is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:40 AM.


Advertisement
Log in to turn off these ads.