I am trying to create a program which adds substracts two matrices. Using switch statements. I am not sure that I am making the correct function calls. I ve been struck on this for two days looking for some guidance. Please see below my code.

I am trying to create a program which adds substracts two matrices. Using switch statements. I am not sure that I am making the correct function calls. I ve been struck on this for two days looking for some guidance. Please see below my code.

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

int input (int [][2],int , int );
int output(int [][2], int, int );
int add ( int [][2], int, int );
int sub (int [][2], int , int );
int input2 (int [][2],int, int );

const int r=2, c=2;

int main ()
{

    int mat[r][c], A;
     input(mat, r, c);
     output(mat, r,c);
     cout<<"enter your choice 1.Add, 2. Sub \n";
     cin>>A;
     switch (A)
     {
     case 1:
     add (mat, r, c);
     break;
     case 2:
         sub (mat, r, c);
     }


}

int input (int a[][2], int r , int c)
{
    for (int i=0; i<r; i++)
        for (int j=0; j<c; j++)
            cin>>a[i][j];
    return a[r][c];
}

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

int add (int b[][2], int r, int c)
{
    int f[2][2];
    input2 (f,2,2);
        int g[2][2];
    for (int r=0;r<2;r++)
        for (int c=0;c<2;c++)
    g[2][2] = f[r][c] + b[r][c];

    output (g,2,2);
    return g[2][2];
}

int sub (int e[][2], int r, int c)
{
    int f[2][2];
    int g[2][2];
    input(f,2,2);

    for (int r=0;r<2;r++)
        for (int c=0;c<2;c++)
    g[3][3] = f[r][c] - e[r][c];

    output (g,2,2);
    return g[2][2];
}

int input2 (int a[][2],int r, int c)
{
    for (int i=0; i<r; i++)
        for (int j=0; j<c; j++)
            cin>>a[i][j];
    return a[r][c];
}

Recommended Answers

All 3 Replies

Just a cursory glance, but you have defined all of your arrays to have 2 columns. Why are you iterating over the passed column count? Also in your sub() function, you define g as a 2x2 array, yet you overrun the size with your g[3][3] = f - e.

You need to do more studying, and understand what you are doing. It is fundamentally, and fatally flawed at a number of levels.

commented: I have studied and solve the problem. 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. +0

I have studied and solve the problem. 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.

#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 );

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 \n";
     cin>>A;
      input();
     switch (A)
     {
     case 1:
     add ( a, b, r, c);
     break;
     case 2:
        sub (a,b, r,c);
     }


}

int input ()
{ cout<<"Enter two matrices \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);

        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 g[2][2];
    for (int r=0;r<2;r++)
        {
            for (int c=0;c<2;c++)
    g[r][c] = a[r][c] + b[r][c];
    }
    cout<<"your answer is \n";

    output (g,r,c);
    }

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

}

You can set the function signature to arrayname[][] and pass the x and y sizes in the argument list. However, this really doesn't work well, and can lead to "unintended results". Since this is C++, I advise that you use C++ STL collection templates instead. You can use a vector of vectors to simulate a 2 dimensional array very easily. I do this all the time with great effect. Since std::vector types are dynamically resized/allocated, you don't need to do much besides add a new element to either the outer or inner vector.

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.