Matrix Add

/* Program MAT_ADD.C
**
** Illustrates how to add two 3X3 matrices.
**
** Peter H. Anderson, Feb 21, '97
*/

#include <stdio.h>

void add_matrices(int a[][3], int b[][3], int result[][3]);
void print_matrix(int a[][3]);

void main(void)
{
   int p[3][3] = {  {1, 3, -4}, {1, 1, -2}, {-1, -2, 5}  };
   int q[3][3] = {  {8, 3, 0}, {3, 10, 2}, {0, 2, 6}  };
   int r[3][3];

   add_matrices(p, q, r);

   printf("\nMatrix 1:\n");
   print_matrix(p);

   printf("\nMatrix 2:\n");
   print_matrix(q);

   printf("\nResult:\n");
   print_matrix(r);
}

void add_matrices(int a[][3], int b[][3], int result[][3])
{
   int i, j;
   for(i=0; i<3; i++)
   {
	 for(j=0; j<3; j++)
	 {
	    result[i][j] = a[i][j] + b[i][j];
	 }
   }
}

void print_matrix(int a[][3])
{
   int i, j;
   for (i=0; i<3; i++)
   {
	 for (j=0; j<3; j++)
	 {
	    printf("%d\t", a[i][j]);
	 }
	 printf("\n");
   }
}