5
Lista de Exercícios: Vetores e Matrizes Gabarito Vetor: 1. Elabore um programa que inicialize um vetor de 5 posições de inteiros (os valores devem ser lidos através do teclado) e, em seguida, calcule e imprima a soma, o produto e a média desses elementos. Mostre os resultados. import javax.swing.JOptionPane; public class Ex1 { public static void main(String[] args) { int vet[] = new int [5]; int soma = 0, prod = 1, med = 0; for (int i = 0; i < 5; i++) { int num = Integer.parseInt(JOptionPane.showInputDialog("Entre com um número:")); vet[i] = num; soma = soma + vet[i]; prod = prod * vet[i]; } med = soma / 5; JOptionPane.showMessageDialog(null, "Soma: " + soma + "Produto: " + prod + "Média: " + med); } } 2. Faça um programa para armazenar 5 valores inteiros num vetor de 5 posições. Após, mostre os valores armazenados multiplicados por divididos por 2. import javax.swing.JOptionPane; public class Ex2 { public static void main(String[] args) { int vet[] = new int [5]; for (int i = 0; i < 5; i++) { int num = Integer.parseInt(JOptionPane.showInputDialog("Entre com um número:")); vet[i] = num*2; } for (int i = 0; i < 5; i++) { System.out.println(vet[i]); } } } 3. Construa um programa que leia dois vetores A e B e, gere um terceiro vetor C, formado pela multiplicação dos dois outros (A e B). import javax.swing.JOptionPane; public class Ex3 { public static void main(String[] args) { int vet1[] = new int [3]; int vet2[] = new int [3]; int vet3[] = new int [3]; for (int i = 0; i < 3; i++) { int num1 = Integer.parseInt(JOptionPane.showInputDialog("Entre com um número para o vetor 1:")); vet1[i] = num1;

Lista Array Prog2 EM-Gabarito

Embed Size (px)

Citation preview

Page 1: Lista Array Prog2 EM-Gabarito

Lista de Exercícios: Vetores e Matrizes

Gabarito

Vetor:

1. Elabore um programa que inicialize um vetor de 5 posições de inteiros (os valores

devem ser lidos através do teclado) e, em seguida, calcule e imprima a soma, o produto

e a média desses elementos. Mostre os resultados.

import javax.swing.JOptionPane;

public class Ex1 {

public static void main(String[] args) {

int vet[] = new int [5];

int soma = 0, prod = 1, med = 0;

for (int i = 0; i < 5; i++) {

int num = Integer.parseInt(JOptionPane.showInputDialog("Entre com um número:"));

vet[i] = num;

soma = soma + vet[i];

prod = prod * vet[i];

}

med = soma / 5;

JOptionPane.showMessageDialog(null, "Soma: " + soma + "Produto: " + prod + "Média: " +

med);

}

}

2. Faça um programa para armazenar 5 valores inteiros num vetor de 5 posições. Após,

mostre os valores armazenados multiplicados por divididos por 2.

import javax.swing.JOptionPane;

public class Ex2 {

public static void main(String[] args) {

int vet[] = new int [5];

for (int i = 0; i < 5; i++) {

int num = Integer.parseInt(JOptionPane.showInputDialog("Entre com um número:"));

vet[i] = num*2;

}

for (int i = 0; i < 5; i++) {

System.out.println(vet[i]);

}

}

}

3. Construa um programa que leia dois vetores A e B e, gere um terceiro vetor C,

formado pela multiplicação dos dois outros (A e B).

import javax.swing.JOptionPane;

public class Ex3 {

public static void main(String[] args) {

int vet1[] = new int [3];

int vet2[] = new int [3];

int vet3[] = new int [3];

for (int i = 0; i < 3; i++) {

int num1 = Integer.parseInt(JOptionPane.showInputDialog("Entre com um número para

o vetor 1:"));

vet1[i] = num1;

Page 2: Lista Array Prog2 EM-Gabarito

}

for (int i = 0; i < 3; i++) {

int num2 = Integer.parseInt(JOptionPane.showInputDialog("Entre com um número para

o vetor 2:"));

vet2[i] = num2;

}

for (int i = 0; i < 3; i++) {

vet3[i] = vet1[i] * vet2[i];

System.out.println(vet3[i]);

}

}

}

4. Criar um programa que permita realizar um controle sobre os produtos de uma

determinada loja. As informações do produto são: Código, descrição, quantidade e

preço. O programa deve contemplar:

- A inclusão de produtos.

- Um relatório que irá listar todos os produtos cadastrados.

import javax.swing.JOptionPane;

public class Ex4 {

public static void main(String[] args) {

int cod[] = new int [3];

String desc[] = new String [3];

int quant[] = new int [3];

float preco[] = new float [3];

int qProd;

qProd = Integer.parseInt(JOptionPane.showInputDialog("Entre com o número de produtos que

serão cadastrados:"));

for (int i = 0; i < qProd; i++) {

int n = i+1;

cod[i] = Integer.parseInt(JOptionPane.showInputDialog("Entre com o código do produto:

" + n));

desc[i] = JOptionPane.showInputDialog("Entre com a descrição do produto: " + n);

quant[i] = Integer.parseInt(JOptionPane.showInputDialog("Entre com a quantidade do

produto " + n));

preco[i] = Float.parseFloat(JOptionPane.showInputDialog("Entre com o preço do

produto: " + n));

}

for (int i = 0; i < qProd; i++) {

int n1 = i+1;

System.out.println("Produto " + n1);

System.out.println("Código " + cod[i]);

System.out.println("Descrição " + desc[i]);

System.out.println("Quantidade " + quant[i]);

System.out.println("Preço " + preco[i] + "\n");

}

}

}

Matriz:

1. Para o conjunto de valores abaixo, escreva o código Java, usando laço(s) de repetição,

que preencha uma matriz com os valores:

Page 3: Lista Array Prog2 EM-Gabarito

0 1 2 3 4 5 6 7 8 9

0 1 2 3 4 5 6 7 8 9

0 1 2 3 4 5 6 7 8 9

import javax.swing.JOptionPane;

public class Ex1 {

public static void main(String[] args) {

int m[][] = new int [3][3];

for (int i = 0; i < 3; i++) {

for (int j = 0; j < 3; j++) {

int n = i+1;

int num = Integer.parseInt(JOptionPane.showInputDialog("Entre com os valores

para a " + n + "º" + " linha: "));

m[i][j] = num;

}

}

for (int i = 0; i < 3; i++) {

for (int j = 0; j < 3; j++) {

System.out.print(m[i][j] + " ");

}

System.out.print("\n");

}

}

}

2. Preencha uma matriz de inteiros com números aleatórios positivos menores que 100.

import javax.swing.JOptionPane;

public class Ex2 {

public static void main(String[] args) {

int m[][] = new int [3][3];

for (int i = 0; i < 3; i++) {

for (int j = 0; j < 3; j++) {

int num = Integer.parseInt(JOptionPane.showInputDialog("Entre com um valor:

"));

if (num > 0 && num < 100)

m[i][j] = num;

}

}

for (int i = 0; i < 3; i++) {

for (int j = 0; j < 3; j++) {

System.out.print(m[i][j] + " ");

}

System.out.print("\n");

}

}

}

3. Escreva um programa que leia 10 valores double do teclado e armazene-os em uma

matriz de dimensões 2x5. Mostre o resultado na tela.

import javax.swing.JOptionPane;

public class Ex3 {

public static void main(String[] args) {

double m1[][] = new double [2][5];

for (int i = 0; i < 2; i++) {

for (int j = 0; j < 5; j++) {

Page 4: Lista Array Prog2 EM-Gabarito

double num = Double.parseDouble(JOptionPane.showInputDialog("Entre com

um valor: "));

m1[i][j] = num;

}

}

for (int i = 0; i < 2; i++) {

for (int j = 0; j < 5; j++) {

System.out.print(m1[i][j] + " ");

}

System.out.print("\n");

}

}

}

4. Crie um programa que recebe uma matriz de inteiros e retorna a soma e o produto de

todos os elementos da matriz.

import javax.swing.JOptionPane;

public class Ex4 {

public static void main(String[] args) {

int mat[][] = new int [2][2];

int soma = 0, prod = 1;

for (int i = 0; i < 2; i++) {

for (int j = 0; j < 2; j++) {

int num = Integer.parseInt(JOptionPane.showInputDialog("Entre com um valor:

"));

mat[i][j] = num;

soma = soma + mat[i][j];

prod = prod * mat[i][j];

}

}

JOptionPane.showMessageDialog(null, "Soma: " + soma + " " + "Produto: " + prod);

}

}

5. Construa uma classe que leia o código e o valor de um item em estoque e, em

seguida, armazene essas informações em uma matriz. Logo após, imprima o valor total

em estoque e liste os itens em estoque com seus códigos. Considere que existem 100

itens e que seus códigos variam de 100 a 199.

import javax.swing.JOptionPane;

public class Ex5 {

public static void main(String[] args) {

int cod[][] = new int [10][10];

String item[][] = new String [10][10];

float valor[][] = new float [10][10];

float total = 0;

for (int i = 0; i < 10; i++) {

for (int j = 0; j < 10; j++) {

item[i][j] = JOptionPane.showInputDialog("Entre com a descrição do produto:

");

int num = Integer.parseInt(JOptionPane.showInputDialog("Entre com o código:

"));

cod[i][j] = num;

Page 5: Lista Array Prog2 EM-Gabarito

float num2 = Float.parseFloat(JOptionPane.showInputDialog("Entre com o

valor: "));

valor[i][j] = num2;

total = total + valor[i][j];

}

}

System.out.println("Valor total em estoque: " + total);

for (int i = 0; i < 10; i++) {

for (int j = 0; j < 10; j++) {

System.out.println("Produto: " + item[i][j] + " " + "Código: " + cod[i][j]);

}

}

}

}

6. Criar um programa que leia os elementos de uma matriz inteira de 3x3 e imprima

outra matriz multiplicando cada elemento da primeira matriz por 2.

import javax.swing.JOptionPane;

public class Ex6 {

public static void main(String[] args) {

int m1[][] = new int [3][3];

int m2[][] = new int [3][3];

for (int i = 0; i < 3; i++) {

for (int j = 0; j < 3; j++) {

m1[i][j] = Integer.parseInt(JOptionPane.showInputDialog("Entre com um

número: "));

m2[i][j] = m1[i][j] * 2;

System.out.println(m2[i][j]);

}

}

}

}