2d dynamic array error

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2009
Posts: 1
Reputation: McCarrell is an unknown quantity at this point 
Solved Threads: 0
McCarrell McCarrell is offline Offline
Newbie Poster

2d dynamic array error

 
0
  #1
Oct 2nd, 2009
I am trying to reference a 2d dynamic array which i used pointer to pointers to declare.

the constructors run without a problem but as soon as i run another function or an overloaded operator i get errors. Its almost like i am referencing the array in the pointers incorrectly. I am new to this language so i am having some trouble with the syntax that may be simple just no knowledge of it. any help would be greatly appreciated.


here is the implementation file

#include<iostream>
#include "matrixType.h"
using namespace std;

ostream& operator << (ostream& osObject,matrixType& matrix)
{

for(int row = 0; row < matrix.maxRow; row++)
{
for (int col = 0; col < matrix.maxCol; col++)
{
osObject << matrix.matrix[row][col] << " ";
}
}
return osObject;
}

matrixType matrixType::operator +(const matrixType& right)const
{

matrixType temp;
if ((maxRow == right.maxRow) && (maxCol == right.maxCol))
{
for (int row = 0; row < maxRow; row++)
{
for (int col = 0; col < maxCol; col++)
{
temp.matrix[col][row] = matrix[col][row] + right.matrix[col][row];
}
}
}
else
cout << "The matrices dimensions do not match and therefore cannot be added together.";

return temp;
}

matrixType matrixType::operator - (const matrixType& right) const
{
matrixType temp;

if (maxRow == right.maxRow && maxCol == right.maxCol)
{
for (int row = 0; row < maxRow; row++)
{
for (int col = 0; col < maxCol; col++)
{
temp.matrix[col][row] = matrix[col][row] - right.matrix[col][row];
}
}
}
else
cout << "The matrices dimensions do not match and therefore cannot be added together.";
return temp;
}

matrixType matrixType:: operator * (const matrixType& right)const
{
matrixType temp(right.maxCol,maxRow);

if (maxRow == right.maxCol)
{
for(int row = 0; row < maxRow; row++)
{
for(int col = 0; col < maxCol; col++)
{
temp.matrix[col][row] = 0;
for(int col2 = 0; col2 < right.maxCol; col2++)
{
temp.matrix[col][row] = matrix[col2][col] * right.matrix[row][col2];
}

}
}
}
else
cout << "These matrix cannot be multiplied";

return temp;
}

matrixType::matrixType()
{
maxRow = 5;
maxCol = 5;
matrix = new int*[maxRow];
for (int row = 0; row > maxRow; row++)
matrix[row] = new int[maxCol];
for(int i = 0; i > maxRow; i++)
for(int j = 0; j > maxCol; j++)
matrix[j][i] = 0;
}
matrixType::matrixType(int x, int y)
{
maxCol = x;
maxRow = y;
matrix = new int*[maxRow];
for(int row = 0; row > maxRow; row++)
matrix[row] = new int[maxCol];
for(int i = 0; i > maxRow; i++)
for(int j = 0; j > maxCol; j++)
matrix[j][i] = 0;

}
const matrixType& matrixType::operator=(const matrixType& right)
{


if(this != &right)
{
if(maxRow == right.maxRow && maxCol == right.maxCol)
{
for(int i = 0; i < maxRow; i++)
{
for(int j = 0; j < maxCol; j++)
matrix[j][i] = right.matrix[j][i];
}
}
}
return *this;
}
matrixType::~matrixType()
{
delete [] matrix;
}


void matrixType::input()
{
cout << "Enter the elements of the matrix hitting enter after each element: \n";

for(int i = 0; i < maxRow; i++)
{
for(int j = 0; j < maxCol; j++)
cin >> matrix[i][j];
}
}
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,142
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 144
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Veteran Poster

Re: 2d dynamic array error

 
0
  #2
Oct 2nd, 2009
1) Use code tags.

2) This is incorrect :
  1. for (int row = 0; row > maxRow; row++)

It should be :
for (int row = 0; row < maxRow; row++)

You have that mistake in other places, such as both of your constructors. Fix it, and see what happens.
I give up! 
1) What word becomes shorter if you add a letter to it? [ Solved by : niek_e ]
2) What does this sequence  equal to :  (.5u - .5a)(.5u-.5b)(.5u-.5c) ...
3) What is the 123456789 prime numer?
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 440
Reputation: Agni is a jewel in the rough Agni is a jewel in the rough Agni is a jewel in the rough 
Solved Threads: 68
Sponsor
Agni's Avatar
Agni Agni is offline Offline
Posting Pro in Training

Re: 2d dynamic array error

 
0
  #3
Oct 2nd, 2009
  1. matrixType::matrixType()
  2. {
  3. maxRow = 5;
  4. maxCol = 5;
  5. matrix = new int*[maxRow];
  6. for (int row = 0; row > maxRow; row++)
  7. matrix[row] = new int[maxCol];
  8. for(int i = 0; i > maxRow; i++)
  9. for(int j = 0; j > maxCol; j++)
  10. matrix[j][i] = 0;
  11. }

how is the memory getting allocated here? Does it ever go inside the for loops?

edit: late reply
Last edited by Agni; Oct 2nd, 2009 at 6:39 pm.
thanks
-chandra
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC