Skip to content

Instantly share code, notes, and snippets.

@johnsogg
Last active December 15, 2015 12:59
Show Gist options
  • Save johnsogg/5263891 to your computer and use it in GitHub Desktop.
Save johnsogg/5263891 to your computer and use it in GitHub Desktop.
// find the largest product of 4 of the numbers in any direction (up,
// down, left, right, diagonal) in my output I get the file location
// "0xbfca024c" repeated
#include <iostream>
#include <fstream>
using namespace std;
int main ()
{
int maxValue;
int value = -9876;
int row, col;
ifstream file;
file.open("greatest_product.txt");
file >> row >> col;
int array20 [row][col];
cout << "File contents (by way of array):" << endl;
for (int i= 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
file >> array20[i][j];
cout << array20[i][j] << " ";
}
}
cout << endl;
cout << "Value: " << value << endl;
cout << "Address of array: " << endl;
cout << array20 << endl;
cout << "Values in array: " << endl;
// here we have to iterate through the cells in the array. Unlike
// Python, we can't just say 'print out this array'. C and C++
// aren't smart, at all.
for (int i=0; i < row; i++) {
for (int j=0; j < col; j++) {
cout << "array cell " << i << ", " << j << ": " << array20[i][j] << endl;
}
}
return 0;
}
3 3
1 2 3
4 5 6
7 8 9
File contents (by way of array):
1 2 3 4 5 6 7 8 9
Value: -9876
Address of array:
0x7fff566416c0
Values in array:
array cell 0, 0: 1
array cell 0, 1: 2
array cell 0, 2: 3
array cell 1, 0: 4
array cell 1, 1: 5
array cell 1, 2: 6
array cell 2, 0: 7
array cell 2, 1: 8
array cell 2, 2: 9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment