SlideShare a Scribd company logo
Array
Md. Imran Hossain Showrov (showrovsworld@gmail.com)
15
1
Outline
 What is An Array?
 Why do we need Array?
 Declaring An One-Dimensional Array
 Accessing Array Elements
 One-Dimensional Array
 Two-Dimensional Array
What is An Array?
 Arrays a kind of data structure that can store a fixed-
size sequential collection of elements of the same
type.
 An array is used to store a collection of data, but it is
often more useful to think of an array as a collection
of variables of the same type.
Why do we need Array?
 We can use normal variables (v1, v2, v3, ..) when we
have small number of objects, but if we want to store
large number of instances, it becomes difficult to
manage them with normal variables.The idea of array
is to represent many instances in one variable.
Defining An One-Dimensional Array
 Arrays are defined in much the same manner as
ordinary variables, except that each array name must
be accompanied by a size specification (the number of
elements).
 For one-dimensional array, the size is specified by a
positive integer expression, enclosed in square
brackets.
 The expression is usually written as
storage-class data-type array[ expression ];
Declaring An One-Dimensional Array
 Each array element can be referred to by specifying
the array name followed by one or more subscripts,
with each subscript enclosed in square brackets.
 Each subscript must be expressed as a nonnegative
integer.
 In an n-element array, the array elements are a[0],
a[1], a[2],…, a[n-1].
Declaring An One-Dimensional Array
(cont..)
 The number of subscripts determines the
dimensionality of the array.
 Here, 3rd element is referred by a[2], nth element is
referred by a[n-1].
Declaring An One-Dimensional Array
(Example)
 Several typical one-dimensional array definitions are shown
below:
int x[100];
char text[80];
static char message[25];
float n[12];
Here, the first line states that x is a 100-element integer array,
second line defines text to be an 80-element character array and
so on…
Initializing An One-Dimensional Array
 Arrays can be initialized C either one by one or using
a single statement as follows −
1. double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0};
2. double balance[] = {1000.0, 2.0, 3.4, 7.0, 50.0};
 In example 1, the number of values between braces
{ } cannot be larger than the number of elements that
we declare for the array between square brackets [ ].
Accessing Array Elements
balance[4] = 50.0;
 The above statement assigns the 5th element in the array
with a value of 50.0.All arrays have 0 as the index of their
first element which is also called the base index and the
last index of an array will be total size of the array minus
1.
One-Dimensional Array Example1
#include <stdio.h>
int main () {
int n[ 10 ]; /* n is an array of 10 integers */
int i, j;
for ( i = 0; i < 10; i++ ) {/* initialize elements of array n to 0 */
n[ i ] = i + 100; /* set element at location i to i + 100 */
}
for (j = 0; j < 10; j++ ) {/* output each array element's value */
printf("Element[%d] = %dn", j, n[j] );
}
return 0;
}
One-Dimensional Array Example1
(cont..)
Output:
Element[0] = 100
Element[1] = 101
Element[2] = 102
Element[3] = 103
Element[4] = 104
Element[5] = 105
Element[6] = 106
Element[7] = 107
Element[8] = 108
Element[9] = 109
One-Dimensional Array Example2
int digit[4] = {1, 2, 3, 4}
float x[6] = {0, 0.25, 0, -0.50, 0, 0}
char color[3] = { ‘R’ ,‘E’ ,‘D’}
13
digit[0] = 1
digit[1] = 2
digit[2] = 3
digit[3] = 4
x[0] = 0
x[1] = 0.25
x[2] = 0
x[3] = -0.50
x[4] = 0
x[5] = 0
color[0] = ‘R’
color[1] = ‘E’
color[2] = ‘D’
One-Dimensional Array Example3
int digit[] = {1, 2, 3, 4}
float x[] = {0, 0.25, 0, -0.50, 0, 0}
char color[] = { ‘R’ ,‘E’ ,‘D’}
14
digit[0] = 1
digit[1] = 2
digit[2] = 3
digit[3] = 4
x[0] = 0
x[1] = 0.25
x[2] = 0
x[3] = -0.50
x[4] = 0
x[5] = 0
color[0] = ‘R’
color[1] = ‘E’
color[2] = ‘D’
One-Dimensional Array Example3
int digit[6] = {1, 2, 3, 4}
float x[6] = {0, 0.25, 0, -0.50, 0, 0}
char color[3] = { ‘R’ ,‘E’ ,‘D’}
15
digit[0] = 1
digit[1] = 2
digit[2] = 3
digit[3] = 4
digit[4] = 0
digit[5] = 0
x[0] = 0
x[1] = 0.25
x[2] = 0
x[3] = -0.50
x[4] = 0
x[5] = 0
color[0] = ‘R’
color[1] = ‘E’
color[2] = ‘D’
One-Dimensional Array Example4
1. char color[3] = { “RED” }
2. char color[] = { “RED” }
16
1.
color[0] = ‘R’
color[1] = ‘E’
color[2] = ‘D’
2.
color[0] = ‘R’
color[1] = ‘E’
color[2] = ‘D’
color[2] = ‘0’
Two-dimensional Arrays
 The simplest form of multidimensional array is the two-
