## TheJackRabbit

hey guys, im new to the forum, but i read the rules already so i wanted to start getting help on this homework problem i have for the weekend. i have to determine output of a recursive array code as follows:

``````import java.io.File;
import java.util.Scanner;
import java.io.IOException;

class RecArray{

private final static int NUM = 6;
private int [][] myGrid = new int[NUM][NUM];

public RecArray(){
}

int row, col;
String fileName = "data2.txt";
try{
Scanner inFile = new Scanner(new File(fileName));
for (row = 0; row < NUM; row++)
for (col = 0; col < NUM; col++)
myGrid[row][col] = inFile.nextInt();
}catch(IOException i){
System.out.println("Error: " + i.getMessage());
}
}

public int calculate(){
return calculate(0);
}

public int calculate(int count){
if (count >= myGrid.length){
return 0;
}else{
int total = 0;
for (int i = 0; i <= count; i++){
total += myGrid[count][i];
total += myGrid[i][count];

}
total -= myGrid[count][count];
System.out.println("count is: " + count + " and this total is: "
+ total);
}
}

public void display(){
int row, col;
for (row = 0; row < NUM; row++){
for (col =0; col < NUM; col++){
System.out.print(myGrid[row][col] + " ");
}
System.out.println();
}
System.out.println();
}

}

public static void main(String[] args) {
RecArray app = new RecArray();
}``````

which uses the array -

5 6 -4 10 -6 7
2 3 6 3 -8 2
4 -9 8 4 9 -3
7 9 2 -3 5 -7
5 -9 -4 7 6 -5
-1 3 8 -4 0 8

anyways, i looked at it and thought that what it does is see if the number is larger than its grid.length, or spot in the grid, the calculate method will return a value of zero. otherwise, the number would be incremented. this all occurs right here:

``````if (count >= myGrid.length){
return 0;
}else{
int total = 0;
for (int i = 0; i <= count; i++){
total += myGrid[count][i];
total += myGrid[i][count];

}
total -= myGrid[count][count];
System.out.println("count is: " + count + " and this total is: "
+ total);

if anyone could help me i would be thankful

-jrab

## verruckt24 438

Figure out what `myGrid.length` is then figure out how arrays work so that you get what `total += myGrid[count][i]` and `total += myGrid[i][count]` does. Take a paper and a pencil and start substituting the values you won't be far from concluding what values you get. Once you start getting values you could deduce the logic (which is what the program does) by making a pattern out of the values. Make use of Java docs and your array knowledge which I am sure you might have been given before giving this assignment, yet if you require help with Java Arrays you can use the Array tutorial from java which can be found here.

## TheJackRabbit

yeah i'm pretty sure it has something to deal with this

``````private final static int NUM = 6;
private int [][] myGrid = new int[NUM][NUM];``````

but i wasn't sure what the i is in the bottom of my first post. i really have little to no knowledge about arrays in java alone, so its a bit difficult to know what

total += myGrid[count]

and

total += myGrid[count]

do. ill take a look at the tutorial you posted and i thank you for your assistance. anymore is appreciated of course.

## verruckt24 438

the i is the index position in the array. So if i is 2 it is the third element in the array (since arrays start from the index 0). In a double dimensional array this is essentially the same meaning, say for example you have a 2 arrays where each array is itself an array of 4 elements, they together make up 8 elements.

View this as a matrix of rows and columns
Columns -> 1 2 3 4
Row 1: 5 8 6 1
Row 2: 9 2 3 7

Imagine that row 1 is the first array and row 2 is the second array (each of four elements)
Now which element does arr[0][2] point to ?
Remember again that arrays have the base index as 0, so [0] means the 1st element and [2] means the 3rd element. So this points to the 3rd element of the 1st array, so in effect it means that it points to the 3rd column in the 1st row which is 6.

I hope you get that. You can practice some more using this.

Also it is important to note that even though I mention array elements laid out in rows and columns here, they are actually not placed in the memory that way. We "visualize" the array this way since it is easy to understand them this way. Arrays are laid out in consecutive memory locations.

Reading the array tutorials further would help much more.