0% found this document useful (0 votes)
21 views13 pages

Two Dimensional 2D

Here is a C program to create a 2D array of dimensions 2x3, take user input for elements and display the elements in matrix form: #include <stdio.h> int main() { int arr[2][3], i, j; printf("Enter elements of 2D array: \n"); for(i=0; i<2; i++) { for(j=0; j<3; j++) { printf("Enter element arr[%d][%d]: ", i,j); scanf("%d", &arr[i][j]); } } printf("\nElements of 2D array in matrix form: \

Uploaded by

Rio Rabino
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views13 pages

Two Dimensional 2D

Here is a C program to create a 2D array of dimensions 2x3, take user input for elements and display the elements in matrix form: #include <stdio.h> int main() { int arr[2][3], i, j; printf("Enter elements of 2D array: \n"); for(i=0; i<2; i++) { for(j=0; j<3; j++) { printf("Enter element arr[%d][%d]: ", i,j); scanf("%d", &arr[i][j]); } } printf("\nElements of 2D array in matrix form: \

Uploaded by

Rio Rabino
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Two Dimensional (2D)

Arrays
DENNIS G. CORLET
Subject Instructor
What is 2D Array?
• An array of arrays is known as 2D array. The two dimensional
(2D) array in C programming is also known as matrix.
• A matrix can be represented as a table of rows and columns.
• The simplest form of multidimensional array is the two-
dimensional array. A two-dimensional array is, in essence, a
list of one-dimensional arrays.
How to declare a 2D Array?
• To declare a two-dimensional integer array of size [x][y], you would
write something as follows −

where
• type can be any valid C data type; and
• arrayName will be a valid C identifier.

• A two-dimensional array a, which contains


three rows and four columns can
be shown as follows −
Initializing Two-Dimensional Arrays
• There are two ways to initialize a two Dimensional arrays during
declaration.
This is more recommended as it is
more readable, because you can
visualize the rows and columns of 2d
array in this method

or
Following is an array with 3 rows and each row has 4 columns.

which is equivalent to
Things that you must consider while
initializing a 2D array
• In a normal array (one dimensional), you need not to
specify the size of it. However that’s not the case with
2D array, you must always specify the second
dimension even if you are specifying elements during
the declaration.
Let’s exercise!
• Determine whether the following array initialization is valid or not.

Valid declaration

Invalid declaration
– you must specify second dimension

Valid declaration

Invalid declaration
– you must specify second dimension
How to store user input data into 2D
array?
• We can calculate how many elements a two dimensional array can
have by using this formula:
n1*n2 elements.
• For instance, array abc has dimensions 5 and 4. These dimensions are
known as subscripts. So this array has first subscript value as 5 and
second subscript value as 4.

• Therefore, array abc[5][4] can have 5*4 = 20 elements.


How to store user input data into 2D
array?
• To store the elements entered by user we are using two for
loops, one of them is a nested loop.

• The outer loop runs from 0 to the (first subscript -1)


• The inner for loops runs from 0 to the (second subscript -1).

• This way the order in which user enters the elements would
be abc[0][0], abc[0][1], abc[0][2]…so on.
Accessing Two-Dimensional Array
Elements
• An element in a two-dimensional array is accessed by using the
subscripts, i.e., row index and column index of the array. For example:

• The above statement will take the 4th element from the 3rd row of
the array.
Let us check the following program where we have used a nested loop
to handle a two-dimensional array −
Example
Exercise 1
• Create a 2D array program
with dimensions of 2 and
3. Allow the user to enter
the values of the
elements. Afterwards,
display the elements in
matrix form.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy