I've actually figure out how I'm going to do the diagonals now.
here's the code:
package stuff;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
import algorithms.StopWatch;
public class tester
{
public static void main(String args[]) throws FileNotFoundException {
StopWatch timer = new StopWatch();
timer.start();
ArrayList<Integer> prods = new ArrayList<Integer>();
int[][] nums = new int[20][20];
Scanner in = new Scanner(new File("prob11.txt"));
for(int i = 0; i < 20; i++)
{
for(int j = 0; j < 20; j++)
{
nums[i][j] = in.nextInt();
}
}
//horizontal products
for(int i = 0; i<20;i++)
{
for(int j = 0; j < 17; j++)
{
int prod = 1;
for(int l = j; l < j + 4; l++ )
{
prod*=nums[i][l];
}
prods.add(prod);
}
}
//vertical products
for(int i = 0; i<20;i++)
{
for(int j = 0; j < 17; j++)
{
int prod = 1;
for(int l = j; l < j + 4; l++ )
{
prod*=nums[l][i];
}
prods.add(prod);
}
}
//diagonal down-left products
for(int i = 0; i < 17; i++)
{
for(int j = 3; j < 20; j++)
{
int prod = nums[i][j]*nums[i+1][j-1]*nums[i+2][j-2]*nums[i+3][j-3];
prods.add(prod);
}
}
//diagonal down-right
for(int i = 0; i < 17; i++)
{
for(int j = 0; j < 17; j++)
{
int prod = nums[i][j]*nums[i+1][j+1]*nums[i+2][j+2]*nums[i+3][j+3];
prods.add(prod);
}
}
//gets the largest product in the arraylist
int largest = 0;
for(int i : prods)
{
if(i>largest)
{
largest=i;
}
}
System.out.println(largest);
timer.stop();
System.out.println("\n" + timer.getElapsedTime());
}
}
Don't worry about the stopwatch stuff, it only times how long it takes, about 50ms.