Here's what i've done, the output of this program is suppose to have
"w(1234) = 6 Sample set: 1 1 2 2 6 10" this works! however for larger numbers it wont have the correct sample set.
The sample set are the cubes that make up that w(n) number, i can't seem to get the correct answer for larger numbers for e.g.
"W(10000) = 3 Sample set: 10 10 20",
i think its something to do with the int w(int n)
its choosing the wrong m.
Please assist!!
Code:
int w(int n, int cubes[], int S[], int T[]);
void printSample(int n,int cubes[],int S[],int T[]);
const int maxCubes = 10; //never more than 9 cubes so 10 is safe
const int maxInput = 16000;
int main()
{
int n; // delclaring variables
int cubes[24]; // holds cubes of numbers. max is 24^3 saves recalculation
int S[maxInput];
int T[maxInput];
int i, cube; // initialize cube table
for(i=1;(cube=(i*i*i)) <= maxInput; i++){ // saves all m^3 from 1-11 data
cubes[i-1] = cube;
}
cout << "Enter a non negative integer: " ;
cin >> n;
while (n > 0) // tests for non-negative numbers
{
for(i=0; i<=n; i++){ // initialize T and S
w(i,cubes,S,T);
}
cout << "W(" << n << ") = " << T[n] << " " << "Sample Set: ";
// printS(n,S,T);
printSample(n,cubes,S,T);
cout << endl;
cout << "Enter a non negative integer: " << endl;
cin >> n;
}
return 0; // terminates when integer is negative
}
void printSample(int n,int cubes[],int S[],int T[]){
if(n == 0)
return;
else{
printSample(n-cubes[(S[n])],cubes,S,T);
cout << " ";
cout << S[n] + 1;
}
}
// Waring algorithm
int w(int n, int cubes[], int S[],int T[])
{
int minValue = maxCubes;
int m;
if (n == 0)
return 0;
else {
for(m = 0; cubes[(m + 1)] <= n; m++){
}
T[n] = 1 + T[n - cubes[m]];
S[n] = m;
}
return T[n];
}