0% found this document useful (0 votes)
18 views3 pages

M CM

Uploaded by

pop pee
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views3 pages

M CM

Uploaded by

pop pee
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

7.

MCM:-

#include <stdio.h>

#include <limits.h>

void printParenthesis(int i, int j, int n, int* bracket, int* mat_count_ptr) {

if (i == j) {

printf("A%d", (*mat_count_ptr)++);

return;

printf("(");

printParenthesis(i, *((bracket + i * n) + j), n, bracket, mat_count_ptr);

printf(" X ");

printParenthesis(*((bracket + i * n) + j) + 1, j, n, bracket,

mat_count_ptr);

printf(")");

void matrix_mul(int n, int arr[]){

int c[n][n];

int k[n][n];

for(int i = 0;i<n;i++){

c[i][i] = 0;

k[i][i] = 0;

}
for(int l = 2;l<n;l++){

int j = l;

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

c[i][j] = INT_MAX;

for(int K = i;K<j;K++){

int val = c[i][K]+c[K+1][j] + (arr[i-1]*arr[K]*arr[j]);

if(val<c[i][j]){

c[i][j] = val;

k[i][j] = K;

++j;

for(int i = 0;i<n;i++){

for(int j = 0;j<n;j++){

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

printf("\n");

printf("\n\n");

for(int i = 0;i<n;i++){

for(int j = 0;j<n;j++){

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

printf("\n");

int mat_count = 1;
printParenthesis(1,n-1,n,(int*)k,&mat_count);

int main(){

int n;

printf("Enter the size of the list: ");

scanf("%d",&n);

int arr[n];

char c;

for(int i = 0;i<n;i++){

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

matrix_mul(n,arr);

You might also like