0% found this document useful (0 votes)
11 views12 pages

2 2darrays

The document outlines the types of arrays in C programming, specifically one-dimensional and multi-dimensional arrays. It details the syntax for declaring and initializing both types, with examples for single and two-dimensional arrays. Additionally, it provides exercises for practicing the declaration and initialization of two-dimensional arrays.

Uploaded by

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

2 2darrays

The document outlines the types of arrays in C programming, specifically one-dimensional and multi-dimensional arrays. It details the syntax for declaring and initializing both types, with examples for single and two-dimensional arrays. Additionally, it provides exercises for practicing the declaration and initialization of two-dimensional arrays.

Uploaded by

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

TYPES OF ARRAY IN C

There are 2 types of arrays


1. One dimensional array / Single dimensional array
2. Multi dimensional array
• Two dimensional array
• Three dimensional array
• four dimensional array etc…
ONE DIMENSIONAL ARRAY
• single dimensional arrays are used to store a row of
values.
• In single dimensional array, data is stored in linear
form.
• Single dimensional arrays are also called as one-
dimensional arrays.
• Syntax: datatype arrayName[size] ;
• Eg: int rollno[5]; // declaration
• Eg: int rollno[5]={1,2,3,4,5}; // Initialization
ONE DIMENSIONAL ARRAY
Example: int rollno[5]={1,2,3,4,5};

roll Array size= 5


no 1 2 3 4 5

0 1 2 3 4
MULTI DIMENSIONAL ARRAY
• An array of arrays is called as multi dimensional
array.
• Multi dimensional array can be of two dimensional
array or three dimensional array or four
dimensional array or more...
• Most popular and commonly used multi dimensional
array is two dimensional array.
• The 2D array is organized as matrices which can be
represented as the collection of rows and columns.
TWO DIMENSIONAL ARRAY
• Declaration of Two Dimensional Array
Syntax:
datatype arrayName[rowSize][columnSize];

Eg: int a[2][3] ;


The above declaration of two dimensional array reserves
memory locations in the form of 2 rows and 3 columns.
Col 0 Col 1 Col 2
Row 0 a[0][0] a[0][1] a[0][2]
a
a[1][0] a[1][1] a[1][2]
Row 1
TWO DIMENSIONAL ARRAY
• Initialization of Two Dimensional Array
Syntax:
datatype arrayName[rows][colmns] = {{r1c1, r1c2, ...},{r2c1,
r2c2,...}...} ;

Eg: int a[2][3] = { {1, 2, 3},{4, 5, 6} } ;


The above declaration of two-dimensional array reserves memory
locations in the form of 2 rows and 3 columns. And the first row is
initialized with values 1, 2 & 3 and second row is initialized with
values 4, 5 & 6.
Col 0 1 2
Row 0 1 2 3
a
1 4 5 6
TWO DIMENSIONAL ARRAY
• Declaration of Two Dimensional Array
Syntax:
datatype arrayName[rowSize][columnSize];

• Eg: int a[3][3] ;


The above declaration of two dimensional array reserves
memory locations in the form of 3 rows and 3 columns.

Col 0 Col 1 Col 2


Row 0 a[0][0] a[0][1] a[0][2]
a
a[1][0] a[1][1] a[1][2]
Row 1
Row 2 a[2][0] a[2][1] a[2][2]
TWO DIMENSIONAL ARRAY
• Initialization of Two Dimensional Array
Syntax:
datatype arrayName[rows][colmns] = {{r1c1, r1c2, ...},{r2c1,
r2c2,...}...} ;

Eg: int a[3][3] = { {1, 2, 3},{4, 5, 6}, {7,8,9} } ;


The above declaration of two-dimensional array reserves memory
locations in the form of 3 rows and 3 columns. And the first row is
initialized with values 1, 2 & 3 and second row is initialized with
values 4, 5 & 6 and third row is initialized with values 7,8 &9.
Col 0 1 2
Row 0 1 2 3
a
1 4 5 6

2 7 8 9
TWO DIMENSIONAL ARRAY
• Declaration of Two Dimensional Array
Syntax:
datatype arrayName[rowSize][columnSize];

• Eg: int a[3][2] ;


The above declaration of two dimensional array reserves
memory locations in the form of 3 rows and 2 columns.

Col 0 Col 1
Row 0 a[0][0] a[0][1]
a
a[1][0] a[1][1]
Row 1
Row 2 a[2][0] a[2][1]
TWO DIMENSIONAL ARRAY
• Initialization of Two Dimensional Array
Syntax:
datatype arrayName[rows][colmns] = {{r1c1, r1c2, ...},{r2c1,
r2c2,...}...} ;

Eg: int a[3][2] = { {1, 2},{4, 5}, {7,8} } ;


The above declaration of two-dimensional array reserves memory
locations in the form of 3 rows and 2 columns. And the first row is
initialized with values 1, 2 and second row is initialized with
values 4, 5 and third row is initialized with 7 and 8.
Col 0 1
Row 0 1 2
a
1 4 5

2 7 8
TWO DIMENSIONAL ARRAY
Homework:
• Eg: int a[3][4] ;
The above declaration of two dimensional array reserves
memory locations in the form of 3 rows and 4 columns.

Fill the index values a[][] below:

Col 0 Col 1 Col 2 Col 3


Row 0 a[][] a[][] a[][] a[][]
a
a[][] a[][] a[][] a[][]
Row 1
Row 2 a[][] a[][] a[][] a[][]
TWO DIMENSIONAL ARRAY
Homework:
Eg: int a[3][4] = { {1, 2,3,4},{5,6,7,8}, {9,10,11,12} } ;
The above declaration of two-dimensional array reserves memory
locations in the form of 3 rows and 4 columns. And the first row is
initialized with values 1, 2,3,4 and second row is initialized with
values 5,6,7,8 and third row is initialized with 9,10,11,12

Fill the above values/elements in correct memory location.

Col 0 Col 1 Col 2 Col 3


Row 0
a
Row 1
Row 2

You might also like