dimensional array.
 A two-dimensional array is, in essence, a list of one-
dimensional arrays.
 To declare a two-dimensional integer array of size [x]
[y], you would write something as follows −
type arrayName [ x ][ y ];
 Where type can be any valid C data type and arrayName will be a
valid C identifier.
Two-dimensional Arrays (cont..)
 A two-dimensional array can be considered as a table which
will have x number of rows and y number of columns.
 A two-dimensional array a, which contains three rows and four
columns can be shown as follows −
Initializing Two-Dimensional Arrays
 Multidimensional arrays may be initialized by specifying bracketed values
for each row. Following is an array with 3 rows and each row has 4
columns.
int a[3][4] = {
{0, 1, 2, 3} , /* initializers for row indexed by 0 */
{4, 5, 6, 7} , /* initializers for row indexed by 1 */
{8, 9, 10, 11} /*initializers for row indexed by 2 */
};
 The nested braces, which indicate the intended row, are optional.
Initializing Two-Dimensional Arrays
int a[3][4] = {
{0, 1, 2, 3} , /* initializers for row indexed by 0 */
{4, 5, 6, 7} , /* initializers for row indexed by 1 */
{8, 9, 10, 11} /*initializers for row indexed by 2 */
};
We can also initialize the previous example as below:
int a[3][4] = {0,1,2,3,4,5,6,7,8,9,10,11};
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 −
int a[3][4] = {
{0, 1, 2, 3} , /* initializers for row indexed by 0 */
{4, 5, 6, 7} , /* initializers for row indexed by 1 */
{8, 9, 10, 11} /*initializers for row indexed by 2 */
};
 int val = a[2][3];
 The above statement will take the 4th element from the 3rd row of the
array.
Two-Dimensional Array Example1
#include <stdio.h>
int main () {
/* an array with 5 rows and 2 columns*/
int a[5][2] = { {0,0}, {1,2}, {2,4}, {3,6},{4,8}};
int i, j;
for ( i = 0; i < 5; i++ ) {
for ( j = 0; j < 2; j++ ) {
printf("a[%d][%d] = %dn", i,j, a[i][j] );
}
}
return 0;
}
Two-Dimensional Array Example1
Output:
– a[0][0]: 0
– a[0][1]: 0
– a[1][0]: 1
– a[1][1]: 2
– a[2][0]: 2
– a[2][1]: 4
– a[3][0]: 3
– a[3][1]: 6
– a[4][0]: 4
– a[4][1]: 8
Lecture 15 - Array

More Related Content

What's hot (20)

PPTX
C programming slide c05
pradeep dwivedi
 
