'Printing Matrix Only Returning Bottom Line

I'm trying to get my program to input a matrix with both negative and positive values and I want it to return the same matrix but with the absolute value of all negatives. I first input a number for rows and a number for columns. Then I input the rest of the matrix. Below is the code.

// input: 
// 5 5
// 22 62 -39 -15 37
// -34 95 -85 26 -57
// 8 33 -36 69 -4
// -36 -55 -92 96 -70
// 79 -93 -42 -44 66

#include <iostream>
#include <cmath>
using namespace std;

int main() {
    int row, col, val;
    int arr[row][col];
    cin >> row;
    cin >> col; 

    for (int i=0; i<row; i++) {
        for (int j=0; j<col; j++) {
            cin >> val;
            arr[i][j] = abs(val);
        }
    }

    // for (int i=0; i<col; i++) {
    //     for (int j=0; j<row; j++) {
    //         std::cout << arr[i][j];
    //     }
    // }

    for (int i=0; i<col; i++) {
        for (int j=0; j<row; j++) {
            cout<<arr[i][j]<<" ";
        }
        cout<<"\n";
    }
    return 0;
}

When I try to print the matrix, it only prints the last line and I'm not sure how to fix this. I believe there is a problem with the printing, but I'm not completely sure if the input code is correct.

c++


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source