473,408 Members | 2,405 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

how to multiply two matrix

how to multiply two matrix in c++?
May 3 '10 #1
2 2407
@sidhusaurabh
hi
i had made one program for matrix multiplication in c++.its definitely very helpful to you.
the code is as under.i hope you will get proper understanding from this program.
Expand|Select|Wrap|Line Numbers
  1. #include<iostream.h>
  2. #include<conio.h>
  3.  
  4. void main()
  5. {
  6.     int a[10][10],b[10][10],c[10][10]={0};
  7.     int i,j,k,r1,r2,c1,c2;
  8.     clrscr();
  9.  
  10.     cout<<"\n Enter No Of Rows Of 1st Matrix:";
  11.     cin>>r1; 
  12.     cout<<"\n Enter No Of Columns Of 1st Matrix:";
  13.     cin>>c1; 
  14.  
  15.     cout<<"\n Enter No Of Rows Of 2nd Matrix:";
  16.     cin>>r2;
  17.     cout<<"\n Enter No Of Columns Of 2nd Matrix:";
  18.     cin>>c2;
  19.  
  20.     if(r1!=c2 ||c1!=r2)
  21.     {
  22.         cout<<"\n Matrix Multiplication Is Not Possible:";
  23.         getch();
  24.     }
  25.     cout<<"\n Enter Element Of 1st Matrix:";
  26.     for(i=0;i<r1;i++)
  27.     {
  28.         for(j=0;j<c1;j++)
  29.         {
  30.             cout<<"\n Enter Value Of a["<<i<<"]"<<"["<<j<<"]";
  31.             cin>>a[i][j];
  32.         }
  33.     }
  34.  
  35.     cout<<"\n Enter Element Of 2nd Matrix:";
  36.     for(i=0;i<r2;i++)
  37.     {
  38.         for(j=0;j<c2;j++)
  39.         {
  40.             cout<<"\n Enter Value Of b["<<i<<"]"<<"["<<j<<"]";
  41.             cin>>b[i][j];
  42.         }
  43.     }
  44.     cout<<"\n 1st Matrix is:";
  45.     for(i=0;i<r1;i++)
  46.     {
  47.         for(j=0;j<c1;j++)
  48.         {
  49.         cout<<"\t"<<a[i][j];
  50.         }
  51.     cout<<"\n";
  52.     }
  53.  
  54.     cout<<"\n 2nd Matrix is:";
  55.     for(i=0;i<r2;i++)
  56.     {
  57.         for(j=0;j<c2;j++)
  58.         {
  59.         cout<<"\t"<<b[i][j];
  60.         }
  61.     cout<<"\n";
  62.     }
  63.  
  64.  
  65.     for(i=0;i<r1;i++)
  66.     {
  67.         for(j=0;j<c2;j++)
  68.         {
  69.             for(k=0;k<r2;k++)
  70.             {
  71.                 c[i][j]=c[i][j]+(a[i][k] * b[k][j]);
  72.             }
  73.         }
  74.     }
  75.     cout<<"\n Result Matrix:";
  76.     for(i=0;i<r1;i++)
  77.     {
  78.         for(j=0;j<c2;j++)
  79.         {
  80.             cout<<"\t"<<c[i][j];
  81.         }
  82.     cout<<"\n";
  83.     }
  84. getch();
  85. }
  86.  
  87.  
- ndhamecha
May 3 '10 #2
ndhamecha did you test your program when matrix size is greater than 10x10 ?
If you get the size of the matrix as an input please be so kind to allocate the memory dynamically
May 7 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Zhang Le | last post by:
Hi, I did a small benchmark of matrix-vector multiply operation using Numeric module. I'm a bit suprised to find matrix*col-vector is much faster than row-vector*matrix. I wonder whether other...
4
by: Yudan Yi | last post by:
I have a problem to copy (assign) a matrix to another matrix. Curreny, I know copy the number using loops, while it will take some time, I wonder if there have faster method. The following code...
15
by: christopher diggins | last post by:
Here is some code I wrote for Matrix multiplication for arbitrary dimensionality known at compile-time. I am curious how practical it is. For instance, is it common to know the dimensionality of...
3
by: robix | last post by:
Hi again. I'm now asking your help because of a smal problem i'm getting with my multiplication matrix code. I'll try to give you as many details as possible. My matrix structure: typedef...
14
by: amitnanda | last post by:
Hi Guys, I have a matrix multiplication program in C that multiplies two matrices. When their size is 3*3 or 800*800, the program runs fine. But above that size, I get a "segmentation fault"....
0
by: DarrenWeber | last post by:
# Copyright (C) 2007 Darren Lee Weber # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free...
18
by: Hypnotik | last post by:
Hello everyone. I'm writing a program which uses a class called matrix. I have written all of the different functions, constructor, etc. When I run the program I receive "Constructor", which I...
12
by: sugard | last post by:
I am a java beginner and I was given this task to do.. to create a class which given 2 matrices,and these bein of the correct dimensions, will produce a third matrix by multiplying both. There is the...
1
by: adik1310 | last post by:
i make a project for improving fault analysis by using sequence components, and i use C++. now i have problem how to multiply that matrix with angle 30 degree but not affected diagonal matrix. i.e...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.