Java - ELIMINAR , BUSCAR UN INT EN UN MATRIZ JAVA

 
Vista:
sin imagen de perfil

ELIMINAR , BUSCAR UN INT EN UN MATRIZ JAVA

Publicado por edgar fabian (5 intervenciones) el 28/03/2017 06:12:30
Hacer un programa que cree una matriz de números enteros de 6 X 5, después de leída que busque los números repetidos y los elimine dejando solo uno. Luego que muestre el total que suman los números de la matriz y lo que suman los números eliminados. Indique el promedio de los números de la matriz, cual es el menor, cual es el mayor. Después que lea un número a buscar, lo busque e indique en qué posición se encuentra. Si el número no se consigue que indique que no se consiguió.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
Scanner leer=new Scanner(System.in);
 
int filas=6;
int columnas=5;
 
int SumaNumMat=0;
int SumaNumEli=0;
int cantNumEli=0;
double promNumMat=0;
int numMenor=0;
int numMayor=0;
int numBuscar;
 
int matriz[][]=new int [filas][columnas];
int matrizNueva[]=new int[(filas*columnas)];
 
for(int i=0;i<filas;i++)
{
    System.out.println("ingrese valor en la fila ["+i+"]");
 
    for(int j=0;j<columnas;j++)
    {
        System.out.println("columna ["+j+"]");
        matriz[i][j]=leer.nextInt();
 
        SumaNumMat=(matriz[i][j]+SumaNumMat);
 
        for(int n=0;n<(filas*columnas);n++)
        {
            matrizNueva[n]=matriz[i][j];
 
        }
    }
}
 
 
 
System.out.println("suma de los numeros de la matriz: "+SumaNumMat);
promNumMat=SumaNumMat/(filas*columnas);
System.out.println("promedio de los numeros de la matriz: "+promNumMat);
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
sin imagen de perfil

ELIMINAR , BUSCAR UN INT EN UN MATRIZ JAVA

Publicado por edgar fabian (5 intervenciones) el 28/03/2017 06:30:10
hola alguien que me ayude a completarlo , como hago para buscar un numero y eliminarlo?
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
sin imagen de perfil
Val: 349
Bronce
Ha mantenido su posición en Java (en relación al último mes)
Gráfica de Java

ELIMINAR , BUSCAR UN INT EN UN MATRIZ JAVA

Publicado por Andrés (340 intervenciones) el 28/03/2017 19:35:12
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import java.util.Arrays;
 
/**
 *
 * @author andreas
 */
public class MatrizOp {
 
    class Posicion {
 
        private int i;
        private int j;
 
        public Posicion(int i, int j) {
            this.i = i;
            this.j = j;
        }
 
        @Override
        public String toString() {
            return "Posicion{" + "i=" + i + ", j=" + j + '}';
        }
    }
 
    private int[][] matrix;
 
    public MatrizOp(int[][] matrix) {
        this.matrix = matrix;
    }
 
    public Posicion buscarPosicionElemento(int elemento) {
 
        Posicion posicion = null;
 
        for (int i = 0; i < matrix.length && null == posicion; i++) {
            for (int j = 0; j < matrix[i].length && null == posicion; j++) {
 
                int e = matrix[i][j];
 
                if (e == elemento) {
                    posicion = new Posicion(i, j);
 
                }
 
            }
        }
 
        return posicion;
 
    }
 
    public void eliminarDuplicados() {
 
        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix[i].length; j++) {
 
                int elemento = matrix[i][j];
 
                if (0 == elemento) {
                    continue;
                }
 
                matrix[i][j] = 0;
 
                eliminarElemento(elemento);
 
                matrix[i][j] = elemento;
            }
        }
 
    }
 
    public int elementoMaximo() {
 
        int maximo = 0;
 
        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix[i].length; j++) {
                if (matrix[i][j] > maximo) {
                    maximo = matrix[i][j];
                }
            }
        }
        return maximo;
    }
 
    public int elementoMinimo(boolean despuesDelCero) {
 
        int[] arreglo = new int[matrix.length * matrix[0].length];
        int k = 0;
 
        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix[i].length; j++) {
                arreglo[k++] = matrix[i][j];
            }
        }
 
        Arrays.sort(arreglo);
 
        if (arreglo[0] < 0) {
            return arreglo[0];
        } else if (despuesDelCero) {
 
            int elemento = 0;
 
            for (int i = 0; i < arreglo.length; i++) {
                if (arreglo[i] > elemento) {
                    return arreglo[i];
                }
            }
            return 0;
 
        } else {
 
            return 0;
 
        }
 
    }
 
    public long sumarElementos() {
 
        long suma = 0;
 
        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix[i].length; j++) {
                suma += matrix[i][j];
            }
        }
        return suma;
    }
 
    private void eliminarElemento(final int elemento) {
 
        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix[i].length; j++) {
 
                int e = matrix[i][j];
 
                if (e == elemento) {
                    matrix[i][j] = 0;
                }
 
            }
        }
    }
 
    public void imprimirMatriz() {
        for (int i = 0; i < matrix.length; i++) {
 
            for (int j = 0; j < matrix[i].length; j++) {
 
                System.out.print(matrix[i][j] + " ");
 
            }
 
            System.out.println();
        }
    }
 
    public static void main(String argv[]) {
 
        int[][] matrix = {{1, 2, 3}, {2, 3, 4}, {4, 5, 6}};
 
        MatrizOp newClass = new MatrizOp(matrix);
 
        System.out.println("Antes de eliminar");
 
        System.out.println("Elemento minimo: " + newClass.elementoMinimo(true));
        System.out.println("Elemento minimo: " + newClass.elementoMinimo(false));
        System.out.println("Elemento maximo: " + newClass.elementoMaximo());
        System.out.println("Suma de elementos: " + newClass.sumarElementos());
 
        newClass.imprimirMatriz();
        newClass.eliminarDuplicados();
 
        System.out.println("Despues de eliminar");
 
        System.out.println("Elemento minimo: " + newClass.elementoMinimo(true));
        System.out.println("Elemento minimo: " + newClass.elementoMinimo(false));
        System.out.println("Elemento maximo: " + newClass.elementoMaximo());
        System.out.println("Suma de elementos: " + newClass.sumarElementos());
 
        newClass.imprimirMatriz();
 
        System.out.println("A buscar la posicion del elemento 6");
        MatrizOp.Posicion posicion = newClass.buscarPosicionElemento(6);
 
        if (null == posicion) {
            System.out.println("Elemento no encontrado");
        } else {
            System.out.println(posicion);
        }
 
        System.out.println("A buscar la posicion del elemento 15");
        posicion = newClass.buscarPosicionElemento(15);
 
        if (null == posicion) {
            System.out.println("Elemento no encontrado");
        } else {
            System.out.println(posicion);
        }
 
    }
}
matriz
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar