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(){
load();
}
public void load(){
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);
return total + calculate(count + 1);
}
}
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();
int answer = app.calculate();
System.out.println("The final answer is: " + answer);
}
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);
return total + calculate(count + 1);
if anyone could help me i would be thankful
-jrab