Hey guys, I had to determine the output of given code that uses a 2D array. The 2D array given looks like this:
data.txt:
5 8 4 3 9 5
6 4 9 5 3 2
2 2 0 9 7 3
7 4 5 6 9 5
8 8 3 2 6 4
9 5 6 3 7 6
The code given had me thinking that it was trick code and simply gave an error output, or that if one of the numbers is less than 6, it will take the number and % by 2. So, if numbers were bigger than 6, nothing happens to them. But, there's a point where I questioned the % 2 and thought that it was simply going to increment everything less than 6. Anyways heres the code let me know if I'm close to being correct >_<
import java.util.Scanner;
public class TwoDArray {
final private static int NUM = 6;
public TwoDArray(){
}
public void load(int[][] grid){
int row, col;
String fileName = "data.txt";
try{
Scanner inFile = new Scanner(new File(filename));
for (row = 0; row < NUM; row++){
for (col = 0; col < NUM; col++){
grid[row][col] = inFile.readInt();
}
}
}catch(IOException e){
System.out.println(“Error: “ + e.getMessage());
}
}
public void display(int[][] grid){
int row, col;
for (row = 0; row < NUM; row++){
for (col =0; col < NUM; col++){
System.out.print(grid[row][col] + " ");
}
System.out.println();
}
System.out.println();
}
public void fun(int[][] grid){
int row, col;
for (row = 0; row < NUM; row++){
for (col = 0; col < NUM; col++)
if ((grid[row][col] % 2) == 0){
grid[row][col] = 0;
}
}
}
}
}
public class driver{
public static void main(String[] args){
TwoDArray app = new TwoDArray();
int [][] matrix = new int[NUM][NUM];
app.load(matrix);
app.display(matrix);
app.fun(matrix);
app.display(matrix);
}
}
Thanks for any of your help guys. I'm new to this particular forum, but I think that the introduction threads are redundant.
-Asmodaii
"There is no spoon" -The Matrix