#include <string>
#include <iostream>
#include <vector>
#include <iomanip>
using namespace std;
int main()
{
double m[7][7]; //2d dimension array m
int s[7][7]; //2d dimension array s
int p[7] = {30,35,15,5,10,20,25}; //array p to store numbers
int j;
double q;
int n = 5;
for(int i = 1; i != n; ++i) //initialize m[1][1], m[2][2], ... to zero
m[i][i] = 0;
for(int l = 2; l != n; ++l) // performing the algorithm
{
for(int i = 1; i != (n-l+1); ++i)
{
j = i + (l-1);
m[i][j] = numeric_limits<double>::max(); // find the max
for(int 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;
}
}
}
}
Test below:
for(int i = 0; i != 4; ++i)
{
for(int j = 0; j != 4; ++j)
{
// print the m[i][j] to test the output
cout << "m[" << i << "][" << j << "] " << " = " << m[i][j] << endl;;
}
}
}
I'm doing a matrix chain order, the output(my test case) should have values after performing the algorithm. But it's all zero like the picture. For example, m1[2] should be 30*35*15 = 15750.
Aucun commentaire:
Enregistrer un commentaire