[Main]
Title=I can't understand C matrices!

[Top]
<TABLE CELLPADDING="4"><TR><TD VALIGN="TOP"><B>Q:</B></TD><TD>
I can't understand matrices in C (not TI-Basic matrices, but matrices which are
the part of the C language)!
</TD></TR><TR><TD VALIGN="TOP"><B>A:</B></TD><TD>
Basically, to declare a matrix, you need to use:
<PRE>type <I>name</I>[<I>dimension1</I>][<I>dimension2</I>];</PRE>
for example:
<PRE>int a[5][5];
</PRE>
And, to access element of the matrix, you need to use:
<PRE><I>name</I>[<I>index1</I>][<I>index2</I>];</PRE>
for example:
<PRE>a[2][3] = 10;</PRE>
or
<PRE>x = a[1][2];
</PRE>
But note that indices are not from 1 to dimension, but from
0 to dimension-1. So, in the above example, both
indices are in range 0 to 4, not 1 to 5. For example to fill all matrix by zeroes,
you can use this code:
<PRE>int a[5][5], i, j;     // <I>A 5x5 array of ints, and two single ints</I><BR>
for(i = 0; i &lt; 5; i++)
  for (j = 0; j &lt; 5; j++)
    a[i][j] = 0;
</PRE>
although the experienced C programmer will simply use
<PRE>memset (a, 0, 5 * 5 * sizeof(int));</PRE>
to make it faster.
</TD></TR></TABLE>
