I need to make program that calculate multiplication of matrice
i made it in main now i need to make it in functions would anybody hepl me with my problem

that is my code

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

void fn(int **arr) {

}
void fn2 (int **arr2){

}
void fn3 (int **arr3){

}
int main() {

    int r, c ,r2 , c2 , r3 , c3 ;

    cout << "Enter size of row 1 : " ;
    cin >> r ;
    cout << "Enter size of column 1 : " ;
    cin >> c ;

    cout << "Enter size of row 2 : " ;
    cin >> r2 ;
    cout << "Enter size of column2 : " ;
    cin >> c2 ;
    cout << "Enter matrix 2 please " ;

    if (c != r2)
        cout << "The dimensions are not valid " << endl ;
    else {
        int **arr = new int*[r];
                for (int i = 0; i < r; i++)
                     arr[i] = new int[c] ;
        cout << "Enter matrix 1 please " ;

        for (int i=0 ; i<r ; i++){
             for (int j=0 ; j<c ; j++)
                    cin >> arr[i][j] ;
             }
        for (int i=0 ; i<r ; i++){
             for (int j=0 ; j<c ; j++)
                    cout << arr[i][j]<<" " ;
                    cout << endl ;
            }

        int **arr2 = new int*[r2];
        for (int i = 0; i < r2 ; i++)
            arr2[i] = new int[c2] ;
        cout << "Enter matrix 2 please " ;

        for (int i=0 ; i<r2 ; i++){
                for (int j=0 ; j<c2 ; j++)
                     cin >> arr2[i][j] ;
        }
        for (int i=0 ; i<r2 ; i++){
                for (int j=0 ; j<c2 ; j++)
                     cout << arr2[i][j] << " " ;
                cout << endl ;
        }
        int **arr3 = new int * [r2] ;
        for(int i=0; i<r2 ;i++){
                arr3[i] = new int [c];
        }
        for (int i = 0; i < r ; i++){
            for (int j=0 ; j<c2; j++){
                    arr3[i][j]=0;
                for (int k=0 ; k<c ; k++){
                    (arr3[i][j])= (arr3[i][j])+((arr[i][k]) * (arr2[k][j]));
                                    }
                                }
            }
        cout<<"your matrix is"<<endl;
        for(int i=0;i<c;i++){
            for(int j=0;j<r2;j++)
                cout<<arr3[i][j]<<" ";
                cout<<endl;
                        }


    fn(arr);
    fn2(arr2);
    fn3(arr3);
    }
    return 0;
}

Recommended Answers

All 6 Replies

any one help ?????!!!

First: never name your functions as fn2 etc. Give them meanigfull names Like Multiply or something.
In your case 3 function names pop up:
ReadInMatrix (no parameters, returns a matrix)
Multiply(with as parameters 2 matrices and returns a matrix)
DisplayMatrix(Multiplied matrix as parameter, return void)

funcltion to multiply 2 matrices....

void multiply_matrices(int a[][3], int b[][3], int result[][3]) 
{ 
  int i, j, k; 
  for(i = 0; i < 3; i++) 
  { 
     for(j = 0; j < 3; j++) 
     { 
        for(k = 0; k < 3; k++) 
        { 
           result[i][j] += a[i][k] * b[k][j]; 
        } 
     } 
  } 
} 

hateb2a keda ???

#include <iostream>
using namespace std;

