answersLogoWhite

0


Best Answer

The previous two versions of the program given here were unnecessarily long and complicated. I am providing a much more simple and succinct version. I have constructed it on the basis of two 3*3 matrices. However you can easily change the order by replacing the "3"s of the program with your required order, provided the number of columns of the premultiplier is equal to the number of rows of the postmultiplier. If you want to dynamically allocate the orders of the matrices, you will have to use calloc and malloc, which I don't know as of yet!

#include

int main()

{

int i,j,l;

int matrix[3][3],matri[3][3],matr[3][3]={{0,0,0},{0,0,0},{0,0,0}};

printf("Enter 1st matrix: \n");

for (i=0;i<3;++i)

{

printf("\nEnter #%d row: ",(i+1));

for (j=0;j<3;++j)

scanf_s("%d",&matrix[i][j]);

}

printf("\n\n");

printf("Enter 2nd matrix: \n");

for (i=0;i<3;++i)

{

printf("\nEnter #%d row: ",(i+1));

for (j=0;j<3;++j)

scanf_s("%d",&matri[i][j]);

}

for (j=0;j<3;++j)

{

for (i=0;i<3;++i)

{

for (l=0;l<3;++l)

matr[i][j] = matr[i][j] + (matrix[i][l])*(matri[l][j]);

}

}

printf("The resultant matrix is:\n\n");

for (i=0;i<3;++i)

{

for (j=0;j<3;++j)

printf("%4d",matr[i][j]);

printf("\n\n");

}

return( 0 );

}

Recent changes to the above code

30/4/12:

Error: main() must return a value.

Error: matr must be initialised in declaration, not afterwards.

Warning: Variable k is unreferenced (removed).

Warning: Use of scanf is unsafe. Replaced with scanf_s

Benign: Commented code removed for clarity.

An alternate version with dynamic arrays is shown below.

#include

typedef unsigned int UINT;

// Forward declarations.

int ** CreateMatrix( UINT rows, UINT cols );

void InputMatrix( int ** Matrix, UINT rows, UINT cols, char * Title );

void PrintMatrix( int ** Matrix, UINT rows, UINT cols, char * Title );

void ReleaseMatrix( int ** Matrix, UINT rows );

int main()

