-1

I'm trying to input a 2D array in c++ but my code doesn't even get to calculation lines:

int dim1, dim2;
cout << "Enter first dimension: ";
cin >> dim1;
cout << endl << "Enter second dimension: ";
cin >> dim2;
int arr[dim1][dim2];
for (int i = 1; i <= dim1; i++) {
    for (int j = 1; j <= dim2; j++) {
        cin >> arr[i][j];
    }
}
for (int i = 1; i <= dim1; i++) {
    for (int j = 1; j <= dim2; j++) {
        cout << arr[i][j] << ' ';
    }
    cout << endl;
}
  • 2
    Note that `int arr[dim1][dim2];` when `dim1` and `dim2` are not constant expressions is not really valid C++. It's only available as an extension is some compilers. It's called a _Variable Length Array_ or _VLA_ for short. Use `std::vector`s instead. – Ted Lyngmo Nov 26 '21 at 14:35

3 Answers3

1

It's simple, you should start inputing an array in c++ from 0, so your loop should begin with i = 0 and should be stopped when i == n.

for (int i = 0; i < dim1; i++) {
    for (int j = 0; j < dim2; j++) {
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
Erfan
  • 11
  • 1
1

Your code has two problems.

First, VLA arrays is not valid C++ code. You have to use std::vector.

Second. C++ array indexes start at 0 and end at size - 1. But you are looping from 1 to n. This creates out-of-bounds.

Your code should look like this:

int dim1, dim2;
std::cout << "Enter first dimension: ";
std::cin >> dim1;
std::cout << endl << "Enter second dimension: ";
std::cin >> dim2;
std::vector<std::vector<int>> arr;
arr.resize(dim1, std::vector<int>(dim2));
for (int i = 0; i < dim1; i++) {
    for (int j = 0; j < dim2; j++) {
        std::cin >> arr[i][j];
    }
}
for (int i = 0; i < dim1; i++) {
    for (int j = 0; j < dim2; j++) {
        std::cout << arr[i][j] << ' ';
    }
    std::cout << endl;
}
  • Instead of creating the vector and then `resize` it, you could create it with the proper dimensions directly: `std::vector> arr(dim1, std::vector(dim2));` Anyway, +1 – Ted Lyngmo Nov 26 '21 at 14:50
1

First in C++, the size of an array must be a compile-time constant.So, take for example the following code snippets:

int n = 10;
int arr[n]; //INCORRECT because n is not a constant expression

The correct way to write the above would be:

const int n = 10;
int arr[n]; //CORRECT

Similarly, the following(which you did in your code example) is incorrect:

    int dim1, dim2;
    cout << "Enter first dimension: "; 
    cin >> dim1;
    cout << endl << "Enter second dimension: ";
    cin >> dim2;
    int arr[dim1][dim2];//INCORRECT

You should instead use std::vector<> as shown below:

#include <iostream>
#include <vector> 

int main()
{
    int dim1, dim2;
    std::cout << "Enter first dimension: ";
    std::cin >> dim1;
    std::cout << std::endl << "Enter second dimension: ";
    std::cin >> dim2;
    std::vector<std::vector<int>> arr(dim1, std::vector<int>(dim2));
    
    for (int i = 0; i < dim1; i++) {
        for (int j = 0; j < dim2; j++) {
            std::cin >> arr[i][j];
        }
    }
    for (int i = 0; i < dim1; i++) {
        for (int j = 0; j < dim2; j++) {
            std::cout << arr[i][j] << ' ';
        }
        std::cout << std::endl;
    }

    return 0;
}

Jason
  • 36,170
  • 5
  • 26
  • 60