Results 1 to 4 of 4

Thread: need help with creating an m x n matrix

  1. #1
    Join Date
    Jan 2010
    Posts
    63
    Qt products
    Qt4
    Platforms
    Windows

    Default need help with creating an m x n matrix

    I need help to creater allocate space.
    Here is what I have
    Qt Code:
    1. class Array
    2. {
    3. friend ostream &operator<<( ostream &, const Array & );
    4. friend istream &operator>>( istream &, Array & );
    5. public:
    6. Array( int = 10,int =10 );
    7. Array( const Array & );
    8. ~Array();
    9. // inequality operator; returns opposite of == operator
    10. bool operator!=( const Array &right ) const
    11. {
    12. return ! ( *this == right ); // invokes Array::operator==
    13. } // end function operator!=
    14.  
    15. // subscript operator for non-const objects returns modifiable lvalue
    16. int &operator[]( int );
    17.  
    18. // subscript operator for const objects returns rvalue
    19. int operator[]( int ) const;
    20. private:
    21. int size, m,n; // pointer-based array size
    22. int a[10][10]; // pointer to first element of pointer-based array
    23. }; // end class Array
    To copy to clipboard, switch view to plain text mode 

    My code so far
    Qt Code:
    1. #include <iostream>
    2. #include <iomanip>
    3. using namespace std;
    4. #include "array.h"
    5.  
    6.  
    7. Array::Array( int m, int n )
    8. {
    9. size = ( m*n > 0 ? m*n : 10 ); // validate arraySize
    10. a[m][n] =*new int[size]; // create space for pointer-based array
    11.  
    12. for ( int i = 0; i < m ;i++ )
    13. for ( int j = 0; j <n; j++ )
    14. a[ i ][j] = 0; // set pointer-based array element
    15. }
    16.  
    17.  
    18. Array::~Array()
    19. {
    20. delete *[]a;
    21. }
    22.  
    23. Array::Array( const Array &arrayToCopy )
    24. : size( arrayToCopy.size )
    25. {
    26. a[m][n]= *new int[ size ];
    27.  
    28. for (int i = 0; i < n; i++ )
    29. for (int j = 0; i < m; j++ )
    30. a[ i ][j] = arrayToCopy.a[ i ][j];
    31.  
    32. }
    33.  
    34.  
    35.  
    36. int main()
    37. {
    38. Array mat1;
    39. Array mat2(4,5);
    40. }
    To copy to clipboard, switch view to plain text mode 

    When I run it, it runs ok . But if I implement the destructor , it crashes. I think my memory allocation for a is probably screwy too.. Helpl

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: need help with creating an m x n matrix

    You need not delete a, ( delete *[]a in the dtor. You have declared a as int a[10][10]; which is allocated on stack and not heap.
    Also your whole program seems to be of wrong concepts. I would suggest you refer some good C++ / Data structure book and try some example from it.

    One more thing, if you are practising arrays, then fine. Else theres QList class in Qt for such things.

  3. #3
    Join Date
    Jan 2010
    Posts
    63
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: need help with creating an m x n matrix

    I am doing an overloading operator exercise from C+ How To Program using multidimension array. I am starting with his Array example and modifying it.

  4. #4
    Join Date
    Jan 2006
    Location
    Frankfurt
    Posts
    500
    Thanks
    1
    Thanked 52 Times in 52 Posts
    Platforms
    MacOS X Unix/X11

    Default Re: need help with creating an m x n matrix

    As long as you do not deal with very big matrices, It does not make much sense to fall back to the C-style arrays in favor of the C++-style collections from either the STL or Qt's own container classes. To implement a matrix I suggest to use QVectors and have a vector of vectors to implement the two dimensions. Depending on your application, you can decide wether to have a list of rows or a list of columns.
    It's nice to be important but it's more important to be nice.

Similar Threads

  1. matrix for QBrush
    By navi1084 in forum Qt Programming
    Replies: 5
    Last Post: 5th February 2010, 11:27
  2. Any suggestions of Matrix library with Qt?
    By Sheng in forum Qt Programming
    Replies: 3
    Last Post: 5th February 2010, 03:48
  3. Dot Matrix Printer
    By estanisgeyer in forum Qt Programming
    Replies: 3
    Last Post: 18th December 2009, 19:30
  4. Image processing via matrix
    By jones.79 in forum Qt Programming
    Replies: 10
    Last Post: 22nd September 2008, 00:42
  5. dynamic matrix of QStrings
    By QiT in forum Newbie
    Replies: 19
    Last Post: 4th April 2007, 09:26

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.