0

I have coded this program to get two matrices and in a different function, it calculates the addition of the two matrices, but when I want to get the two arrays as input arguments in my "sum" function it generates errors and it doesn't work.

My efforts include reviewing this question at Stackoverflow : Passing a 2D array to a C++ function It helps if only I set A and B arrays with numerical dimensions like : A[2][2] not A[ar][ac]. I want the function to work for n*n matrices not up to a limited number.

#include <iostream>
#include <iomanip>
using namespace std;

template <size_t row, size_t column, size_t row2, size_t column2 >
void sum(int(&array)[row][column], int(&array2)[row2][column2]);

int main()
{
    int ar, ac, br, bc;
    
    cout << "Welcome to my matrix calculator. \nPlease enter the values of you matrices.";
    cout << "\n\n\tFor your FIRST matrix;";
    cout << "\n\tNumber of \'ROWS\' : ";
    cin >> ar;
    cout << "\tNumber of \'COLUMNS\' : ";
    cin >> ac;
    
    int A[ar][ac];
    
    cout << "\n\tPlease enter the values of your matrix." << endl << endl;
    
    for (int i=0; i<ar; i++)
    {
        for (int j=0; j<ac; j++)
        {
            int m = i;
            m++;
            int n = j;
            n++;
            
            cout << "\tNumber[" << m << "][" << n << "] : ";
            cin >> A[i][j];
        }
        cout << endl;
    }
    
    
    cout << "\n\tFor your SECOND matrix;";
    cout << "\n\tNumber of \'ROWS\' : ";
    cin >> br;
    cout << "\tNumber of \'COLUMNS\' : ";
    cin >> bc;
    
    int B[br][bc];
    
    cout << "\n\tPlease enter the values of your matrix." << endl << endl;
    
    for (int i=0; i<br; i++)
    {
        for (int j=0; j<bc; j++)
        {
            int m = i;
            m++;
            int n = j;
            n++;
            
            cout << "\tNumber[" << m << "][" << n << "] : ";
            cin >> B[i][j];
        }
        cout << endl;
    }
    

    cout << "\nYour FIRST matrix is : " << endl;
    
    for (int i=0; i<ar; i++)
    {
        cout << endl  << "\t" << "|";
        for (int j=0; j<ac; j++)
        {
            cout << setw(4) << A[i][j];
        }
        cout << "  |";
    }
    
    cout << "\n\nYour SECOND matrix is : " << endl;
    
    for (int i=0; i<br; i++)
    {
        cout << endl  << "\t" << "|";
        for (int j=0; j<bc; j++)
        {
            cout << setw(4) << B[i][j];
        }
        cout << "  |";
    }
    
    sum(A, B);
}


template <size_t row, size_t column, size_t row2, size_t column2 >
void sum(int(&array)[row][column], int(&array2)[row2][column2])
{
    if(row==row2 && column==column2)
    {
        cout << "\n\n\tThe addition of your matrices is = ";
        int add[row][column];
        
        for(int i=0; i<row; i++)
        {
            for(int j; j<column; j++)
            {
                add[i][j] = array[i][j] + array2[i][j];
            }
        }
        
        for(int i=0; i<row; i++)
        {
            cout << endl  << "\t" << "|";
            for(int j=0; j<column; j++)
            {
                cout << setw(4) << add[i][j];
            }
            cout << "  |";
        }
    }
    
    else
    {
        cout<< "Matrixes with different rows and columns can \'not\' be added together.";
    }
}

I did try whatever was on the internet but none of work for n*n matrices, could you please help? Thanks in advance!

Anonymous
  • 25
  • 4
  • 1
    `int A[ar][ac];` isn't valid c++. – πάντα ῥεῖ Dec 16 '22 at 16:07
  • 1
    This is impossible in Standard C++ and extremely hard in all of the Variable Length Array allowing extended C++ implementations that I know of. Use [something like this instead](https://stackoverflow.com/a/36123944/4581301). – user4581301 Dec 16 '22 at 16:08
  • 1
    IMO The best way is to model your matrices as what they are matrix classes and pass those around. E.g. make your 2D vector a member of a class called Matrix and use a 2d array inside it e.g. std::vector> values. But if you can just use a matrix/math library, then you can reuse tested code and focus at your real problem. – Pepijn Kramer Dec 16 '22 at 16:08
  • 1
    Bah! `vector` of `vector`s have severe performance problems due to poor spatial locality. – user4581301 Dec 16 '22 at 16:09
  • @πάνταῥεῖ I have already declared and initialized ar and ac. – Anonymous Dec 16 '22 at 16:13
  • 1
    @Anonymous it's not standard c++ though: https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard?r=Saves_AllUserSaves – πάντα ῥεῖ Dec 16 '22 at 16:14
  • 1
    *I did try whatever was on the internet* -- `int A[ar][ac];` -- And by using the internet as the way to learn C++ instead of using good C++ books, you ran right into the invalid use of arrays. This is the danger of going to websites that use this invalid code. Every *good* C++ *book* would never show arrays declared this way -- why? -- because it isn't valid C++. – PaulMcKenzie Dec 16 '22 at 16:22
  • @PaulMcKenzie Thanks for your tip, but I learned C++ by book, searched on websites to solve my current problen, but I have seen codes like this : 'int size=10; int Array[size]; That's why I coded so. – Anonymous Dec 16 '22 at 16:27
  • 1
    @Anonymous *but I have seen codes like this : 'int size=10; int Array[size];* -- Where did you see such code? That's my whole point -- you got this code from a dodgy and crap websites that supposedly "teach C++". Look in the C++ books you say you are using -- do any of them show arrays being declared this way? – PaulMcKenzie Dec 16 '22 at 16:29
  • @PaulMcKenzie I wrote the whole code on my own, not using any website, looked for a way to get a 2D array in a function on the net, could you be as kind as to say if I don't declare A like: A[n1][n2] how should I declare it to get as much numbers as wanted? I don't insist my code is right, but I didn't know any other way. Thanks. – Anonymous Dec 16 '22 at 16:33
  • 1
    Dynamic arrays are done by using `std::vector`. This was mentioned already in the previous comments. – PaulMcKenzie Dec 16 '22 at 16:34

1 Answers1

1

You could just interpret a 1-dimensional array as two-dimensional (one row after another)

// To allocate the array, multiply the number of rows by the number of columns
int *A = new int[ar * ac];
int *B = new int[br * bc];

// To access [i][j], multiply i by the number of columns, and add j
A[(i*ac) + j] = x;

sum(A, ar, ac, B, br, bc);

// don't forget to delete the arrays (because you new'd them)
delete[] A;
delete[] B;

// ....

void sum (int *array1, size_t r1, size_t c1, int *array2, size_t r2, size_t c2) {
   // check for compatibility, nested loop and then ....  
   add[c1 * i + j] = array1[c1 * i + j] + array2[c1 * i + j];
   // ....
}

Leaving out a lot of code, but hopefully you get the idea.

This is how to do it with just regular int arrays. To do complex matrix stuff, you should be building up a custom class that hides the details and makes it easier to just think of the objects as 2-d matrices.

Lou Franco
  • 87,846
  • 14
  • 132
  • 192