Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two-dimensional array initialization values

Tags:

c++

arrays

I'm working on a program using arrays and I'm trying to figure out

First, with the following array declaration, what is the value stored in the scores[2][2] element?

int scores[3][3] = { {1, 2, 3} };

And also with this array declaration, what is the value stored in the scores[2][3] element?

int scores[5][5] = {5};

Could someone please explain this for me.

like image 415
michael m Avatar asked Dec 02 '14 11:12

michael m


People also ask

How do you initialize a 2D array value?

Here is how we can initialize a 2-dimensional array in Java. int[][] a = { {1, 2, 3}, {4, 5, 6, 9}, {7}, }; As we can see, each element of the multidimensional array is an array itself.

How do you initialize a two-dimensional character array?

A 2D character array is declared in the following manner: char name[5][10]; The order of the subscripts is important during declaration. The first subscript [5] represents the number of Strings that we want our array to contain and the second subscript [10] represents the length of each String.

How do you initialize a 2D?

Two-dimensional arrays may be initialized by specifying bracketed values for each row. int [,] a = new int [4,4] { {0, 1, 2, 3} , {4, 5, 6, 7} , {8, 9, 10, 11} , {12, 13, 14, 15} }; The following is an example showing how to work with two-dimensional arrays in C#.

What is 2D array How is a 2D array initialize?

The 2D array is organized as matrices which can be represented as the collection of rows and columns. However, 2D arrays are created to implement a relational database lookalike data structure.


1 Answers

int scores[3][3] = { {1, 2, 3} };

is equivalent to:

int scores[3][3] = { {1, 2, 3}, {0, 0, 0}, {0, 0, 0}};

The other one is similar. You know the answer.

like image 101
Yu Hao Avatar answered Sep 28 '22 04:09

Yu Hao