You are on page 1of 2

ESCUELA INDUSTRIAL SUPERIOR PEDRO DOMINGO MURILLO

LABORATORIO
Sumar los elementos de la diagonal secundaria de una matriz cuadrada A Nx

Ejemplo. Si NxN=4x4 la matriz A

1 2 5 4
6 0 2 1
0 10 1 7
1 3 3 4

Entonces la suma de sus elementos:

sumaA= 4+2+10+1 = 17

Suma46(M[][],f,c leeDatos( )

S=0 F,C

i = 0, f-1, 1
M=array [0.. F-1, 0..C-1]

j = 0, c-1, 1
leeMat (M[][],N)

i=f-(j+1) S=Suma46 (M[][],N)


S=S+M[i,j]
mostrarMatriz(M[][],N)

s
Return (s)

return

import java.util.Scanner;

public class SumaMatrizDiagSec47 {

Scanner lee = new Scanner(System.in);

void leeMat(int M[][], int f, int c) {


int i, j;
for (i = 0; i <= f - 1; i++) {

GABRIEL TITO DURAN IYL


ESCUELA INDUSTRIAL SUPERIOR PEDRO DOMINGO MURILLO

for (j = 0; j <= c - 1; j++) {


System.out.print("M[" + i + "," + j + "]="); //M[1,1]=
M[i][j] = lee.nextInt();
}
}
}

void mostrarMat(int M[][], int f, int c) {


int i, j;
for (i = 0; i <= f - 1; i++) {
for (j = 0; j <= c - 1; j++) {
System.out.print(M[i][j] + "\t");
}
System.out.println("");
}
}
int suma47(int M[][], int f, int c) {
int i, j, s;
s = 0;
for (i = 0; i <= f - 1; i++) {
for (j = 0; j <= c - 1; j++) {
if (i == f-(j+1)) {
s = s + M[i][j];
}
}
}
return s;
}
void leeDatos() {
int f, c,s;
int M[][];
System.out.print("Nro de filas:");
f = lee.nextInt();
System.out.print("Nro de Columnas:");
c = lee.nextInt();
M = new int[f][c];
leeMat(M, f, c);
s=suma47(M,f,c);
System.out.println("Mostrando Matriz");
mostrarMat(M, f, c);
System.out.println("La suma de la Diagonal secundaria es:"+s);
}
public static void main(String arg[]) {
SumaMatrizDiagSec47 mat = new SumaMatrizDiagSec47();
mat.leeDatos();
}
}

GABRIEL TITO DURAN IYL

You might also like