Re: A89: matrix in C


[Prev][Next][Index][Thread]

Re: A89: matrix in C




Basically, to declare a matrix, you need to use:

type name[dimension1][dimension2];

for example:

int a[5][5];

And, to access element of the matrix, you need to use:

name[index1][index2];

for example:

a[2][3]=10;

or

x=a[1][2];

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.

To fill all matrix by zeroes, you can use this code (more
clear than the code given by Robin):

int main ( void )
{
  int a[5][5],i,j; // A 5x5 array of ints, and a single one
  for(i=0;i<5;i++)
    for(j=0;j<5;j++)
      a[i][j]=0;
}

although the experienced C programmer will simply use:

memset(a,0,25*sizeof(int));

Zeljko Juric



Follow-Ups: