# Array of Arrays?

David Hunt

Greenhorn

Posts: 17

posted 12 years ago

- 0

I am working on a question from a Java tutorial website(as I am trying to learn Java myself) could someone give me help on the following:

Whats Required:

Sequential program that receives as input a single Y x Z dimensional matrix (A) with a large amount of unsorted integer numbers(Created randomly).

The program then has to produce an N-dimensional array B containing the sum of the numbers in each A column(for example Bi = Ai1 + Ai2 + etc + AiN

(i = 1 .....M)

so the output I think would be as follows from what I understand from the question:

If the user entered 5,5:

3 2 2 4 8

1 4 2 3 7

2 4 7 2 6

2 5 1 7 9

1 2 8 3 4

9 17 20 19 34 << Columns added together

The only code I have came up with so far is(which as you can probably see is just the skeleton of the code):

import java.io.*;

public class Matrix

{

public static void main(String[ ] args)

{

Matrix theApp = new Matrix();

theApp.run();

}

private void run()

{

// Code to generate a number of random numbers (depending on user input)

// Adding the columns together and printing to screen grid

}

Could someone help me implement the missing code please.

Thanks for any help.

Whats Required:

Sequential program that receives as input a single Y x Z dimensional matrix (A) with a large amount of unsorted integer numbers(Created randomly).

The program then has to produce an N-dimensional array B containing the sum of the numbers in each A column(for example Bi = Ai1 + Ai2 + etc + AiN

(i = 1 .....M)

so the output I think would be as follows from what I understand from the question:

If the user entered 5,5:

3 2 2 4 8

1 4 2 3 7

2 4 7 2 6

2 5 1 7 9

1 2 8 3 4

9 17 20 19 34 << Columns added together

The only code I have came up with so far is(which as you can probably see is just the skeleton of the code):

import java.io.*;

public class Matrix

{

public static void main(String[ ] args)

{

Matrix theApp = new Matrix();

theApp.run();

}

private void run()

{

// Code to generate a number of random numbers (depending on user input)

// Adding the columns together and printing to screen grid

}

Could someone help me implement the missing code please.

Thanks for any help.

Stan James

(instanceof Sidekick)

Ranch Hand

Ranch Hand

Posts: 8791

posted 12 years ago

- 0

I hope this answer is at the right level - I apologize if you're really well beyond this ...

I'm glad you said "array of arrays" as that's what Java really does. It's often close enough to say "multi-dimensional arrays" but Java does not require these things to be "rectangular". That is, it ain't necessarily so that every row is the same length. But let's keep things simple today and say they are.

Are you familiar with the common code for looping through an array? Let's say we wanted to sum up the numbers in a simple array.

The for loop declares an index called "i" initialized to zero. It says as long as i is less than the length of the array do the stuff in the braces then add one to i. "Less than the length" often trips up new folks. For our 5 slots, the index "i" is going to have values 0..4.

Now your example showed a 5x5 matrix. So the code is a little trickier:

The first "for" is familiar. The second one creates another index and compares it to b[i].length. This tells us that b[i] is an array, just as you said at the start with "array of arrays".

Finally, we have to get some interesting numbers. The load loop will look a lot the same:

Rows and columns are just in our heads. I iterated column then row which may seem very strange. You can play with that, too. Just decide which "dimension" is rows and which is columns. Let's change it around now and say the array is b[rownum][colnum] and you want users to enter a row at a time.

One potential gotcha in that last loop is that we assume all rows have the same number of columns. In your simple example, this will be true. In more complex examples, with a non-rectangular array of arrays, it might not be.

Hope that gives you something to play with. Get something going and let us know if you have more questions.

I'm glad you said "array of arrays" as that's what Java really does. It's often close enough to say "multi-dimensional arrays" but Java does not require these things to be "rectangular". That is, it ain't necessarily so that every row is the same length. But let's keep things simple today and say they are.

Are you familiar with the common code for looping through an array? Let's say we wanted to sum up the numbers in a simple array.

The for loop declares an index called "i" initialized to zero. It says as long as i is less than the length of the array do the stuff in the braces then add one to i. "Less than the length" often trips up new folks. For our 5 slots, the index "i" is going to have values 0..4.

Now your example showed a 5x5 matrix. So the code is a little trickier:

The first "for" is familiar. The second one creates another index and compares it to b[i].length. This tells us that b[i] is an array, just as you said at the start with "array of arrays".

Finally, we have to get some interesting numbers. The load loop will look a lot the same:

Rows and columns are just in our heads. I iterated column then row which may seem very strange. You can play with that, too. Just decide which "dimension" is rows and which is columns. Let's change it around now and say the array is b[rownum][colnum] and you want users to enter a row at a time.

One potential gotcha in that last loop is that we assume all rows have the same number of columns. In your simple example, this will be true. In more complex examples, with a non-rectangular array of arrays, it might not be.

Hope that gives you something to play with. Get something going and let us know if you have more questions.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi

David Hunt

Greenhorn

Posts: 17

David Hunt

Greenhorn

Posts: 17

posted 12 years ago

- 0

Ok I have done the following code, it asks the user for an integer then when it gets to 5(no of columns) it prints the a message with the total for that column(eg " Sum for column 4 = 23"). I would like to change it so it outputs the results at the end only and not after each column is entered. So could someone show me how this would be done as I cannot work out how to convert it. ie so after the user has entered each number when prompted it prints to screen a grid like below with the sum of each column underneath:

3 2 2 4 8

1 4 2 3 7

2 4 7 2 6

2 5 1 7 9

1 2 8 3 4

9 17 20 19 34

---------------------------------------------------------------------------

import java.io.*;

public class Matrix

{

public static void main(String[ ] args)

{

Matrix theApp = new Matrix();

theApp.run();

}

private void run()

{

int[][] b = new int[5][5];

for(int i = 0; i < b.length; i++ )

{

int sum = 0;

for (int j = 0; j < b[i].length; j++ )

{

sum += b[i][j] = getInt();

}

System.out.println("Sum for column " + i + " = " + sum );

}

}

private int getInt()

{

int num;

String inStr = null;

try

{

BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

System.out.print("Enter integer data: ");

inStr = in.readLine();

}

catch(IOException ioe)

{

System.out.println(ioe);

}

num = Integer.parseInt(inStr);

return num;

}

}

---------------------------------------------------------------------------

3 2 2 4 8

1 4 2 3 7

2 4 7 2 6

2 5 1 7 9

1 2 8 3 4

9 17 20 19 34

---------------------------------------------------------------------------

import java.io.*;

public class Matrix

{

public static void main(String[ ] args)

{

Matrix theApp = new Matrix();

theApp.run();

}

private void run()

{

int[][] b = new int[5][5];

for(int i = 0; i < b.length; i++ )

{

int sum = 0;

for (int j = 0; j < b[i].length; j++ )

{

sum += b[i][j] = getInt();

}

System.out.println("Sum for column " + i + " = " + sum );

}

}

private int getInt()

{

int num;

String inStr = null;

try

{

BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

System.out.print("Enter integer data: ");

inStr = in.readLine();

}

catch(IOException ioe)

{

System.out.println(ioe);

}

num = Integer.parseInt(inStr);

return num;

}

}

---------------------------------------------------------------------------

I agree. Here's the link: http://aspose.com/file-tools |