SlideShare a Scribd company logo
1 of 24
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

Arrays and library functions
Arrays and library functionsArrays and library functions
Arrays and library functions
Swarup Kumar Boro
 

What's hot (20)

C programming slide c05
C programming slide c05C programming slide c05
C programming slide c05
 
Arrays
ArraysArrays
Arrays
 
C Programming : Arrays
C Programming : ArraysC Programming : Arrays
C Programming : Arrays
 
2- Dimensional Arrays
2- Dimensional Arrays2- Dimensional Arrays
2- Dimensional Arrays
 
Arrays and Strings
Arrays and Strings Arrays and Strings
Arrays and Strings
 
Arrays and library functions
Arrays and library functionsArrays and library functions
Arrays and library functions
 
Array in C
Array in CArray in C
Array in C
 
Arrays in C language
Arrays in C languageArrays in C language
Arrays in C language
 
Two dimensional array
Two dimensional arrayTwo dimensional array
Two dimensional array
 
concept of Array, 1D & 2D array
concept of Array, 1D & 2D arrayconcept of Array, 1D & 2D array
concept of Array, 1D & 2D array
 
Python programming workshop
Python programming workshopPython programming workshop
Python programming workshop
 
Multidimensional arrays in C++
Multidimensional arrays in C++Multidimensional arrays in C++
Multidimensional arrays in C++
 
Java: Introduction to Arrays
Java: Introduction to ArraysJava: Introduction to Arrays
Java: Introduction to Arrays
 
Lecture 18 - Pointers
Lecture 18 - PointersLecture 18 - Pointers
Lecture 18 - Pointers
 
Arrays basics
Arrays basicsArrays basics
Arrays basics
 
Programming in c arrays
Programming in c   arraysProgramming in c   arrays
Programming in c arrays
 
Programming in c Arrays
Programming in c ArraysProgramming in c Arrays
Programming in c Arrays
 
Array Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional arrayArray Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional array
 
Array in c
Array in cArray in c
Array in c
 
Arrays in c
Arrays in cArrays in c
Arrays in c
 

Similar to Lecture 15 - Array

array-191103180006.pdf
array-191103180006.pdfarray-191103180006.pdf
array-191103180006.pdf
HEMAHEMS5
 

Similar to Lecture 15 - Array (20)

Arrays 1D and 2D , and multi dimensional
Arrays 1D and 2D , and multi dimensional Arrays 1D and 2D , and multi dimensional
Arrays 1D and 2D , and multi dimensional
 
Arrays
ArraysArrays
Arrays
 
Introduction to Arrays in C
Introduction to Arrays in CIntroduction to Arrays in C
Introduction to Arrays in C
 
Arrays and library functions
Arrays and library functionsArrays and library functions
Arrays and library functions
 
Array
ArrayArray
Array
 
Arrays
ArraysArrays
Arrays
 
Chapter 13.pptx
Chapter 13.pptxChapter 13.pptx
Chapter 13.pptx
 
Algo>Arrays
Algo>ArraysAlgo>Arrays
Algo>Arrays
 
Array
ArrayArray
Array
 
Data structure array
Data structure  arrayData structure  array
Data structure array
 
7.basic array
7.basic array7.basic array
7.basic array
 
Unit 6. Arrays
Unit 6. ArraysUnit 6. Arrays
Unit 6. Arrays
 
array-191103180006.pdf
array-191103180006.pdfarray-191103180006.pdf
array-191103180006.pdf
 
Arrays
ArraysArrays
Arrays
 
02 arrays
02 arrays02 arrays
02 arrays
 
Array in c
Array in cArray in c
Array in c
 
Arrays in C++ in Tamil - TNSCERT SYLLABUS PPT
Arrays in C++ in Tamil - TNSCERT SYLLABUS PPT Arrays in C++ in Tamil - TNSCERT SYLLABUS PPT
Arrays in C++ in Tamil - TNSCERT SYLLABUS PPT
 
Arrays In C
Arrays In CArrays In C
Arrays In C
 
arrays.pptx
arrays.pptxarrays.pptx
arrays.pptx
 
Array
ArrayArray
Array
 

More from Md. Imran Hossain Showrov

More from Md. Imran Hossain Showrov (18)

Lecture 22 - Error Handling
Lecture 22 - Error HandlingLecture 22 - Error Handling
Lecture 22 - Error Handling
 
Lecture 21 - Preprocessor and Header File
Lecture 21 - Preprocessor and Header FileLecture 21 - Preprocessor and Header File
Lecture 21 - Preprocessor and Header File
 
Lecture 20 - File Handling
Lecture 20 - File HandlingLecture 20 - File Handling
Lecture 20 - File Handling
 
Lecture 19 - Struct and Union
Lecture 19 - Struct and UnionLecture 19 - Struct and Union
Lecture 19 - Struct and Union
 
Lecture 14 - Scope Rules
Lecture 14 - Scope RulesLecture 14 - Scope Rules
Lecture 14 - Scope Rules
 
Lecture 13 - Storage Classes
Lecture 13 - Storage ClassesLecture 13 - Storage Classes
Lecture 13 - Storage Classes
 
Lecture 12 - Recursion
Lecture 12 - Recursion Lecture 12 - Recursion
Lecture 12 - Recursion
 
Lecture 11 - Functions
Lecture 11 - FunctionsLecture 11 - Functions
Lecture 11 - Functions
 
Lecture 10 - Control Structures 2
Lecture 10 - Control Structures 2Lecture 10 - Control Structures 2
Lecture 10 - Control Structures 2
 
Lecture 8- Data Input and Output
Lecture 8- Data Input and OutputLecture 8- Data Input and Output
Lecture 8- Data Input and Output
 
Lecture 9- Control Structures 1
Lecture 9- Control Structures 1Lecture 9- Control Structures 1
Lecture 9- Control Structures 1
 
Lecture 7- Operators and Expressions
Lecture 7- Operators and Expressions Lecture 7- Operators and Expressions
Lecture 7- Operators and Expressions
 
Lecture 6- Intorduction to C Programming
Lecture 6- Intorduction to C ProgrammingLecture 6- Intorduction to C Programming
Lecture 6- Intorduction to C Programming
 
Lecture 5 - Structured Programming Language
Lecture 5 - Structured Programming Language Lecture 5 - Structured Programming Language
Lecture 5 - Structured Programming Language
 
Lecture 4- Computer Software and Languages
Lecture 4- Computer Software and LanguagesLecture 4- Computer Software and Languages
Lecture 4- Computer Software and Languages
 
Lecture 3 - Processors, Memory and I/O devices
Lecture 3 - Processors, Memory and I/O devicesLecture 3 - Processors, Memory and I/O devices
Lecture 3 - Processors, Memory and I/O devices
 
Lecture 2 - Introductory Concepts
Lecture 2 - Introductory ConceptsLecture 2 - Introductory Concepts
Lecture 2 - Introductory Concepts
 
Lecture 1- History of C Programming
Lecture 1- History of C Programming Lecture 1- History of C Programming
Lecture 1- History of C Programming
 

Recently uploaded

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 

Recently uploaded (20)

Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Role Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxRole Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesEnergy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 

Lecture 15 - Array

  • 1. Array Md. Imran Hossain Showrov (showrovsworld@gmail.com) 15 1
  • 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