## treyd

I have an assignment that I dont know where to begin. If someone could guide me along I would much appreciate it! Thanks alot.

Write a method
public static int[] avgRows(int[][] a) {}

This method takes a 2-dimensional array as input and returns an array containing the averages of each row of the 2-d array. For example, for input
{{1, 2, 3}, {3, 4, 5}}
{2, 4}
(beacuse (1+2+3)/3=2, (3+4+5)/3=4).

Write a main method to construct an example 2d array, pass it as an argument to your avgRows method, and print the resulting array.

This, unfortunately, is what I have so far:

``````public class Avgtwod {

public static int[][] avgRows(int[][] a) {

int[][] avgRows =  {{1, 2, 3}, {3, 4, 5}};

int total = 0;
for (int row = 0; row < avgRows.length; row++) {
total += avgRows;
}

return avgRows;

}
}``````

## treyd

This is where I am now:

``````public class Avgtwod {
public static int[][] avgRows(int[][] a) {

int[][] avgRows =  {{1, 2, 3}, {3, 4, 5}};

for (int row = 0; row < avgRows[0].length; row++) {
int total = 0;
for (int column = 0; column < avgRows.length; column++) {
total += avgRows[row][column];
System.out.println("sum for row" + row + "is" + total);
}
}

return avgRows;
}
public static void main (String[] args) {
}

}``````

Why wont it print anything?
Thanks

## VernonDozier 2,218

This is where I am now:

``````public class Avgtwod {
public static int[][] avgRows(int[][] a) {

int[][] avgRows =  {{1, 2, 3}, {3, 4, 5}};

for (int row = 0; row < avgRows[0].length; row++) {
int total = 0;
for (int column = 0; column < avgRows.length; column++) {
total += avgRows[row][column];
System.out.println("sum for row" + row + "is" + total);
}
}

return avgRows;
}
public static void main (String[] args) {
}

}``````

Why wont it print anything?
Thanks

The code is very hard to read the way you have indented it. Your program doesn't print anything because you have an empty main method.

## treyd

Sorry about that. I was trying to get everything in my head down on "paper". Can you give me any instruction on where to start?

## VernonDozier 2,218

Sorry about that. I was trying to get everything in my head down on "paper". Can you give me any instruction on where to start?

This thread is marked "Solved". If it is not solved, please mark it as unsolved. Regarding where to start, you should start by formatting the code so it is readable. NetBeans will do this for you in one click. The code cannot be worked with as it is now. You simply cannot get a feel for the program without consistent indentation.

Next step is to create a simple 2-D array in main. Next step is to pass that array to your function. Then compile everything and see if it compiles and runs. You should write something that displays the results. Compare them to see if they are correct. If so, try a few more different arrays.

But the absolute first step is to format the code so it is readable.

## treyd

Thanks alot man. I really appreciate it. I actually have NetBeans so I will do that now. Thanks for the advice!

## treyd

Does this look like I am going in the right direction? Sorry for all the questions; I am trying.
Thanks

``````public class Main {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {

int[][] a = {{1, 2, 3}, {3, 4, 5}};
avgRows(a);
}
public static int[] avgRows(int[][] a) {

}
}``````

## __avd 1,826

Write statements to calculate an average of elements of each row and assign them to a one dimensional int array inside the avgRows method. At the end of avgRows method use return arrayVar; statement to return an array containing averages.

## treyd

Update:

``````public class Main {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {

int[][] a = {{1, 2, 3}, {3, 4, 5}};
avgRows(a);
}
public static int[] avgRows(int[][] a) {

int total = 0;
for (int row = 0; row < a.length; row++) {
total += a[0][row];
System.out.println(row + total);

}

}
}``````

Will this add up the two rows? I am just at a loss here on what to do.

## VernonDozier 2,218

Update:

``````public class Main {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {

int[][] a = {{1, 2, 3}, {3, 4, 5}};
avgRows(a);
}
public static int[] avgRows(int[][] a) {

int total = 0;
for (int row = 0; row < a.length; row++) {
total += a[0][row];
System.out.println(row + total);

}

}
}``````

Will this add up the two rows? I am just at a loss here on what to do.

I think adatapost is recommending you write a helper function rather than do everything in one function. It's a good idea. Your formatting has improved, but it still has a ways to go before being readable IMO. See the formatted code below. It is much easier to follow.

``````public class Main
{
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
int[][] a = {{1, 2, 3}, {3, 4, 5}};
avgRows(a);  // this line is a problem.  You don't do anything with the return value.
// any display statements should be here.
}

public static int[] avgRows(int[][] a)
{
// declare a one dimensional array of int here.
// set up a loop
// call helper function below several times to fill in int[] array

return null;   // change this to return something useful
}

// helper function
public static int avgRow (int[] a)
{
return 0;  // change this to return something useful.
}
}``````

## treyd

What would be the correct way to pass the original 2d array to the avgRow method?

*deleted*