Hello.
For 2D matrix multiplication, i tried a new method, which uses only 2 for loops.
(im basically using a 1D array to access the elements).
Here is the code:
// Mutiplication.cpp : Defines the entry point for the console application.
//
#include <stdio.h>
int main(void)
{
int a[] = {2, 2, 1, 2};
int b[] = {2, 2, 2, 1, 1, 1};
int ans[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int NoOfColsMat2 = 3; // Number of columns in Matrix2
int noOfColsMat1 = 2; // Number of rows in Matrix1
int m = -(noOfColsMat1), k = 0; // m and k are used as indices
for (int p = 0; p < NoOfColsMat2 * noOfColsMat1; p++) {
k = p % noOfColsMat1;
m = ((p % NoOfColsMat2) == 0) ? (m += noOfColsMat1) : (m);
for (int j = 0; j < noOfColsMat1; j++) {
ans[p] = ans[p] + (a[m] * b[k]);
k += NoOfColsMat2;
m++;
}
//Reduce m
m -= noOfColsMat1;
}
for (int i = 0; i < NoOfColsMat2 * noOfColsMat1; i++) {
printf(i % NoOfColsMat2 ? "" : "\n");
printf("%d ", ans[i]);
}
getchar();
return 0;
}
[B]So basically,
{2, 2, 1, 2} is :
2 2
1 2
and
{2, 2, 2, 1, 1, 1} is :
2 2 2
1 1 1[/B]
Can anyone please tell me how i can analyze this against the brute force method(of having 2D arrays and 3 for loops).
Thanks in advance.