include "stdio.h"
#include "stdlib.h"
int main()
{
int i,j,k,l; k=l;// Matrix dimensions
int o,p; // iterations of cycles
printf("\t Matrix multiplication by pointers\n");
do {
do{
fflush (stdin);
printf("Enter rows of matrix A: ");
} while (scanf("%d",&i) == 0);
do{
fflush (stdin);
printf("Enter columns of matrix A: ");
} while (scanf("%d",&k) == 0);
printf ("\n\n\n");
do{
fflush (stdin);
printf("Enter rows of matrix B: ");
} while (scanf("%d",&l) == 0);
do{
fflush (stdin);
printf("Enter columns of matrix B: ");
} while (scanf("%d",&j) == 0);
if (k != l) printf ("Wrong -- cannot multiply enter same columns A as rows B");
} while (k != l);
int matrixA[i*k];
int matrixB[k*j];
int matrixC[i*j];
// Pointer inititiation
int *ukA = &matrixA[0];
int *ukB = &matrixB[0];
int *ukC = &matrixC[0];
for (o=0; o
{
do
{
fflush (stdin);
printf("Enter element of matrix A of coordinates a%d: ",o);
}
while (scanf("%d",&(*ukA)) == 0);
ukA++;
}
printf ("\n\n\n");
for (o=0; o
{
do
{
fflush (stdin);
printf("Enter element of matrix B of coordinates b%d: ",o);
}
while (scanf("%d",&(*ukB)) == 0);
ukB++;
}
printf("\n Begining output of matrix A[%d, %d]\n",i,k);
ukA=&matrixA[0]; // reset the pointer adress
for (o=0; o
{
printf(" %d ", *ukA);
if ((o+1)%k==0) { printf ("\n");} // this will create a column
ukA++;
}
printf("\n Zahajuji vypis matice B[%d, %d]\n",k,j);
ukB=&matrixB[0]; // reset the pointer adress
for (o=0; o
{
printf(" %d ", *ukB);
if ((o+1)%j==0) { printf ("\n");} // this will create a column
ukB++;
}
//Cycle for zeroing the matrix C
for (o=0; o
{
*ukC = 0;
ukC++;
}
ukA=&matrixA[0]; // reset the pointer adress
ukB=&matrixB[0]; // reset the pointer adress
ukC=&matrixC[0]; // reset the pointer adress
int *zacA = &matrixA[0]; // this points to the begining of row in A
int *zacB = &matrixB[0]; // this points to the begining of column in B
// This is actual algorithm for multiplication
printf ("\n\n\n");
printf ("Multpng. matrices A[%d, %d] x B[%d, %d] = C[%d, %d] \n",i,k,k,j,i,j);
for (o=0;o
for (p=0;p
*ukC = (*ukA) * (*ukB) + *ukC;
ukA= ukA+1;
ukB = ukB + j;
}
if ((o+1)%j==0)
{
zacA=ukA;
zacB=&matrixB[0];
ukB=zacB;
}
ukB=zacB+((o+1)%j);
ukA = zacA;
ukC++;
}
ukC=&matrixC[0]; // this resets the pointer again
for (o=0; o
{
printf(" %d ", *ukC);
if ((o+1)%j==0) { printf ("\n");} // this will create a column
ukC++;
}
printf("\n\n");
system("pause");
return 0;
}