euler problem 11

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

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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
  #include <iostream>
#include <vector>
#include <string>
#include <fstream>


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<<max;    
     
}
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.