Documentos de Académico
Documentos de Profesional
Documentos de Cultura
APURIMAC
EP: INGENIERIA INFORMÁTICA Y SISTEMAS
GUÍA DE LABORATORIO
Asignatura: Fundamentos de programación
2021-I Docente: Mg. Rafael Ricardo Quispe Merma
12 1 0 10 11
2 11 20 1 21
3 15 2 14 12
15 0 1 1 1
1 1 1 1 1
En este caso se esta considerado un arreglo bidimensional M, donde el primer subíndice representa
las filas y el segundo subíndice representa las columnas.
Cada elemento del arreglo se puede manipular como una variable independiente, siempre y cuando
se acompañe de sus dos subíndices. Por ejemplo si en el elemento de la fila 4 y la columna 4 se
desea almacenar el valor 5,la instrucción es:
M4,4 ←5
Similar a los arreglos unidimensionales, en los arreglos bidimensionales también se puede utilizar
como subíndices variables o expresiones aritméticas cuyo resultado sea un valor entero.
Para leer los datos de una matriz sobre un arreglo bidimensional, se debe efectuar la lectura fila a
fila, mediante dos estructuras repetitivas anidadas; una para efectuar la lectura de los elementos
de una fila y la otra para leer todas las filas. Si se considera una matriz M5×5 , el código para leer
los elementos de esta matriz es:
{ Leer 5 filas de elementos }
1
UNIVERSIDAD NACIONAL MICAELA BASTIDAS DE
APURIMAC
EP: INGENIERIA INFORMÁTICA Y SISTEMAS
GUÍA DE LABORATORIO
Asignatura: Fundamentos de programación
2021-I Docente: Mg. Rafael Ricardo Quispe Merma
EscribirMatriz(matriz);system("pause");break;
}
} while (opcion!=3);
system("pause");
2
UNIVERSIDAD NACIONAL MICAELA BASTIDAS DE
APURIMAC
EP: INGENIERIA INFORMÁTICA Y SISTEMAS
GUÍA DE LABORATORIO
Asignatura: Fundamentos de programación
2021-I Docente: Mg. Rafael Ricardo Quispe Merma
int MostrarMenu(){
int opc;
system("cls");
cout<<"******MENU**************"<<endl;
cout<<"*[1]Leer Matriz *"<<endl;
cout<<"*[2]Mostrar Matriz *"<<endl;
cout<<"*[3]Fin *"<<endl;
cout<<"*************************"<<endl;
cout<<"Elija opcion: ";
cin>>opc;
return opc;
}
void LeerMatriz(int m[3][5]){
for(int f=0;f<3;f++)
for(int c=0;c<5;c++){
cout<<"Ingrese el elemento "<<f+1<<","<<c+1<<":";
cin>>m[f][c];
}
}
void EscribirMatriz(int m[3][5]){
for(int f=0;f<3;f++){
for(int c=0;c<5;c++){
cout<<m[f][c]<<" ";
}
cout<<endl;
}
}
Problema 2:
Crear y cargar una matriz de 4 filas por 4 columnas. Imprimir la diagonal principal y su suma.
x - - -
- x - -
- - x -
- - - x
#include<iostream>
using namespace std;
int MostrarMenu(void);
void LeerMatriz(int m[4][4]);
3
UNIVERSIDAD NACIONAL MICAELA BASTIDAS DE
APURIMAC
EP: INGENIERIA INFORMÁTICA Y SISTEMAS
GUÍA DE LABORATORIO
Asignatura: Fundamentos de programación
2021-I Docente: Mg. Rafael Ricardo Quispe Merma
EscribirMatriz(matriz);system("pause");break;
case 3:
system("cls");
cout<<"MOSTRAR DIAGONAL PRINCIPAL "<<endl;
//escribir matriz
MostrarDiagonal(matriz);system("pause");break;
case 4:
system("cls");
cout<<"SUMA DIAGONAL PRINCIPAL"<<endl;
//escribir matriz
SumaDiagonal(matriz);system("pause");break;
}
} while (opcion!=5);
system("pause");
}
int MostrarMenu(){
int opc;
system("cls");
cout<<"******MENU**************"<<endl;
cout<<"*[1]Leer Matriz *"<<endl;
4
UNIVERSIDAD NACIONAL MICAELA BASTIDAS DE
APURIMAC
EP: INGENIERIA INFORMÁTICA Y SISTEMAS
GUÍA DE LABORATORIO
Asignatura: Fundamentos de programación
2021-I Docente: Mg. Rafael Ricardo Quispe Merma
cin>>opc;
return opc;
}
void LeerMatriz(int m[4][4]){
for(int f=0;f<4;f++)
for(int c=0;c<4;c++){
cout<<"Ingrese el elemento "<<f+1<<","<<c+1<<":";
cin>>m[f][c];
}
}
void EscribirMatriz(int m[4][4]){
for(int f=0;f<4;f++){
for(int c=0;c<4;c++){
cout<<m[f][c]<<" ";
}
cout<<endl;
}
}
void MostrarDiagonal(int m[4][4]){
cout<<"Los elementos de la diagonal principal son:"<<endl;
for(int f=0;f<4;f++){
for(int c=0;c<4;c++){
if(f==c)
cout<<m[f][c]<<" ";
}
}
cout<<endl;
}
void SumaDiagonal(int m[4][4]){
int suma=0;
for(int f=0;f<4;f++){
for(int c=0;c<4;c++){
if(f==c)
suma=suma+m[f][c];
5
UNIVERSIDAD NACIONAL MICAELA BASTIDAS DE
APURIMAC
EP: INGENIERIA INFORMÁTICA Y SISTEMAS
GUÍA DE LABORATORIO
Asignatura: Fundamentos de programación
2021-I Docente: Mg. Rafael Ricardo Quispe Merma
}
}
cout<<"La suma de la diagonal principal es:"<<suma<<endl;
}
Problema 3:
Crear y cargar una matriz de 3 filas por 4 columnas. Imprimir la primera fila. Imprimir la última
fila e imprimir la primer columna.
#include<iostream>
using namespace std;
int MostrarMenu(void);
void LeerMatriz(int m[4][4]);
void EscribirMatriz(int m[4][4]);
void MostrarFila(int m[3][4]);
main(){
//declarar las variables
int matriz[3][4],opcion;
do{
opcion=MostrarMenu();
switch(opcion){
case 1://Leer matriz
system("cls");
cout<<"LEER MATRIZ"<<endl;
LeerMatriz(matriz);break;
case 2:
system("cls");
cout<<"ESCRIBIR MATRIZ"<<endl;
//escribir matriz
EscribirMatriz(matriz);system("pause");break;
case 3:
system("cls");
cout<<"MOSTRAR FILAS "<<endl;
//escribir matriz
MostrarFila(matriz);system("pause");break;
}
} while (opcion!=4);
system("pause");
6
UNIVERSIDAD NACIONAL MICAELA BASTIDAS DE
APURIMAC
EP: INGENIERIA INFORMÁTICA Y SISTEMAS
GUÍA DE LABORATORIO
Asignatura: Fundamentos de programación
2021-I Docente: Mg. Rafael Ricardo Quispe Merma
int MostrarMenu(){
int opc;
system("cls");
cout<<"******MENU**************"<<endl;
cout<<"*[1]Leer Matriz *"<<endl;
cout<<"*[2]Mostrar Matriz *"<<endl;
cout<<"*[3]Mostrar Filas *"<<endl;
cout<<"*[4]Fin *"<<endl;
cout<<"*************************"<<endl;
cout<<"Elija opcion: ";
cin>>opc;
return opc;
}
void LeerMatriz(int m[3][4]){
for(int f=0;f<3;f++)
for(int c=0;c<4;c++){
cout<<"Ingrese el elemento "<<f+1<<","<<c+1<<":";
cin>>m[f][c];
}
}
void EscribirMatriz(int m[3][4]){
for(int f=0;f<3;f++){
for(int c=0;c<4;c++){
cout<<m[f][c]<<" ";
}
cout<<endl;
}
}
void MostrarFila(int m[3][4]){
cout<<"Los elementos de la fila 1 son:"<<endl;
for(int f=0;f<3;f++){
for(int c=0;c<4;c++){
if(f==1)
cout<<m[f][c]<<" ";
}
}
cout<<endl;
cout<<"Los elementos de la fila 4 son:"<<endl;
for(int f=0;f<3;f++){
7
UNIVERSIDAD NACIONAL MICAELA BASTIDAS DE
APURIMAC
EP: INGENIERIA INFORMÁTICA Y SISTEMAS
GUÍA DE LABORATORIO
Asignatura: Fundamentos de programación
2021-I Docente: Mg. Rafael Ricardo Quispe Merma
for(int c=0;c<4;c++){
if(f==2)
cout<<m[f][c]<<" ";
}
}
cout<<endl;
cout<<"Los elementos de la columna 1 son:"<<endl;
for(int f=0;f<3;f++){
for(int c=0;c<4;c++){
if(c==0)
cout<<m[f][c]<<" ";
}
}
cout<<endl;
}
Problema propuesto
Crear una matriz de 2 filas y 5 columnas. Realizar la carga de componentes por columna (es
decir primero ingresar toda la primer columna, luego la segunda columna y así sucesivamente)
Imprimir luego la matriz.