Alright so I'm completely stuck I don't understand my assignment.
My biggest problem is learning what gets passed where since our instructor has us working
across 2 .cpp files and a header file. I think I might be able to write the code if I understood HOW it was being passed into and accessed between the files.
The matrix.cpp file I created on my own but didn't know how to build the remaining functions.

matrix.h

#ifndef __MATRIX_H__
#define __MATRIX_H__

#include <iostream>

using namespace std;

#define MAXROWS 3
#define MAXCOLS 3

class Matrix { 
    private:
        int elements[MAXROWS][MAXCOLS];
    public:
        Matrix();
        void operator++();
        void operator+=(int array[MAXROWS][MAXCOLS]);
        Matrix operator-(Matrix& other) const;
        Matrix operator+(Matrix& other) const;
        void display(); 
};

#endif

matrix.cpp

#include <iostream>
#include "matrix.h"
#include <string>

using namespace std;

#define MAXROWS 3
#define MAXCOLS 3

Matrix::Matrix()
{
        int elements[MAXROWS][MAXCOLS] = { {0} }; // Tried to initialize all to 0
}

void Matrix::operator++()
{
        //I need to increment the array to {1,2,3,4,5,6,7,8,9} using array1 in main
}

void Matrix::operator+=(int array[MAXROWS][MAXCOLS])
{
       
}

Matrix Matrix::operator-(Matrix& other) const
{

}

Matrix Matrix::operator+(Matrix& other) const
{

}

void Matrix::display()
{
        for(int i = 0; i < MAXROWS; i++)
        {
                for(int j = 0; j < MAXCOLS; j++)
                {

                        cout << "[ " << elements[i][j] << " ]";
                }
        cout << endl;
        }
        cout << endl;

}

main.cpp

#include <iostream>
#include "matrix.h"

using namespace std;

#define MAXROWS 3
#define MAXCOLS 3

int main()
{
    Matrix matrix1, matrix2, matrix3, matrix4;
    
    int array1[MAXROWS][MAXCOLS] = 
    {
      {1, 2, 3},
      {4, 5, 6}, 
      {7, 8, 9}
    };

    int array2[MAXROWS][MAXCOLS] = 
    {
      {10, 11, 12},
      {13, 14, 15}, 
      {16, 17, 18}
    };


    cout << "Matrix object 'matrix1' instantiated:" << endl;
    matrix1.display();    //How do I instantiate matrix1?
    
    cout << "2D array1 added to matrix1 using += " << endl;
    matrix1 += array1;
    matrix1.display();   

    // Use overloaded assignment operator to copy the values across
    cout << "2D array2 added to matrix2 using += " << endl;
    matrix2 += array2;
    matrix2.display();

    cout << "All elements of matrix1 incremented with ++ " << endl;
    ++matrix1;
    matrix1.display();

    cout << "Using addition and subtraction operators" << endl;
    matrix3 = matrix1 + matrix2; 
    matrix4 = matrix2 - matrix1; 
    
    cout << "matrix3 = matrix1 + matrix2" << endl;
    matrix1.display();
    cout << "+" << endl;
    matrix2.display(); 
    cout << "--------------" << endl;
    matrix3.display(); 
    
    cout << "matrix4 = matrix2 - matrix1" << endl;
    matrix2.display();
    cout << "-" << endl;
    matrix1.display(); 
    cout << "--------------" << endl;
    matrix4.display(); 
    
    
    return 0;                
}

Sorry for throwing all the code out there I just wanted to be thorough.
Also the fact that it's a 2d array is confusing me even more.
I've been at this for hours but I have never done anything like this and my searches have always come up dry....what do you guys suggest?

Recommended Answers

All 4 Replies

Four simple rules, which are slightly harder to combine in practice than their simplicity suggests.

1) When you call a function, you effectively initialize each parameter from the corresponding argument.

2) There is no such thing as a two-dimensional array; what looks like a two-dimensional array is really an array of arrays.

3) When you use the name of an array in an expression, what you get is usually a pointer to the initial element of the array. The only exception is when the array is used as an argument to unary &, or to initialize a reference to an array.

4) When you write a function parameter with an array type, that parameter gets quietly translated to the corresponding pointer type.

Here is an example of how these rules combine. When you write

int array[MAXROWS][MAXCOLS];

you are really defining an array with MAXROWS elements. Each of these elements is an array of MAXCOLS elements.

Suppose you now write

void Matrix::operator+=(int array[MAXROWS][MAXCOLS]){ }

Here, array is declared as if it were an array of MAXROWS elements, and it is a function parameter, so it is automatically translated into

void Matrix::operator+=(int (*array)[MAXCOLS]){ }

In other words, array becomes a pointer to an array of MAXCOLS elements.

When you call it:

matrix2 += array2;

