### euler problem 11

why am i getting the wrong answer?i'm pretty sure everything is right

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107`` `````` #include #include #include #include using namespace std; class square{ private: int gridS[20][20]; int product; int max; public: square(); void horizantal (); void vertical(); void diagonal_R(); void diagonal_L(); void setMax(); void output(); }; int main(){ square a; square b[20][20]; string line; ifstream infile; infile.open("grid.txt"); if (infile.good()){ for (int i=0;i<20;i++){ getline (infile,line); } } a.setMax(); a.horizantal(); a.vertical(); a.diagonal_L(); a.diagonal_R(); a.output(); system("Pause"); return 0; } square::square(){ product=1; } void square::setMax(){ max=product; } void square:: horizantal (){ for (int i=0; i<20; ++i){ for (int j=0;j<17; ++j){ product = gridS [i][j]*gridS[i][j+1]*gridS [i][j+2]*gridS[i][j+3]; if(product>max){ max=product; } } } } void square:: vertical(){ for (int i=0; i<20; ++i){ for (int j=0;j<17; ++j){ product = gridS [i][j]*gridS[i][j+1]*gridS [i][j+2]*gridS[i][j+3]; if(product>max){ max=product; } } } } void square::diagonal_R(){ for (int i=0; i<17; ++i){ for (int j=0;j<17; ++j){ product = gridS [i][j]*gridS[i+1][j+1]*gridS [i+2][j+2]*gridS[i+3][j+3]; if(product>max){ max=product; } } } } void square::diagonal_L(){ for (int i=3; i<20; ++i){ for (int j=0;j<17; ++j){ product = gridS [i][j]*gridS[i-1][j+1]*gridS [i-2][j+2]*gridS[i-3][j+3]; if(product>max){ max=product; } } } } void square::output(){ cout<
 why am i getting the wrong answer?i'm pretty sure everything is right

You should begin by getting rid of the notion that you're "pretty sure everything is right" and look for what isn't.

If a square object represents a 20 by 20 grid, why do you need a 20x20 two dimensional array of squares? Where do you even use b in main? Where does the stuff you read from the file in main actually make it into a grid?

[Edit: Your square type has absolutely no way to set the internal array's data to anything (including the constructor.) Doesn't that throw up a red flag for you? Why didn't you test this on a smaller grid where you know the answer?]
Last edited on
so how can i push the matrix to the array and find the four numbers?
 so how can i push the matrix to the array and find the four numbers?

You can begin by addressing the issues I pointed out in your code.

The Euler site is one where you are intended to do the work. You won't get much out of it if you are unable to test/debug your own code and are unwilling to do any research to solve the problem, and you will certainly not get much out of it if you have to go around asking for solutions.
Topic archived. No new replies allowed.