Close Back

#include<stdio.h>
#include<conio.h>

main()
{
	int mtrx1[5][5],mtrx2[5][5],product[5][5],ro1,ro2,col1,col2;
	unsigned short int posblty;
	static unsigned short int call_main = 0;
	if(call_main == 0 || call_main % 10 == 0)
	{
		clrscr();
		printf("\nThis program will calculate the product of two matrices");
		printf("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
	}
	call_main++;
	getnum(&ro1,1);
	getnum(&col1,3);
	getnum(&ro2,2);
	getnum(&col2,4);
	posblty = is_posble(col1,ro2);
	if(!posblty)
	{
		getch();
		repeat();
	}
	getmatrix(mtrx1,ro1,col1,1);
	getmatrix(mtrx2,ro2,col2,2);
	multiply(mtrx1,mtrx2,product,ro1,col2,col1);
	print_matrix(product,ro1,col2);
	find_trace(product,ro1,col2);
	repeat();
}

getnum(num,message)
int *num,message;
{
	do
	{
	switch(message)
	{
		case 1:
		printf("\nEnter the number of rows in matrix 1:");
		break;

		case 2:
		printf("\nEnter the number of rows in matrix 2:");
		break;

		case 3:
		printf("\nEnter the number of columns in matrix 1:");
		break;

		case 4:
		printf("\nEnter the number of columns in matrix 2:");
		break;

		default:
		printf("\nEnter a number:");
	}
	scanf("%d",num);
	}
	while(*num > 5);
	return;
}

is_posble(col_1,row_2)
int row_2,col_1;
{
	if(col_1 != row_2)
	{
		printf("\nThe multiplication of given matrices is not possible");
		return(0);
	}
	else
	{
		return(1);
	}
}

getmatrix(array,row,column,mtrx_num)
int *array[5],row,column,mtrx_num;
{
	int i,j;
	printf("\nEnter matrix %d\n",mtrx_num);

	for(i = 0;i < row;i++)
	{
		for(j = 0;j < column;j++)
		{
			scanf("%d",array+(5*i+j));
		}
	}
	return;
}

multiply(matrix1,matrix2,product,l,m,n)
int *matrix1[5],*matrix2[5],*product[5],l,m,n;
{
	int i,j,k,temp1,temp2,temp3;

	for(i=0;i < l;i++)
	{
		for(j = 0;j < m;j++)
		{
			*(product+(5*i+j)) = 0;
			for(k = 0;k < n;k++)
			{
				temp1 = *(matrix1+(5*i+k));
				temp2 = *(matrix2+(5*k+j));
				temp3 = *(product+(5*i+j));
				*(product+(5*i+j)) = temp1 * temp2 + temp3;
			}
		}
	}
	return;
}

print_matrix(array,row,col)
int *array[5];
{
	int i,j;
	printf("\nThe product is :");
	for(i = 0;i < row;i++)
	{
		printf("\n");
		for(j = 0;j < col;j++)
		{
			printf("%5d \t",*(array+(5*i+j)));
		}
	}
	return;
}

find_trace(ip_array,r,c)
int *ip_array[5],r,c;
{
	int trace = 0,temp;
	short int i,k = (r == c);
	if(!k)
	{
		printf("\nTrace of matrix dosen\'t exist");
	}
	else
	{
		for(i = 0;i < r;i++)
		{
			temp = *(ip_array+(5 * i + i));
			trace += temp;
		}
		printf("\nTrace is:%d",trace);
	}
	return;
}

repeat()
{
	char response[4];
	printf("\n\nMultiply two more matrices? (y/n):");
	scanf("%3s",response);
	if(response[0] == 'y' || response[0] == 'Y')
		main();
	else
	{
		printf("\n\n\tHit any key to continue....");
		getch();
		print_rajesh();
	}
}

/****************************************************************************************/
/* A C Function to print 'Rajesh' in a stylish way can (should) be used in any c program*/
/****************************************************************************************/

print_rajesh()
{
	clrscr();
	printf("\nThis program made by :");
	printf("\n\n\n\n\n\007");
	printf(",##########.                   ,####.                          ,####.\n");
	printf(":#############.                `####;                          :####.\n");
	printf(":#####. :#####.                                                :####.\n");
	printf(":#############;   `#########.  ,####.  ,#######.    ,########. :###########.\n");
	printf(":############;          :####. :####. ,###. :###.  ,####.  :#. :###########.\n");
	printf(":##########.       :#########. :####.,####. :####.`##########. :####. :####.\n");
	printf(":###########.   ,############. :####.:###########;  `#########.:####. :####.\n");
	printf(":############.  :#####. :####. :####.`#####.            :#####;:####. :####.\n");
	printf(":#####. `######  ############. :####. `#########.  ,#########; :####. :####.\n");
	printf("`#####;  `######; `##########; :####.     `#####;  `#######;   `####; `####;\n");
	printf("                              #####");
	printf("\n\n\n\tRajesh _ThE gReAt in 2k.....\n\n\n\t\t\tHit any key to continue.....");
	getch();
	exit();
}