One Dimensional And Two Dimensional Array In C



Two Dimensional Array in C The Two Dimensional Array in C language is nothing but an Array of Arrays. If the data is linear, we can use the One Dimensional Array. However, to work with multi-level data, we have to use the Multi-Dimensional Array. Two Dimensional Array in C. The two-dimensional array can be defined as an array of arrays. 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. Two Dimensional Array In Vb.net
  2. One Dimensional And Two Dimensional Array In C++
  3. C Language Two Dimensional Array
  4. One Dimensional And Two Dimensional Array In C Image

The two-dimensional array can be defined as an array of arrays. 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. It provides ease of holding the bulk of data at once which can be passed to any number of functions wherever required.

Declaration of two dimensional Array in C

The syntax to declare the 2D array is given below.

Consider the following example.

Two Dimensional Array In Vb.net

Two dimensional array example

Here, 4 is the number of rows, and 3 is the number of columns.

Initialization of 2D Array in C

In the 1D array, we don't need to specify the size of the array if the declaration and initialization are being done simultaneously. However, this will not work with 2D arrays. We will have to define at least the second dimension of the array. The two-dimensional array can be declared and defined in the following way.

Two-dimensional array example in C

Output

C 2D array example: Storing elements in a matrix and printing it.

Output

Next TopicReturn an Array in C

Back to: C#.NET Tutorials For Beginners and Professionals

2d Array in C# with Examples

In this article, I am going to discuss the 2d Array in C# with Examples. Please read our previous article before proceeding to this article where we discussed one-dimensional Arrays in C# with examples. As part of this article, we are going to discuss the following pointers which are related to Two-Dimensional Array in C#.

  1. What is Two-Dimensional Array in C#?
  2. Understanding the rectangular and jagged array in C# with examples.
What is Two-Dimensional Array in C#?

The arrays which store the elements in the form of rows and columns are called Two-Dimensional Array in C#. The two-dimensional array you can also say multidimensional array are of two types in C#. They are as follows

  1. Rectangular array: The array whose rows and columns are equal are called rectangular array
  2. Jagged array: The array whose rows and columns are not equal are called jagged array
Rectangular 2d Array in C#:

Let us first understand the syntax of Two-Dimensional Array in C#. Please have a look at the following diagram.

Let us see an example for a better understanding of the rectangular array in C#.

OUTPUT:

In the above example, we assigned the two-dimensional array element using nested for loop. It is also possible that we can assign the values to two-dimensional array in C# at the time of its declaration:

Assigning values to two-dimensional array in C# at the time of declaration:

OUTPUT:

Jagged Array in C#:

One Dimensional And Two Dimensional Array In C++

Syntax two dimensional array

These are also two-dimensional array which will also store the data in the forms of rows and columns. But here in the jagged array, the column size will differ from row to row. That means if the first row contains 5 columns then the second row may contain 4 columns while the third row may contain 10 columns. So the point that you need to remember is if the column size varies from row to row then it is a jagged array. If the column size remains the same for all the rows then it is a rectangular two-dimensional array.

The jagged array in C# is also called the array of arrays. This is because in the case of the jagged array each row is a single dimensional array. So a combination of multiple single-dimensional arrays with different column sizes form a jagged array in C#.

Syntax: <type> [][] <name> = new <type> [rows][];

Example:

To declare a jagged array in C#, at the time of its declaration, you only need to specify the number of rows that you want in the array. for example

int [][] arr = new int[4][];

In the above array declaration, we are specifying that we want four rows in the array. Once you specify the number of rows that you want in the array, then you need to initialize each row with the number of columns by using a single dimensional array as shown below.

C Language Two Dimensional Array

arr[0] = new int[5]; // we want five columns in the first row
arr[1] = new int[6]; // we want six columns in the first row
arr[2] = new int[4]; // we want four columns in the first row
arr[3] = new int[5]; // we want five columns in the first row

Example of the jagged array in C#:

When we run the application, it will give us the following output:

In the above example, we are assigning the elements of the jagged array by using nested for loop. It is also possible to assign the values of the jagged array at the time of its declaration.

Assigning jagged array at the time of its declaration:

One Dimensional And Two Dimensional Array In C Image

OUTPUT:

In the next article, I am going to discuss the advantages and disadvantages of arrays in C# with examples. Here, in this article, I try to explain the 2d Array in C# with examples. I hope this article will help you with your needs. I would like to have your feedback. Please post your feedback, question, or comments about this article.