Code Scrappers


Matrix Chain Multiplication C Program


                #include<stdio.h>
                #include<stdlib.h>
                #include<limits.h>

                int matrixChainOrder(int p[], int, int);

                void main()

                {
                        int n, i;

                        printf("enter the no of matrices\n");

                        scanf("%d",&n);

                        int arr[n++];

                        printf("enter the dimensions\n");

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

                        {
                                printf("enter the d %d : ",i);

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

                        printf("total no.  of multilication are %d",matrixChainOrder(arr, 1, n-1));
                }

                int matrixChainOrder(int p[], int i, int j)

                {
                        if(i == j)
                        {
                                return 0;
                        }

                        int k, minimum, count;

                        minimum = INT_MAX;

                        for(k=i;k<j;k++)
                        {
                                count = matrixChainOrder(p, i, k) + matrixChainOrder(p, k+1, j) + p[i-1]*p[k]*p[j];


                                if(count < minimum)
                                {
                                      minimum = count;
                                }
                        }
                        return minimum;
                }
            



Output

          enter the no of matrices
          4

          enter the dimensions
          enter the d 0 : 10
          enter the d 1 : 100
          enter the d 2 : 20
          enter the d 3 : 5
          enter the d 4 : 80

          total no.  of multilication are 19000