Lenguaje de programación
SEMANA 13
Ejercicio en clase 1 y practica terminada (PRODUCTO) :
# include <math.h>
# include <cstdlib>
using namespace std;
int opcion = 0, i, j;
int A[3][3], B[3][3], C[3][3];
void ASIGNAR();
void MOSTRAR();
void PROMEDIO();
void SUMARMATRICES();
void PRODUCTO();
int main()
{
do
{
cout << " M E N U P R I N C I P A L D E M A T R I C E S\n "<< endl;
cout << " ----------------------------"<< endl;
cout << " 1.- ASIGNAR VALORES A LA MATRIZ\n "<< endl;
cout << " 2.- MOSTRAR LOS ELEMENTOS DE LA MATRIZ\n "<< endl;
cout << " 3.- PROMEDIO DE LOS ELEMENTOS\n "<< endl;
cout << " 4.- SUMA DE DOS MATRICES\n "<< endl;
cout << " 5.- PRODUCTO DE 2 MATRICES\n "<< endl;
cout << " 0.- SALIR\n "<< endl;
cout << "\n INGRESE UNA OPCION <> 0 : "; cin >> opcion;
cout << " ----------------------------"<< endl;
switch (opcion)
{
case 1:
cout << " 1.- ASIGNAR VALORES A LA MATRIZ "<< endl;
cout << " ----------------------------"<< endl;
ASIGNAR();
cout << " ----------------------------"<< endl;
break;
case 2:
cout << " 2.- MOSTRAR LOS ELEMENTOS DE LA MATRIZ "<< endl;
cout << " ----------------------------"<< endl;
MOSTRAR();
cout << "\n ----------------------------"<< endl;
break;
case 3:
cout << " 3.- PROMEDIO DE LOS ELEMENTOS "<< endl;
cout << " ----------------------------"<< endl;
PROMEDIO();
cout << " ----------------------------"<< endl;
break;
case 4:
cout << " 4.- SUMA DE DOS MATRICES "<< endl;
cout << " ----------------------------"<< endl;
SUMARMATRICES();
cout << " ----------------------------"<< endl;
break;
case 5:
cout << " 5.- PRODUCTO DE 2 MATRICES "<< endl;
cout << " ----------------------------"<< endl;
PRODUCTO();
cout << " ----------------------------"<< endl;
break;
case 0:
cout << "Saliendo..." << endl;
break;
default:
cout << "Opción no válida. Inténtelo de nuevo." << endl;
}
} while (opcion != 0);
return 0;
}
void ASIGNAR()
{
cout << "LLENAR DE ELEMENTOS DE LA MATRIZ A[3][3]" << endl;
cout << " ----------------------------"<< endl;
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++)
{
A[i][j] = rand() % 20;
}
}
void MOSTRAR()
{
cout << "MOSTRAR LOS ELEMENTOS DE LA MATRIZ A[3][3]" << endl;
cout << " ----------------------------"<< endl;
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++)
{
cout << " A[" << i << "][" << j << "] = " << A[i][j] << endl;
}
}
void PROMEDIO()
{
float sumar = 0;
cout << "PROMEDIO DE LOS ELEMENTOS DE LA MATRIZ A[3][3]" << endl;
cout << " ----------------------------"<< endl;
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++)
{
sumar += A[i][j];
}
cout << "PROMEDIO DE LOS ELEMENTOS DE LA MATRIZ A[3][3] = " << sumar/9 << endl;
}
void SUMARMATRICES()
{
cout << "LLENADO DE ELEMENTOS DE LA MATRIZ A[3][3] y B[3][3]" << endl;
cout << " ----------------------------"<< endl;
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++)
{
A[i][j] = rand() % 20;
B[i][j] = rand() % 20;
}
cout << endl;
cout << "MOSTRAR LOS ELEMENTOS DE LA MATRIZ A[3][3]" << endl;
cout << " ----------------------------"<< endl;
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++)
{
cout << " A[" << i << "][" << j << "] = " << A[i][j] << endl;
}
cout << endl;
cout << "MOSTRAR LOS ELEMENTOS DE LA MATRIZ B[3][3]" << endl;
cout << " ----------------------------"<< endl;
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++)
{
cout << " B[" << i << "][" << j << "] = " << B[i][j] << endl;
}
cout << endl;
cout << "SUMA DE MATRICES: A[3][3] + B[3][3]" << endl;
cout << " ----------------------------"<< endl;
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++)
{
C[i][j] = A[i][j] + B[i][j];
}
cout << endl;
cout << "RESULTADO DE LA SUMA DE MATRICES: C[3][3]" << endl;
cout << " ----------------------------"<< endl;
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++)
{
cout << " C[" << i << "][" << j << "] = " << C[i][j] << endl;
}
}
void PRODUCTO()
{
cout << "PRODUCTO DE LAS MATRICES A[3][3] y B[3][3]" << endl;
cout << " ----------------------------"<< endl;
int producto[3][3]; // Matriz para almacenar el resultado del producto
// Calcular el producto de las matrices A y B
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
producto[i][j] = 0;
for (int k = 0; k < 3; k++) {
producto[i][j] += A[i][k] * B[k][j];
}
}
}
// Mostrar el resultado del producto
cout << "RESULTADO DEL PRODUCTO: C[3][3]" << endl;
cout << " ----------------------------"<< endl;
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++)
{
cout << " C[" << i << "][" << j << "] = " << producto[i][j] << endl;
}
}
}