dimanche 2 octobre 2022

Cannot find the error in this code that multiplies matricies, could someone please help me?

#include <iostream>
#include <vector>

using namespace std;


int main() {
    int matrix1_rows;
    int matrix1_columns;
    int matrix2_columns;

    vector<vector<int>> matrix1;
    vector<vector<int>> matrix2;
    vector<vector<int>> output_matrix;

    int current_input;

    cout << "Please specify the total row of your first matrix:" << endl;
    cin >> matrix1_rows;

    cout << "Please specify the total column of your first matrix:" << endl;
    cin >> matrix1_columns;

    for (int i = 0; i < matrix1_rows; i++) {
        vector<int> row;

        for (int j = 0; j < matrix1_columns; i++) {
            cout << "Please specify the element of " << i << " row " << j << " col:" << endl;
            cin >> current_input;
            row.push_back(current_input);
        }

        matrix1.push_back(row);
    }

    cout << "Please specify the total column of your second matrix:" << endl;
    cin >> matrix2_columns;

    for (int i = 0; i < matrix1_columns; i++) {
        vector<int> row;

        for (int j = 0; j < matrix2_columns; j++) {
            cout << "Please specify the element of " << i << " row " << j << " col:" << endl;
            cin >> current_input;
            row.push_back(current_input);
        }

        matrix2.push_back(row);
    }

    for (int i = 0; i < matrix1.size(); i++) {
        vector<int> current_row;

        for (int j = 0; j < matrix2[0].size(); j++) {
            int current_entry = 0;

            for (int k = 0; k < matrix1[0].size(); k++) {
                current_entry += matrix1[i][k]*matrix2[k][j];
            }
            current_row.push_back(current_entry);

        }
        output_matrix.push_back(current_row);

    }

    cout << "The input matrix A is:" << endl;

    for (int i = 0; i < matrix1.size(); i++) {
        for (int j = 0; j < matrix1[i].size(); j++) {
            cout << "   " << matrix1[i][j];
        }
        cout << endl;
    }
    cout << "The input matrix B is:" << endl;

    for (int i = 0; i < matrix2.size(); i++) {
        for (int j = 0; j < matrix2[i].size(); j++) {
            cout << "   " << matrix2[i][j];
        }
        cout << endl;
    }

    cout << "The final matrix is:" << endl;

    for (int i = 0; i < output_matrix.size(); i++) {
        for (int j = 0; j < output_matrix.size(); j++) {
            cout << "   " << output_matrix[i][j];
        }
        cout << endl;
    }

}

The output allows me to put an infinite number of values for a row and won't allow for input into the columns, I have checked many times but I must be missing something stupid.

I think it may be due to an issue with the current_entry or in my for loops a bit later on but I can't be sure. I have tried to adjust both of them but I haven't gotten anything to work

the output should look like this

Please specify the total row of your first matrix: 
2
Please specify the total column of your first matrix: 
2
Please specify the element of 0 row 0 col: 
10
Please specify the element of 0 row 1 col: 
-12
Please specify the element of 1 row 0 col: 
55
Please specify the element of 1 row 1 col: 
74
Please specify the total row of your second matrix: 
2
Please specify the total column of your second matrix: 
2
Please specify the element of 0 row 0 col: 
-1
Please specify the element of 0 row 1 col: 
0
Please specify the element of 1 row 0 col: 
10

however, it looks like this:

Please specify the total row of your first matrix:
2
Please specify the total column of your first matrix:
2
Please specify the element of 0 row 0 col:
2
Please specify the element of 1 row 0 col:
12
Please specify the element of 2 row 0 col:
23
Please specify the element of 3 row 0 col:
23
Please specify the element of 4 row 0 col:
2
Please specify the element of 5 row 0 col:
24
Please specify the element of 6 row 0 col:
24
Please specify the element of 7 row 0 col:

Aucun commentaire:

Enregistrer un commentaire