1 Replies - 855 Views - Last Post: 17 May 2008 - 10:56 AM Rate Topic: -----

#1 chess0905  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 26-October 07

matrix operations HELP!ASAP!

Posted 17 May 2008 - 09:40 AM

/* Addition , Subtraction and Multiplication of Matrices */
#include<stdio.h>

int mul(int m1[20][20],int m2[20][20],int r,int c);
int diff(int m1[20][20],int m2[20][20],int r,int c);
int add(int m1[20][20],int m2[20][20],int r,int c);

main()
{
	int a[20][20], b[20][20];
	int i,j,m,n,p,q,choice;
	clrscr();
	/*Getting the element of the Matrix1 */
	printf("Enter the order of first matrix\n");
	scanf("%d %d", &m, &n);
	printf("%d x %d", m,n);
	printf("\n Enter the element of first matrix\n");
	for(i=0; i<m; i++)
	for(j=0; j<n; j++)
	{
		scanf("%d", &a[i][j]);
	}

	/*Getting the element of the Matrix2 */
	printf("\n Enter the order of second matrix:\n");

	scanf("%d %d", &p,&q);
	printf("%d x %d", p,q);
	printf("\n Enter the element of second matrix\n");
	for(i=0; i<p; i++)
	for(j=0; j<q; j++)
	{
		scanf("%d",&b[i][j]);
	}
	printf("\n");
	/* Displaying Matrix1 and Matrix2 */
	clrscr();
	printf("\n\t\t Addition, Subtraction, Multiplication of Matrixes\n\n");

	if(m==p && n==q)
	printf ("\n");
	printf("First matrix:\n");
	for(i=0; i<m; i++)
	{
	for(j=0; j<n; j++)
	   printf("\t%d", a[i][j]);
	printf("\n");
	}

	printf("\n");
	printf("Second Matrix: \n");
	for(i=0; i<p; i++)
	{
	for(j=0; j<q; j++)
		printf("\t%d", b[i][j]);
	printf("\n");
	}
	if(n==p)	  /* If columns(Mat.1) = Rows (Mat.2) it Multiplication only possible */
	printf("\n INVALID DIMENSIONS");




	//MENU/
	printf ("\n---MENU---\n");
	printf ("1: Addition\n");
	printf ("2: Subtraction\n");
	printf ("3: Multiplication\n");
	printf ("Enter choice:");
	scanf ("%d",&choice);
	printf ("\n");

	switch (choice){
	case 1: printf ("The sum is:\n");

	add(a,b,m,n);

	  }
getch();
return 0;
}

mul(int m1[20][20], int m2[20][20], int r,int c)

{
	int i,j,p[20][20];

	 for(i=0;i<r;i++)
	   for(j=0;j<c;j++)
	   {
		p[i][j]=m1[i][j]*m2[i][j];
	}

	printf("\n\t The product: \n");
	for(i=0; i<r; i++)
	{
	for(j=0; j<c; j++)
	{
		printf("\t%d", p[i][j]);
	}
	printf("\n\n");
	} return p[i][j];
}

add(int m1[20][20],int m2[20][20],int r,int c)
{
	   int i,j,sum[20][20];
	   for(i=0;i<r;i++)
	   for(j=0;j<c;j++)
	   {
		sum[i][j]=m1[i][j]+m2[i][j];
		  }
	   printf("\n");
	  // printf("\t The sum:\n");
	   for(i=0; i<r; i++)
	   {
		for(j=0;j<c;j++)
		printf("\t%d",sum[i][j]);
		printf("\n\n");

	}return sum[i][j];

}

diff(int m1[20][20],int m2[20][20],int r,int c)
{
 int i,j,diff[20][20];
	   for(i=0;i<r;i++)
	   for(j=0;j<c;j++)
	   {
		diff[i][j]=m1[i][j]-m2[i][j];
	   }
   printf("\t The difference:\n");
	for(i=0; i<r; i++)
	{
	for(j=0; j<c; j++)
	printf("\t%d", diff[i][j]);
	printf("\n\n");
	}
	return diff[i][j];
	}


**Im in need of help, as in serious help, whenever i input an unequal dimension, the "invalid dimension" doesn't work, it goes straight to the menu. The output should not display any menu and only "invalid dimensions" whenever my dimensions are unequal.

Is This A Good Question/Topic? 0
  • +

Replies To: matrix operations HELP!ASAP!

#2 Oscar53  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 15-May 08

Re: matrix operations HELP!ASAP!

Posted 17 May 2008 - 10:56 AM

you must put the "{" after if(m==p && n==q) and "}" before if(n==p)

you can do:

if(m==p && n==q)
{
....
....
}
else
printf("\n INVALID DIMENSIONS");

the error should be this.

Bye :-)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1