intanto ecco il codice del programma
Testo nascosto, fai click qui per vederlo
- Codice:
import java.math.*;
import java.util.Random;
public class e1 {
public static void main(String[] args) {
double [] [] matrice;
double [] arr;
double somma = 0;
matrice = generaMatrice(5, 5);
System.out.println("stampo matrice");
stampaMatrice(matrice);
arr = diagonale(matrice);
System.out.println("Stampo array");
stampaArr(arr);
somma = sommaR(arr);
System.out.println("Stampo somma: "+somma);
double [][] matrice2 = diffVicini(matrice);
System.out.println("stampo matrice");
stampaMatrice(matrice2);
}
public static double[][] generaMatrice(int righe,int colonne){
double [][] mat = new double [righe][colonne];
for(int i = 0; i<righe;i++){
for(int j = 0;j<colonne;j++){
mat[i][j] = (double)(Math.random()*100);
}
}
return mat;
}
public static void stampaMatrice(double[][]matrice){
for(int i = 0; i<matrice.length;i++){
for(int j=0;j<matrice[0].length;j++){
System.out.print(matrice[i][j]+ " ");
}
System.out.println();
}
}
public static double [] diagonale(double[][]matrice){
double [] array = new double [matrice.length];
for(int i = 0;i<matrice.length;i++){
for(int j = 0; j<matrice.length;j++){
if(j == i){
array[j] = matrice[i][j];
}
}
}
return array;
}
public static void stampaArr(double []arr){
for(int i = 0;i<arr.length;i++){
System.out.print(arr[i]+" ");
}
}
public static double sommaR(double[]arr){
double s = 0;
for(int i = 0; i<arr.length;i++){
s+=arr[i];
}
return s;
}
public static double[][] diffVicini(double[][]matrice){
double [][] matriceDiff = new double[matrice.length][matrice[0].length];
double sup,inf,sx,dx,supSx,supDx,infSx,infDx;
for(int i = 0; i<matrice.length;i++){
for(int j = 0; j<matrice[0].length;j++){
if(j == 0&& i == 0){
dx = matrice[i][j+1];
inf = matrice[i+1][j];
infDx = matrice[i+1][j+1];
matriceDiff[i][j] = matrice[i][j]-dx-inf-infDx;
}else if(i == matrice.length-1 && j == 0){
sup = matrice[i-1][j];
supDx = matrice[i-1][j+1];
dx = matrice[i][j+1];
matriceDiff[i][j] = matrice[i][j]-sup-dx-supDx;
}else if(i == 0 && j == matrice[0].length-1){
sx = matrice[i][j-1];
inf = matrice[i+1][j];
infSx = matrice[i+1][j-1];
matriceDiff[i][j] = matrice[i][j]-sx-inf-infSx;
}else if(i == matrice.length-1 && j == matrice[0].length-1){
sup = matrice[i-1][j];
supSx = matrice[i-1][j-1];
sx = matrice[i][j-1];
matriceDiff[i][j] = matrice[i][j]-sx-supSx-sup;
}else if((j>0)&&(j<matrice[0].length)){
if(i==0){
inf = matrice[i+1][j];
dx = matrice[i][j+1];
sx = matrice[i][j-1];
infDx = matrice[i+1][j+1];
infSx = matrice[i+1][j-1];
matriceDiff[i][j] = matrice[i][j]-sx-inf-infSx-dx-infDx;
}else if(i == matrice.length-1){
sup = matrice[i-1][j];
dx = matrice[i][j+1];
sx = matrice[i][j-1];
supDx = matrice[i-1][j+1];
supSx = matrice[i-1][j-1];
matriceDiff[i][j] = matrice[i][j]-sx-sup-supDx-dx-supSx;
}
}else if(j==0){
if(i> 0 && i <matrice.length-1){
sup = matrice [i-1][j];
inf = matrice [i+1][j];
supDx = matrice [i-1][j+1];
infDx = matrice [i+1][j+1];
dx = matrice [i][j+1];
matriceDiff[i][j] = matrice[i][j]-sup-inf-supDx-infDx-dx;
}
}else if(j == matrice[0].length-1){
if(i> 0 && i <matrice.length-1){
sup = matrice [i-1][j];
inf = matrice [i+1][j];
supSx = matrice [i-1][j-1];
infSx = matrice [i+1][j-1];
sx = matrice [i][j-1];
matriceDiff[i][j] = matrice[i][j]-sup-inf-supSx-infSx-sx;
}
}
else{
matriceDiff[i][j] = 1;
}
}
}
return matriceDiff;
}
}
Il problema ce l'ho con questo metodo
praticamente data una matrice in ingresso devo restituire una matrice il cui elemento i,j è dato dalla differenza degli elementi vicini quindi se ho una matrice
123
456
789
la nuova matrice per esempio in 0.0 sarà data da : 1 -2 -5 -4 = -10
(ovviamente se sono sui bordi non devo considerare gli indici che vanno fuori dalla matrice)
- Codice:
public static double[][] diffVicini(double[][]matrice){
double [][] matriceDiff = new double[matrice.length][matrice[0].length];
double sup,inf,sx,dx,supSx,supDx,infSx,infDx;
for(int i = 0; i<matrice.length;i++){
for(int j = 0; j<matrice[0].length;j++){
if(j == 0&& i == 0){
dx = matrice[i][j+1];
inf = matrice[i+1][j];
infDx = matrice[i+1][j+1];
matriceDiff[i][j] = matrice[i][j]-dx-inf-infDx;
}else if(i == matrice.length-1 && j == 0){
sup = matrice[i-1][j];
supDx = matrice[i-1][j+1];
dx = matrice[i][j+1];
matriceDiff[i][j] = matrice[i][j]-sup-dx-supDx;
}else if(i == 0 && j == matrice[0].length-1){
sx = matrice[i][j-1];
inf = matrice[i+1][j];
infSx = matrice[i+1][j-1];
matriceDiff[i][j] = matrice[i][j]-sx-inf-infSx;
}else if(i == matrice.length-1 && j == matrice[0].length-1){
sup = matrice[i-1][j];
supSx = matrice[i-1][j-1];
sx = matrice[i][j-1];
matriceDiff[i][j] = matrice[i][j]-sx-supSx-sup;
}else if((j>0)&&(j<matrice[0].length)){
if(i==0){
inf = matrice[i+1][j];
dx = matrice[i][j+1];
sx = matrice[i][j-1];
infDx = matrice[i+1][j+1];
infSx = matrice[i+1][j-1];
matriceDiff[i][j] = matrice[i][j]-sx-inf-infSx-dx-infDx;
}else if(i == matrice.length-1){
sup = matrice[i-1][j];
dx = matrice[i][j+1];
sx = matrice[i][j-1];
supDx = matrice[i-1][j+1];
supSx = matrice[i-1][j-1];
matriceDiff[i][j] = matrice[i][j]-sx-sup-supDx-dx-supSx;
}
}else if(j==0){
if(i> 0 && i <matrice.length-1){
sup = matrice [i-1][j];
inf = matrice [i+1][j];
supDx = matrice [i-1][j+1];
infDx = matrice [i+1][j+1];
dx = matrice [i][j+1];
matriceDiff[i][j] = matrice[i][j]-sup-inf-supDx-infDx-dx;
}
}else if(j == matrice[0].length-1){
if(i> 0 && i <matrice.length-1){
sup = matrice [i-1][j];
inf = matrice [i+1][j];
supSx = matrice [i-1][j-1];
infSx = matrice [i+1][j-1];
sx = matrice [i][j-1];
matriceDiff[i][j] = matrice[i][j]-sup-inf-supSx-infSx-sx;
}
}
else{
matriceDiff[i][j] = 1;
}
}
}
return matriceDiff;
}
forse non è il massimo dell'efficienza ma funziona tutto almeno fino a questo punto
- Codice:
}else if(j == matrice[0].length-1){
if(i> 0 && i <matrice.length-1){
sup = matrice [i-1][j];
inf = matrice [i+1][j];
supSx = matrice [i-1][j-1];
infSx = matrice [i+1][j-1];
sx = matrice [i][j-1];
matriceDiff[i][j] = matrice[i][j]-sup-inf-supSx-infSx-sx;
}
}
else{
matriceDiff[i][j] = 1;
}
quando ho aggiunto questo pezzo non mi fa i calcoli ecco l'output che mi ritrovo
mi ritrovo tutti quegli 0 quando invece dovrei ritrovarmi 1
e inoltre mi appare questo errore
Sapete aiutarmi?