M
mojo
I have my code pretty much done except I have a few problems that I
need to fix and can't really quite remember how to fix them. Thanks for
the help
Error 1:cannot convert from 'const int' to 'int [6]'
Error 2:cannot add two pointers
Error 3:no conversion from 'int *' to 'int'
Error 4:'int' differs in levels of indirection from 'int [6]'
I have labeled my code with the listed errors above.
This code should output the Recursive Matrix Chain (which is fine) and
the Matrix chain order(which has problems).
#include <iomanip>
#include <iostream>
#include <cstdlib>
using namespace std;
const int SIZE = 6;
const int MAX = 999999;
int M[SIZE][SIZE];
int P[SIZE + 1] = { 5, 10, 3, 12, 5, 50, 6};
int Recursive_Matrix(int i, int j);
void Print_M();
int Matrix_Chain_Order();
int main()
{
Recursive_Matrix( 0, SIZE - 1);
Print_M();
return 0;
}
int Recursive_Matrix(int i, int j)
{
int q;
if( i == j )
return 0;
else
{
M[j] = MAX;
for(int k = i; k <= j - 1; k++)
{
q = Recursive_Matrix(i, k);
q += Recursive_Matrix( k+1, j);
q += P * P[k + 1] * P[j + 1];
if( q < M[j])
M[j] = q;
}
}
return M[j];
}
void Print_M()
{
for(int x = 0; x < SIZE; x++)
{
for(int y = 0; y < SIZE; y++)
{
cout << setw(7) << M[x][y];
}
cout << endl;
}
}
int Matrix_Chain_Order()
{
int n = SIZE;
int s[SIZE][SIZE];
for (int i=1;i<=n;i++)
{ M[i,i] = 0; } //Error 1
for (int l=2;l<=n;l++){
for (int i=1;i<=n-l+1;i++)
{
int j = i+l-1;
M[i,j] = MAX;//Error 1
for(int k=i;k<=j-1;k++)
{
int q = M[i,k] + M[k+1,j] + P[i-1]*(P[k])*(P[j]);//Error 2
if(q<M[i,j])//Error 3 and Error 4
{
M[i,j] = q;//Error 1
s[i,j] = k;//Error 1
}
}
}
}
}
need to fix and can't really quite remember how to fix them. Thanks for
the help
Error 1:cannot convert from 'const int' to 'int [6]'
Error 2:cannot add two pointers
Error 3:no conversion from 'int *' to 'int'
Error 4:'int' differs in levels of indirection from 'int [6]'
I have labeled my code with the listed errors above.
This code should output the Recursive Matrix Chain (which is fine) and
the Matrix chain order(which has problems).
#include <iomanip>
#include <iostream>
#include <cstdlib>
using namespace std;
const int SIZE = 6;
const int MAX = 999999;
int M[SIZE][SIZE];
int P[SIZE + 1] = { 5, 10, 3, 12, 5, 50, 6};
int Recursive_Matrix(int i, int j);
void Print_M();
int Matrix_Chain_Order();
int main()
{
Recursive_Matrix( 0, SIZE - 1);
Print_M();
return 0;
}
int Recursive_Matrix(int i, int j)
{
int q;
if( i == j )
return 0;
else
{
M[j] = MAX;
for(int k = i; k <= j - 1; k++)
{
q = Recursive_Matrix(i, k);
q += Recursive_Matrix( k+1, j);
q += P * P[k + 1] * P[j + 1];
if( q < M[j])
M[j] = q;
}
}
return M[j];
}
void Print_M()
{
for(int x = 0; x < SIZE; x++)
{
for(int y = 0; y < SIZE; y++)
{
cout << setw(7) << M[x][y];
}
cout << endl;
}
}
int Matrix_Chain_Order()
{
int n = SIZE;
int s[SIZE][SIZE];
for (int i=1;i<=n;i++)
{ M[i,i] = 0; } //Error 1
for (int l=2;l<=n;l++){
for (int i=1;i<=n-l+1;i++)
{
int j = i+l-1;
M[i,j] = MAX;//Error 1
for(int k=i;k<=j-1;k++)
{
int q = M[i,k] + M[k+1,j] + P[i-1]*(P[k])*(P[j]);//Error 2
if(q<M[i,j])//Error 3 and Error 4
{
M[i,j] = q;//Error 1
s[i,j] = k;//Error 1
}
}
}
}
}