PPSX
C Programming : Arrays
Gagan Deep
 
PPTX
2- Dimensional Arrays
Education Front
 
DOC
Arrays and Strings
Dr.Subha Krishna
 
PDF
Arrays and library functions
Swarup Kumar Boro
 
PPTX
Array in C
adityas29
 
PPTX
Arrays in C language
Shubham Sharma
 
PPT
Two dimensional array
Rajendran
 
PPTX
concept of Array, 1D & 2D array
Sangani Ankur
 
PPTX
Python programming workshop
BAINIDA
 
PDF
Multidimensional arrays in C++
Ilio Catallo
 
PPT
Java: Introduction to Arrays
Tareq Hasan
 
PPT
Lecture 18 - Pointers
Md. Imran Hossain Showrov
 
PPTX
Arrays basics
sudhirvegad
 
PPTX
Programming in c arrays
Uma mohan
 
PPTX
Programming in c Arrays
janani thirupathi
 
PPTX
Array Introduction One-dimensional array Multidimensional array
imtiazalijoono
 
PPTX
Array in c
AnIsh Kumar
 
PPTX
Arrays in c
Jeeva Nanthini
 
C programming slide c05
pradeep dwivedi
 
C Programming : Arrays
Gagan Deep
 
2- Dimensional Arrays
Education Front
 
Arrays and Strings
Dr.Subha Krishna
 
Arrays and library functions
Swarup Kumar Boro
 
Array in C
adityas29
 
Arrays in C language
Shubham Sharma
 
Two dimensional array
Rajendran
 
concept of Array, 1D & 2D array
Sangani Ankur
 
Python programming workshop
BAINIDA
 
Multidimensional arrays in C++
Ilio Catallo
 
Java: Introduction to Arrays
Tareq Hasan
 
Lecture 18 - Pointers
Md. Imran Hossain Showrov
 
Arrays basics
sudhirvegad
 
Programming in c arrays
Uma mohan
 
Programming in c Arrays
janani thirupathi
 
Array Introduction One-dimensional array Multidimensional array
imtiazalijoono
 
Array in c
AnIsh Kumar
 
Arrays in c
Jeeva Nanthini
 

Similar to Lecture 15 - Array (20)

PPTX
Arrays 1D and 2D , and multi dimensional
Appili Vamsi Krishna
 
PDF
Introduction to Arrays in C
Thesis Scientist Private Limited
 
PDF
Arrays and library functions
Swarup Boro
 
PPTX
Arrays
Trupti Agrawal
 
PPTX
Chapter 13.pptx
AnisZahirahAzman
 
PPT
Algo>Arrays
Ain-ul-Moiz Khawaja
 
PPTX
Array
Allah Ditta
 
PPTX
Data structure array
MajidHamidAli
 
PPT
uderstanding arrays and how to declare arrays
ShirishaBuduputi
 
PPT
7.basic array
Mir Riyanul Islam
 
PPTX
Unit 6. Arrays
Ashim Lamichhane
 
PDF
array-191103180006.pdf
HEMAHEMS5
 
PPT
Arrays
SARITHA REDDY
 
PDF
02 arrays
Rajan Gautam
 
PPT
Array in c
Ravi Gelani
 
PPTX
Arrays in C++ in Tamil - TNSCERT SYLLABUS PPT
LATHA LAKSHMI
 
PDF
Arrays In C
yndaravind
 
PPTX
arrays.pptx
HarmanShergill5
 
Arrays 1D and 2D , and multi dimensional
Appili Vamsi Krishna
 
Introduction to Arrays in C
Thesis Scientist Private Limited
 
Arrays and library functions
Swarup Boro
 
Chapter 13.pptx
AnisZahirahAzman
 
Algo>Arrays
Ain-ul-Moiz Khawaja
 
Data structure array
MajidHamidAli
 
uderstanding arrays and how to declare arrays
ShirishaBuduputi
 
