Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
12 views

C++ Matrix Multiplication Program - The Crazy Programmer

This document provides a C++ program to multiply two matrices. It reads the dimensions of two matrices, checks if multiplication is possible, and if so, it multiplies the matrices and displays the result. It initializes the result matrix to 0, reads the two input matrices, initializes a nested for loop to multiply corresponding elements and store in the result matrix, and finally displays the result matrix.

Uploaded by

hsyedhazrat
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

C++ Matrix Multiplication Program - The Crazy Programmer

This document provides a C++ program to multiply two matrices. It reads the dimensions of two matrices, checks if multiplication is possible, and if so, it multiplies the matrices and displays the result. It initializes the result matrix to 0, reads the two input matrices, initializes a nested for loop to multiply corresponding elements and store in the result matrix, and finally displays the result matrix.

Uploaded by

hsyedhazrat
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

HOME C C++ JAVA ANDROID PYTHON .

NET PHP BATCH FILE JAVASCRIPT SQL PL/SQL

PROGRAMMING INTERVIEW Q & A BOOKS DSA DESIGN DEVELOPMENT REVIEW SPONSORED

C++ Matrix Multiplication Program Powered by


FEATURED VIDEOS

Here you will get C++ matrix multiplication program.

What we are doing in this program.

Read number of rows and columns for two matrix.

Then check if matrix multiplication is possible or not. wearing a tie in DC for the second
time which in many ways is a big

If not possible then show a message to user otherwise multiply them. Finally display the
result.
Search

C++ Matrix Multiplication Program

#include<iostream> SUBSCRIBE FOR


LATEST UPDATES
using namespace std;

int main() Subscribe to our mailing list and


{ get interesting stuff and updates
int a[5][5],b[5][5],c[5][5],m,n,p,q,i,j,k;
cout<<"Enter rows and columns of first matrix:"; to your email inbox.
cin>>m>>n;
cout<<"Enter rows and columns of second matrix:";
cin>>p>>q;
Enter your email here
if(n==p)
{
cout<<"\nEnter first matrix:\n";
for(i=0;i<m;++i)
for(j=0;j<n;++j) SUBSCRIBE NOW
cin>>a[i][j];

cout<<"\nEnter second matrix:\n"; we respect your privacy and take


for(i=0;i<p;++i) protecting it seriously
for(j=0;j<q;++j)
cin>>b[i][j];

cout<<"\nThe new matrix is:\n";


for(i=0;i<m;++i)
{
for(j=0;j<q;++j)
Important Pages
{
c[i][j]=0;
for(k=0;k<n;++k) About
c[i][j]=c[i][j]+(a[i][k]*b[k][j]);
cout<<c[i][j]<<" "; Contact
}
cout<<"\n";
} Advertise
}
else Privacy Policy
cout<<"\nSorry!!!! Matrix multiplication can't be done";
Hire Me
return 0;
}

Output

Right, On Time, Or It's Free


We Provide a One-Stop Shop For All Your Printing
and Mailing Needs! Get In Touch.

Printing For Less Shop Now

You May Also Like:

Matrix Multiplication in C
Matrix Addition in C
C++ Program to Delete an Element from Array
C++ Program to Add Two Matrices
Transpose of Matrix in C

Category: Array

← C program to find the sum of the series C++ program to create a loading bar →
x+x^2/2+x^3/3+…..+x^n/n

30 thoughts on “C++ Matrix Multiplication Program”

Aditya's Art Work


July 31, 2013

This somehow does not work/compile for me.


It throws out the follwing error:

No main found…simply generating .o files…


In file included from
/usr/lib/gcc/i386-redhat-
linux/3.4.6/../../../../include/c++/3.4.6/backward/iostream.h:31,
from mat.cpp:1:
mat.cpp:2:18: conio.h: No such file or directory
mat.cpp:5: error: main' must return int'
mat.cpp:5: error: return type for main' changed to int'
mat.cpp: In function int main(...)':
mat.cpp:6: error: clrscr' was not declared in this scope

