Adição de Duas Matrizes

C++Beginner
Pratique Agora

Introdução

Neste laboratório, vamos escrever um programa em C++ para somar duas matrizes usando arrays 2D.

Criar o arquivo C++

Crie um arquivo C++ chamado add_matrices.cpp dentro do diretório ~/project. Este será nosso arquivo principal onde escreveremos o código.

cd ~/project
touch add_matrices.cpp

Escrever o código

Copie o código abaixo e cole-o no arquivo add_matrices.cpp.

#include <iostream>

using namespace std;

int main() {
    cout << "\n\nWelcome to the Add Matrices Program\n\n\n";

    // Initialize variables
    int row, col, i, j;

    // Declare the three matrices (2D arrays)
    int m1[10][10], m2[10][10], sum[10][10];

    // Get the number of rows and columns of matrix from user
    cout << "\nEnter the number of Rows and Columns of matrix : ";
    cin >> row >> col;

    // Get the elements of the first matrix from user
    cout << "\nEnter the " << row * col << " elements of first matrix : \n";
    for (i = 0; i < row; i++) {
        for (j = 0; j < col; j++) {
            cin >> m1[i][j];
        }
    }

    // Get the elements of the second matrix from user
    cout << "\nEnter the " << row * col << " elements of second matrix : \n";
    for (i = 0; i < row; i++) {
        for (j = 0; j < col; j++) {
            cin >> m2[i][j];
        }
    }

    // Calculate the sum matrix
    for (i = 0; i < row; i++) {
        for (j = 0; j < col; j++) {
            sum[i][j] = m1[i][j] + m2[i][j];
        }
    }

    // Display the matrices
    cout << "\n\nThe first matrix is : \n";
    for (i = 0; i < row; i++) {
        for (j = 0; j < col; j++) {
            cout << m1[i][j] << "  ";
        }
        cout << endl;
    }

    cout << "\n\nThe second matrix is : \n";
    for (i = 0; i < row; i++) {
        for (j = 0; j < col; j++) {
            cout << m2[i][j] << "  ";
        }
        cout << endl;
    }

    cout << "\n\nThe Sum matrix is : \n";
    for (i = 0; i < row; i++) {
        for (j = 0; j < col; j++) {
            cout << sum[i][j] << "  ";
        }
        cout << endl;
    }

    return 0;
}

Compilar e Executar o código

Para compilar o código, abra o terminal no diretório ~/project e execute o seguinte comando:

g++ add_matrices.cpp -o add_matrices

Para executar o programa, digite o seguinte comando no terminal:

./add_matrices

Isso executará o programa e exibirá o resultado da adição de duas matrizes.

Verificar a saída

Após executar o programa, você deverá ver a saída como abaixo:

Welcome to the Add Matrices Program

Enter the number of Rows and Columns of matrix : 2 2

Enter the 4 elements of first matrix :
1 2 3 4

Enter the 4 elements of second matrix :
5 6 7 8


The first matrix is :
1  2
3  4


The second matrix is :
5  6
7  8


The Sum matrix is :
6  8
10  12

Resumo

Neste laboratório, aprendemos como adicionar duas matrizes usando arrays 2D na linguagem de programação C++. Criamos um programa que recebe duas matrizes como entrada, soma os elementos correspondentes das duas matrizes e exibe a matriz soma.