May we put int [1][2] in place of [2][3] in the code below if not then why??🙄 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

May we put int [1][2] in place of [2][3] in the code below if not then why??🙄

#include <iostream> using namespace std; int main() { int a [2] [3] = {{1,2,3},{4,5,6}}; cout << a[0] [2]; return 0; }

7th Aug 2019, 2:53 PM
Ravi Henry
Ravi Henry - avatar
2 Answers
+ 3
Hello Ravi, `int a [2] [3] = {{1,2,3},{4,5,6}};` This creates a 2 dimensional array, having 2 rows, with 3 columns at each row. First row {1,2,3} and second row {4,5,6}. Now, if we change that into `int a [1] [2]` it doesn't really make sense, because this way, we'll have a 1 dimensional array having 2 columns. Why not just create a 1 dimensional array in the first place? `int a[2]` will result the same anyways, a 1 dimensional array of 2 columns. I guess you may put it that way (I could be wrong), but it just doesn't really make sense, the subscript operator "[]" indicates dimension of the array; one [] means 1 dimension, two [] means 2 dimensions, etc. Why we want [][] when we can do with [] ?? Another note, you may get an error, if you're not being careful with indexes - when trying to print a certain element from the array <a>, after changing the row & column definition. Hth, cmiiw
7th Aug 2019, 3:44 PM
Ipang
+ 1
Thx
9th Aug 2019, 1:00 AM
Ravi Henry
Ravi Henry - avatar