Matrix Multiplication

ashishtrivedi 0 Tallied Votes 214 Views Share

A console application to multiply two matrix of float numbers and print result on console. Some memory checks are done as it is allocating memory dynamicaly. May give error and exit when short of memory.

// Matrix Multiplication.cpp : Defines the entry point for the console application.
//

//#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>


//Global Variables.
float **matrix[2];
double **ResultMatrix;
int row[3], col[3];

//Function Declarations.
void ErrorExit(char *msg);
void InitMatrix(int MatrixNo,int r,int c);
void ReadMatrix(int MatrixNo,int r,int c);
void PrintMatrix(int MatrixNo,int r,int c);
void freeMatrix(int MatrixNo,int r);
void MultiplyMatrix();


int main()
{
	printf("\n\t/================================================\\");
	printf("\n\t|          Matrix Multiplication Program.        |");
	printf("\n\t|          Developed By Aashish Trivedi.         |");
	printf("\n\t|          E-mail : aashishktrivedi@yahoo.com    |");
	printf("\n\t\\================================================/");

	printf("\nFor first metrix.");
	printf("\nEnter number of rows :: ");
	scanf("%d",&row[0]);
	printf("\nEnter number of columns :: ");
	scanf("%d",&col[0]);

	printf("For Second metrix.\n");
	printf("\nEnter number of rows :: ");
	scanf("%d",&row[1]);
	printf("\nEnter number of columns :: ");
	scanf("%d",&col[1]);

	if(col[0] != row[1])
	{
		printf("\nUSAGE : Matrix Multiplication.");
		printf("\n  Matrix 1 \t*\t Matrix 2\t\t    RESULT");
		printf("\n| 1 \t 2 | \t\t | 5 \t 6 |\t\t |  5 22 |");
		printf("\n|   \t   | \tX\t |  \t 6 |\t\t |       |");
		printf("\n| 3 \t 4 | \t\t | 0 \t 8 |\t\t | 15 50 |");
		printf("\n====================================================");
 		ErrorExit("\n\nERROR:: Number of Columns in First matrix must be equal to \n\t\tnumber of ROWs in second matrix.");
	}

	row[2] = row[0];
	col[2] = col[1]; 

	InitMatrix(0,row[0],col[0]);
	InitMatrix(1,row[1],col[1]);
	InitMatrix(2,row[2],col[2]);

	ReadMatrix(0,row[0],col[0]);
	ReadMatrix(1,row[1],col[1]);

	MultiplyMatrix();
	printf("\nMatrix 1");
	PrintMatrix(0,row[0],col[0]);

	printf("\nMatrix 2");
	PrintMatrix(1,row[1],col[1]);

	printf("\nResult Matrix ");
	PrintMatrix(2,row[2],col[2]);



	printf("\nPress Any key to exit.");
	getch();
	freeMatrix(0,row[0]);
	freeMatrix(1,row[1]);
	freeMatrix(2,row[2]);

	return 0;
}

void InitMatrix(int MatrixNo,int r,int c)
{
	int i;
	if(MatrixNo != 2)
	{
		matrix[MatrixNo] = (float **)calloc(r,sizeof(float));

		if(matrix[MatrixNo] == NULL)
			ErrorExit("Insufficient Memory.");


		for(i=0;i<r;i++)
		{
			matrix[MatrixNo][i] = (float *)calloc(c, sizeof(float));

			if(matrix[MatrixNo][i] == NULL)
				ErrorExit("Insufficient Memory.");
		}
	}
	else
	{
		ResultMatrix = (double **)calloc(r,sizeof(double));

		if(ResultMatrix == NULL)
			ErrorExit("Insufficient Memory.");


		for(i=0;i<r;i++)
		{
			ResultMatrix[i] = (double *)calloc(c, sizeof(double));

			if(ResultMatrix[i] == NULL)
				ErrorExit("Insufficient Memory.");
		}
	}
}

void ReadMatrix(int MatrixNo,int r,int c)
{
	int i,j;
	printf("\nEnter matrix elements.");
	for(i=0;i<r;i++)
	{
		printf("\nRow No %d \'s %d elements.",i+1,c);
		for(j=0;j<c;j++)
			scanf("%f",&matrix[MatrixNo][i][j]);
	}
}

void PrintMatrix(int MatrixNo,int r,int c)
{
	int i,j;
	if(MatrixNo != 2)
	{
		for(i=0;i<r;i++)
		{
			printf("\n\t|");
			for(j=0;j<c;j++)
				printf("%10.4f",matrix[MatrixNo][i][j]);
			printf("|");
		}
	}
	else
	{
		for(i=0;i<r;i++)
		{
			printf("\n\t| ");
			for(j=0;j<c;j++)
				printf("%lf ",ResultMatrix[i][j]);
			printf("|");
		}

	}
}

void ErrorExit(char *msg)
{
	printf(msg);
	getch();
	exit(0);
}

void freeMatrix(int MatrixNo,int r)
{
	int i;

	if(MatrixNo != 2)
	{
		for(i=0;i<r;i++)
			free(matrix[MatrixNo][i]);

		free(matrix[MatrixNo]);
	}
	else
	{
		for(i=0;i<r;i++)
			free(ResultMatrix[i]);

		free(ResultMatrix);
	}

}

void MultiplyMatrix()
{
	int i,j,k;

	for(i=0;i<row[2];i++)
		for(j=0;j<col[2];j++)
			for(k=0;k<col[0];k++)
				ResultMatrix[i][j] += matrix[0][i][k] * matrix[1][k][j];
}
aravindanne 0 Newbie Poster

The program you wrote is not correct...
and that too long...
i dislike this program...

aravindanne 0 Newbie Poster

===>If you want the full program for the multiplication of two or three or four matrices contact me...
here i have written only few codings...
the main code should not be shown..
contact...[snipped]


// Multiplication of two matrices…
#include<stdio.h>
void main()
{
int a[10][10],b[10][10],c[10][10];
int i,j,k,r1,c1,r2,c2;
printf(

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.