-1

I am trying to solve the following problem: https://projecteuler.net/problem=8

Basically I am going through the 2D array and getting the 13 digit product from each of the eight directions (N, NW, NE, E, W, S, SW, SE) and setting the return value to the largest one (if there is one).

But I am getting an answer of 64497254400, which is incorrect. Any hints as to what I could possibly be doing wrong?

Following is my straightforward, inefficient solution:

#include <stdio.h>
#include <iostream>
#include <fstream>

using namespace std;

long long int MAX(long long int a, long long int b)
{
    if (a > b)
    {
        return a;
    }
    else
    {
        return b;
    }
}

void largest_product_in_series(int param)
{
    char filename[] = "8_.txt";
    char my_character ;
    int column = 0; int row = 0;
    int c = 0; int r = 0; int cc = 0; int rr = 0;
    int list[100][100];
    int num = param - 1;

    ifstream fin;
    fin.open(filename, ios::in);

    while (!fin.eof() ) 
    {
        fin.get(my_character);
        if (my_character == '\n')
        {
            row++;
            r++;
            c = 0;
            cout << endl;
        }
        else
        {
            if (row == 0)
            {
                column++;
            }
            list[r][c] = (my_character - '0');
            cout << list[r][c];
            c++;
        }
    }

    cout << "Column: " << column << endl;
    cout << "Row: " << row << endl;

    long long int greatest_product = 0;

    long long int product = 0;

    for (rr = 0; rr < row; rr++)
    {
        for (cc = 0; cc < column; cc++)
        {
            //cout << "Column: " << cc << " Row: " << rr << endl;
            if (rr >= num)
            {
                r = rr;
                c = cc;
                product = list[r--][c];

                for (int i = 0; i < num; i++)
                {
                    product *= list[r--][c];
                }
                greatest_product = MAX(greatest_product, product);
            }
            if ((rr + num) <= row)
            {
                r = rr;
                c = cc;
                product = list[r++][c];

                for (int i = 0; i < num; i++)
                {
                    product *= list[r++][c];
                }
                greatest_product = MAX(greatest_product, product);
            }
            if (cc >= num)
            {
                r = rr;
                c = cc;
                product = list[r][c--];

                for (int i = 0; i < num; i++)
                {
                    product *= list[r][c--];
                }
                greatest_product = MAX(greatest_product, product);
            }
            if ((cc + num) <= column)
            {
                r = rr;
                c = cc;
                product = list[r][c++];

                for (int i = 0; i < num; i++)
                {
                    product *= list[r][c++];
                }
                greatest_product = MAX(greatest_product, product);
            }
            if ((rr >= num) && (cc >= num)) // NW
            {
                r = rr;
                c = cc;
                product = list[r--][c--];
                for (int i = 0; i < num; i++)
                {
                    product = list[r--][c--];
                }
                greatest_product = MAX(greatest_product, product);
            }
            if ((rr >= num) && ((cc + num) <= column)) // NE
            {
                r = rr;
                c = cc;
                product = list[r--][c++];
                for (int i = 0; i < num; i++)
                {
                    product = list[r--][c++];
                }
                greatest_product = MAX(greatest_product, product);
            }
            if (((rr + num) <= row) && ((cc >= num))) // SW
            {
                r = rr;
                c = cc;
                product = list[r++][c--];
                for (int i = 0; i < num; i++)
                {
                    product = list[r++][c--];
                }
                greatest_product = MAX(greatest_product, product);
            }
            if (((rr + num) <= row) && ((cc + num) <= column)) // SE
            {
                r = rr;
                c = cc;
                product = list[r++][c++];
                for (int i = 0; i < num; i++)
                {
                    product = list[r++][c++];
                }
                greatest_product = MAX(greatest_product, product);
            }
            //cout << "G: " << greatest_product << endl;
        }
    }
    cout << "Greatest Product: " << greatest_product << endl;
}

int main(void)
{
    largest_product_in_series(13);
    return 0;
}
  • 1
    Probably not your bug, but `while (!fin.eof() )` is worth fixing. More information here: [Why is iostream::eof inside a loop condition considered wrong?](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – user4581301 Apr 02 '17 at 01:15
  • 1
    What you're doing wrong is very obvious: you're not using your debugger in order to step through your code while it's executing, one line a time, and examine the values of all variables to determine how your program's actual logic diverges from your expected results. Knowing how to use a debugger is a required skill for every C++ developer. You should be able to figure this out by yourself. This is precisely what a debugger is for. – Sam Varshavchik Apr 02 '17 at 01:20

1 Answers1

0

When I run your code, it says the column is 50, and the row is 21. That seems a little odd, since it should be a 20x50 array, not a 21x50 array, that would be a 1050-digit number. Best guess is that you're accidentally storing some unprintable characters in a 21st row and the product involves some of those.

Also, some of the loops have product = ... instead of product *= ...

And lastly, it wouldn't influence the answer, but multiplication is commutative, so if you get the product of some numbers forward and backward, the result will be the same. So you can simplify your solution a little by getting rid of everything with N xor W in the direction.

Charles_F
  • 186
  • 1