 # matrix opreation in c

Write a C program for the matrix operations. In particular,
write the following functions:

a) Function
int ** allocMatrix(int m, int n)
that will allocates, using malloc, space for an mXn matrix.

b) Function
void freeMatrix(int **a, int m)
to free up the memory allocated for an mXn matrix.
(Why do we not need “n” as an argument here?)

c) Function
void readMatrix(int **a, int m, int n)

d) Function
void printMatrix(int **a, int m, int n)
that prints an mXn matrix

e) Function
int ** mulMatrix(int **a, int **b, int m, int n, int p)
that multiplies an mXn matrix with an nXp matrix. The
result is an mXp matrix.

f) Function
int ** mul3Matrix(int **a, int **b, int **c,
int m, int n, int p, int q)
that multiplies 3 matrices a, b and c of sizes mXn,
nXp and pXq respectively.

Use mulMatrix to compute intermediate result. Any
intermediate matrix should be freed once its use is over.

Your main function should use these functions to read three matrices and
multiply them.

A sample run is (user inputs should be obvious) :

\$ ./a.out
enter the number m of rows of first matrix: 3
enter the number n of columns of first matrix/rows of second matrix: 2
enter the number n of columns of second matrix/rows of third matrix: 1
enter the number p of columns of third matrix: 2
enter the first matrix:
enter  th entry: 2
enter  th entry: 3
enter  th entry: 4
enter  th entry: 2
enter  th entry: 4
enter  th entry: 5
enter the second matrix:
enter  th entry: 2
enter  th entry: 3
enter the third matrix:
enter  th entry: 1
enter  th entry: 0
The product of the matrices:
2 3
4 2
4 5
and
2
3
and
1 0
is
13 0
14 0
23 0

It sounds like a homework. If you want to learn programming, you have to try yourself !!!

better ask for concept than the sol. directly. Anyways… if you’r using c++ then

``````a=new int*[m];
for(int i=0;i<m;i++)
a[m]=new int[n];
``````

line#1 allocates int pointer array of size m. Use malloc if that is needed.
for-loop: allocates int array to each int pointer allocated above.

``````a->  ->  ...[n-1]
->  ...[n-1]
.
.
[m-1]->  ...[n-1]
``````

Now, if you know the concept you can free the array and do other operations with it.
Look through net for multiplation algos. hope it helps you to get started.

//