🔢 Capítulo: Matrizes - Exercícios Práticos
Nesta lista de exercícios, você praticará a manipulação de matrizes bidimensionais ($linhas \times colunas$) em memória, laços aninhados (for dentro de for) e transformações matriciais.
🗺️ Mapa de Progressão Pedagógica
graph LR
A["Nível 1: Fundamentos\n(diagonal_negativos)"] --> B["Nível 2: Prática\n(soma_linhas)"]
B --> C["Nível 3: Integração\n(cada_linha)"]
C --> D["Nível 4: Desafio\n(matriz_geral: transformações)"]
style A fill:#e1f5fe,stroke:#03a9f4,stroke-width:2px
style B fill:#e8f5e9,stroke:#4caf50,stroke-width:2px
style C fill:#fff3e0,stroke:#ff9800,stroke-width:2px
style D fill:#fce4ec,stroke:#e91e63,stroke-width:2px
🟢 Nível 1 — Fundamentos
Exercício 1: Problema “diagonal_negativos”
Enunciado: Fazer um programa para ler um número inteiro $N$ ($N \le 10$) e uma matriz quadrada de ordem $N$ contendo números inteiros. Em seguida, o programa deve mostrar a diagonal principal e a quantidade de valores negativos presentes na matriz.
Resultado Esperado
flowchart TD
A["Ler ordem N"] --> B["Loop aninhado (i, j):\nLer matriz[i][j]"]
B --> C["Diagonal Principal:\nelementos onde i == j"]
B --> D["Contar elementos onde matriz[i][j] < 0"]
C & D --> E["Exibir Diagonal e Total de Negativos"]
Exemplo de Execução:
Qual a ordem da matriz? 3
Elemento [0,0]: 5
Elemento [0,1]: -3
Elemento [0,2]: 10
Elemento [1,0]: 15
Elemento [1,1]: 8
Elemento [1,2]: 2
Elemento [2,0]: 7
Elemento [2,1]: 9
Elemento [2,2]: -4
DIAGONAL PRINCIPAL:
5 8 -4
QUANTIDADE DE NEGATIVOS = 2
📤 Instruções de Entrega (Microsoft Teams)
Após validar seus códigos:
- Salve os arquivos DO CÓDIGO no formato:
Atividade_07_Matrizes_Diagonal_SeuNome - Envie os arquivos no Microsoft Teams na tarefa:
Atividade Cap 07 - Matrizes e Tabelas
🔑 Gabarito de Código (C, Java e Python)
Solução em C:
#include <stdio.h>
int main() {
int n, i, j, negativos = 0;
printf("Qual a ordem da matriz? ");
scanf("%d", &n);
int mat[n][n];
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
printf("Elemento [%d,%d]: ", i, j);
scanf("%d", &mat[i][j]);
if (mat[i][j] < 0) {
negativos++;
}
}
}
printf("\nDIAGONAL PRINCIPAL:\n");
for (i = 0; i < n; i++) {
printf("%d ", mat[i][i]);
}
printf("\n\nQUANTIDADE DE NEGATIVOS = %d\n", negativos);
return 0;
}
Solução em Java:
import java.util.Scanner;
public class DiagonalNegativos {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Qual a ordem da matriz? ");
int n = sc.nextInt();
int[][] mat = new int[n][n];
int negativos = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.printf("Elemento [%d,%d]: ", i, j);
mat[i][j] = sc.nextInt();
if (mat[i][j] < 0) {
negativos++;
}
}
}
System.out.println("\nDIAGONAL PRINCIPAL:");
for (int i = 0; i < n; i++) {
System.out.print(mat[i][i] + " ");
}
System.out.println("\n\nQUANTIDADE DE NEGATIVOS = " + negativos);
sc.close();
}
}
Solução em Python:
n = int(input("Qual a ordem da matriz? "))
mat = []
negativos = 0
for i in range(n):
linha = []
for j in range(n):
val = int(input(f"Elemento [{i},{j}]: "))
linha.append(val)
if val < 0:
negativos += 1
mat.append(linha)
print("\nDIAGONAL PRINCIPAL:")
print(" ".join(str(mat[i][i]) for i in range(n)))
print(f"\nQUANTIDADE DE NEGATIVOS = {negativos}")
🟡 Nível 2 — Prática
Exercício 2: Problema “soma_linhas”
Enunciado: Fazer um programa para ler dois números inteiros $M$ e $N$ ($M, N \le 10$). Em seguida, ler uma matriz de $M$ linhas e $N$ colunas contendo números reais. Gerar um vetor unidimensional onde cada elemento é a soma dos elementos da linha correspondente da matriz. Ao final, mostrar o vetor gerado.
Resultado Esperado
Exemplo de Execução:
Qual a quantidade de linhas da matriz? 2
Qual a quantidade de colunas da matriz? 3
Digite os elementos da 1a. linha:
7.0
8.0
10.0
Digite os elementos da 2a. linha:
2.0
3.0
5.0
VETOR GERADO:
25.0
10.0
📤 Instruções de Entrega (Microsoft Teams)
Após validar seus códigos:
- Salve os arquivos DO CÓDIGO no formato:
Atividade_07_Matrizes_SomaLinhas_SeuNome - Envie os arquivos no Microsoft Teams na tarefa:
Atividade Cap 07 - Matrizes e Tabelas
🔑 Gabarito de Código (C, Java e Python)
Solução em C:
#include <stdio.h>
int main() {
int m, n, i, j;
printf("Qual a quantidade de linhas da matriz? ");
scanf("%d", &m);
printf("Qual a quantidade de colunas da matriz? ");
scanf("%d", &n);
double mat[m][n], vet[m];
for (i = 0; i < m; i++) {
printf("Digite os elementos da %da. linha:\n", i + 1);
vet[i] = 0.0;
for (j = 0; j < n; j++) {
scanf("%lf", &mat[i][j]);
vet[i] += mat[i][j];
}
}
printf("\nVETOR GERADO:\n");
for (i = 0; i < m; i++) {
printf("%.1lf\n", vet[i]);
}
return 0;
}
Solução em Java:
import java.util.Locale;
import java.util.Scanner;
public class SomaLinhas {
public static void main(String[] args) {
Locale.setDefault(Locale.US);
Scanner sc = new Scanner(System.in);
System.out.print("Qual a quantidade de linhas da matriz? ");
int m = sc.nextInt();
System.out.print("Qual a quantidade de colunas da matriz? ");
int n = sc.nextInt();
double[][] mat = new double[m][n];
double[] vet = new double[m];
for (int i = 0; i < m; i++) {
System.out.printf("Digite os elementos da %da. linha:\n", i + 1);
for (int j = 0; j < n; j++) {
mat[i][j] = sc.nextDouble();
vet[i] += mat[i][j];
}
}
System.out.println("\nVETOR GERADO:");
for (int i = 0; i < m; i++) {
System.out.printf("%.1f\n", vet[i]);
}
sc.close();
}
}
Solução em Python:
m = int(input("Qual a quantidade de linhas da matriz? "))
n = int(input("Qual a quantidade de colunas da matriz? "))
vet = []
for i in range(m):
print(f"Digite os elementos da {i+1}a. linha:")
soma_linha = 0.0
for _ in range(n):
soma_linha += float(input())
vet.append(soma_linha)
print("\nVETOR GERADO:")
for v in vet:
print(f"{v:.1f}")
🟠 Nível 3 — Integração
Exercício 3: Problema “cada_linha”
Enunciado: Ler um inteiro $N$ ($N \le 10$) e uma matriz quadrada de ordem $N$ de números inteiros. Mostrar o maior elemento de cada linha da matriz.
Resultado Esperado
Exemplo de Execução:
Qual a ordem da matriz? 4
Elemento [0,0]: 5
Elemento [0,1]: -2
Elemento [0,2]: 8
Elemento [0,3]: 2
Elemento [1,0]: 12
Elemento [1,1]: 3
Elemento [1,2]: 4
Elemento [1,3]: 9
Elemento [2,0]: 4
Elemento [2,1]: 5
Elemento [2,2]: 16
Elemento [2,3]: 1
Elemento [3,0]: 2
Elemento [3,1]: 7
Elemento [3,2]: 6
Elemento [3,3]: 3
MAIOR ELEMENTO DE CADA LINHA:
8
12
16
7
📤 Instruções de Entrega (Microsoft Teams)
Após validar seus códigos:
- Salve os arquivos DO CÓDIGO no formato:
Atividade_07_Matrizes_CadaLinha_SeuNome - Envie os arquivos no Microsoft Teams na tarefa:
Atividade Cap 07 - Matrizes e Tabelas
🔑 Gabarito de Código (C, Java e Python)
Solução em C:
#include <stdio.h>
int main() {
int n, i, j;
printf("Qual a ordem da matriz? ");
scanf("%d", &n);
int mat[n][n];
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
printf("Elemento [%d,%d]: ", i, j);
scanf("%d", &mat[i][j]);
}
}
printf("\nMAIOR ELEMENTO DE CADA LINHA:\n");
for (i = 0; i < n; i++) {
int maior = mat[i][0];
for (j = 1; j < n; j++) {
if (mat[i][j] > maior) {
maior = mat[i][j];
}
}
printf("%d\n", maior);
}
return 0;
}
Solução em Java:
import java.util.Scanner;
public class CadaLinha {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Qual a ordem da matriz? ");
int n = sc.nextInt();
int[][] mat = new int[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.printf("Elemento [%d,%d]: ", i, j);
mat[i][j] = sc.nextInt();
}
}
System.out.println("\nMAIOR ELEMENTO DE CADA LINHA:");
for (int i = 0; i < n; i++) {
int maior = mat[i][0];
for (int j = 1; j < n; j++) {
if (mat[i][j] > maior) {
maior = mat[i][j];
}
}
System.out.println(maior);
}
sc.close();
}
}
Solução em Python:
n = int(input("Qual a ordem da matriz? "))
mat = []
for i in range(n):
linha = []
for j in range(n):
linha.append(int(input(f"Elemento [{i},{j}]: ")))
mat.append(linha)
print("\nMAIOR ELEMENTO DE CADA LINHA:")
for i in range(n):
print(max(mat[i]))
🔴 Nível 4 — Desafio
Exercício 4: Problema “matriz_geral”
Enunciado: Emitir um relatório completo para uma matriz quadrada de ordem $N$ contendo números reais:
- Calcular e imprimir a soma de todos os elementos positivos da matriz.
- Fazer a leitura do índice de uma linha e imprimir todos os elementos desta linha.
- Fazer a leitura do índice de uma coluna e imprimir todos os elementos desta coluna.
- Imprimir os elementos da diagonal principal.
- Elevar ao quadrado todos os números negativos da matriz e imprimir a matriz alterada.
Resultado Esperado
Exemplo de Execução:
Qual a ordem da matriz? 3
Elemento [0,0]: 7.0
Elemento [0,1]: -8.0
Elemento [0,2]: 10.0
Elemento [1,0]: -2.0
Elemento [1,1]: 3.0
Elemento [1,2]: 5.0
Elemento [2,0]: 11.0
Elemento [2,1]: -15.0
Elemento [2,2]: 4.0
SOMA DOS POSITIVOS: 40.0
Escolha uma linha: 1
LINHA ESCOLHIDA: -2.0 3.0 5.0
Escolha uma coluna: 2
COLUNA ESCOLHIDA: 10.0 5.0 4.0
DIAGONAL PRINCIPAL: 7.0 3.0 4.0
MATRIZ ALTERADA:
7.0 64.0 10.0
4.0 3.0 5.0
11.0 225.0 4.0
📤 Instruções de Entrega (Microsoft Teams)
Após validar seus códigos:
- Salve os arquivos DO CÓDIGO no formato:
Atividade_07_Matrizes_MatrizGeral_SeuNome - Envie os arquivos no Microsoft Teams na tarefa:
Atividade Cap 07 - Matrizes e Tabelas
🔑 Gabarito de Código (C, Java e Python)
Solução em C:
#include <stdio.h>
int main() {
int n, i, j, linha, coluna;
double somaPositivos = 0.0;
printf("Qual a ordem da matriz? ");
scanf("%d", &n);
double mat[n][n];
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
printf("Elemento [%d,%d]: ", i, j);
scanf("%lf", &mat[i][j]);
if (mat[i][j] > 0) {
somaPositivos += mat[i][j];
}
}
}
printf("\nSOMA DOS POSITIVOS: %.1lf\n", somaPositivos);
printf("\nEscolha uma linha: ");
scanf("%d", &linha);
printf("LINHA ESCOLHIDA: ");
for (j = 0; j < n; j++) {
printf("%.1lf ", mat[linha][j]);
}
printf("\n\nEscolha uma coluna: ");
scanf("%d", &coluna);
printf("COLUNA ESCOLHIDA: ");
for (i = 0; i < n; i++) {
printf("%.1lf ", mat[i][coluna]);
}
printf("\n\nDIAGONAL PRINCIPAL: ");
for (i = 0; i < n; i++) {
printf("%.1lf ", mat[i][i]);
}
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
if (mat[i][j] < 0) {
mat[i][j] = mat[i][j] * mat[i][j];
}
}
}
printf("\n\nMATRIZ ALTERADA:\n");
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
printf("%.1lf ", mat[i][j]);
}
printf("\n");
}
return 0;
}
Solução em Java:
import java.util.Locale;
import java.util.Scanner;
public class MatrizGeral {
public static void main(String[] args) {
Locale.setDefault(Locale.US);
Scanner sc = new Scanner(System.in);
System.out.print("Qual a ordem da matriz? ");
int n = sc.nextInt();
double[][] mat = new double[n][n];
double somaPositivos = 0.0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.printf("Elemento [%d,%d]: ", i, j);
mat[i][j] = sc.nextDouble();
if (mat[i][j] > 0) {
somaPositivos += mat[i][j];
}
}
}
System.out.printf("\nSOMA DOS POSITIVOS: %.1f\n", somaPositivos);
System.out.print("\nEscolha uma linha: ");
int linha = sc.nextInt();
System.out.print("LINHA ESCOLHIDA: ");
for (int j = 0; j < n; j++) {
System.out.printf("%.1f ", mat[linha][j]);
}
System.out.print("\n\nEscolha uma coluna: ");
int coluna = sc.nextInt();
System.out.print("COLUNA ESCOLHIDA: ");
for (int i = 0; i < n; i++) {
System.out.printf("%.1f ", mat[i][coluna]);
}
System.out.print("\n\nDIAGONAL PRINCIPAL: ");
for (int i = 0; i < n; i++) {
System.out.printf("%.1f ", mat[i][i]);
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (mat[i][j] < 0) {
mat[i][j] = Math.pow(mat[i][j], 2);
}
}
}
System.out.println("\n\nMATRIZ ALTERADA:");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.printf("%.1f ", mat[i][j]);
}
System.out.println();
}
sc.close();
}
}
Solução em Python:
n = int(input("Qual a ordem da matriz? "))
mat = []
soma_positivos = 0.0
for i in range(n):
linha = []
for j in range(n):
val = float(input(f"Elemento [{i},{j}]: "))
linha.append(val)
if val > 0:
soma_positivos += val
mat.append(linha)
print(f"\nSOMA DOS POSITIVOS: {soma_positivos:.1f}")
linha_sel = int(input("\nEscolha uma linha: "))
print("LINHA ESCOLHIDA:", " ".join(f"{x:.1f}" for x in mat[linha_sel]))
coluna_sel = int(input("\nEscolha uma coluna: "))
print("COLUNA ESCOLHIDA:", " ".join(f"{mat[i][coluna_sel]:.1f}" for i in range(n)))
print("\nDIAGONAL PRINCIPAL:", " ".join(f"{mat[i][i]:.1f}" for i in range(n)))
for i in range(n):
for j in range(n):
if mat[i][j] < 0:
mat[i][j] = mat[i][j] ** 2
print("\nMATRIZ ALTERADA:")
for i in range(n):
print(" ".join(f"{mat[i][j]:.1f}" for j in range(n)))
🔗 Navegação Pedagógica
- ⬅️ Teoria: Capítulo 08: Matrizes
- ➡️ Próximo Tópico: Capítulo 09: Funções
- 📋 Lista Completa: Índice de Exercícios