#include<iostream.h>
#include<conio.h>
class chained
{
private:
int n,i,j,k,min1,min2;
int m[100],p[100],q[100][100];
public:
void get();
void process();
}c;
void chained::get()
{
cout<<"\nEnter the total number of matrices:";
cin>>n;
for(i=1;i<=n;i++)
{
cout<<"\nEnter the size of the matrix M:";
cout<<i<<"\n";
cin>>m[i];
cin>>p[i];
}
}
void chained::process()
{
for(i=2;i<=n;i++)
{
for(j=1;j<i;j++)
{
q[i][j]=9999;
}
}
for(i=1;i<=n;i++)
q[i][i]=0;
for(i=1;i<n;i++)
q[i][i+1]=(m[i]*p[i+1])*p[i];
for(i=1;i<n-1;i++)
{
min1=((m[i]*p[i+2])*p[i])+q[i][i+1];
min2=((m[i]*m[i+2])*p[i])+q[i+1][i+3];
if(min1<min2)
q[i][i+2]=min1;
else
q[i][i+2]=min2;
}
for(i=1;i<=n;i++)
{
cout<<"\n";
for(j=1;j<=n;j++)
{
cout<<"\t";
cout<<q[i][j];
}
}
}
void main()
{
clrscr();
c.get();
c.process();
getch();
}
Chained matrix
Share This!
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment