By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,851 Members | 2,097 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,851 IT Pros & Developers. It's quick & easy.

matrix multiplication code

P: 1
Hi everyone
I am new to this site and also new to programming world
can anybody help me writing C code for matrix multiplication (without using pointers)

Amit
Oct 2 '06 #1
Share this Question
Share on Google+
6 Replies


10K+
P: 13,264
Hi everyone
I am new to this site and also new to programming world
can anybody help me writing C code for matrix multiplication (without using pointers)

Amit
You'd have to try writting something first. It is eaier to help when you've shown some of your ideas first.
Oct 2 '06 #2

P: 2
Hi everyone
I am new to this site and also new to programming world
can anybody help me writing C code for matrix multiplication (without using pointers)

Amit
Hi amrit...Iam writing the simple matrix multiplication code for you without using pointers...
Expand|Select|Wrap|Line Numbers
  1. /* Description:To multiply given to matrix using arrays*/
  2. #include<stdio.h>
  3. #include<math.h>
  4. main()
  5. {
  6.   int i,j,k;
  7.  int a,b,c;/*three variables to get the row and colum of the two matrix*/
  8.  int sum;
  9.  int m[10][10],n[10][10],l[10][10];
  10.  printf("The first matrix:\n");
  11.  printf("Enter the number of rows and columns for the first matrix:\t")  ;
  12.  scanf("%d%d",&a,&b);
  13.  printf("The Second matrix:\n");
  14.  printf(Enter the rows and columns of the second matrix:\t:);
  15.  scanf("%d%d",&b,&c);
  16.  
  17.  /* Note: For matrix multiplication the number of columns in the first matrix should be equal to the number of rows in the second message*/
  18.  
  19.  printf(":Enter the values of the first matrix:\n");
  20.  for(i=0;i<a;i++)
  21.    {
  22.      for(j=0;j<b;j++)
  23.        {
  24.          scanf("%d",&m[i][j]);
  25.        }
  26.    }
  27.   printf("Enter the values of the second matrix:\n");
  28.   for(i=0;i<b;i++)
  29.    {
  30.      for(j=0;j<c;j++)
  31.       {
  32.         scanf("%d",&n[i][j]);
  33.       }
  34.    }
  35.    for(i=0;i<a;i++)
  36.      {
  37.     for(j=0;j<c;j++)
  38.      {
  39.        sum=0;
  40.        for(k=0;k<b;k++)
  41.         {
  42.           sum=sum+(m[i][k]*n[k][j]);
  43.           l[i][j]=sum;
  44.         }
  45.      }
  46.   }
  47.   printf("The multiplied matrix is:\n);
  48.   for(i=0;i<a;i++)
  49.    {
  50.      for(j=0;j<c;j++)
  51.       {
  52.          printf ("%d",l[i][j]);
  53.          printf("\t");
  54.       }
  55.        printf("\n");
  56.    }
  57.  
/*This the simple way to do matrix multiplication....You can also modify the program to your own choice...*/
Mar 19 '07 #3

P: 2
Hi everyone
I am new to this site and also new to programming world
can anybody help me writing C code for matrix multiplication (without using pointers)

Amit
Amit I have told you the program.just work on th logic Behind it and try to modify the program...
Mar 19 '07 #4

P: 1
Hey! Can you post the code in C++ also, I really needs this code for my homework, but you have it in C.
Dec 6 '07 #5

P: 1
here is the code for c++=====>

# include <iostream.h>
# include <conio.h>
void main ()
{
clrscr();
int i,j,k,sum,a,b,c;
int m[10][10];
int n[10][10];
int l[10][10];
cout<<"ENTER THE NO. OF ROWS AND COLUMN OF FIRST MATRIX";
cin>>a>>b;
cout<<"ENTER THE VALUES OF FIRST MATRIX-";
for(i=0;i<a;i++)
{
for(j=0;j<b;j++)
{
cin>>m[i][j];
}
}
cout<<"ENTER THE NO. OF ROWS AND COLUMN OF SECOND MATRIX";
cin>>b>>c;
cout<<"ENTER THE VALUES OF SECOND MATRIX-";
for(i=0;i<b;i++)
{
for(j=0;j<c;j++)
{
cin>>n[i][j];
}
}

for(i=0;i<a;i++)
{
for(j=0;j<c;j++)
{
sum=0;
for(k=0;k<b;k++)
{
sum=sum+m[i][k]*n[k][j];
l[i][j]=sum;
}
}
}
cout<<"THE RESULTANT MATRIX IS-"<<endl;
for(i=0;i<a;i++)
{
for(j=0;j<c;j++)
{
cout<<l[i][j]<<" ";
}
cout<<endl;
}
getch();
}
May 1 '10 #6

P: 1
======> HERE IS FOR JAVA MATRIX MULTIPLICATION
int A[][]=new int[3][3];
int B[][]=new int[3][3];
int C[][]=new int[3][3];
Scanner srh=new Scanner(System.in);
Random rnd=new Random();
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
System.out.println("Enter A: ");
A[i][j]=srh.nextInt();
}
}
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
System.out.println("Enter B: ");
B[i][j]=srh.nextInt();
}
}
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
C[i][j]=0;
for(int k=0;k<3;k++){
C[i][j]+=A[i][k]*B[k][j];
}
}
}
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
System.out.println("Value of C: "+C[i][j]);
}
}
Nov 19 '11 #7

Post your reply

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