I am working on project Euler problem #4 which states:
A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 99.
Find the largest palindrome made from the product of two 3-digit numbers.
The problem in my program is it only goes through the for loop only one time. I do not understand why its doing it though. Any help is appreciated.
variable length arrays are not part of the C++ standard.
1 2 3 4 5 6
int i = numD;
int t[i];
int mul = 10;
while(i > 0)
{
t[i]=temp%10; //out of bounds
Your reverse function is too complicated, consider instead
1 2 3 4 5 6 7 8 9 10 11
int reverse(int num){
int result = 0;
//`stack' flip
while(num){ //num.is_empty()
result *= 10;
result += num%10; //result.push( num.back() );
num /= 10; //num.pop()
}
return result;
}
Chervil
Thanks That was the problem all along. Works perfect after it.
@ne555
I know my code is really messy I am going to clean it up now.
Also I love your Reverse function! you just did what I was doing in 11 lines of code! Also what did you mean by out of bounds?
Thanks for the help both of you!
Seems like something is still wrong. Because my code found 924 * 962 = 888888 as the largest palindrome number but thats not the right answer -.-
Any suggestions ?
The codes doing what its supposed to be
If you say T array[N]; //N is a constant you've got an array with `N' elements.
You can use an index from 0 to N-1.
However in your code you were trying to access array[N], that's outside the valid range (out-of-bounds)