7.basic array
Mir Riyanul Islam
 
Unit 6. Arrays
Ashim Lamichhane
 
array-191103180006.pdf
HEMAHEMS5
 
02 arrays
Rajan Gautam
 
Array in c
Ravi Gelani
 
Arrays in C++ in Tamil - TNSCERT SYLLABUS PPT
LATHA LAKSHMI
 
Arrays In C
yndaravind
 
arrays.pptx
HarmanShergill5
 
Ad

More from Md. Imran Hossain Showrov (18)

PPT
Lecture 22 - Error Handling
Md. Imran Hossain Showrov
 
PPT
Lecture 21 - Preprocessor and Header File
Md. Imran Hossain Showrov
 
PPT
Lecture 20 - File Handling
Md. Imran Hossain Showrov
 
PPT
Lecture 19 - Struct and Union
Md. Imran Hossain Showrov
 
PPT
Lecture 14 - Scope Rules
Md. Imran Hossain Showrov
 
PPT
Lecture 13 - Storage Classes
Md. Imran Hossain Showrov
 
PPT
Lecture 12 - Recursion
Md. Imran Hossain Showrov
 
PPT
Lecture 11 - Functions
Md. Imran Hossain Showrov
 
PPT
Lecture 10 - Control Structures 2
Md. Imran Hossain Showrov
 
PPT
Lecture 8- Data Input and Output
Md. Imran Hossain Showrov
 
PPT
Lecture 9- Control Structures 1
Md. Imran Hossain Showrov
 
PPT
Lecture 7- Operators and Expressions
Md. Imran Hossain Showrov
 
PPT
Lecture 6- Intorduction to C Programming
Md. Imran Hossain Showrov
 
PPT
Lecture 5 - Structured Programming Language
Md. Imran Hossain Showrov
 
PPT
Lecture 4- Computer Software and Languages
Md. Imran Hossain Showrov
 
PPT
Lecture 3 - Processors, Memory and I/O devices
Md. Imran Hossain Showrov
 
PPT
Lecture 2 - Introductory Concepts
Md. Imran Hossain Showrov
 
PPT
Lecture 1- History of C Programming
Md. Imran Hossain Showrov
 
Lecture 22 - Error Handling
Md. Imran Hossain Showrov
 
Lecture 21 - Preprocessor and Header File
Md. Imran Hossain Showrov
 
Lecture 20 - File Handling
Md. Imran Hossain Showrov
 
Lecture 19 - Struct and Union
Md. Imran Hossain Showrov
 
Lecture 14 - Scope Rules
Md. Imran Hossain Showrov
 
Lecture 13 - Storage Classes
Md. Imran Hossain Showrov
 
Lecture 12 - Recursion
Md. Imran Hossain Showrov
 
Lecture 11 - Functions
Md. Imran Hossain Showrov
 
Lecture 10 - Control Structures 2
Md. Imran Hossain Showrov
 
Lecture 8- Data Input and Output
Md. Imran Hossain Showrov
 
Lecture 9- Control Structures 1
Md. Imran Hossain Showrov
 
Lecture 7- Operators and Expressions
Md. Imran Hossain Showrov
 
Lecture 6- Intorduction to C Programming
Md. Imran Hossain Showrov
 
Lecture 5 - Structured Programming Language
Md. Imran Hossain Showrov
 
Lecture 4- Computer Software and Languages
Md. Imran Hossain Showrov
 
Lecture 3 - Processors, Memory and I/O devices
Md. Imran Hossain Showrov
 
Lecture 2 - Introductory Concepts
Md. Imran Hossain Showrov
 
Lecture 1- History of C Programming
Md. Imran Hossain Showrov
 
Ad

Recently uploaded (20)

PPTX
DIGITAL CITIZENSHIP TOPIC TLE 8 MATATAG CURRICULUM
ROBERTAUGUSTINEFRANC
 