array2 automatically becomes a pointer to the first subarray of array2, so it is as if you had written

matrix2 += &array2[0];

Does this help?

You don't pass data from one array to another. You can pass an array to a function. That array is the address in memory of the first element. One way or another, for the function to use the array, it needs the address where the array is located. There are lots of good threads on pointers, arrays, pointers versus arrays, when they should be thought of as the same and when not, but it all boils down to passing the address and, sometimes, the array dimension(s).

Since you have constant array dimensions, it's pretty easy. Create a second constructor that takes an array as an argument and do a copy.

#include <iostream>
using namespace std;

#define MAXROWS 3
#define MAXCOLS 3


class Matrix 
{ 
    private:
        int elements[MAXROWS][MAXCOLS];
    public:
        Matrix();
        Matrix(const int array[MAXROWS][MAXCOLS]);
        void display(); 
};


Matrix::Matrix()
{
}


Matrix::Matrix(const int array[MAXROWS][MAXCOLS])
{
    for(int i = 0; i < MAXROWS; i++)
    {
        for(int j = 0; j < MAXCOLS; j++)
        {
            elements[i][j] = array[i][j];
        }
    }
}


void Matrix::display()
{
    for(int i = 0; i < MAXROWS; i++)
    {
        for(int j = 0; j < MAXCOLS; j++)
        {

            cout << "[ " << elements[i][j] << " ]";
        }
        cout << endl;
    }
    cout << endl;
}


int main()
{  
    int array1[MAXROWS][MAXCOLS] = 
    {
      {1, 2, 3},
      {4, 5, 6}, 
      {7, 8, 9}
    };

    Matrix matrix1(array1);

    cout << "Matrix object 'matrix1' instantiated:" << endl;
    matrix1.display();
   
    return 0;                
}

Thanks a lot arkoenig it really helped seeing it in that new light :)
Also Thank you VeronDozier this explains why I have been getting really random numbers...because they have been addresses!!

So far some success I still need to do the addition and subtraction of the arrays so here is my current output as of your advice :)

Matrix object 'matrix1' instantiated:
[ 587353336 ][ 32767 ][ 1157364736 ]
[ 32659 ][ 4197936 ][ 0 ]
[ 0 ][ 0 ][ 587353328 ]

2D array1 added to matrix1 using +=
[ 1 ][ 2 ][ 3 ]
[ 4 ][ 5 ][ 6 ]
[ 7 ][ 8 ][ 9 ]

2D array2 added to matrix2 using +=
[ 10 ][ 11 ][ 12 ]
[ 13 ][ 14 ][ 15 ]
[ 16 ][ 17 ][ 18 ]

All elements of matrix1 incremented with ++
[ 1 ][ 2 ][ 3 ]
[ 4 ][ 5 ][ 6 ]
[ 7 ][ 8 ][ 9 ]

Using addition and subtraction operators
matrix3 = matrix1 + matrix2
[ 1 ][ 2 ][ 3 ]
[ 4 ][ 5 ][ 6 ]
[ 7 ][ 8 ][ 9 ]

+
[ 10 ][ 11 ][ 12 ]
[ 13 ][ 14 ][ 15 ]
[ 16 ][ 17 ][ 18 ]

--------------
[ 587353352 ][ 32767 ][ 587353336 ]
[ 32767 ][ 1 ][ 0 ]
[ 6296436 ][ 0 ][ 0 ]

matrix4 = matrix2 - matrix1
[ 10 ][ 11 ][ 12 ]
[ 13 ][ 14 ][ 15 ]
[ 16 ][ 17 ][ 18 ]

-
[ 1 ][ 2 ][ 3 ]
[ 4 ][ 5 ][ 6 ]
[ 7 ][ 8 ][ 9 ]

--------------
[ 1143661232 ][ 32659 ][ 1157260464 ]
[ 32659 ][ 1155067176 ][ 32659 ]
[ -1 ][ 0 ][ 1 ]

Thanks to you guys I'm finally making some progress :) I can't thank you enough!!

>> Also Thank you VeronDozier this explains why I have been getting really random numbers...because they have been addresses!!

Actually no. Originally they were simply uninitialized integers, which can be anything, but they weren't addresses. The addresses are a separate issue. Your constructor didn't take an array as a parameter.

You may want to initialize them all to 0 in your null constructor.

>> So far some success I still need to do the addition and subtraction of the arrays so here is my current output as of your advice :)

Matrix object 'matrix1' instantiated:
[ 587353336 ][ 32767 ][ 1157364736 ]
[ 32659 ][ 4197936 ][ 0 ]
[ 0 ][ 0 ][ 587353328 ]


If you are using MY advice, you shouldn't be getting that output. You should get 1 through 9. Those look like the random uninitialized numbers from your original code. Post the updated code if you're still getting that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.