Project Euler problem 4 [SPOILERS AHEAD!!!]

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
using namespace 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)) return true;
    else return false;
}
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?
Last edited on
closed account (48T7M4Gy)
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...
Last edited on
closed account (48T7M4Gy)
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.
Ok, I'll rephrase my question then. How do I pass a whole array to a function?
closed account (E0p9LyTq)
DDomjosa wrote:
Ok, I'll rephrase my question then. How do I pass a whole array to a function?

1. Pass the array as a pointer, with a second parameter indicating the actual size of the array. The size is the number of elements in the array.

2. Or use a C++ STL container, such as a vector, and pass it as a pointer. The STL containers include the size as member data, easily retrieved.

Now how YOU implement either suggestion is up to you to solve. The intent of the PE challenge.
Ok, so this works:

1
2
3
4
5
6
7
8
9
10
11
12
#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?
closed account (E0p9LyTq)
void print(int, int); is not the same as void print(int[], int); or void print(int*, int);. That is why you received the error.

DDomjosa wrote:
Also I didn't use any pointers and it still worked?

You did use pointers.

Pointers and arrays, Pointer arithmetic
http://raven.iab.alaska.edu/~ntakebay/teaching/programming/gendrift/node4.html
hint : two pointers...(begin end)
Thank you, FurryGuy, for the help. That works perfectly!
closed account (E0p9LyTq)
@DDomjosa,

Your array printing program rewritten using STL vector:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <vector>

void print(std::vector<int>);

int main()
{
   std::vector<int> A = { 8, 9, 4, 52, 97 };

   print(A);

   return 0;
}

void print(std::vector<int> A)
{
   for (auto it = 0; it != A.size(); it++)
   {
      std::cout << A[it] << " ";
   }
   std::cout << std::endl;
}


or
1
2
3
4
5
6
7
8
void print(std::vector<int> A)
{
   for (auto it = std::begin(A); it != std::end(A); it++)
   {
      std::cout << *it << " ";
   }
   std::cout << std::endl;
}


or even
1
2
3
4
5
6
7
8
void print(std::vector<int> A)
{
   for (auto it: A)
   {
      std::cout << it << " ";
   }
   std::cout << std::endl;
}


The STL containers are much better than using "raw" C-style arrays.
Topic archived. No new replies allowed.