Multiplying Dynamic Arrays
This code will compile and give my results, although I don't if this is correct. If I make the array 2X2, and I make each matrix:
1,1
1,1
2,2
2,2
I get this as a result:
3,6
3,6
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
|
#include <iostream>
using namespace std;
int main()
{
//Preconditions: Arrays must have equal dimensions
//Postconditions: Multiplies the contents of the two arrays
int a1[20][20];
int a2[20][20];
int product[20][20];
int d1,d2;
int i,j,k;
cout << "Enter the number of row and column dimensions of the array: \n";
cin >> d1 >> d2;
cout << "Enter the elements in your first matrix\n";
for (i = 0; i < d1; i++)
{
for (j = 0; j < d2; j++)
{
cout << "Element " << i << "," << j << " = ";
cin >> a1[i][j];
}
}
cout << "Enter the elements in your second matrix : \n";
for (i = 0; i < d1; i++)
{
for (j = 0; j < d2; j++)
{
cout << "Element " << i << "," << j <<" = ";
cin >> a2[i][j];
}
}
cout << "First Matrix \n";
for (i = 0; i < d1; i++)
{
for (j = 0; j < d2; j++)
{
cout << " " << a1[i][j];
}
cout << "\n";
}
cout << "Second Matrix \n";
for (i = 0; i < d1; i++)
{
for (j = 0; j < d2; j++)
{
cout << " " << a2[i][j];
}
cout << "\n";
}
cout << "The product of two matrices \n";
for(i = 0; i < d1; i++)
{
for(j = 0; j < d2; j++)
{
product[i][j] = 0;
for(k = 0; k < d1; k++)
{
product[i][j] = product[i][j] + a1[i][k] * a2[k][j];
}
}
}
for (i = 0; i < d1; i++)
{
for (j = 0; j < d2; j++)
{
cout << " " << product[i][j];
}
cout << "\n";
}
return 0;
}
|
My problem is that I think the multiplication is off...
Interesting. When I run the program, I'm getting the correct result of an all-4 matrix. :/
Topic archived. No new replies allowed.