answersLogoWhite

0


Best Answer

# include <stdio.h>

# include <conio.h>

# include <stdlib.h>

# define sz 20

# define INF 200000

void print(unsigned long s[][sz], int i, int j)

{

if (i == j)

printf(" A%d ",i);

else

{

printf(" ( ");

print(s, i, s[i][j]);

print(s, s[i][j] + 1, j);

printf(" ) ");

}

}

void printm(unsigned long m[][sz], int n)

{

int i,j;

for(i=1;i<=n;i++)

{

for(j=1;j<=n;j++)

{

printf("%5d",m[i][j]);

}

printf("\n\n");

}

printf("\nThe No. of multiplication required is : %d",m[1][n]);

}

void Matrix_Chain_Order(int p[],int num)

{

unsigned long m[sz][sz] = {0};

unsigned long s[sz][sz] = {0};

unsigned int q = 0;

int i, j, k, l;

int n = num;

for(i = 1; i <= n; i++)

m[i][i] = 0;

for(l = 2; l <= n; l++)

for(i = 1; i <= (n - l + 1); i++)

{

j = i + l - 1;

m[i][j] = INF;

for(k = i; k <= j - 1; k++)

{

q = m[i][k] + m[k+1][j] + p[i-1] * p[k] * p[j];

if(q < m[i][j])

{

m[i][j] = q;

s[i][j] = k;

}

}

}

print(s, i-1, j);

printf("\n\n");

printf("The Minimum No. of Multiplication Required is:\n\n");

printm(m,n);

}

void main()

{

int i,num=0,p[sz]={0};

clrscr();

printf("Enter the number of matrix : ");

scanf("%d",&num);

printf("Enter %d no. of order sequence for %d matrix :\n",num+1,num);

for(i=0;i<=num;i++)

scanf("%d",&p[i]);

printf("\n\n");

printf("MULTIPLICATION SEQUENCE IS : ");

printf("\n\n\t");

Matrix_Chain_Order(p,num);

getch();

}

User Avatar

Wiki User

11y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

12y ago

/*QUES=> Write a program to execute Matrix Multiplication.*/

/* MATRICES MULTIPLICATION */

/*-----------------------*/

#include<stdio.h>

#include<conio.h>

void main()

{

int a[3][3], b[3][3], c[3][3], i, j, k;

clrscr();

printf("\n MATRICES MULTIPLICATION");

printf("\n-------------------------");

// Input Of Matrix 'A'

printf("\n\n Enter the elements of Matrix A =>\n\n");

for(i=0; i<3; i++)

{

for(j=0;j<3;j++)

{

scanf("%d",&a[i][j]);

}

}

// Input Of Matrix 'B'

printf("\n\n Enter the elements of Matrix B =>\n\n");

for(i=0; i<3; i++)

{

for(j=0;j<3;j++)

{

scanf("%d",&b[i][j]);

}

}

// Display Of Matrix 'A'

printf("\n\n Elements of Matrix A =>\n");

for(i=0; i<3; i++)

{

for(j=0;j<3;j++)

{

printf("%d\t",a[i][j]);

}

printf("\n");

}

// Display Of Matrix 'B'

printf("\n\n Elements of Matrix B =>\n");

for(i=0; i<3; i++)

{

for(j=0;j<3;j++)

{

printf("%d\t",b[i][j]);

}

printf("\n");

}

printf("\n\n Multiplication of Matrices [Matrix A * Matrix B] =>\n");

// Matrix Multiplication Logic

for(i=0; i<3; i++)

{

for(j=0; j<3; j++)

{

c[i][j]=0;

for(k=0; k<3; k++)

{

c[i][j]=c[i][j]+(a[i][k]*b[k][j]);

}

}

}

// Display Of Matrix Multiplied

for(i=0; i<3; i++)

{

for(j=0;j<3;j++)

{

printf("%d\t",c[i][j]);

}

printf("\n");

}

getch();

}

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

Matrix can be created using Multidimensional array in C.To perform a Matrix Multiplication the number of columns of the first matrix should be equal to number of rows of the second matrix.

The basic Code involved in this process is C[i][j]+=A[i][p]*B[p][j];

Here C refers to product matrix A and B refers to Source matrices respectively.

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

// do you mean matrix multiplication

// matrix multiplication_m_n.cpp : Defines the entry point for the console application.

// Rakesh

#include<stdio.h>

#include<conio.h>

void AcceptMatrix(int m[10][10], int r, int c);

void PrintMatrix(int m[10][10], int r, int c);

void main()

{

int a[10][10], b[10][10], c[10][10], r1, r2, c1, c2, i, j, k;

clrscr();

printf("Enter the rows & columns of first matrix ");

scanf("d",&r1, &c1);

printf("Enter the rows & columns of second matrix ");

scanf("d",&r2, &c2);

if (c1 != r2)

{

printf("Error, size of matrices are not compatible for multiplication");

}

printf("Enter Matrix A ");

AcceptMatrix(a, r1, c1);

printf("\nEnter Matrix B ");

AcceptMatrix(b, r2, c2);

for (i = 0; i < r1 ; i++)

{

for (j = 0; j < c1; j++)

{

c[i][j]=0;

for (k = 0; k < c1 ; k++)

{

c[i][j] += a[i][k] * b[k][j];

}

}

}

printf("Matrix A\n");

PrintMatrix(a, r1, c1);

printf("\nMatrix B\n");

PrintMatrix(b, r2, c2);

printf("\nMatrix C, the result matrix\n");

PrintMatrix(c, r1, c2);

getch();

}

void AcceptMatrix(int m[10][10], int r, int c)

{

int i, j;

for (i = 0; i < r; i++)

{

for (j = 0; j < c; j++)

{

scanf("%d", &m[i][j]);

}

}

}

void PrintMatrix(int m[10][10], int r, int c)

{

int i, j;

for (i = 0; i < r; i++)

{

for (j = 0; j < c; j++)

{

printf("%-10d", m[i][j]);

}

printf("\n");

}

}

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Program of Matrix Chain Multiplication in c language?
Write your answer...
Submit
Still have questions?
magnify glass
imp