Multiplication in C language is very useful, below is a simple example on matrix multiplication, later on i will post matrix arithematic operations using C++
#include<>
void main()
{
int a[10][10],b[10][10],c[10][10],i,j,k,m,n,p,q;
clrscr();
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");
for(i=0;i< m;i++)
{
for(j=0;j< n;j++)
printf(" %d ",a[i][j]); //print the first matrix
printf("\n");
}
printf("The Second Matrix Is:\n");
for(i=0;i< p;i++) // print the second matrix
{
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("\nMultiplication Of The Above Two Matrices Are:\n\n");
for(i=0;i< m;i++)
{
for(j=0;j< q;j++)
{
printf(" %d ",c[i][j]);
}
printf("\n");
}
}
getch();
}
Subscribe to:
Post Comments (Atom)
23 comments:
i found some anomalies in your code:
printf("\nEnter The Rows And Cloumns And Of The Second Matrix:");
scanf("%d %d",&p,&q);
^ here, you put row and col into the variables p and q... but the next part...
printf("\nEnter Elements Of The Second Matrix:\n");
for(i=0;i< m;i++)
{
for(j=0;j< n;j++)
{
scanf("%d",&b[i][j]);
}
}
^ here, you used m and n as sentinels, instead of p and q...
printf("The Second Matrix Is:\n");
for(i=0;i< n;i++) // print the second matrix
^ same here...
Error Regreated !!!!!!!!!
Have corrected it .....
any future hlp warmly welcomed!
You are saying:
printf("\nMultiplication Of The Above Two Matrices Are:\n\n");
for(i=0;i< n;i++)
{
for(j=0;j< p;j++)
{
printf(" %d ",c[i][j]);
}
printf("\n");
}
However, the resulting matrix should be R[r1][c2], which based on your syntax are: m and q. Is that right?
cause of the previous changes the program code was false
now the code is corrected thanx 4 yr support
sir can we show row by coloumn multiplicatn as a
out put if yes send me a code of this program
could u care your explain yr Query.
an example would be sufficient.
there is an error in this program
there is an error in this program
the program was little big
Hmm thanks! acutally i got struck in the main loop in taking the limits, u done write!
Thanks again!
hi Lionel Noronha nice code request would u like to be my friend over the net my email id is manish4mirth@gmail.com, i would like to exchange views over c language if u r intrested than mail me or we can use orkut also
printf("\nEnter The Rows And Cloumns And Of The Second Matrix:");
scanf("%d %d",&p,&q);
this is wrong code for entering the values in array. here must be a loop needed.
printf("\nEnter The Rows And Cloumns And Of The Second Matrix:");
scanf("%d %d",&p,&q);
this is wrong code for entering the values in array. here must be a loop needed.
Thanxx.... I have just started coding in c++ and i got stuck in that... but you provided a better material... thanxxx alot...
thanks for the matrix example. It helped me a lot.
Thanks for this useful example.
why have you used a third variable "k" in the multiplication matrix?? why not use "j"??
kindly reply ASAP
Here im presenting a sample code to multiply two matrix a , b and the result will be stored in matrix C
#include
void main()
{
int a[3][3] , b[3][3] , c[3][3];
int i , j , k;
cout<<"Enter Matrix A";
for( i = 0 ; i < 3 ; i++)
for( j = 0 ; j < 3 ; j++)
cin>>a[i][j];
cout<<"Enter Matrix B";
for( i = 0 ; i < 3 ; i++)
for( j = 0 ; j < 3 ; j++)
cin>>b[i][j];
for( i = 0 ; i < 3 ; i++)
for( j = 0 ; j < 3 ; j++)
{
c[i][j] = 0;
for( k = 0 ;k < 3 ; k++)
c[i][j] += a[i][k]*b[k][j];
}
cout<<"The resultant matrix is ";
for( i = 0 ; i < 3 ; i++)
{
for( j = 0 ; j < 3 ; j++)
cout<<a[i][j]<<" ";
cout<<endl;
}
}
hi
there is a mistake in your code ...
cout<<"The resultant matrix is ";
for( i = 0 ; i < 3 ; i++)
{
for( j = 0 ; j < 3 ; j++)
here --> ////cout< ////cout<<c[i][j]<<" "; /////
hi,there is a mistake in your code ...
cout<<"The resultant matrix is ";
for( i = 0 ; i < 3 ; i++)
{
for( j = 0 ; j < 3 ; j++)
here --> ////cout< ////cout<<c[i][j]<<" "; /////
Thank you!!! was usefule
Good and easy
great job....
Post a Comment