# Help with Arrays!!

• 03-12-2012, 05:15 PM
gable.able
Help with Arrays!!
How do you write a program that randomly inputs 20 numbers from 1 to 10 into a 2-D array of 5 rows and 4 columns. I want it to output the 2-D array, the sum of each row storing numbers in a parallel array and sum up each column storing number in a parallel array.

Im having trouble with parallel arrays!
• 03-12-2012, 05:20 PM
KevinWorkman
Re: Help with Arrays!!
Which part of this are you having trouble with? Break your problem up into separate pieces, and then focus on each individual problem by itself. Create an SSCCE that demonstrates one problem at a time, ask a specific technical question, and we'll go from there.
• 03-12-2012, 05:41 PM
gable.able
Re: Help with Arrays!!
I actually need to add a method into this.... i need this exactly how it is but with the use of a method

import java.io.*;
import java.util.Random;

public class arrays
{

public static void main (String[] args)
{
System.out.println("Project 3");

int array[][] = new int[5][4];
Random randomizer = new Random();
for (int i = 0; i < array.length; i++){
for (int j = 0; j < array[i].length; j++){
array[i][j] = randomizer.nextInt(11);
}
}
System.out.print("2D Array elements: ");
for (int i = 0; i < array.length; i++){
for (int j = 0; j < array[i].length; j++){
System.out.print(" " +array[i][j]+ ","); //Output 2D Array
}
}

int[] sumOfRows = new int[array.length];
int[] sumOfColumns = new int[array[0].length];

int sumR = 0;
int sumC = 0;
for(int row = 0; row < array.length; row++){
for(int col = 0; col < array[0].length; col++){ //Sum of rows
sumR += array[row][col];
}
sumOfRows[row] = sumR;
}
System.out.println(" ");
System.out.println("Sum of Rows: " + sumR);

for (int col = 0; col < array.length; col++){
for (int row = 0; row < array[row].length; row++){
sumC += array[col][row];
}
sumOfColumns[col] = sumC;
}
System.out.println("Sum of Columns:" + sumC);

}
}
• 03-12-2012, 05:42 PM
KevinWorkman
Re: Help with Arrays!!
When posting code, please use the code tags. Unformatted code is very hard to read.

I still don't understand what your actual question is.
• 03-12-2012, 05:51 PM
gable.able
Re: Help with Arrays!!
import java.io.*;
import java.util.Random;

Code:

``` public class arrays {   public static void main (String[] args) {     System.out.println("Project 3");     int array[][] = new int[5][4];     Random randomizer = new Random();     for (int i = 0; i < array.length; i++){         for (int j = 0; j < array[i].length; j++){             array[i][j] = randomizer.nextInt(11);         }     }     System.out.print("2D Array elements: ");     for (int i = 0; i < array.length; i++){         for (int j = 0; j < array[i].length; j++){             System.out.print(" " +array[i][j]+ ",");    //Output 2D Array         }     }     int[] sumOfRows = new int[array.length];     int[] sumOfColumns = new int[array[0].length];     int sumR = 0;     int sumC = 0;     for(int row = 0; row < array.length; row++){         for(int col = 0; col < array[0].length; col++){    //Sum of rows         sumR += array[row][col];         }     sumOfRows[row] = sumR;     }     System.out.println(" ");     System.out.println("Sum of Rows: " + sumR); for (int col = 0; col < array.length; col++){         for (int row = 0; row < array[row].length; row++){             sumC += array[col][row];             }     sumOfColumns[col] = sumC;     }     System.out.println("Sum of Columns:" + sumC);  } }```
Sorry, idk if that worked?
• 03-12-2012, 06:02 PM
KevinWorkman
Re: Help with Arrays!!
Okay, and which one of your problems are you having trouble with?
• 03-12-2012, 06:07 PM
gable.able
Re: Help with Arrays!!
Just incorporating a method into it. Like what method should I use to have the same outcome of the code when I run it?
• 03-12-2012, 06:11 PM
KevinWorkman
Re: Help with Arrays!!