My favorites
▼
|
Sign in
lineadecodigo
Web Code Samples in spanish
Project Home
Downloads
Wiki
Issues
Source
Checkout
Browse
Changes
Source path:
svn
/
trunk
/
lineadecodigo_java
/
src
/
com
/
lineadecodigo
/
java
/
basico
/
arrays
/
SumarMatrices.java
r478
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
package com.lineadecodigo.java.basico.arrays;
/**
* @file SumarMatrices.java
* @version 1.0
* @author Linea de Codigo (http://lineadecodigo.com)
* @date 28.julio.2010
* @url http://lineadecodigo.com/java/sumar-matrices-en-java/
* @description Programa que suma dos matrices en Java
*/
public class SumarMatrices {
public static void pintarmatriz(int[][] matriz){
for (int x=0; x < matriz.length; x++) {
for (int y=0; y < matriz[x].length; y++) {
System.out.println (matriz[x][y]);
}
}
}
public static int[][] sumarmatrices (int[][] m1,int[][] m2) throws Exception {
// Comprobamos que las matrices son de la misma dimensión
if ((m1.length == m2.length) && (m1[0].length==m2[0].length)){
int[][] suma = new int[m1.length][m1[0].length];
for (int x=0; x < m1.length; x++) {
for (int y=0; y < m1[x].length; y++) {
suma[x][y]=m1[x][y]+m2[x][y];
}
}
return suma;
} else
throw new Exception();
}
public static void main(String[] args) {
// Matrices
int [][] matriz1 = {{2,4,4},{6,6,9}};
int [][] matriz2 = {{2,4,4},{6,6,9}};
int [][] matriz = new int[3][3];
try {
matriz = sumarmatrices(matriz1,matriz2);
pintarmatriz(matriz);
} catch(Exception e){
System.out.println("Matrices de diferente dimensión");
}
}
}
Show details
Hide details
Change log
r262
by vcuervo on Jul 28, 2010
Diff
Sumar matrices
Go to:
...basico/arrays/SumarMatrices.java
Project members,
sign in
to write a code review
Older revisions
All revisions of this file
File info
Size: 1408 bytes, 60 lines
View raw file
Powered by
Google Project Hosting