Como resolver este problema?

1 resposta
A

Crie uma matriz 51 x 10 e carregue todas as suas posições com números inteiros digitados
pelo usuário, exceto a linha 51 (serão lidas, portanto, 50 linhas e 10 colunas). Faça a soma
dos valores de cada uma das colunas da matriz (da 1ª à 50ª linha) e armazene o resultado
na linha 51. Imprima somente a linha de resultados das somas.
Atenção:
? A matriz deve estar totalmente carregada para, posteriormente, ser iniciada a soma das
colunas

comecei assim:

import javax.swing.JOptionPane;

public class Entregar {

public static void main(String args[]) {
    int il = 0;
    int ic = 0;
    int soma = 0;
    int vetor[]=new int[10];
    int i=0;

    int matriz[][] = new int[2][2];
    for (il = 0; il < matriz.length; il++) {//percorrendo a linha
        for (ic = 0; ic < matriz.length; ic++) {//percorrendo a coluna
            matriz[il][ic] = Integer.parseInt(JOptionPane.showInputDialog("Digite numeros: "));//preenchendo a matriz
        }
    }
for (il = 0; il < matriz.length; il++){

soma += matriz[il][ic];

vetor[i]=soma;

}

//vetor[i]=soma;
for (i = 0; i < matriz.length; i++) {
        JOptionPane.showMessageDialog(null, "soma numeros: " + vetor[i]);
    }
}

}

1 Resposta

JxtaNode

Bom dia,

Para os tests eu utilizei so 3 linhas e 2 colunas
Normalment :
LINES=50;
COLS=10;

O programma não gera Exceptions mas avia de gerar ...

import javax.swing.JOptionPane;

public class Matriz {
	
	static final int LINES=3;
	static final int COLS=2;
	

	public static void main(String args[]) {
		
		int il = 0;
		int ic = 0;
		int soma = 0;
		int[] vetor =new int[COLS];
		

		int matriz[][] = new int[LINES][COLS];
		for (il = 0; il < LINES; il++) {
			for (ic = 0; ic < COLS; ic++) {
				matriz[il][ic] = Integer.parseInt(JOptionPane.showInputDialog("Digite numeros: Line"+il+" Column"+ic));
			}
		}
		for (ic = 0; ic < COLS; ic++) {
		   for (il = 0; il < LINES; il++){ 
			soma += matriz[il][ic];
		  }
		   vetor[ic]=soma;
		   soma=0; // inicializa a var soma
		}

		String str="soma numeros: [";
		for (ic = 0; ic < vetor.length; ic++) {
			str= str+"  "+ vetor[ic];
		}
		JOptionPane.showMessageDialog(null, str+" ]");
	}
}

Abraços

Criado 2 de dezembro de 2009
Ultima resposta 2 de dez. de 2009
Respostas 1
Participantes 2