Code
#include <iostream>
using namespace std;
typedef int intarrayptr;
int main()
{
int d1, d2;
cout << "Enter the row and column [U]demensions[/U] of the array: " << endl; // spell check :P
cin >> d1 >> d2;
intarrayptr *m = new intarrayptr[d1];
int i, j;
for (i=0; i < d1; i++)
m[i]= new int[d2];
//m is now a d1 by d2 array.
cout << "Enter " << d1 << "rows of " << d2 << " integers each: \n";
for (i=0; i < d1; i++)
for(j=0; j < d2; j++)
cin >> m[i][j];
cout << "Echoing the two-dimensional array: \n";
for(i=0; i < d1; i++)
{
for(j=0; j < d2; j++)
cout << m[i][j] << " ";
cout << endl;
}
int c1, c2;
cout << "Enter the row and column demensions of the array: " << endl;
cin >> c1, c2;
intarrayptr *n = new intarrayptr[c1];
int x, z;
for(x=0; x < c1; x++)
n[x] = new int[c2];
//n is now a c1 by c2 array.
cout << "Enter" << c1 << " rows of " << c2 << " integerys each: \n";
for(x=0; x < c1; x++)
for(z=0; z < c2; z++)
cin >> n[x][z];
cout << "Echoing the two-dimensional array: \n";
for(x=0; x < c1; x++)
{
for(z=0; z < c2; z++)
cout << m[x][z] << " ";
cout << endl;
}
if(d1 != c2)
{
cout << "The columns of the first matrices and the rows of the second matrices must match" << endl;
return 0;
}
int temp = d1 * c2;
int temp2 = d2 * c1;
intarrayptr *p = new intarrayptr[temp];
int a,b;
for(a=0; a < temp; a++)
p[a] = new int[temp2];
int a1, b1, z1;
for(a1 = 0; a1 < d1; a1++)
for(b1=0; b1 < c2; b1++)
{
p[a1][b1] = 0;
for(z1=0; z1 < d2; z1++)
p[a1][b1] += m[a1][z1] * n[z1][b1];
}
cout << "The matrix product is: \n";
for(a1=0; a1 < temp; a1++)
{
cout << "\n";
for(b1=0; b1 < temp2; b1++)
//Display the result
cout << " " << p[a1][b1];
}
cout << endl;
}
I found something online that might help
Quote
I believe you have to create an array of pointers to an array of ints to get this to work. Here is an example:
int **array_ptr; //two * are needed because it is a pointer to a pointer
array_ptr=new int*[firstnumber]; //creates a new array of pointers to int objects
for(int i=0; i<firstnumber; ++i)
array_ptr[i]=new int[secondnumber];
//now, you can access the members like you can with normal 2d arrays
//like array_ptr[1][3]
aka, it should be declared as a double pointer.
Seems your allocation would have to be
Code
new int*[blahblahblah]
notice the *
This post was edited by Eep on Oct 18 2012 03:49pm