Riesco a mettere l'elemento massimo nella prima posizione, ma poi tutti gli altri elementi sono a zero...come mai?
Ecco il codice aggiornato:
codice:
import java.util.Scanner;
public class occorrenzeNumero {
public static void main(String[] args) {
Scanner Input=new Scanner(System.in);
System.out.print("Numero di righe della matrice: ");
int row=Input.nextInt();
System.out.print("Numero di colonne della matrice: ");
int col=Input.nextInt();
int[][] matrice=new int[row][col];
int[][] newMatrix=new int[row][col];
int massimoElemento=matrice[0][0];
int maxIndexRow=0, maxIndexCol=0;
for(int i=0; i<row; i++){
for(int j=0; j<col; j++){
matrice[i][j]=1+(int)(Math.random()*100);
System.out.print(matrice[i][j]+"\t");
}
System.out.println();
}
for(int i=0; i<row; i++){
for(int j=0; j<col; j++){
if(matrice[i][j]>massimoElemento){
massimoElemento=matrice[i][j];
maxIndexRow=i;
maxIndexCol=j;
}
}
}
System.out.println("\nL'elemento massimo e': "+massimoElemento);
System.out.println("e si trova in posizione "+(maxIndexRow+1)+" "+(maxIndexCol+1));
for(int i=0; i<row; i++){
for(int j=0; j<col; j++){
newMatrix[0][0]=matrice[maxIndexRow][maxIndexCol];
System.out.print(newMatrix[i][j]+"\t");
}
System.out.println();
}
}
}