PPTX
Nitrogen rule, ring rule, mc lafferty.pptx
nbisen2001
 
PPTX
ENG8_Q1_WEEK2_LESSON1. Presentation pptx
marawehsvinetshe
 
PPTX
SD_GMRC5_Session 6AB_Dulog Pedagohikal at Pagtataya (1).pptx
NickeyArguelles
 
PPTX
AIMA UCSC-SV Leadership_in_the_AI_era 20250628 v16.pptx
home
 
PPTX
Lesson 1 Cell (Structures, Functions, and Theory).pptx
marvinnbustamante1
 
PPTX
Iván Bornacelly - Presentation of the report - Empowering the workforce in th...
EduSkills OECD
 
PDF
I3PM Case study smart parking 2025 with uptoIP® and ABP
MIPLM
 
PPTX
The Gift of the Magi by O Henry-A Story of True Love, Sacrifice, and Selfless...
Beena E S
 
PDF
Our Guide to the July 2025 USPS® Rate Change
Postal Advocate Inc.
 
PDF
I3PM Industry Case Study Siemens on Strategic and Value-Oriented IP Management
MIPLM
 
PPTX
How to Send Email From Odoo 18 Website - Odoo Slides
Celine George
 
PPT
Indian Contract Act 1872, Business Law #MBA #BBA #BCOM
priyasinghy107
 
PDF
IMPORTANT GUIDELINES FOR M.Sc.ZOOLOGY DISSERTATION
raviralanaresh2
 
PPTX
Introduction to Indian Writing in English
Trushali Dodiya
 
PPTX
How to Create & Manage Stages in Odoo 18 Helpdesk
Celine George
 
PDF
Android Programming - Basics of Mobile App, App tools and Android Basics
Kavitha P.V
 
DOCX
Lesson 1 - Nature and Inquiry of Research
marvinnbustamante1
 
PDF
CAD25 Gbadago and Fafa Presentation Revised-Aston Business School, UK.pdf
Kweku Zurek
 
PPTX
Marketing Management PPT Unit 1 and Unit 2.pptx
Sri Ramakrishna College of Arts and science
 
DIGITAL CITIZENSHIP TOPIC TLE 8 MATATAG CURRICULUM
ROBERTAUGUSTINEFRANC
 
Nitrogen rule, ring rule, mc lafferty.pptx
nbisen2001
 
ENG8_Q1_WEEK2_LESSON1. Presentation pptx
marawehsvinetshe
 
SD_GMRC5_Session 6AB_Dulog Pedagohikal at Pagtataya (1).pptx
NickeyArguelles
 
AIMA UCSC-SV Leadership_in_the_AI_era 20250628 v16.pptx
home
 
Lesson 1 Cell (Structures, Functions, and Theory).pptx
marvinnbustamante1
 
Iván Bornacelly - Presentation of the report - Empowering the workforce in th...
EduSkills OECD
 
I3PM Case study smart parking 2025 with uptoIP® and ABP
MIPLM
 
The Gift of the Magi by O Henry-A Story of True Love, Sacrifice, and Selfless...
Beena E S
 
Our Guide to the July 2025 USPS® Rate Change
Postal Advocate Inc.
 
I3PM Industry Case Study Siemens on Strategic and Value-Oriented IP Management
MIPLM
 
How to Send Email From Odoo 18 Website - Odoo Slides
Celine George
 
Indian Contract Act 1872, Business Law #MBA #BBA #BCOM
priyasinghy107
 
IMPORTANT GUIDELINES FOR M.Sc.ZOOLOGY DISSERTATION
raviralanaresh2
 
Introduction to Indian Writing in English
Trushali Dodiya
 
How to Create & Manage Stages in Odoo 18 Helpdesk
Celine George
 
Android Programming - Basics of Mobile App, App tools and Android Basics
Kavitha P.V
 
