0

I want to create a program that take size of rows and columns of 2D array from user, and then take all entries of array from user. And Finally display all the entries from array[0][0] to array[size][size].

My code is:

#include <iostream>
using namespace std;

int main()
{
    int rows, columns;
    int tom[rows][columns];
    cout << "Size of array rows: ";
    cin >> rows;
    cout << "Size of array columns: ";
    cin >> columns;

    for(int count1 = 0; count1 < rows; count1++)
    {
        for(int count2 = 0; count2 < columns; count2++)
        {
            cout << "Enter entry of row " << count1 << " and column " << count2 << ": ";
            cin >> tom[count1][count2];
        }
    }
    for(int i = 0; i < rows; i++)
    {
        for(int j = 0; j < columns; j++)
        {
            cout << tom[i][j] << endl;
        }
    }
    return 0;
}

Output is:

Size of array rows: 2
Size of array columns: 3
Enter entry of row 0 and column 0: 1
Enter entry of row 0 and column 1: 2
Enter entry of row 0 and column 2: 3
Enter entry of row 1 and column 0: 12
Enter entry of row 1 and column 1: 13
Enter entry of row 1 and column 2: 14
12
13
14
12
13
14

It should give output:

1
2
3
12
13
14

What is the problem? Please help.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

3 Answers3

2

You can't dynamically create an array like this. This shouldn't even compile. And even if it did, you are creating the array before letting the user input the dimension. For the proper Approach use std::vector:

#include <iostream>
#include <vector>

int main()
{
    int rows, columns;
    std::cout << "Size of array rows: ";
    std::cin >> rows;
    std::cout << "Size of array columns: ";
    std::cin >> columns;
    std::vector<std::vector<int>> tom(rows, std::vector<int>(columns));

    for (int count1 = 0; count1 < rows; count1++)
    {
        for (int count2 = 0; count2 < columns; count2++)
        {
            std::cout << "Enter entry of row " << count1 << " and column " << count2 << ": ";
            std::cin >> tom[count1][count2];
        }
    }
    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < columns; j++)
        {
            std::cout << tom[i][j] << std::endl;
        }
    }
    return 0;
}

output:

Size of array rows: 2
Size of array columns: 3
Enter entry of row 0 and column 0: 1
Enter entry of row 0 and column 1: 2
Enter entry of row 0 and column 2: 3
Enter entry of row 1 and column 0: 12
Enter entry of row 1 and column 1: 13
Enter entry of row 1 and column 2: 14
1
2
3
12
13
14

please don't use using namespace std; - read here why.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Stack Danny
  • 7,754
  • 2
  • 26
  • 55
  • Thanks for the answer. **Your Words: **_You can't dynamically create an array like this. This shouldn't even compile. And even if it did, you are creating the array before letting the user input the dimension._ made my program. I just put the array `tom[rows][coloumns]` after the `cin >> coloumns;` and now it is working fine. – Abdul Raheem Jan 30 '19 at 15:08
  • @AbdulRaheem what compiler are you using? this shouldn't really work unless there are some hidden _alloca() sort of hings going on. – Stack Danny Jan 30 '19 at 15:13
  • Actually I'm at school level and I'm using dev cpp. – Abdul Raheem Jan 30 '19 at 15:15
  • @AbdulRaheem i am saying it, because normally this doesn't work. `tom` wants to be a fixed-size array, allocated on the stack. you can't dynamically allocate an array on the stack. `E0028 expression must have a constant value` - you will have to make a dynamically allocated array - on the heap. this can be done with `int** tom` or with (the modern alternative) `std::vector>`. Your code might work for now, but once you run it else-where it will fail. It's just not a good practise to be stuck with These dependencies. When you want to improve, it's best to stick with good practises. – Stack Danny Jan 30 '19 at 15:17
  • Okay sir. I'll be carefull next time. ☺ – Abdul Raheem Jan 30 '19 at 15:26
  • 1
    @StackDanny, *This shouldn't even compile.* --> eh! surprise surprise. "variable length arrays" (VLAs) are not part of standard C++, yes that is true. They were in C99, however. I know, I know this question is marked C++. Both `gcc` and `clang` compilers have implemented compiler-extensions, that allow VLAs. I don't know what `dev-cpp` uses as a compiler under the hood, but probably it is `mingw a.k.a. gcc`. – Duck Dodgers Jan 30 '19 at 15:27
  • @JoeyMallone yes I heard of that and I have used dev-cpp before but I never thought that it would allow something like that! thanks for the Information. – Stack Danny Jan 30 '19 at 15:29
  • 1
    @StackDanny, I knew gcc had that extension. I didn't know that clang also has that extension. It was surprising for me also, when it compiled on my system with both compilers. – Duck Dodgers Jan 30 '19 at 15:30
1

You initialized tom before actually getting the number of rows/columns.

#include <iostream>

int main()
{
    int rows, columns;
    std::cout << "Rows: ";
    std::cin >> rows;
    std::cout << "Columns: ";
    std::cin >> columns;
    int arr[rows][columns];

    for(int i = 0; i < rows; i++)
    {
        for(int j = 0; j < columns; j++)
        {
            std::cout << "Enter the value for [" << i << "][" << j << "] : ";
            std::cin >> arr[i][j];
        }
    }

    for(int i = 0; i < rows; i++)
    {
        for(int j = 0; j < columns; j++)
        {
            std::cout << arr[i][j] << " ";
        }
        std::cout << std::endl;
    }

    return 0;
}
heunetik
  • 567
  • 1
  • 8
  • 21
0

You cannot declare an array like this:

    int tom[rows][columns];

since values of rows and columns are not known yet.

Instead you should initialize this array dynamically after asking for values. Here is your code fixed:

#include <iostream>
using namespace std;

int main()
{
    int rows, columns;
    int **tom;
    cout << "Size of array rows: ";
    cin >> rows;
    cout << "Size of array columns: ";
    cin >> columns;

    tom = new int*[rows];
    for (int i = 0; i < rows; i++) {
      tom[i] = new int[columns];
    }

    for(int count1 = 0; count1 < rows; count1++)
    {
        for(int count2 = 0; count2 < columns; count2++)
        {
            cout << "Enter entry of row " << count1 << " and column " << count2 << ": ";
            cin >> tom[count1][count2];
        }
    }

    for(int i = 0; i < rows; i++)
    {
        for(int j = 0; j < columns; j++)
        {
            cout << tom[i][j] << endl;
        }
    }
    return 0;
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Karol Samborski
  • 2,757
  • 1
  • 11
  • 18