Question

In: Computer Science

In C++ write a function to find a product of two matrices using arrays. The function...

In C++ write a function to find a product of two matrices using arrays. The function should be general and should accept any size matrices.

Solutions

Expert Solution


The Program Code is :

///Cpp program for Product of two Arrays
/// Using Function
#include<iostream>
using namespace std;
void matrix_Multilication(int **m1,int **m2,int r1,int c1,int c2,int r2)
{
                        ///Function Definition
    int i,j,k;
    int **m3;
    int sum = 0;

    m3=new int *[r1];
    for(i = 0;i < r1 ;i++)
    {
        m3[i]=new int[c2];///Dynamic memory allocation for Matrix3
    }

     cout<<"The Matrix 1 is :"<<endl;
     for(i = 0; i < r1; i++)
     {
      for(j = 0; j < c1; j++)
      {
          cout<<m1[i][j]<<"\t";///It will display Matrix1 Elements
      }
      cout<<endl;
    }

    cout<<"The Matrix 2 is :"<<endl;
    for(i = 0; i < r2; i++)
    {
    for(j = 0; j < c2; j++)
      {
          cout<<m1[i][j]<<"\t";///It will display Matrix2 Elements
      }
      cout<<endl;


    }
/// Matrix Multiplication logic
    for(i = 0; i < r1; ++i)
    {
        for(j = 0; j < c2; ++j)
        {
            for(k = 0; k < c1; ++k)
            {
               sum += m1[i][k] * m2[k][j];
            }
            m3[i][j] = sum;
            sum = 0;
        }
    }

cout<<"The Product of Matrix 1 and Matrix2 is :"<<endl;
for(i = 0; i < r1; i++)
{
      for(j = 0; j < c2; j++)
      {
          cout<<m3[i][j]<<"\t";///It will display Product Elements
      }
      cout<<endl;


    }
}

int main()
{
int **m1; ///Double Pointer m1 for 2D Array
int **m2; ///Double Pointer m2 for 2D Array

int i,j,k,r1,c1,r2,c2;
cout<<"Enter Number of Rows and Columns of Matrix 1 "<<endl;
cin>>r1>>c1;

cout<<"Enter Number of Rows and Columns of Matrix 2 "<<endl;
cin>>r2>>c2;


while(c1 != r2)///It will check Matrix Multiplication is Possible or Not
{
      cout<<" Error : ";
      cout<<"Column of matrix 1 and Rows of matrix 2 Must be Equal "<<endl;
      cout<<"Again : "<<endl;

      cout<<"Enter Number of Rows and Columns of Matrix 1 "<<endl;
      cin>>r1>>c1;

      cout<<"Enter Number of Rows and Columns of Matrix 2 "<<endl;
      cin>>r2>>c2;

}


m1=new int *[r1];
    for(i = 0;i < r1 ;i++)
    {
         m1[i]=new int[c1];///Dynamic memory allocation for Matrix1

    }
   cout<<"Enter Matrix1 : "<<r1*c1<<" values"<<endl;

   for(i = 0; i < r1; i++)
{
      for(j = 0; j < c1; j++)
      {
          cin>>m1[i][j];/// It will Read the matrix1 Elements
      }
}
    m2=new int *[r2];
    for(i = 0;i < r2 ;i++)
    {
         m2[i]=new int[c2]; ///Dynamic memory allocation for Matrix2
    }

   cout<<"Enter Matrix2 : "<<r2*c2<<" values"<<endl;
for(i = 0; i < r2; i++)
{
      for(j = 0; j < c2; j++)
      {
          cin>>m2[i][j];/// It will Read the matrix2 Elements
      }
}
matrix_Multilication(m1,m2,r1,c1,c2,r2);///Function Calling

return 0;
}


Related Solutions

Write a program of Binary Search in C++ by using function and arrays with the explanation.
Write a program of Binary Search in C++ by using function and arrays with the explanation.
In C++, write a function that takes in as inputs two arrays, foo and bar, and...
In C++, write a function that takes in as inputs two arrays, foo and bar, and their respective array sizes. The function should then output the concatenation of the two arrays as a singly linked list. You may assume that you are provided a singly linked list header file.
Write a function that will accept two integer matrices C and D by reference parameters. The...
Write a function that will accept two integer matrices C and D by reference parameters. The function will compute the transpose of C and store it in D. For your information, the transpose of matrix C is D, where D[j][i] = C[i][j]. [7 marks] Explain the time complexity of this function inside of the function code as a comment. [3 marks] in C++
Using pseudocode or C++ code, write a function to compute and return the product of two...
Using pseudocode or C++ code, write a function to compute and return the product of two sums. The first is the sum of the elements of the first half of an array A. The second is the sum of the elements of the second half of A. The function receives A and N ≥ 2, the size of A, as parameters. (in c++)
In C++ Write a function that accepts two int arrays of the same size. The first...
In C++ Write a function that accepts two int arrays of the same size. The first array will contain numbers and the second array will be filled out inside the function. THE FUNCTION SHOULD FIND ALL NUMBERS IN THE ARRAY THAT ARE GREATER THAN OR EQUAL TO THE AVERAGE. You need to design the function. so the output code should be: (show contents of 1st array) The numbers that are greater than the average of the first array are: (the...
Write a function in C that uses the Merge Sort sorting algorithm with arrays. The function...
Write a function in C that uses the Merge Sort sorting algorithm with arrays. The function must not be void and must output type int* i.e. it must take the form: int* merge_sort(int a[], int n) where a[ ] is the input matrix and n is the size of the matrix. You may use an auxiliary functions such as "merge." The returned array should be sorted using merge_sort and should not modify the array that was input (a[ ] ).
Write a C function to add the elements of two same-sized integer arrays and return ptr...
Write a C function to add the elements of two same-sized integer arrays and return ptr to a third array. int *addTwoArrays(int *a1, int *b1, int size); The function should follow the following rules: If the sum for any element is negative, make it zero. If a1 and b1 point to the same array, it returns a NULL If any input array is NULL, it returns a NULL. Please call this function with the following arrays and print the sums...
C++ Write a function called gen_dates() that generates random dates. It takes two arrays of integers...
C++ Write a function called gen_dates() that generates random dates. It takes two arrays of integers called months and days to store the month and day of each date generated, a constant array of 12 integers called num_of_days that specify the number of days of each of the 12 months and an integer called size that specifies how many dates to generate and randomly generates size dates, storing the generated months in months array and generated days in days array....
Please, write code in c++. Using iostream and cstring library Write a function that will find...
Please, write code in c++. Using iostream and cstring library Write a function that will find and return most recent word in the given text. The prototype of the function have to be the following void mostRecent(char *text,char *word) In char *word your function have to return the most recent word that occurce in the text. Your program have to be not case-sensitive(ignore case - "Can" and "CAN" are the same words) Also note than WORD is sequence of letters...
This is for c++ Write a program that works with two arrays of the same size...
This is for c++ Write a program that works with two arrays of the same size that are related to each other in some way (or parallel arrays). Your two arrays must be of different data types. For example, one array can hold values that are used in a formula that produces the contents of the second array. Some examples might be:  from a previous program, populations and the associated flowrates for those populations (an int array of populations...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT