Hi, so I'm writing a program to find the largest palindrome made from the product of two 3-digit numbers. Everything works perfectly, it even writes the palindromes to an array (there are 2470 of them in total), but when it comes to the function largestpalindrome() to find the largest of them it just doesn't work. Here is my code:
#include <iostream>
usingnamespace std;
bool ispalindrome(int);
int palindrome(int), palindromes[2470], largestpalindrome(int, int);
int main()
{
int counter = 0;
for (int i=999;i>=100;i--)
{
for (int j=999;j>=100;j--)
{
int x = i*j;
if (ispalindrome(x) == true)
{
palindromes[counter] = x;
counter++;
}
}
}
cout << largestpalindrome(palindromes[2470], counter) << endl;
}
bool ispalindrome(int x)
{
if (x == palindrome(x)) returntrue;
elsereturnfalse;
}
int palindrome(int x)
{
int x2 = 0;
while (x != 0)
{
int base = 10;
int y = x % base;
x2 = x2 * base + y;
x /= 10;
}
return x2;
}
int largestpalindrome(int A[], int x)
{
int y = A[0];
for (int i=1;i<x;i++)
if (A[i] > y) y = A[i];
return y;
}
It doesn't work and I have no idea why, says something about undefined references, if I move the largestpalindrome() function above main() it spews out a few more errors about pointers. What am I doing wrong?
PE is basically an individual challenge and you shouldn't be posting part or total solutions because it is against the spirit of PE and spoils it for other people. PE has a help forum. Use that.
This is problem #4 (pretty much everyone who has started Project Euler has done it) and I'm just asking for someone to tell me where I went wrong. Do I have to rewrite the whole program just so I can show it here and ask for help?
Also, the title is literally "Project Euler problem 4", so it's pretty much obvious it will contain spoilers...
It's up to you how you handle it. There are many ways you could ask for help without spoiling it for others and making excuses. You didn't have to mention PE and it being #4 has zip to do with it.
Writing
It doesn't work and I have no idea why
along with not showing the error messages is just the same ol' same ol' - Ive got a problem, you fix it. Well in this case you fix it, that's the nature of the PE challenge.
#include <iostream>
void print(int A[], int x)
{
for (int i=0;i<x;i++)
std::cout << A[i] << " ";
}
int main()
{
int A[] = {8,9,4,52,97};
print(A, 5);
return 2;
}
But this produces an error:
1 2 3 4 5 6 7 8 9 10 11 12 13
#include <iostream>
void print(int,int);
int main()
{
int A[] = {8,9,4,52,97};
print(A, 5);
return 2;
}
void print(int A[], int x)
{
for (int i=0;i<x;i++)
std::cout << A[i] << " ";
}
1 2
line 6 | error: invalid conversion from 'int*' to 'int' [-fpermissive]
line 2 | error: initializing argument 1 of 'void print(int, int)' [-fpermissive]
So can I not prototype a function that does something with arrays? Also I didn't use any pointers and it still worked?