Lesson 1 - Nature and Inquiry of Research
marvinnbustamante1
 
CAD25 Gbadago and Fafa Presentation Revised-Aston Business School, UK.pdf
Kweku Zurek
 
Marketing Management PPT Unit 1 and Unit 2.pptx
Sri Ramakrishna College of Arts and science
 

Lecture 15 - Array

  • 2. Outline  What is An Array?  Why do we need Array?  Declaring An One-Dimensional Array  Accessing Array Elements  One-Dimensional Array  Two-Dimensional Array
  • 3. What is An Array?  Arrays a kind of data structure that can store a fixed- size sequential collection of elements of the same type.  An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.
  • 4. Why do we need Array?  We can use normal variables (v1, v2, v3, ..) when we have small number of objects, but if we want to store large number of instances, it becomes difficult to manage them with normal variables.The idea of array is to represent many instances in one variable.
  • 5. Defining An One-Dimensional Array  Arrays are defined in much the same manner as ordinary variables, except that each array name must be accompanied by a size specification (the number of elements).  For one-dimensional array, the size is specified by a positive integer expression, enclosed in square brackets.  The expression is usually written as storage-class data-type array[ expression ];
  • 6. Declaring An One-Dimensional Array  Each array element can be referred to by specifying the array name followed by one or more subscripts, with each subscript enclosed in square brackets.  Each subscript must be expressed as a nonnegative integer.  In an n-element array, the array elements are a[0], a[1], a[2],…, a[n-1].
  • 7. Declaring An One-Dimensional Array (cont..)  The number of subscripts determines the dimensionality of the array.  Here, 3rd element is referred by a[2], nth element is referred by a[n-1].
  • 8. Declaring An One-Dimensional Array (Example)  Several typical one-dimensional array definitions are shown below: int x[100]; char text[80]; static char message[25]; float n[12]; Here, the first line states that x is a 100-element integer array, second line defines text to be an 80-element character array and so on…
  • 9. Initializing An One-Dimensional Array  Arrays can be initialized C either one by one or using a single statement as follows − 1. double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0}; 2. double balance[] = {1000.0, 2.0, 3.4, 7.0, 50.0};  In example 1, the number of values between braces { } cannot be larger than the number of elements that we declare for the array between square brackets [ ].
  • 10. Accessing Array Elements balance[4] = 50.0;  The above statement assigns the 5th element in the array with a value of 50.0.All arrays have 0 as the index of their first element which is also called the base index and the last index of an array will be total size of the array minus 1.
  • 11. One-Dimensional Array Example1 #include <stdio.h> int main () { int n[ 10 ]; /* n is an array of 10 integers */ int i, j; for ( i = 0; i < 10; i++ ) {/* initialize elements of array n to 0 */ n[ i ] = i + 100; /* set element at location i to i + 100 */ } for (j = 0; j < 10; j++ ) {/* output each array element's value */ printf("Element[%d] = %dn", j, n[j] ); } return 0; }
  • 12. One-Dimensional Array Example1 (cont..) Output: Element[0] = 100 Element[1] = 101 Element[2] = 102 Element[3] = 103 Element[4] = 104 Element[5] = 105 Element[6] = 106 Element[7] = 107 Element[8] = 108 Element[9] = 109
  • 13. One-Dimensional Array Example2 int digit[4] = {1, 2, 3, 4} float x[6] = {0, 0.25, 0, -0.50, 0, 0} char color[3] = { ‘R’ ,‘E’ ,‘D’} 13 digit[0] = 1 digit[1] = 2 digit[2] = 3 digit[3] = 4 x[0] = 0 x[1] = 0.25 x[2] = 0 x[3] = -0.50 x[4] = 0 x[5] = 0 color[0] = ‘R’ color[1] = ‘E’ color[2] = ‘D’
  • 14. One-Dimensional Array Example3 int digit[] = {1, 2, 3, 4} float x[] = {0, 0.25, 0, -0.50, 0, 0} char color[] = { ‘R’ ,‘E’ ,‘D’} 14 digit[0] = 1 digit[1] = 2 digit[2] = 3 digit[3] = 4 x[0] = 0 x[1] = 0.25 x[2] = 0 x[3] = -0.50 x[4] = 0 x[5] = 0 color[0] = ‘R’ color[1] = ‘E’ color[2] = ‘D’
  • 15. One-Dimensional Array Example3 int digit[6] = {1, 2, 3, 4} float x[6] = {0, 0.25, 0, -0.50, 0, 0} char color[3] = { ‘R’ ,‘E’ ,‘D’} 15 digit[0] = 1 digit[1] = 2 digit[2] = 3 digit[3] = 4 digit[4] = 0 digit[5] = 0 x[0] = 0 x[1] = 0.25 x[2] = 0 x[3] = -0.50 x[4] = 0 x[5] = 0 color[0] = ‘R’ color[1] = ‘E’ color[2] = ‘D’
  • 16. One-Dimensional Array Example4 1. char color[3] = { “RED” } 2. char color[] = { “RED” } 16 1. color[0] = ‘R’ color[1] = ‘E’ color[2] = ‘D’ 2. color[0] = ‘R’ color[1] = ‘E’ color[2] = ‘D’ color[2] = ‘0’
  • 17. Two-dimensional Arrays  The simplest form of multidimensional array is the two- dimensional array.  A two-dimensional array is, in essence, a list of one- dimensional arrays.  To declare a two-dimensional integer array of size [x] [y], you would write something as follows − type arrayName [ x ][ y ];  Where type can be any valid C data type and arrayName will be a valid C identifier.
  • 18. Two-dimensional Arrays (cont..)  A two-dimensional array can be considered as a table which will have x number of rows and y number of columns.  A two-dimensional array a, which contains three rows and four columns can be shown as follows −
  • 19. Initializing Two-Dimensional Arrays  Multidimensional arrays may be initialized by specifying bracketed values for each row. Following is an array with 3 rows and each row has 4 columns. int a[3][4] = { {0, 1, 2, 3} , /* initializers for row indexed by 0 */ {4, 5, 6, 7} , /* initializers for row indexed by 1 */ {8, 9, 10, 11} /*initializers for row indexed by 2 */ };  The nested braces, which indicate the intended row, are optional.
  • 20. Initializing Two-Dimensional Arrays int a[3][4] = { {0, 1, 2, 3} , /* initializers for row indexed by 0 */ {4, 5, 6, 7} , /* initializers for row indexed by 1 */ {8, 9, 10, 11} /*initializers for row indexed by 2 */ }; We can also initialize the previous example as below: int a[3][4] = {0,1,2,3,4,5,6,7,8,9,10,11};
  • 21. 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 − int a[3][4] = { {0, 1, 2, 3} , /* initializers for row indexed by 0 */ {4, 5, 6, 7} , /* initializers for row indexed by 1 */ {8, 9, 10, 11} /*initializers for row indexed by 2 */ };  int val = a[2][3];  The above statement will take the 4th element from the 3rd row of the array.
  • 22. Two-Dimensional Array Example1 #include <stdio.h> int main () { /* an array with 5 rows and 2 columns*/ int a[5][2] = { {0,0}, {1,2}, {2,4}, {3,6},{4,8}}; int i, j; for ( i = 0; i < 5; i++ ) { for ( j = 0; j < 2; j++ ) { printf("a[%d][%d] = %dn", i,j, a[i][j] ); } } return 0; }
  • 23. Two-Dimensional Array Example1 Output: – a[0][0]: 0 – a[0][1]: 0 – a[1][0]: 1 – a[1][1]: 2 – a[2][0]: 2 – a[2][1]: 4 – a[3][0]: 3 – a[3][1]: 6 – a[4][0]: 4 – a[4][1]: 8