H_beginner 0 Newbie Poster

I wanted to know if I can make the matrix size entered by the user dynamic. Currently I have fixed it as a 2*2 matrix. If I try making it a dynamic array it says the integer should be constant.

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

int input ();
void output(int [][2], int, int );
void add ( int [][2],int [][2], int, int );
void sub (int [][2],int [][2], int , int );
void multi (int [][2], int [][2], int, int);
void trans (int [2][2], int , int );
const int r=2, c=2;
int a[r][c],b[r][c];

int main ()
{
int A;  
     cout<<"enter your choice 1.Add, 2. Sub 3. Multi 4.trans \n";
     cin>>A;
      input();
     switch (A)
     {
     case 1:
     add ( a, b, r, c);
     break;
     case 2:
     sub (a, b, r, c);
     break;
     case 3:
         multi (a, b, r, c);
     break;
     case 4:
         trans (a,r,c);
                 break;

     default:
         cout<<"wrong choice \n";
    break; }

     return 0;

}

int input ()
{ cout<<"Enter matrix 1 \n";
    for (int i=0; i<r; i++)
        for (int j=0; j<c; j++)
            {
                cin>>a[i][j];
        }
        cout<<"your matrix 1 \n";
        output(a,r,c);

        cout<<"enter matrix 2 \n";
                for (int i=0; i<r; i++)
        for (int j=0; j<c; j++)
            {
                cin>>b[i][j];
        }
        cout<<"your matrix 2 \n";
        output (b,r,c);
        return (a[r][c], b[r][c]);
}

void output (int g [r][c], int r, int c)
{
    for (int i=0; i<r; i++)
    {   
        for (int j=0; j<c; j++)
        {   cout<<setw(10)<<g[i][j];
    }   
    cout<<endl;
    }

}

void add (int a[r][c],int b[r][c], int r, int c)
{   int i=0,j=0;
        int g[2][2];
    for (int i=0;i<r;i++)
        {
            for (int j=0;j<c;j++)
    g[i][j] = a[i][j] + b[i][j];
    }
    cout<<"your answer is \n";

    output (g,r,c);
    }

void sub (int a[r][c], int b[r][c], int r, int c)
{
    int i=0,j=0;
    int g[2][2];
    for (int i=0;i<r;i++)
        {
            for (int j=0;j<c;j++)
    g[i][j] = a[i][j] - b[i][j];
    }
    cout<<"your answer is \n";

    output (g,r,c);
    }

void multi (int a[2][2], int [2][2], int r, int c)
{   int g[2][2] = {};
    for (int i=0; i<r; i++)
        for (int j=0; j<c; j++)
            for (int k=0;k<r;k++)
            {
                g[i][j] += a[i][k] * a[k][j];
            }
            cout<<"the answer is \n";
            output (g,r,c);
}

void trans (int a[r][c], int r, int c)
{ 
    int g[2][2]={};

    for (int i=0; i<r;i++)
        for (int j=0; j<c;j++)
        {
            g[i][j]=a[j][i];
        }
        cout<<"the ans is : \n";
output (g,r,c);
}
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.