Code:
#include <stdlib.h>
#include <stdio.h>
#include<conio.h>
//int rows = 20;
//int cols = 30;
int rows;
int cols;
int i,j;
//void readin( int *a[][cols], int rows, int cols); /* declare function 'readin' */
//void compute(int *a[][cols], int rows,int cols, int *b[][cols], int *c[][cols]); /* declare function 'compute' */
//void printout(int *a[][cols], int rows,int cols, int *b[][cols], int *c[][cols]); /* declare function 'printout' */
void readin( int **a, int rows, int cols); /* declare function 'readin' */
void compute(int **a, int rows,int cols, int **b, int **c); /* declare function 'compute' */
void printout(int **a, int rows,int cols, int **b, int **c); /* declare function 'printout' */
main() /* start of main */
{
//int *a[rows][cols]; /* array 'a' */
//int *b[rows][cols]; /* array 'b' */
//int *c[rows][cols]; /* array 'c' */
int **a; /* array 'a' */
int **b; /* array 'b' */
int **c; /* array 'c' */
printf("Please enter no of rows ");
scanf("%d",&rows);
printf("Please enter no of cols ");
scanf("%d",&cols);
a = malloc(rows*sizeof(int *));
b = malloc(rows*sizeof(int *));
c = malloc(rows*sizeof(int *));
for ( i = 0 ; i < rows ; i++ ) /* memory allocation */
{
a[i] = malloc(cols*sizeof(int));
b[i] = malloc(cols*sizeof(int));
c[i] = malloc(cols*sizeof(int));
//for ( j = 0 ; j < cols ; j++ )
//{
//a[i][j] = (int*) malloc(cols * sizeof(int));
//b[i][j] = (int*) malloc(cols * sizeof(int));
//c[i][j] = (int*) malloc(cols * sizeof(int));
//}
}
clrscr(); /* clear screen */
readin( a, rows, cols); /* call function 'readin' to obtain contents of array'a' */
readin( b, rows, cols); /* call function 'readin' to obtain contents of array'b'*/
compute(a,rows, cols,b,c); /* call function 'compute' */
printout(a,rows, cols,b,c); /* call function 'printout' */
for ( i = 0 ; i < rows ; i++ ) /* memory allocation */
{
free(a[i]);
free(b[i]);
free(c[i]);
}
free(a);
free(b);
free(c);
} /* end of main */
//void readin(int *a[][cols], int rows, int cols) /* function 'readin' */
void readin(int **a, int rows, int cols) /* function 'readin' */
{
int i,j;
printf("\nEnter values for Array \n\n");
for ( i = 0 ; i < rows ; i++ ) /* read in array 'a' */
{
for ( j = 0 ; j < cols ; j++ )
{
printf( "%d,%d ",i, j ) ;
scanf( "%d" ,(*(a + i) +j) ) ;
}
}
clrscr(); /* clear screen */
} /* end of function 'readin' */
//void compute(int *a[][cols], int rows, int cols, int *b[][cols], int *c[][cols]) /* function 'compute' */
void compute(int **a, int rows, int cols, int **b, int **c) /* function 'compute' */
{
int i,j;
for ( i = 0 ; i < rows ; i++ ) /* Compute contents of Array 'ca' */
{
for ( j = 0 ; j < cols ; j++ )
{
//*(*(c + i) +j) = *(*(a + i) +j) + *(*(a + i) +j) ;
*(*(c + i) +j) = *(*(a + i) +j) + *(*(b + i) +j) ;
}
}
printf("\n");
} /* end of function 'compute' */
//void printout(int *a[][cols], int rows, int cols, int *b[][cols], int *c[][cols]) /* function 'printout' */
void printout(int **a, int rows, int cols, int **b, int **c) /* function 'printout' */
{
int i,j;
printf("\tArray\t\tArray\t\tArray \n");
printf("\ta \t\tb\t\tc \n");
printf("\t-------------------------------------\n");
for ( i = 0 ; i < rows ; i++ )
{
for ( j = 0 ; j <cols ; j++ )
{
printf("\t%d\t+\t%d\t =\t%d",*(*(a + i) +j), *(*(b + i) +j), *(*(c + i) +j) ) ; /* array 'a' + array 'b' = array 'c' */
printf("\n");
}
}
printf("\n");
} /* end of function 'printout' */