d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Pointer Issue In Dynamic Arrays/matrices > Halp. Will Pay Fgz
Add Reply New Topic New Poll
Member
Posts: 23,261
Joined: Dec 17 2006
Gold: 18,129.00
Oct 18 2012 12:51pm
The goal of the project is to allow the user to enter the size of 2 different matrices. It should then multiply these two matrices and put the answer in an answer matrices.

Here is the code I have so far

#include <iostream>
using namespace std;

typedef int intarrayptr;

int main()
{
int d1, d2;
cout << "Enter the row and column demensions of the array: " << endl;
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;
}

This should work in theory, but I have the errors "invalid conversion from int* to int on the bolded lines. As a result of this invalid conversion I get the error "invalid types int[int] for array subscript on lines in red.

Hopefully my code and the way I displayed my errors is efficient enough for ya'll to help and read. I have d3 igg I can convert to fg if needed.

Thanks a ton guys!

edit: Of course jsp would mess up my code blocks. Sorry guize.

This post was edited by PureOwnage2 on Oct 18 2012 12:57pm
Member
Posts: 4,605
Joined: Sep 15 2011
Gold: 9,464.00
Oct 18 2012 01:22pm
use [code] tags
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Oct 18 2012 03:36pm
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
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll