-3

I am programming for simple matrices multiplication. However, for large values of matrix size, I faced with matrix overflow error. Could someone help me with this.

here the code!

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

int main()
{
    int r,c;
    cout<<"Rows: ";
    cin>>r; // 5000
    cout<<"Clumns: ";
    cin>>c; // 5000
    int m[r][c];
    for (int i=0;i<r;i++)

    {
        for (int j=0;j<c;j++)
        {
            m[i][j]=i+j;

            cout<<setw(4)<<m[i][j];

        }
        cout<<endl;
    }
}
  • 4
    Your code shows no matrix multiplication – armagedescu May 17 '22 at 10:17
  • 1
    what's error message? – mugiseyebrows May 17 '22 at 10:17
  • 2
    You are defining a 5000*5000 element array on the stack. That's 100,000,000 bytes required.... – ChrisMM May 17 '22 at 10:46
  • 2
    See also https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard – ChrisMM May 17 '22 at 10:49
  • @armagedescu : I am going to use this matrix for multiplication. As I tried 5000 for rows and columns, the complire did not complite anything. I would appreciate if youc could help how I can define large matrix. – Shahin Sharafi May 17 '22 at 11:23
  • @ShahinSharafi What are you going to use for for is irrelevant. You don't have matrix multiplication in code and you don't have problems with matrix multiplication yet. Your problem is the very basic compilation of something you intend to use as a matrix. Reformulate your question to reflect correctly your problem, also consider showing your compile errors, and skip irrelevant details. – armagedescu May 17 '22 at 12:35
  • Just use a proper matrix [class](https://stackoverflow.com/a/2076668/4944425). – Bob__ May 17 '22 at 12:42

1 Answers1

0

I ran your program with different sizes. And the problem is simply that the array is too big. It works with smaller array sizes, but you can only put so much onto the stack.

So I did this:

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

class Matrix {
public:
    Matrix(int r, int c)
    : rows(r), cols(c)
    {
        m = new int *[rows];
        for (int index = 0; index < rows; ++index) {
            m[index] = new int[cols];
        }
    }

    int & at(int r, int c) {
        return m[r][c];
    };

    int rows = 0;
    int cols = 0;
    int ** m = nullptr;
};

int main(int argc, char **argv)
{
    int r = atoi(argv[1]);
    int c = atoi(argv[2]);

    Matrix m(r, c);
    for (int i = 0; i < r; i++) {
        for (int j = 0; j < c; j++) {
            m.at(i, j) = i+j;

            cout << setw(4) << m.at(i, j);
        }
        cout<<endl;
    }
}

And that appears to work. Now, there are some things in here that are bad. I didn't write a destructor, so there's a memory leak. And I didn't do any range checking in the at() method. I was only showing what you could do for very large arrays.

Now, I'm going to beg you... PLEASE put white space in your code. You're going to have no end of errors when you shove everything together the way you do. Notice my for-loops have a lot more space than you do. I didn't fix everywhere, but the coding policy where I work is to include white space for readability. Walls of numbers and operators can be very, very hard to read.

Also, name your variables something longer than a single character.

Both these changes will dramatically reduce future bugs.

Joseph Larson
  • 8,530
  • 1
  • 19
  • 36