Search code examples
c++matrixmultidimensional-arrayfunction-call

Error sum of two matrix in function By using 2d array


    #include <iostream>
   using namespace std;
int add(int x[3][3], int y[3][3],int z[3][3]);

int main()
{
 int x[3][3],y[3][3],z[3][3];
    cout<<"Enter value for x "<<endl;
    for(int i=0;i<3;i++)
    {
        for(int j=0;j<3;j++)
        {
        
        cout<<"Enter the value of index "<<i<<","<<j<<" :";
        cin>>x[i][j];
        }
        
    }
    cout<<"Enter value for y "<<endl;
    for(int i=0;i<3;i++)
    {
        for(int j=0;j<3;j++)
        {
        
        cout<<"Enter the value of index "<<i<<","<<j<<" :";
        cin>>y[i][j];
        }
        
    }
    
    cout<<"Your result is :";   
    for(int i=0;i<3;i++)
    {
        for(int j=0;j<3;j++)
        {
        
        cout<<add(x[3][3],y[3][3]);
        }
        cout<<endl;
        
    }
    return 0;}
int add(int x[3][3], int y[3][3],int z[3][3])
{
for(int i=0;i<3;i++)
{
    for(int j=0;j<3;j++)
    {
        z[i][j]=x[i][j]+y[i][j];
    }
}
for(int i=0;i<3;i++)
{
    for(int j=0;j<3;j++)
    {
       cout<<z[i][j];
    }
    cout<<endl;
}
}

================================================================================= you See the program in which I am want to add 2 matrix by using 2d array and I am also use Function add(int x[3][3], int y[3][3],int z[3][3]) . In this program One thing will give me error when i am try to compile it that are cout<<add(x[3][3],y[3][3]) compiler say you have store three values in Function so write three values like add(int x[3][3], int y[3][3],int z[3][3]) but WE know that at this piont we write only two integer that I am want to add that are declared in Function z[i][j]=x[i][j]+y[i][j]. So that program cannot be execute from me. So that why I am want your correction in this program .


Solution

  • The function add returns nothing though its return type is int.

    So this statement

    cout<<add(x[3][3],y[3][3]);
    

    does not make a sense and moreover the function call has invalid arguments (the number of arguments and their types).

    Maybe you mean

    void add( const int x[3][3], const int y[3][3], int z[3][3]);
    

    and

    add( x, y, z );
    
    cout<<"Your result is :";   
    for(int i=0;i<3;i++)
    {
        for(int j=0;j<3;j++)
        {
            cout<< z[i][j] << ' ';
        }
        cout<<endl;
        
    }
    

    Here is your updated program.

    #include <iostream>
    
    const size_t N = 3;
    
    void add( const int x[N][N], const int y[N][N], int z[N][N] );
    
    int main() 
    {
        int x[N][N], y[N][N], z[N][N];
        
        std::cout << "Enter value for x " << '\n';
        
        for ( size_t i = 0; i < N; i++ )
        {
            for ( size_t j = 0; j < N; j++ )
            {
                std::cout << "Enter the value of index " << i << "," << j << " : ";
                std::cin >> x[i][j];
            }
        }
    
        std::cout << "Enter value for y " << '\n';
        
        for ( size_t i = 0; i < N; i++ )
        {
            for ( size_t j = 0; j < N; j++ )
            {
                std::cout << "Enter the value of index " << i << "," << j << " : ";
                std::cin >> y[i][j];
            }
        }
    
        add( x, y, z );
        
        std::cout << "Your result is:\n";
        
        for ( size_t i = 0; i < N; i++ )
        {
            for ( size_t j = 0; j < N; j++ )
            {
                std::cout << z[i][j] << ' ';
            }
            std::cout << '\n';
        }
        
        return 0;
    }
    
    void add( const int x[N][N], const int y[N][N], int z[N][N] )
    {
        for ( size_t i = 0; i < N; i++ )
        {
            for ( size_t j = 0; j < N; j++ )
            {
                z[i][j] = x[i][j] + y[i][j];
            }
        }
    }