Java Programming: Basic 2D Array


/*
Name: Kevin Whetstone
Date: 11/15/13
Program: Basic 2D Array
Description: For Loop, and Array
**/

//professor’s code
import java.util.Scanner;
import java.util.Random;

public class GridPractice
{

public static void main(String[] args)
{
//declarations
Scanner in = new Scanner(System.in);
Random  generator = new Random();
int [][] grid;    //un-instantiated grid
int size = 0;    //number of rows and columns

//get size of grid – no validation & instantiate
System.out.print(“Enter size of grid: “);
size = in.nextInt();
grid = new int[size][size];
//end of professor’s code

//beginning of my own code:

//fill grid with random number from 1..99
System.out.println();
for (int row=0; row<size; row++)
{
for (int col=0; col<size; col++)
{
grid[row][col] = generator.nextInt(100); //random numbers 0.99 – not 100
}
}

//print grid with formatting to align
System.out.println();
System.out.print(“Random values assigned to ” + size + ” by ” + size + ” grid.\n”);
for(int row = 0; row < size; row++)
{
for(int col = 0; col < size; col++)
{
System.out.printf(“%4d”, + grid[row][col]);
}
System.out.println();
}

//print each row sum
System.out.println();

int rowSum = 0;

System.out.print(“Row    Sum\n”);
System.out.print(“———-\n”);
for(int row = 0; row < size; row++)
{
for(int col = 0; col < size; col++)
{
rowSum += grid[row][col];
}

System.out.printf(“%3d”, + row);
System.out.printf(“%6d”, + rowSum);
System.out.println();
rowSum = 0;
}

//print each column sum
System.out.println();

int colSum = 0;

System.out.print(“Col    Sum\n”);
System.out.print(“———-\n”);
for(int col = 0; col < size; col++)
{
for(int row = 0; row < size; row++)
{
colSum += grid[row][col];
}

System.out.printf(“%3d”, + col);
System.out.printf(“%6d”, + colSum);
System.out.println();
colSum = 0;
}

//print two diagonal sums
int upperLeft2LowerRight = 0;
int upperRight2LowerLeft = 0;

for(int col = 0; col < grid[0].length; col++)
{
for(int row = 0; row < grid[0].length; row++)
{
if(row == col)
{
upperLeft2LowerRight += grid[row][col];
}
}
}

for(int row = 0; row < grid[0].length; row++)
{
for(int col = 0; col < grid[0].length; col++)
{
if(!(row == col))
{
upperRight2LowerLeft += grid[row][col];
}
}

}
System.out.print(“Diagonal from upper left to lower right is ” + upperLeft2LowerRight);
System.out.print(“\nDiagonal from upper right to lower left is ” + upperRight2LowerLeft);
}
}

You can test with any number from 1 to 99, and I will start with 2

Here is the output

2Darray

Advertisements

Posted on November 15, 2013, in Electronics, Programming and tagged , , , . Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: