-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheuler11.cpp
More file actions
201 lines (137 loc) · 5.63 KB
/
Copy patheuler11.cpp
File metadata and controls
201 lines (137 loc) · 5.63 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
//What is the greatest product of four adjacent numbers in the same direction (up, down, left, right, or diagonally) in the grid?
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
using namespace std;
const int grid_dim = 20;
int checkMax (int arr[grid_dim][grid_dim]) ;
int main () {
int eulerGrid [grid_dim][grid_dim]; // initialize 2d array
//how to open a file
ifstream inputFile("array.txt");
//error handling for file opening
if (!inputFile) {
cerr << "Error opening array file." << endl;
return 1; //exit with error code 1
}
//read data from file into the array
for (int i = 0; i < grid_dim; i++) {
for (int j = 0; j < grid_dim; j++) {
string value; //read value as a string
if (inputFile >> value) {
eulerGrid [i] [j] = stoi(value); //stoi = String To Integer
}
else {
cerr << "error reading data in array file" <<endl;
inputFile.close();
return 2; // exit with error code 2
}
}
}
//close file
inputFile.close();
checkMax(eulerGrid);
return 0; // exit without error
}
int checkMax (int gCheck[grid_dim][grid_dim] ) {
//even tho it doesnt matter what order you multiply numbers in,
// I am going to do all the directions to make sure I can access
// 2d array data predictably
int maximumValue = 0;
//print array to make sure its passed correctly
/*
for (int i = 0; i < grid_dim; ++i) {
for (int j = 0; j < grid_dim; ++j) {
std::cout << gCheck[i][j] << ' ';
}
std::cout << std::endl; // Move to the next line for the next row
}
*/
//check forward adjacent values
int forBuffer = 0;
int forMax = 0;
for (int i = 0; i < grid_dim; ++i) {
for (int j = 0; j < grid_dim - 3; ++j) {
forBuffer = ( gCheck[i][j] * gCheck[i][j+1] * gCheck[i][j+2] * gCheck[i][j+3] );
if (forBuffer > forMax) {
forMax = forBuffer;
// for troubleshooting cout << gCheck[i][j] << " * " << gCheck[i][j+1] << " * " << gCheck[i][j+2] << " * " << gCheck[i][j+3] << " = " << forBuffer << endl;
}
}
}
cout << "Largest Forward Value : " <<forMax << endl;
cout << endl;
//check backward adjacent values
int backBuffer = 0;
int backMax = 0;
for (int i = 0; i < (grid_dim); ++i) {
for (int j = 0; j < grid_dim - 3 ; j++) {
backBuffer = ( gCheck[i][j + 3] * gCheck[i][j+ 2] * gCheck[i][j + 1] * gCheck[i][j] );
if (backBuffer > backMax) {
backMax = backBuffer;
// for troubleshooting cout << gCheck[i][j+3] << " * " << gCheck[i][j+2] << " * " << gCheck[i][j+1] << " * " << gCheck[i][j] << " * " << " = " << backBuffer << endl;
}
}
}
cout << "Largest Backward Value : " <<backMax << endl;
cout << endl;
//check upward values
int upBuffer = 0;
int upMax = 0;
for (int i = 3; i < grid_dim; ++i) {
for (int j = 0; j < grid_dim; ++j) {
upBuffer = ( gCheck[i][j] * gCheck[i-1][j] * gCheck[i-2][j] * gCheck[i-3][j] );
if (upBuffer > upMax) {
upMax = upBuffer;
// for troubleshooting cout << gCheck[i][j] << " * " << gCheck[i-1][j] << " * " << gCheck[i-2][j] << " * " << gCheck[i-3][j] << " = " << upMax << endl;
}
}
}
cout << "Largest upward Value : " <<upMax << endl;
cout << endl;
//check downward values
int downBuffer = 0;
int downMax = 0;
for (int i = 0; i < grid_dim - 3; ++i) {
for (int j = 0; j < grid_dim; ++j) {
downBuffer = ( gCheck[i][j] * gCheck[i+1][j] * gCheck[i+2][j] * gCheck[i+3][j] );
if (downBuffer > downMax) {
downMax = downBuffer;
// for troubleshooting cout << gCheck[i][j] << " * " << gCheck[i+1][j] << " * " << gCheck[i+2][j] << " * " << gCheck[i+3][j] << " = " << downMax << endl;
}
}
}
cout << "Largest downward Value : " <<downMax << endl;
cout << endl;
// calculate forward diagonal values
int diagforBuffer = 0;
int diagforMax = 0;
for (int i = 0; i < grid_dim - 3; ++i) {
for (int j = 0; j < grid_dim -3; ++j) {
diagforBuffer = ( gCheck[i][j] * gCheck[i+1][j+1] * gCheck[i+2][j+2] * gCheck[i+3][j+3] );
if (diagforBuffer > diagforMax) {
diagforMax = diagforBuffer;
// for troubleshooting cout << gCheck[i][j] << " * " << gCheck[i+1][j+1] << " * " << gCheck[i+2][j+2] << " * " << gCheck[i+3][j+3] << " = " << diagforMax << endl;
}
}
}
cout << "Largest Forward Diagonal Value : " <<diagforMax << endl;
cout << endl;
//calculate backwards diagonal value
int diagbackBuffer = 0;
int diagbackMax = 0;
for (int i = 0; i < grid_dim - 3; ++i) {
for (int j = 3; j < grid_dim ; ++j) {
diagbackBuffer = ( gCheck[i][j] * gCheck[i+1][j-1] * gCheck[i+2][j-2] * gCheck[i+3][j-3] );
if (diagbackBuffer > diagbackMax) {
diagbackMax = diagbackBuffer;
// for troubleshooting cout << gCheck[i][j] << " * " << gCheck[i+1][j-1] << " * " << gCheck[i+2][j-2] << " * " << gCheck[i+3][j-3] << " = " << diagbackMax << endl;
}
}
}
cout << "Largest backward Diagonal Value : " <<diagbackMax << endl;
cout << endl;
maximumValue = max ( max (upMax, forMax), max(diagforMax, diagbackMax));
cout << "Largest Of All Values : " << maximumValue << endl;
}