+ 1
array
Type in a code to declare a multidimensional array with 2 rows and 3 columns. Enter the value using cin for the element in the second column of the second row.
4 Réponses
+ 4
"Type in a code ..."
Very...interesting!
Hold it!... Hold it right there, my dear!
We love to see the code along with your question because we are programmers!
We love to do it if we see your best shot because we need motivation!
We love to appreciate your effort because we know how hard it can be to formulate a solution!
+ 2
By declaring a 2D array as
int arr[2][];
you end up with a compilation error like so
"error: declaration of 'arr' as multidimensional array must have bounds for all dimensions except the first"
It tells you in the case of manual initialization, you are obliged to at least specify the second dimension's size like so
int arr[][2] = {
{1, 2},
{3, 4},
{5, 6}
};
Although if you plan to fill it later on, again, you need to specify both dimensions. For example,
int arr[2][2];
Otherwise, another error pops up like
"error: storage size of 'arr' isn't known"
It's much better to define the arrays' dimensions in separate constant variables like this
const unsigned x = 2;
const unsigned y = x;
and then assign the size to `arr` like this
int arr[x][y];
~~~~~~~~~
For the second part — filling the array — you almost always use a nested loop to do so as
for (unsigned i = 0; i < x; ++i)
for (unsigned j = 0; j < x; ++j)
cin >> arr[i][j];
unless you want to make your code lengthy like this
cin >> arr[0][0];
cin >> arr[0][1];
cin >> arr[1][0];
cin >> arr[1][1];
Internal scheme of the array:
+####+####+####+####+
| [0][0] | [0][1] | [1][0] | [1][1] |
+####+####+####+####+
+ 1
Hey Manizha Ezadyaar, I suppose your question is about one of the quizzes in this lesson:
https://www.sololearn.com/learn/CPlusPlus/1629/
Rather than just asking for a solution, you should ask for what you don't understand, what disables you from solving the problem on your own. Having the answer presented won't bring you anywhere, however, if you really want to unlock it, you can always use some XP of yours to skip the question or to get a hint.
So try going through the lesson again, often the answer to a quiz can be found right there, and if you still can't get the right solution, ask about what you don't understand. 😊
0
i just stuck in this question what comes in the blank
as the method is in the above
int arr[2][];
cin >> arr[ ][ ];