Last active
December 12, 2015 06:48
-
-
Save lrlucena/4731273 to your computer and use it in GitHub Desktop.
Lista de Exercícios 09 - Programação de Computadores
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Faça um programa que leia uma matriz de 8x4 e crie um array de 4 | |
# elementos, onde cada elemento do array é a soma dos elementos coluna. | |
matriz = | |
8.times.map do | |
4.times.map do | |
gets.to_i | |
end | |
end | |
soma = [0,0,0,0] | |
for linha in matriz do | |
for i in linha.size.times do | |
soma[i] = soma[i] + linha[i] | |
end | |
end | |
# Alternativa (linhas 11-16) | |
# soma = matriz.transpose.map do |coluna| | |
# coluna.inject(:+) | |
# end | |
puts soma |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Faça um programa que leia uma matriz 15x10 e imprima o número de linhas | |
# e o número de colunas nulas da matriz. | |
matriz = | |
15.times.map do | |
10.times.map do | |
gets.to_i | |
end | |
end | |
linhas_nulas = 0 | |
for linha in matriz do | |
nulo = true | |
for valor in linha do | |
nulo = (nulo and valor==0) | |
end | |
linhas_nulas += 1 if nulo | |
end | |
colunas_nulas = 0 | |
for coluna in matriz.transpose do | |
nulo = true | |
for valor in coluna do | |
nulo = (nulo and valor==0) | |
end | |
colunas_nulas += 1 if nulo | |
end | |
# Alternativa (linhas 11-27) | |
# linhas_nulas = | |
# matriz.map do |linha| | |
# linha.inject(true) do |nulo, valor| | |
# nulo and valor==0 | |
# end | |
# end | |
# .count(true) | |
# | |
# linhas_nulas = | |
# matriz.transpose.map do |linha| | |
# linha.inject(true) do |nulo, valor| | |
# nulo and valor==0 | |
# end | |
# end | |
# .count(true) | |
puts linhas_nulas | |
puts colunas_nulas |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Faça um programa que leia uma matriz de 8x6 e calcule a matriz transposta | |
m, n = 8, 6 | |
matriz = | |
m.times.map do | |
n.times.map do | |
gets.to_i | |
end | |
end | |
matriz_t = | |
n.times.map do |i| | |
m.times.map do |j| | |
matriz[j][i] | |
end | |
end | |
# Alternativa (linhas 11-16) | |
# matriz_t = matriz.transpose | |
print matriz_t |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Faça um programa que leia duas matrizes de 8x4 e realize a soma delas | |
# gerando uma terceira matriz. | |
m, n = 8,4 | |
matriz1 = | |
m.times.map do | |
n.times.map do | |
gets.to_i | |
end | |
end | |
matriz2 = | |
m.times.map do | |
n.times.map do | |
gets.to_i | |
end | |
end | |
matriz = | |
m.times.map do |i| | |
n.times.map do |j| | |
matriz1[i][j] + matriz2[i][j] | |
end | |
end | |
print matriz |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Faça um programa leia uma matriz de 5x5 e calcule a sua transposta. | |
# OBS: A transposta deve ser calculada na PRÓPRIA matriz | |
matriz = | |
5.times.map do | |
5.times.map do | |
gets.to_i | |
end | |
end | |
for i in 1..4 do | |
for j in (i+i)..4 do | |
matriz[i][j], matriz[j][i] = matriz[j][i], matriz[i][j] | |
end | |
end | |
# Alternativa (linhas 11-15) | |
# matriz.transpose! | |
puts matriz |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Faça um programa que leia uma matriz de 5x5 e mostre a soma de TODOS | |
# os seus elementos. | |
matriz = | |
5.times.map do | |
5.times.map do | |
gets.to_i | |
end | |
end | |
soma = 0 | |
for linha in matriz do | |
for valor in linha do | |
soma = soma + valor | |
end | |
end | |
#Alternativa (linhas 11-16) | |
#soma = matriz.flatten.inject(:+) | |
puts soma |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment