2 Replies - 29960 Views - Last Post: 06 September 2008 - 12:17 PM Rate Topic: -----

Replies To: multiplication of two matrices

#2 arnabraha1989  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 14
  • Joined: 02-August 08

Re: multiplication of two matrices

Posted 06 September 2008 - 01:42 AM

I have written a program on matrix multiplication using arrays like this
//program to multiply 2 matrices
#include<stdio.h>
#include<conio.h>
#define max 10
void main()
{
int a[max][max];
int a1[max][max];
int a2[max][max];
int i,j,n,m,k,l,e;
clrscr();
printf("\nEnter the number of rows of first matrix:");//insertion of first matrix
scanf("%d",&n);
printf("\nEnter the number of columns of second matrix:");
scanf("%d",&m);
printf("\nEnter the elements of first matrix:\n");
for(i=0;i<n;i++)
for(j=0;j<m;j++)
scanf("%d",&a[i][j]);
printf("\nEnter the number of rows of second matrix:");//insertion of second matrix
scanf("%d",&k);
printf("\nEnter the number of columns of second matrix:");
scanf("%d",&l);
printf("\nEnter the elements of second matrix:\n");
for(i=0;i<k;i++)
for(j=0;j<l;j++)
scanf("%d",&a1[i][j]);
printf("\n First Matrix you entered:\n");//display of first matrix
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
printf("%d ",a[i][j]);
printf("\n");
}
printf("\n Second Matrix you entered:\n");//display of second matrix
for(i=0;i<k;i++)
{
for(j=0;j<l;j++)
printf("%d ",a1[i][j]);
printf("\n");
}
//Multiplication of these two matrices
for(i=0;i<n;i++)
{
for(j=0;j<k;j++)
{
a2[i][j]=0;
for(e=0;e<k;e++)
{
a2[i][j]=a2[i][j] + (a[i][e] * a1[e][j]);
}
}
}
printf("\n Multiplication of matrix is:\n");
for(i=0;i<n;i++)
{
for(j=0;j<l;j++)
printf("%d ",a2[i][j]);
printf("\n");
}
getch();
}


It runs perfectly.
I recently saw a question to write a C program on matrix multiplication using array of pointers.
Whats the difference. Do I have to refer all the elements in the matrix using pointers or what?
Was This Post Helpful? 1

#3 joske  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 43
  • View blog
  • Posts: 297
  • Joined: 04-September 07

Re: multiplication of two matrices

Posted 06 September 2008 - 11:57 AM

In your current code you have to define the maximum size of the matrices beforehand, with max. When you use pointers, you can create matrices with dynamic size. Your current program will crash when the user enters a number of rows or columns larger than max, and you start filling in the elements...
Was This Post Helpful? 0
  • +
  • -

#4 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4332
  • View blog
  • Posts: 12,127
  • Joined: 18-April 07

Re: multiplication of two matrices

Posted 06 September 2008 - 12:17 PM

Just to jump in here and give you a source that may help you out in the future with this problem you can check out my blog on the topic. I do matrice math with waffles! A humorous twist to a C++ topic. :)

Martyr2's Programming Underground - Waffle Matrices... Mmmm where is the syrup?

It shows code and everything in C++ for multiplying matrices. :)

This post has been edited by Martyr2: 06 September 2008 - 12:18 PM

Was This Post Helpful? 1

Page 1 of 1