If i have a, for example, 3x3 matrix, i declare it like this:

int array [] [3] = { {1,2,3}, {4,5,6},{7,8,9}}

But what if my matrix is like this:

{{1,2},{3,4,5,6,7,8},{1}}


Also, why is this:


Code:
{
   int array [3] [3] = {{1,2,3},{4,5,6}, {7,8,9}};
   printf ( "%d %d\n\n", array [3][1], array [3][2]);
}


displaying random numbers in printf instead of the array's elements?

also, this:
printf ( "%d %d\n\n", array [1][1], array [1][2]);

displays 5 and 6. what am i doing wrong?

EDIT:
If this is of any help, I used this:

Code:
{
int i,j;
   int array [] [3] = {{1,2,3},{4,5,6},{7,8,9}};
   for (i=0; i<=3 ; i++)
      for (j=0; j<=3 ; j++)
      {
      printf( "%d ", array [i][j]);
      if (j==3)
      putchar ( '\n');
      }

}


to display the array, and the rows and columns always have 4 elements

if the number of rows and columns is smaller than 4 in the declaration, some numbers get a random value, if the number is greater or equal to 4, they get 0.

EDIT: Nevermind my second question, I solved it. The first one is still open tho
The main thing that you are doing wrong is counting starting at 1. When you create an array[3], this creates an array of 3 elements, which starts counting from 0.

array[0] is the first element
array[1] is the middle element
array[2] is the last element

Just subtract 1 from you values.

Also, you cannot create an array like {{1,2},{3,4,5,6,7,8},{1}} because the dimensions do not line up. Throw some padding or use a 1D array if need be. Hope this helps!

EDIT: Also, if you want to change the size of your dimensions at runtime, that is also possible without having to specify the size of the array when you compile. If you need this, just ask.
Thanks, that helped
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement