EJERCICIOS DE MATRICES
1. Programa que permite al usuario ingresar los elementos de una matriz de 2 filas y 3 columnas, y
luego imprima dicha matriz en pantalla tal como fue ingresada.
#include <iostream>
using namespace std;
int main() {
int matriz[2][3];
// Leer datos de la matriz
cout << "Ingrese los elementos de una matriz de 2 filas y 3 columnas:"
<< endl;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
cout << "Elemento [" << i << "][" << j << "]: ";
cin >> matriz[i][j];
}
}
// Mostrar la matriz
cout << "\nLa matriz ingresada es:" << endl;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
cout << matriz[i][j] << "\t";
}
cout << endl;
}
return 0;
}
2. Programa que permite al usuario ingresar por teclado las filas y columnas de una matriz, y luego
imprima dicha matriz en pantalla tal como fue ingresada.
#include <iostream>
using namespace std;
const int limite = 100; // Limite maximo para filas y columnas
int main() {
int filas, columnas;
int matriz[limite][limite]; // Declaracion de la matriz con tamanho
maximo
// Validar numero de filas
do {
cout << "Ingrese el numero de filas (1 a " << limite << "): ";
cin >> filas;
} while (filas < 1 || filas > limite);
// Validar numero de columnas
do {
cout << "Ingrese el numero de columnas (1 a " << limite << "): ";
cin >> columnas;
} while (columnas < 1 || columnas > limite);
// Lectura de los elementos de la matriz
cout << "Ingrese los elementos de la matriz:" << endl;
for (int i = 0; i < filas; i++) {
for (int j = 0; j < columnas; j++) {
cout << "Elemento [" << i << "][" << j << "]: ";
cin >> matriz[i][j];
}
}
// Mostrar la matriz
cout << endl << "La matriz ingresada es:" << endl;
for (int i = 0; i < filas; i++) {
for (int j = 0; j < columnas; j++) {
cout << matriz[i][j] << "\t";
}
cout << endl;
}
return 0;
}
3. Desarrolla un programa en C++ que, después de leer una matriz de números enteros desde
teclado, calcule y muestre la suma total de todos sus elementos.
#include <iostream>
using namespace std;
int main() {
const int limite_filas = 10;
const int limite_columnas = 10;
int filas, columnas;
// Validar numero de filas
do {
cout << "Ingrese el numero de filas (1 - " << limite_filas << "): ";
cin >> filas;
} while (filas < 1 || filas > limite_filas);
// Validar numero de columnas
do {
cout << "Ingrese el numero de columnas (1 - " << limite_columnas <<
"): ";
cin >> columnas;
} while (columnas < 1 || columnas > limite_columnas);
int matriz[limite_filas][limite_columnas];
// Leer los elementos de la matriz
for (int i = 0; i < filas; i++) {
for (int j = 0; j < columnas; j++) {
cout << "Elemento [" << i << "][" << j << "]: ";
cin >> matriz[i][j];
}
}
int suma_total = 0;
// Calcular la suma de todos los elementos
for (int i = 0; i < filas; i++) {
for (int j = 0; j < columnas; j++) {
suma_total += matriz[i][j];
//suma_total = suma_total + matriz[i][j];
}
}
cout << "La suma de todos los elementos de la matriz es: " << suma_total
<< endl;
return 0;
}
4. Escribe un programa que lea una matriz de N filas y M columnas (ambas ingresadas por el
usuario con validación), almacene los elementos ingresados por teclado y calcule la suma
de cada fila, mostrando el resultado al finalizar.
#include <iostream>
using namespace std;
int main() {
const int limite_filas = 10;
const int limite_columnas = 10;
int filas, columnas;
// Validar numero de filas
do {
cout << "Ingrese el numero de filas (1 - " << limite_filas << "): ";
cin >> filas;
} while (filas < 1 || filas > limite_filas);
// Validar numero de columnas
do {
cout << "Ingrese el numero de columnas (1 - " << limite_columnas <<
"): ";
cin >> columnas;
} while (columnas < 1 || columnas > limite_columnas);
int matriz[limite_filas][limite_columnas];
// Leer los elementos de la matriz
for (int i = 0; i < filas; i++) {
for (int j = 0; j < columnas; j++) {
cout << "Elemento [" << i << "][" << j << "]: ";
cin >> matriz[i][j];
}
}
// Sumar y mostrar la suma de cada fila
for (int i = 0; i < filas; i++) {
int suma_fila = 0;
for (int j = 0; j < columnas; j++) {
suma_fila += matriz[i][j];
}
cout << "Suma de la fila " << i << " = " << suma_fila << endl;
}
return 0;
}
5. Leer una matriz de N filas y M columnas, luego mostrar el mayor y el menor valor
contenido en ella.
#include <iostream>
using namespace std;
int main() {
const int limite_filas = 10;
const int limite_columnas = 10;
int filas, columnas;
// Validar numero de filas
do {
cout << "Ingrese el numero de filas (1 - " << limite_filas << "): ";
cin >> filas;
} while (filas < 1 || filas > limite_filas);
// Validar numero de columnas
do {
cout << "Ingrese el numero de columnas (1 - " << limite_columnas <<
"): ";
cin >> columnas;
} while (columnas < 1 || columnas > limite_columnas);
int matriz[limite_filas][limite_columnas];
// Leer los elementos de la matriz
for (int i = 0; i < filas; i++) {
for (int j = 0; j < columnas; j++) {
cout << "Elemento [" << i << "][" << j << "]: ";
cin >> matriz[i][j];
}
}
// Inicializnado el mayor y menor con el primer elemento
int mayor = matriz[0][0];
int menor = matriz[0][0];
// Buscar el mayor y el menor valor
for (int i = 0; i < filas; i++) {
for (int j = 0; j < columnas; j++) {
if (matriz[i][j] > mayor) {
mayor = matriz[i][j];
}
if (matriz[i][j] < menor) {
menor = matriz[i][j];
}
}
}
// Mostrar resultados
cout << "El valor mayor de la matriz es: " << mayor << endl;
cout << "El valor menor de la matriz es: " << menor << endl;
return 0;
}
6. Leer una matriz cuadrada de tamaño n x n desde teclado, luego solicitar un número entero k
y multiplicar todos los elementos de la matriz por k. Finalmente, mostrar la matriz
resultante.
#include <iostream>
using namespace std;
const int limite = 100;
// Prototipos
int leer_tamanho();
void leer_matriz(int matriz[][limite], int n);
int pedir_numero();
void multiplicar_matriz(int matriz[][limite], int n, int k);
void imprimir_matriz(int matriz[][limite], int n);
int main() {
int matriz[limite][limite];
int n = leer_tamanho(); // Leer y validar tamanho
leer_matriz(matriz, n); // Leer matriz
int k = pedir_numero(); // Pedir numero para multiplicar
multiplicar_matriz(matriz, n, k);// Multiplicar matriz
imprimir_matriz(matriz, n); // Mostrar matriz
return 0;
}
/**
* En C++ al pasar una matriz bidimensional como parámetro,
* el tamanho de todas las dimensiones excepto la primera debe
especificarse,
* porque el compilador necesita saber el "stride" (el tamanho de fila en
memoria)
* para calcular correctamente la dirección de cada elemento.
*
* Por eso usamos [][limite] y NO simplemente [][].
* La primera dimensión (filas) puede quedar vacia porque es manejada
* como puntero a puntero, pero la segunda dimensión (columnas) debe ser
fija.
*/
int leer_tamanho() {
int n;
do {
cout << "Ingrese tamanho n (1 a " << limite << "): ";
cin >> n;
} while (n < 1 || n > limite);
return n;
}
void leer_matriz(int matriz[][limite], int n) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cout << "Elemento [" << i << "][" << j << "]: ";
cin >> matriz[i][j];
}
}
}
int pedir_numero() {
int k;
cout << "Ingrese el numero para multiplicar la matriz: ";
cin >> k;
return k;
}
void multiplicar_matriz(int matriz[][limite], int n, int k) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
matriz[i][j] *= k;
}
}
}
void imprimir_matriz(int matriz[][limite], int n) {
cout << "\nMatriz multiplicada por el numero ingresado:\n";
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cout << matriz[i][j] << "\t";
}
cout << endl;
}
}