mat.cpp:40: error: `getch' was not declared in this scope


mat.cpp:6: warning: unused variable 'clrscr'
mat.cpp:40: warning: unused variable 'getch'

Reply ↓

Neeraj Mishra
August 1, 2013

By taking a look on the errors i concluded that you are using gcc on linux os. The
above code is written in turbo c++ so it will not work in gcc.

Reply ↓

Platin21
March 3, 2018

This will work with gcc and clang and msvc as it doesn’t use any turbo c++
features
You need to call g++ in order to compile it as cpp code else this will throw
an error every time you try to compile it.
CmdLine: g++ main.cpp -o main.out -std=c++11 -O3

Reply ↓

raghu
August 22, 2013

Mind blowing.

Reply ↓

Sarthak Mohanty
February 19, 2014

Why do we use getch() before ending any program? What is the function of getch()?

Reply ↓

Neeraj Mishra
February 19, 2014

Actually getch() is a function used to read character value without displaying it on


the screen. When it is added at the end of the program, compiler waits and stops
the screen for reading any character, as we press any key the program ends. I
hope this will make you understand the use of getch().

Reply ↓

kishan vaishnav
March 11, 2015

I think u r new to c getch () is for inputting character value bt to keep output on


the screen we hv to use getch other wise output will be terminate in just mili
seccends

Reply ↓

Muhammad Asad
December 6, 2017

Getch function holds the programm output screen until any key is pressed.

Reply ↓

aditya srinevas
November 11, 2014

why r u using c[i][j]=0

Reply ↓

kishan vaishnav
March 11, 2015

Jst to initialize destination array


U can skip c [i][j]=0

Reply ↓

sayan
December 8, 2015

nice written but you must use //comments

Reply ↓

kamili zefrine
December 10, 2015

why are we leave space btn cout<<[i][j]<<" ";

Reply ↓

Tech From India


February 15, 2016

so that there are spaces in between the matrices and two numbers can be
differentiated.
for ex:- 80 90 39 without space can be 809039. it will be difficult for us to know
the numbers without spaces.

Reply ↓

ssd
February 14, 2016

thanx …but correct the inverted commas

Reply ↓

thoufiq
February 21, 2016

Why u r equating the rows and columns of two matrices ?!

Reply ↓

Radhika Bhosale
July 19, 2018

Only then the multiplication is possible.

Reply ↓

Sreekant
March 8, 2016

Your Website really helps me in passing my exams since I need to do ctrl + F on my


notebook (real notebook) Hehhee.

Reply ↓

Admin Post author


March 8, 2016

Hey Shreekant, I am so happy to know that my website is helping you, keep


visiting and recommend it to others

Reply ↓

Muhammad Umair
March 11, 2016

man that program isn’t helpful for me


may be it’s a wrong code

Reply ↓

Ahmado
June 9, 2016

Hey the code gives the wrong answer when i input 1->9 for matrix 1 and 9->1 for matrix
2

Reply ↓

Mannat Singh
September 13, 2016

hey…plz tell me the use of “k” in the program….

Reply ↓

Aishwarya
January 2, 2017

Hey I need some coding clarification ,Sorry to use this page in doing so .
I want to create a function with inputs of type vector and integer but the output should
be a matrix .
{ If you are comfortable in matlab it has A = function_name(v,x) ; where x = 5;v =
ones(5,1);A = zeros(5,4). }

I want to use the output matrix as input to another function .I’m new to c++ ,How could
I do that ?
I tried a few methods ;but the function is of type void , which wont output a result ,and
wont give me the freedom to do A = function_name(input1,input2) in c++.

Reply ↓

jojo
March 8, 2017

Your the best ….

Reply ↓

Raj Reghu
June 8, 2017

using namespace std;

what does this line indicate

Reply ↓

christy george
June 19, 2017

The built in C++ library routines are kept in the standard namespace. That
includes stuff like cout, cin, string, vector, map, etc. Because these tools are used
so commonly, it’s popular to add “using namespace std” at the top of your source
code so that you won’t have to type the std:: prefix constantly.

Reply ↓

christy george
June 19, 2017

thanks a lot

Reply ↓

Piyush kumar dubey


July 20, 2017

is we can get the .cpp file which we direct copy and paste in our turbo c++ folder and
the program will run automatically…

Reply ↓

Sanjay
August 6, 2017

Write a C++ program for matrix multiplication. Multiplication function should notify if the
order of the matrix is invalid, using exception.
Can you help me to solve this

Reply ↓

payal
October 14, 2018

thank you sir. very helpful..keep uploading difficult programs

Reply ↓

Ninad
November 24, 2018

what is math behind the formulae…at the last….

Reply ↓

Leave a Reply

Your email address will not be published. Required fields are marked *

Comment

Name *

Email *

Website

Post Comment

Copyright © 2012-19 The Crazy Programmer

You might also like