C/Visual C - Se puede convertir de C++ a Pesl

 
Vista:

Se puede convertir de C++ a Pesl

Publicado por KEYNER GABRIEL (1 intervención) el 28/03/2023 20:58:41
#include<iostream>
using namespace std;

const int N = 3; // define dimensions of the matrices

void inputMatrix(int matrix[][N])
{
// Prompt user to input each element of the matrix
for (int i = 0; i < N; ++i)
{
for (int j = 0; j < N; ++j)
{
cout << "Enter element [" << i+1 << "][" << j+1 << "]: ";
cin >> matrix[i][j];
}
}
}

void printMatrix(int matrix[][N])
{
// Display the matrix with the new line character
for (int i = 0; i < N; ++i)
{
for (int j = 0; j < N; ++j)
{
cout << matrix[i][j] << " ";
}
cout << endl;
}
}

void multiplyMatrices(int matrix1[][N], int matrix2[][N], int result[][N])
{
// Multiply the two matrices and store the result in the result array
for (int i = 0; i < N; ++i)
{
for (int j = 0; j < N; ++j)
{
result[i][j] = 0;
for (int k = 0; k < N; ++k)
{
result[i][j] += matrix1[i][k] * matrix2[k][j];
}
}
}
}

void confirmOrRetry()
{
cout << "Enter [y] to confirm or [n] to retry." << endl;
char choice;
cin >> choice;

if (choice == 'n')
{
system("CLS"); // Clear the console window
int main(); // Restart the program
}
}

int main()
{
int matrix1[N][N], matrix2[N][N], result[N][N];
char choice;

do
{
system("CLS");
cout << "*** Enter Matrix 1 ***" << endl;
inputMatrix(matrix1);
cout << endl;
printMatrix(matrix1);

cout << endl << "*** Enter Matrix 2 ***" << endl;
inputMatrix(matrix2);
cout << endl;
printMatrix(matrix2);

cout << endl << "*** Confirm matrices? ***" << endl;
confirmOrRetry();

} while (choice == 'n');

multiplyMatrices(matrix1, matrix2, result);

cout << endl << "*** Resulting Matrix ***" << endl;
printMatrix(result);

return 0;
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder