Reputation: 35
How can i find the maximum value of a column in a matrix using only 2 for's in Java?
for(int i = 1; i< N; i++)
for(int j = 1; j < M; j++)
i want to find the maxim for each column
Upvotes: 0
Views: 303
Reputation: 5882
public int findMaxInCol(int colIndex){
int max = Integer.Min;
for(int row=0;row<Matrix.Rows;row++){
if(matrix[row][colIndex] > max){
max = matrix[row]colIndex];
}
}
return max;
}
void int findMaxOfMaxes() {
int maxOfMaxs = Integer.min;
for(int col=0;col<j;col++){
int maxInCol = findMaxInCol(col);
if( maxInCol > maxOfMaxs)
maxOfMaxs = maxInCol;
}
return maxOfMaxs
}
//pseudocode
//editing after finding that you wanted max in a matrix. You are right, you need 2 for loops.
Upvotes: 1