void multiply_matrices(int a[][3], int b[][3], int result[][3]) {
    int i, j, k;
    for(i = 0; i < 3; i++){
        for(j = 0; j < 3; j++){
            for(k = 0; k < 3; k++){
                result[i][j] = 0;
                result[i][j] += a[i][k] * b[k][j];

        }
    } 

}

int main() {

    int r, c ,r2 , c2 , r3 , c3 ;

    cout << "Enter size of row 1 : " ;
    cin >> r ;
    cout << "Enter size of column 1 : " ;
    cin >> c ;

    cout << "Enter size of row 2 : " ;
    cin >> r2 ;
    cout << "Enter size of column2 : " ;
    cin >> c2 ;
    cout << "Enter matrix 2 please " ;

    if (c != r2)
        cout << "The dimensions are not valid " << endl ;
    else {
        int **arr1 = new int*[r];
                for (int i = 0; i < r; i++)
                     arr[i] = new int[c] ;
        cout << "Enter matrix 1 please " ;

        for (int i=0 ; i<r ; i++){
             for (int j=0 ; j<c ; j++)
                    cin >> arr1[i][j] ;
             }
        for (int i=0 ; i<r ; i++){
             for (int j=0 ; j<c ; j++)
                    cout << arr1[i][j]<<" " ;
                    cout << endl ;
            }

        int **arr2 = new int*[r2];
        for (int i = 0; i < r2 ; i++)
            arr2[i] = new int[c2] ;
        cout << "Enter matrix 2 please " ;

        for (int i=0 ; i<r2 ; i++){
                for (int j=0 ; j<c2 ; j++)
                     cin >> arr2[i][j] ;
        }
        for (int i=0 ; i<r2 ; i++){
                for (int j=0 ; j<c2 ; j++)
                     cout << arr2[i][j] << " " ;
                cout << endl ;
        }      
        int **arr3 = new int * [r2] ;
        for(int i=0; i<r2 ;i++){
                arr3[i] = new int [c];
        }
    }

    return 0;
}

it will be like that or what i ned to do ???

That's the final code is it right ???

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

void multiply_matrices(int **a, int **b, int **result, int r, int c2 , int c) {
    int i, j, k;
    for(i = 0; i <r ; i++){
        for(j = 0; j <c2 ; j++){
            result[i][j] = 0;
            for(k = 0; k <c; k++)                
                result[i][j] += a[i][k] * b[k][j];

        }
    } 
    for(int i=0;i<r;i++){
        for(int j=0;j<c2;j++)
            cout << setw(4) << result[i][j] <<"    ";
        cout<<endl;}
    }
int main() {

    int r, c ,r2 , c2 , r3 , c3 ;

    cout << "Enter size of row 1    : " ;
    cin >> r ;
    cout << "Enter size of column 1 : " ;
    cin >> c ;

    cout << "Enter size of row 2    : " ;
    cin >> r2 ;
    cout << "Enter size of column 2 : " ;
    cin >> c2 ;
    cout << "Enter matrix 2 please " ;

    if (c != r2)
        cout << "The dimensions are not valid " << endl ;
    else {
        int **arr1 = new int*[r];
                for (int i = 0; i < r; i++)
                     arr1[i] = new int[c] ;

        cout << "Enter matrix 1 please "<< endl ;

        for (int i=0 ; i<r ; i++){
             for (int j=0 ; j<c ; j++)
                    cin >> arr1[i][j] ;
             }
        for (int i=0 ; i<r ; i++){
             for (int j=0 ; j<c ; j++)
                    cout << setw(4) << arr1[i][j]<<" " ;
                    cout << endl ;
            }

        int **arr2 = new int*[r2];
        for (int i = 0; i < r2 ; i++)
            arr2[i] = new int[c2] ;
        cout << "Enter matrix 2 please " << endl ;

        for (int i=0 ; i<r2 ; i++){
                for (int j=0 ; j<c2 ; j++)
                     cin >> arr2[i][j] ;
        }
        for (int i=0 ; i<r2 ; i++){
                for (int j=0 ; j<c2 ; j++)
                     cout << setw(4) << arr2[i][j] << " " ;
                cout << endl ;
        }      
        int **arr3 = new int * [r2] ;
        for(int i=0; i<r2 ;i++){
                arr3[i] = new int [c];
        }
        cout<<"The result is " <<endl;
        multiply_matrices (arr1 , arr2 , arr3 , r , c2 , c);
        arr1 = 0 ;
        arr2 = 0 ;
        arr3 = 0 ;
    }

    return 0;
}
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.