-1

I just started c++

here is the code for a 2D array and storing the values row-wise. The first loop is for row index and 2nd loop for column index, also I'm getting errors in the arr[x][y] and arr[col]

#include <iostream>
using namespace std;

int main()
{
    int x, y;
    int arr[x][y];
    cout << "Enter row number and column number :";
    cin >> x >> y;

    int row, col;
    cout << "Enter value of array\n";

    for (int row = 0; row < x; ++row)
    {
        for (int col = 0; col < y; ++col)
        {
            cin >> arr[row][col] << " ";
        }
    }

    cout << "Value of array are:\n";
    for (row = 0; row < x; row++)
    {
        for (col = 0; col < y; col++)
        {
            cout << arr[row] << arr[col] << " ";
        }
        cout << "\n";
    }
    return 0;
}
user4581301
  • 33,082
  • 7
  • 33
  • 54
Lucas
  • 1
  • 1
  • Array bounds in C++ have to be constant expressions (that is, compiletime constants). `x` and `y` are not constant expressions. If you need an array the size of which is determined at runtime, you will have to resort to dynamic memory allocation. std::vector maybe what you're looking for… – Michael Kenzel Nov 22 '19 at 21:36
  • 2
    Some compilers do allow variables `x` and `y` to be used as array dimensions. It's non Standard, I don't recommend it, and often it's dangerous, but it is sometimes an option. I recommend using something like [what is outlined in this linked answer](https://stackoverflow.com/a/2076668/4581301). Regardless of how you define the matrix, it is strongly recommended that you only use `x` and `y` AFTER they are given values by the user. – user4581301 Nov 22 '19 at 21:39
  • Recommendation: When you have errors, get a text version of them and add them to the question so that potential answers know exactly what you are dealing with. Prefer to make question that deal with one problem at a time by removing everything that is not related to the problem from the program. If you make a program that does nothing but cause the error, it's much easier for you or anyone else to answer the question. Very often it's so easy that you don't even need to ask the question. – user4581301 Nov 22 '19 at 21:48
  • A side note: In addition to compiler errors you will also get compiler warnings. While warnings do not prevent the program from compiling, do not ignore them as they are the compiler trying to tell you that your logic is suspect. Do your best to understand and resolve the warning. Failure to resolve warnings often leads to the program failing to execute as intended, so you might as well fix the problem before wasting time debugging. – user4581301 Nov 22 '19 at 21:52
  • `cin >> arr[row][col] << " ";` contains both input and output. One cannot `<<` and `>>` at the same time. Clarify your intent here and odds are good someone can explain how best to fix it. – user4581301 Nov 22 '19 at 21:55

1 Answers1

0

Problem 1: Order Matters.

int x, y; // x and y have undefined values
int arr[x][y]; // attempts to dimension an array with undefined values.
cout << "Enter row number and column number :";
cin >> x >> y; // now we have x and y values. 

Solution:

int x, y; // x and y have undefined values
cout << "Enter row number and column number :";
cin >> x >> y; // now we have x and y values. 
int arr[x][y]; // can now dimension array

Problem 2: Non-Standard code

Arrays can only be dimensioned with constant values in Standard C++

int arr[x][y]; // fails on many compilers. 
               // Don't be angry, they are following the language rules.

There are many reasons for this. The biggest, in my opinion, is they make it hard to control the amount of Automatic Memory being used by the program. Enter 2000 for x and y and watch the program fail in interesting and often bizarre ways.

Solution:

Use a std::vector here.

vector<vector<int>> arr(x, vector<int>(y));

Note:

vectors of vectors have poor performance due to low spatial locality. Each vector contains its own block of storage located somewhere in dynamic memory, and every time the program moves from one vector to another, the new vector's data must be looked up and loaded.

If this is a concern, use a single vector approach like the one described here.

Problem 3: Writing to an input stream

cin >> arr[row][col] << " ";

reads from cin into arr[row][col] and then attempts to write" "to the stream.cin` is input-only and cannot be written to.

Solution:

Since it's likely you do not need this write at all, >> automatically separates tokens based on whitespace separation, we can safely discard the write.

cin >> arr[row][col];

Problem 4: Writing arrays rather than the data in the array

cout << arr[row] << arr[col] << " ";

writes arr[row] a row in the array rather than a single data value to cout. It then writes arr[col], another row in the array, to cout.

Solution:

to write the value at arr[row][col], we need to do literally that

cout << arr[row][col] << " ";

I think that about covers it.

Recommended reading

Why is "using namespace std;" considered bad practice?

A good C++ reference text. Whatever you are currently learning from is not doing you any favours.

user4581301
  • 33,082
  • 7
  • 33
  • 54