You are on page 1of 4

//PRACTICA 2014-2 D

/*
PROBLEMA UNICO:
Considere una matriz de 8x5 para nmeros enteros y a travs de un men desarrolle y
ejecute las siguientes opciones:

[1] Generar: genere nmeros enteros aleatorios de 2 cifras, gurdelos en la matrz e


inmediatamente muestre su contenido en forma de tabla.

[2] Intercambia filas: ingresa los nmeros de fila que se quiere intercambiar y luego de
intercambiar dichas filas muestra el nuevo contenido de la matriz en forma de tabla.

[3] Muestra bordes: muestra el contenido de los bordes de la matrz en forma de letra O
cuadrada.

[0] Fin

Criterio
*/

//librerias
#include "stdafx.h"
#include <iostream>
#include <conio.h>
#include <time.h>
#include <math.h>

using namespace std;

//declaro vector de tipo global


#define FILAS 8
#define COLUMNAS 5
int matriz[FILAS][COLUMNAS];

//prototipos
int menu();
void opcion1();
void opcion2();
void opcion3();

void llenar_matriz();
int aleatorio(int max, int min);
void mostrar_matriz();
void intercambio(int a, int b);
void mostrar_bordes();

//funcion principal
void main() {

int seguir;

llenar_matriz();
opcion1();

do{
int opcion;

opcion = menu();
switch (opcion){
case 2:
opcion2();
break;
case 3:
opcion3();
break;

}
cout << endl << "Desea seguir \t SI=[1] NO=[0]" << endl;
cin >> seguir;
}
while (seguir == 1);
}

//desarrollo de los prototipos


int menu(){

int opcion;
cout << "Elija su opcion:" << endl;
cout << "OPCION[2] = Intercambiar filas de la matriz" << endl;
cout << "OPCION[3] = Mostrar los bordes de la matriz" << endl;
cin >> opcion;
cout << endl;

return opcion;
}
void opcion1(){

mostrar_matriz();

}
void opcion2(){

int a, b;

cout << "Ingrese las filas a intercambiar" << endl;


cout << "Fila : "; cin >> a; cout << endl;
cout << "Fila : "; cin >> b; cout << endl;

if (a < b)
intercambio(a, b);
else
intercambio(b, a);
}
void opcion3(){
mostrar_bordes();
}

void llenar_matriz(){

srand(time(NULL));

for (int i = 0; i < FILAS; i = i + 1){


for (int j = 0; j < COLUMNAS; j = j + 1){
matriz[i][j] = aleatorio(99, 10);
}
}

}
int aleatorio(int max, int min){

int x;
x = rand() % (max - min + 1) + min;

return x;
}
void mostrar_matriz(){

cout << "La matriz generada es: " << endl;

for (int i = 0; i < FILAS; i = i + 1){


for (int j = 0; j < COLUMNAS; j = j + 1){
cout << matriz[i][j]<<"\t";
}
cout << endl;
}

}
void intercambio(int a, int b){

int aux[5];

for (int i = 0; i < COLUMNAS; i = i + 1){


aux[i] = matriz[a-1][i];
}
for (int j = 0; j < COLUMNAS; j = j + 1){
matriz[a-1][j] = matriz[b-1][j];
}
for (int k = 0; k < COLUMNAS; k = k + 1){
matriz[b-1][k] = aux[k];
}

cout << "La nueva matriz es:" << endl;

for (int x = 0; x <= a-2; x = x + 1){


for (int y = 0; y < COLUMNAS; y = y + 1){
cout << matriz[x][y] << "\t";
}
cout << endl;
}

for (int c = 0; c < COLUMNAS; c = c + 1){


cout << matriz[a-1][c] << "\t";
}
cout << endl;

for (int d =a ; d <=


b - 2; d = d + 1){
for (int e = 0; e < COLUMNAS; e = e + 1){
cout << matriz[d][e] << "\t";
}
cout << endl;
}
for (int f = 0; f < COLUMNAS; f = f + 1){
cout << matriz[b-1][f] << "\t";
}
cout << endl;

for (int g = b ; g < FILAS; g = g + 1){


for (int h = 0; h < COLUMNAS; h = h + 1){
cout << matriz[g][h] << "\t";
}
cout << endl;
}

}
void mostrar_bordes(){

for (int i = 0; i < COLUMNAS; i = i + 1){


cout << matriz[0][i] << "\t";
}
cout << endl<<endl;

for (int k = 1; k < FILAS - 1; k = k + 1){

cout << matriz[k][0] << "\t";


for (int j = 1; j < COLUMNAS - 1; j = j + 1){
cout << " \t";
}
cout << matriz[k][4] << endl<<endl;
}

for (int i = 0; i < COLUMNAS; i = i + 1){


cout << matriz[7][i] << "\t";
}
}

You might also like