Reputation: 85
Writing a program where I randomly insert 0's and 1's into a 4 x 4 array matrix and then need to return the column and row index where 1's occur the most. My first method which checks the rows works fine, but I am having issues in my second check column method.
The error is saying that it cannot find the symbol for variable "col".
public class AssignmentEight_MultiArray {
public static void main(String[] args) {
System.out.println("Beginning to insert random integers into array..");
// Declare array matrix
int[][] matrix = new int[4][4];
// For loop to insert integers into the array
for (int row = 0; row < matrix.length; row++) {
for (int col = 0; col < matrix[row].length; col++) {
int ranNum = (Math.random() < 0.5) ? 0:1;
matrix[row][col] = ranNum;
}
}
// Display contents of the array
System.out.println("The values of the array are printed below");
showArray(matrix);
System.out.println();
System.out.print("The largest row index: " + scanRow(matrix) + "\n");
System.out.print("The largst column index: " + scanColumn(matrix) + "\n");
} // end main
// method to show array contents
private static void showArray(int[][] array) {
for (int row = 0; row < array.length; row++) {
for (int col = 0; col < array[row].length; col++)
System.out.print(array[row][col] + "\t");
System.out.println();
}
}
// row index method
private static int scanRow(int[][] array) {
int result = -1;
int highest = -1;
for (int row = 0; row < array.length; row++) {
int temp = 0;
for (int col = 0; col < array[row].length; col++) {
// assign current location to temp variable
temp = temp + array[row][col];
}
if (temp > highest) {
highest = temp;
result = row;
}
}
return result;
} // end of row method
// column index method
private static int scanColumn(int[][] array) {
int result = -1;
int highest = -1;
for (int row = 0; row < array.length; row++) {
int temp = 0;
for (int col = 0; col < array[row].length; col++) {
// assing current location to temp variable
temp = temp + array[row][col];
}
if (temp > highest) {
highest = temp;
result = col;
}
}
return result;
} // end column method
} // end class
Upvotes: 2
Views: 2667
Reputation: 36
I think, for a good way, you should use the variable col
like this:
private static int scanColumn(int[][] array) {
int result = -1;
int highest = -1;
// declare and initialize the variable(here you've 'created' it, to then call it on if statement)
int col = 0;
for (int row = 0; row < array.length; row++) {
int temp = 0;
//declare the variable in the for loop
for (col = 0; col < array[row].length; col++) {
// assing current location to temp variable
temp = temp + array[row][col];
}
if (temp > highest) {
highest = temp;
result = col;
}
}
return result;
}
Upvotes: 0
Reputation: 412
I think you are talking about the error caused by this piece of code:
for (int col = 0; col < array[row].length; col++) {
// assing current location to temp variable
temp = temp + array[row][col];
}
if (temp > highest) {
highest = temp;
result = col;
}
if you notice the braces closes before the your result = col;
col doesn't exist outside of the for loop.
Upvotes: 2