Code:
void multiply()
{
for(i=1;i<=2;i++)
{
for(j=1;j<=2;j++)
{
for(k=1;k<=2;k++)
{
sum=sum+(arrayA[i][k]*arrayB[k][j]);
}
answer[i][j]=sum;
cout<<sum<<" sum "<<endl;
sum=0;
}
}
}
1) Are you aware that arrays have zero based indexes, i.e. they start at index 0? The rows start at index 0, and the columns in every row start at index 0. You are skipping the entire first row of a matrix as well as the first spot in every row, which is a waste of memory--although only a tiny bit is wasted.
2) You don't need three for-loops to multiply a couple of 2 x 2 matrices together--you should only need two for-loops. I assume you want to multiply each value in one matrix by the value in the same spot in the other matrix. Let's take a look at what your three loops do:
Code:
i=1
j=1
k=1
arrayA[1,1] * arrayB[1,1] //ok
k=2
arrayA[1,2] * arrayB[2,1] //??
j=2
k=1
arrayA[1,1] * arrayB[1,2] //??
k=2
arrayA[1,2] * arrayB[2,2] //??
i=2
etc., etc.
Is that what you want?
3) To output an array to look like a matrix, in your case, use two for-loops. The outer loop will specify the row, and the inner loop will specify the column in the row. You can use cout in the inner loop to output the value at that position, and follow it with a space: " ". Just after the closing bracket of the inner for loop, which is before the outer loop has a chance to increment the row, use cout to start a newline: cout<<endl;