{

// Initialise some variables.

UINT rows=0, comm=0, cols=0;

UINT i=0, j=0, k=0;

// 2D arrays (pointer-to-pointer-to-int).

int ** M1 = NULL;

int ** M2 = NULL;

int ** M3 = NULL;

printf("Calculate the dot products of two matrices.\n\n");

printf( "Enter the number of rows in the 1st matrix:\t");

scanf_s("%u", &rows );

printf( "Enter the number of common elements:\t\t");

scanf_s("%u", &comm );

printf( "Enter the number of columns in the 2nd matrix:\t");

scanf_s("%u", &cols );

// Allocate the 2D arrays:

M1 = CreateMatrix( rows, comm );

M2 = CreateMatrix( comm, cols );

M3 = CreateMatrix( rows, cols );

// Input values:

InputMatrix( M1, rows, comm, "Matrix 1" );

InputMatrix( M2, comm, cols, "Matrix 2" );

// Calculate the dot product.

for( i=0; i

for( j=0; j

for( k=0; k

M3[i][k] += M1[i][j] * M2[j][k];

// Report.

PrintMatrix( M1, rows, comm, "\nMatrix 1" );

PrintMatrix( M2, comm, cols, "Matrix 2" );

PrintMatrix( M3, rows, cols, "Dot Product of Matrix 1 and Matrix 2" );

printf("\n");

// Release memory.

ReleaseMatrix( M1, rows );

ReleaseMatrix( M2, comm );

ReleaseMatrix( M3, rows );

return( 0 );

}

int ** CreateMatrix( UINT rows, UINT cols )

{

UINT i = 0;

int ** Matrix = (int **) malloc(rows * sizeof( int * ));

for( i=0; i

{

Matrix[i] = (int *) malloc( cols * sizeof( int ));

memset( Matrix[i], 0, cols * sizeof( int ));

}

return( Matrix );

}

void InputMatrix( int ** Matrix, UINT rows, UINT cols, char * Title )

{

printf( "\nValues for %s:\n\n", Title );

UINT i=0, j=0;;

for( i=0; i

{

for (j=0; j

{

printf("Enter value for row %u, col %u:\t", i+1, j+1);

scanf_s("%d", &Matrix[i][j]);

}

}

}

void PrintMatrix( int ** Matrix, UINT rows, UINT cols, char * Title )

{

printf( "\n%s:\n\n", Title );

UINT i=0, j=0;

for( i=0; i

{

for (j=0; j

printf( "%4d", Matrix[i][j] );

printf("\n");

}

}

void ReleaseMatrix( int ** Matrix, UINT rows )

{

UINT i = rows;

while(i)

free( Matrix[--i] );

free( Matrix );

}

User Avatar

Wiki User

12y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

13y ago

void main()

{

int a[10][10],b[10][10],c[10][10],i,j,k,m,n,p,q;

printf("Enter The Rows And Cloumns And Of The First Matrix:");

scanf("%d %d",&m,&n);

printf("\nEnter The Rows And Cloumns And Of The Second Matrix:");

scanf("%d %d",&p,&q);

printf("\nEnter Elements Of The First Matrix:\n");

for(i=0;i< m;i++)

{

for(j=0;j< n;j++)

{

scanf("%d",&a[i][j]);

}

}

printf("\nEnter Elements Of The Second Matrix:\n");

for(i=0;i< p;i++) {

for(j=0;j< q;j++)

scanf("%d",&b[i][j]);

}

printf("The First Matrix Is:\n"); /* Print the first matrix */

for(i=0;i< m;i++) {

for(j=0;j< n;j++)

printf(" %d ",a[i][j]);

printf("\n");

}

printf("The Second Matrix Is:\n"); /* Print the second matrix */

for(i=0;i< p;i++) {

for(j=0;j< q;j++)

printf(" %d ",b[i][j]);

printf("\n");

}

if(n!=p) {

printf("Aborting./nMultiplication Of The Above Matrices Not Possible.");

exit(0);

}

else {

for(i=0;i< m;i++) {

for(j=0;j< q;j++) {

c[i][j] = 0;

for(k=0;k< n;k++) {

c[i][j] = c[i][j] + a[i][k] * b[k][j];

}

}

}

printf("\nThe Product Of The Two Matrices Is:\n\n");

for(i=0;i< m;i++) {

for(j=0;j< q;j++) {

printf(" %d ",c[i][j]);

}

printf("\n");

}

}

return 0;

}

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

#include <stdio.h>

#include <conio.h>

#define MAX_ROWS 5

#define MAX_COLS 5

int main()

{

int iRow1 = 0, iCol1 = 0;

int iRow2 = 0, iCol2 = 0;

int iLoop = 0, iRowLoop = 0, iColLoop = 0, iColLoop2 = 0;

int iarrMatrix_1[MAX_ROWS][MAX_COLS] = {{0},{0}};

int iarrMatrix_2[MAX_ROWS][MAX_COLS] = {{0},{0}};

int iarrMatrix_3[MAX_ROWS][MAX_COLS] = {{0},{0}};

printf("Matrix Multiplication");

printf("\nEnter the Rows and Columns of first Matrix:");

while(1)

{

scanf("%d",&iRow1);

scanf("%d",&iCol1);

if((iRow1 > MAX_ROWS) | (iCol1 > MAX_COLS))

printf("\nMax Rows: %d, Max Columns: %d: Enter a valid number!",MAX_ROWS,MAX_COLS);

else

break;

}

//Get the elements first matrix from user

printf("\nEnter first (%d X %d) Matrix:",iRow1,iCol1);

for(iRowLoop = 0; iRowLoop < iRow1; iRowLoop++)

{

printf("\nEnter elements in Row %d\n",iRowLoop+1);

for(iColLoop = 0; iColLoop < iCol1; iColLoop++)

{

printf("Enter element (%d X %d)",iRowLoop+1,iColLoop+1);

scanf("%d",&iarrMatrix_1[iRowLoop][iColLoop]);

}

}

//Number of columns in first matrix and number of rows in second matrix

//must be equal for matrix multiplication

iRow2= iCol1;

printf("\nNumber of Rows in second Matrix: %d",iRow2);

printf("\nEnter the number of Columns in second Matrix:");

while(1)

{

scanf("%d",&iCol2);

if(iCol2 > MAX_COLS)

printf("\nMax allowed columns are %d: Please enter a valid number!",MAX_COLS);

else

break;

}

//Get the elements second matrix from user

printf("\nEnter second (%d X %d) Matrix:",iRow2,iCol2);

for(iRowLoop = 0; iRowLoop < iRow2; iRowLoop++)

{

printf("\nEnter elements in Row %d\n",iRowLoop+1);

for(iColLoop = 0; iColLoop < iCol2; iColLoop++)

{

printf("Enter element (%d X %d)",iRowLoop+1,iColLoop+1);

scanf("%d",&iarrMatrix_2[iRowLoop][iColLoop]);

}

}

//Matrix Multiplication

for(iRowLoop = 0; iRowLoop < iRow1; iRowLoop++)

{

for(iColLoop = 0; iColLoop < iCol1; iColLoop++)

{

printf("\nM1(%d, %d): %d",iRowLoop,iColLoop,iarrMatrix_1[iRowLoop][iColLoop]);

for(iColLoop2 = 0; iColLoop2 < iCol2; iColLoop2++)

{

iarrMatrix_3[iRowLoop][iColLoop2] += iarrMatrix_1[iRowLoop][iColLoop] * iarrMatrix_2[iColLoop][iColLoop2];

printf("\nM2(%d, %d): %d",iColLoop,iColLoop2,iarrMatrix_2[iColLoop][iColLoop2]);

}

}

}

//Display First Matrix

printf("\n\nFirst (%d X %d) Matrix",iRow1,iCol1);

for(iRowLoop = 0; iRowLoop < iRow1; iRowLoop++)

{

printf("\n");

for(iColLoop = 0; iColLoop < iCol1; iColLoop++)

{

printf("%d\t",iarrMatrix_1[iRowLoop][iColLoop]);

}

}

//Display Second Matrix

printf("\nSecond (%d X %d) Matrix",iRow2,iCol2);

for(iRowLoop = 0; iRowLoop < iRow2; iRowLoop++)

{

printf("\n");

for(iColLoop = 0; iColLoop < iCol2; iColLoop++)

{

printf("%d\t",iarrMatrix_2[iRowLoop][iColLoop]);

}

}

//Display Result

printf("\nResult (%d X %d) Matrix",iRow1,iCol2);

for(iRowLoop = 0; iRowLoop < iRow1; iRowLoop++)

{

printf("\n");

for(iColLoop = 0; iColLoop < iCol2; iColLoop++)

{

printf("%d\t",iarrMatrix_3[iRowLoop][iColLoop]);

}

}

getche();

return 0;

}

Output:

Matrix Multiplication

Enter the Rows and Columns of first Matrix:3

2

Enter first (3 X 2) Matrix:

Enter elements in Row 1

Enter element (1 X 1)1

Enter element (1 X 2)2

Enter elements in Row 2

Enter element (2 X 1)3

Enter element (2 X 2)4

Enter elements in Row 3

Enter element (3 X 1)5

Enter element (3 X 2)6

Number of Rows in second Matrix: 2

Enter the number of Columns in second Matrix:2

Enter second (2 X 2) Matrix:

Enter elements in Row 1

Enter element (1 X 1)1

Enter element (1 X 2)2

Enter elements in Row 2

Enter element (2 X 1)3

Enter element (2 X 2)4

M1(0, 0): 1

M2(0, 0): 1

M2(0, 1): 2

M1(0, 1): 2

M2(1, 0): 3

M2(1, 1): 4

M1(1, 0): 3

M2(0, 0): 1

M2(0, 1): 2

M1(1, 1): 4

M2(1, 0): 3

M2(1, 1): 4

M1(2, 0): 5

M2(0, 0): 1

M2(0, 1): 2

M1(2, 1): 6

M2(1, 0): 3

M2(1, 1): 4

First (3 X 2) Matrix

1 2

3 4

5 6

Second (2 X 2) Matrix

1 2

3 4

Result (3 X 2) Matrix

7 10

15 22

23 34

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

#include <stdio.h>

void main()

{

int a[3][3],b[3][3],c[3][3],i,j;

printf("enter the element of a matrics a");

for(i=0;i<3;i++)

{

for(j=0;j<3;j++)

{

scanf("%d ",&a[i][j]);

}}

printf("enter the element of a matrics b");

for(i=0;i<3;i++)

{

for(j=0;j<3;j++)

{

scanf("%d ",&b[i][j]);

}}

for(i=0;i<3;i++)

{

for(j=0;j<3;j++)

{

c[i][j])=a[i][j]*b[i][j];

}}

printf(" the element of a matrics c are");

for(i=0;i<3;i++)

{

for(j=0;j<3;j++)

{

printf("%d ",c[i][j]);

}

printf("\n");

}

}

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

Dev c++:

PROGRAM: TWO MATRIX MULTIPACTION 3X3

#include<conio.h>

#include<iostream.h>

main()

{

int a[3][3],b[3][3],c[3][3],pro=0,i,j;

int k=0,z=0;

cout<<"enter A : ";

for (i=0;i<3;i++)

{

for (j=0;j<3;j++)

{

cin>>a[i][j];

}

}

cout<<"\nenter B : ";

for (i=0;i<3;i++)

{

for (j=0;j<3;j++)

{

cin>>b[i][j];

}

}

for (int n=0;n<3;n++)

{

for (i=0;i<3;i++)

{

// c[z][k]=0;

for (j=0;j<3;j++)

{

pro=pro+(a[i][j]*b[j][i]);

if(j==2)

{

c[z][k]=pro;

k++;

if(k==3)

{

z++;

k=0;

}

}

}

pro=0;

}

}

//cout<<"\n\t"<<z;

cout<<" Product of two matrix \n";

for (i=0;i<3;i++)

{

for (j=0;j<3;j++)

{

cout<<"\t"<<c[i][j];

}

cout<<endl;

}

getch();

}

This answer is:
User Avatar

User Avatar

Wiki User

9y ago

#include<iostream>

#include<vector>

#include<exception>

#include<assert.h>

#include<time.h>

class matrix

{

private:

size_t m_rows;

size_t m_cols;

std::vector<std::vector<int>> m_data;

public:

matrix (const size_t rows, const size_t cols);

size_t rows() const { return m_rows; }

size_t cols() const { return m_cols; }

std::vector<int>& operator[] (const size_t index) { return m_data[index]; }

const std::vector<int>& operator[] (const size_t index) const { return m_data[index]; }

std::vector<int>& row (const size_t index) { return m_data[index]; }

const std::vector<int>& row (const size_t index) const { return m_data[index]; }

std::vector<int> col (const size_t index);

const std::vector<int> col (const size_t index) const;

void randomise();

};

void matrix::randomise()

{

for (size_t r=0; r<rows(); ++r)

{

for (size_t c=0; c<cols(); ++c)

{

row(r)[c] = rand() % 9 + 1;

}

}

}

std::ostream& operator<< (std::ostream& os, const matrix& m)

{

for (size_t row=0; row<m.rows(); ++row)

{

for (size_t col=0; col<m.cols(); ++col)

{

os << m[row][col] << '\t';

}

os << std::endl;

}

os << std::endl;

return os;

}

matrix::matrix (const size_t rows, const size_t cols)

: m_rows (rows)

, m_cols (cols)

, m_data ()

{

if (!m_cols !m_rows)

throw std::out_of_range ("matrix: dimensions must be non-zero!");

for (size_t row=0; row<m_rows; ++row)

m_data.push_back (std::vector<int>(m_cols));

}

std::vector<int> matrix::col (const size_t index)

{

std::vector<int> column(m_rows);

for (size_t r=0; r<m_rows; ++r)

column[r] = row(r)[index];

return column;

}

const std::vector<int> matrix::col (const size_t index) const

{

std::vector<int> column(m_rows);

for (size_t r=0; r<m_rows; ++r)

column[r] = row(r)[index];

return column;

}

matrix operator* (const matrix a, const matrix b)

{

if (a.cols() != b.rows())

throw std::out_of_range("matrix operator* : row/column mismatch!");

matrix result (a.rows(), b.cols());

for (size_t r=0; r<a.rows(); ++r)

{

for (size_t c=0; c<b.cols(); ++c)

{

result[r][c]=0;

const std::vector<int>& Row = a[r];

const std::vector<int> Col = b.col(c);

assert (Row.size() == Col.size());

for (size_t index=0; index<Row.size(); ++index)

result[r][c] += (Row[index]*Col[index]);

}

}

return result;

}

int main()

{

srand((unsigned)time(nullptr));

matrix a (3,4);

a.randomise();

std::cout << "Matrix A:\n\n" << a << std::endl;

matrix b (4,5);

b.randomise();

std::cout << "Matrix B:\n\n" << b << std::endl;

std::cout << "Matrix A * B:\n\n" << a * b << std::endl;

}

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Write a c plus plus program that multiplies two matrices?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Is there any benefit in a C plus plus program to add two matrices?

No.


A program c plus plus on automorphic numbers or not?

how to write a program that counts automorphic number from 1 to 999


How do you write a C plus plus program that will display the first 10 positive prime numbers?

By learning how to program on C+.


Write a program in c plus plus to implement macro processor?

Don't write, it is already written, google for 'cpp'.


Do I need to write a program to find a substring in a given string in c plus plus?

No.


How do you write program to convert meter to kilometer in c plus plus?

Divide it by 1000.


How do you write an Algorithm for a C plus plus Program?

You don't write an algorithm for a C++ program, unless you are documenting the C++ program after-the-fact. The normal procedure is to write the algorithm first, in a language independent fashion, and then translate that stated algorithm into C++ code, or into whatever language you wish.


Write a program in c plus plus to compute first of non-terminal?

there is no solution of this problem...........that's it..........


How many classes can we write in a single c plus plus program?

Its limited only by available memory.


How do you write a C plus plus program that displays a pyramid of Xes on the screen using a for loop?

printf ("x")


Write a program in C programming language that computes the roots of the quadratic equation ax2 plus bx plus c?

Write your program and if you are having a problem post it here with a description of the problem you are having. What you are asking is for someone to do your homework for you.


Write a c plus plus program to compute two integers?

int x= 1; int y= 2;