SlideShare a Scribd company logo
1 of 17
(Managed By Shree Tapi Brahmcharyashram Sabha)
Shree Swami AtmanandVidhya Sankul, Kapodra,Varachha Road, Surat, Gujarat, India. 395006
Phone: 0261-2573552 Fax No.: 0261-2573554 Email: ssasit@yahoo.in Web: www.ssasit.ac.in
Shree Swami Atmanand Saraswati Institute of Technology,
Surat
Arrays1
Presented by:
Patel Raj G
Arrays2
Arrays
 Suppose, you need to store years of 100 cars. Will you define
100 variables?
int y1, y2,…, y100;
 An array is an indexed data structure to represent several
variables having the same data type: int y[100];
3
y[0] y[1] y[2] … y[k-1] y[k] y[k+1] … y[98] y[99]
One-Dimensional Arrays (cont’d)
 An element of an array is accessed using the array name
and an index or subscript, for example: y[5] which can be
used like a variable
 In C, the subscripts always start with 0 and increment by 1,
so y[5] is the sixth element
 The name of the array is the address of the first element
and the subscript is the offset
4
y[0] y[1] y[2] … y[k-1] y[k] y[k+1] … y[98] y[99]
Definition and Initialization
 An array is defined using a declaration statement.
data_type array_name[size];
allocates memory for size elements
subscript of first element is 0
subscript of last element is size-1
size must be a constant
5
Example
int list[5];
 allocates memory for 5 integer variables
 subscript of first element is 0
 subscript of last element is 4
 C does not check bounds on arrays
 list[6] =5; /* may give segmentation fault or overwrite
other memory locations*/
6
list[0]
list[1]
list[2]
list[3]
list[4]
Initializing Arrays
 Arrays can be initialized at the time they are declared.
 Examples:
double rate[3] ={0.15, 0.25, 0.3};
char list[5] = {‘h’, ’e’, ’l’, ’l’, ’o’};
double vector[100] = {0.0}; /* assigns
zero to all 100 elements */
int s[] = {5,0,-5}; /*the size of s is 3*/
7
Assigning values to an array8
For loops are often used to assign values to an array
Example:
int list[5], i;
for(i=0; i<5; i++){
list[i] = i;
}
list[0]
list[3]
list[4]
list[1]
list[2]
0
1
2
3
4
list[0]
list[1]
list[2]
list[3]
list[4]
OR
for(i=0; i<=4; i++){
list[i] = i;
}
Matrices (2D-array)
 A matrix is a set of numbers arranged in a grid with rows and
columns.
 A matrix is defined using a type declaration statement.
datatype array_name[row_size][column_size];
int matrix[3][4];
9
in memory
Column 0 Column 1 Column 2 Column 3
Row 0
Row 1
Row 2
4
1
0
2
-1
2
4
3
0
-1
3
1
4 1 0 2
-1 2 4 3
0 -1 3 1
Accessing Array Elements
int matrix[3][4];
 matrix has 12 integer elements
 matrix[0][0] element in first row, first column
 matrix[2][3] element in last row, last column
 matrix is the address of the first element
 matrix[1] is the address of the Row 1
 matrix[1] is a one dimensional array (Row 1)
10
Initialization
int x[4][4] = { {2, 3, 7, 2},
{7, 4, 5, 9},
{5, 1, 6, -3},
{2, 5, -1, 3}};
int x[][4] = { {2, 3, 7, 2},
{7, 4, 5, 9},
{5, 1, 6, -3},
{2, 5, -1, 3}};
11
Initialization
int i, j, matrix[3][4];
for (i=0; i<3; i++)
for (j=0; j<4; j++)
matrix[i][j] = i;
12
j
0 1 2 3
0
1
2
i
0 0 0 0
1 1 1 1
2 2 2 2
j
0 1 2 3
0
1
2
i
0 1 2 3
0 1 2 3
0 1 2 3
matrix[i][j] = j;
Multidimensional Arrays
 An array can have many dimensions – if it has more than
one dimension, it is called a multidimensional array
 Each dimension subdivides the previous one into the
specified number of elements
 Each dimension has its own length constant
 Because each dimension is an array of array references,
the arrays within one dimension can be of different lengths
these are sometimes called ragged arrays
13
Strings
 A string is an array of characters
char data[10] = “Hello”;
char data2[] = {‘H’, ‘e’, ‘l’, ‘l’, ‘o’, ‘0’}
 Use printf to print strings
printf(“%s”,data);
 Can be accessed char by char
data[0] is first character
14
H e l l o 0
0 1 2 3 4 5 6 7 8 9
data
End of String
Symbol
Strings
 Each character has an integer representation
15
a b c d e z…………
97 98 99 100 101 ………………………112
A B C D E Z…………
65 66 67 68 69 ……………………… 90
0 1 2 3 4 98765
48 49 50 51 52 53 54 55 56 57
0
0
n
10
Strings
 Characters can be interpreted as integers
char c = ‘A’;
printf(“%c n”,c);
prints A
printf(“%d n”,c);
prints 65
Printf(“%c n”,65);
prints A
16
17

More Related Content

What's hot (19)

Multi-Dimensional Lists
Multi-Dimensional ListsMulti-Dimensional Lists
Multi-Dimensional Lists
 
Basic data structures in python
Basic data structures in pythonBasic data structures in python
Basic data structures in python
 
2D arrays
2D arrays2D arrays
2D arrays
 
Algo>Arrays
Algo>ArraysAlgo>Arrays
Algo>Arrays
 
Fuzzy sets
Fuzzy setsFuzzy sets
Fuzzy sets
 
Arrays
ArraysArrays
Arrays
 
Chap1 array
Chap1 arrayChap1 array
Chap1 array
 
List , tuples, dictionaries and regular expressions in python
List , tuples, dictionaries and regular expressions in pythonList , tuples, dictionaries and regular expressions in python
List , tuples, dictionaries and regular expressions in python
 
Day 4a iteration and functions.pptx
Day 4a   iteration and functions.pptxDay 4a   iteration and functions.pptx
Day 4a iteration and functions.pptx
 
Array
ArrayArray
Array
 
Day 4b iteration and functions for-loops.pptx
Day 4b   iteration and functions  for-loops.pptxDay 4b   iteration and functions  for-loops.pptx
Day 4b iteration and functions for-loops.pptx
 
2D-Array
2D-Array 2D-Array
2D-Array
 
bobok
bobokbobok
bobok
 
Python Lecture 11
Python Lecture 11Python Lecture 11
Python Lecture 11
 
Working with arrays in php
Working with arrays in phpWorking with arrays in php
Working with arrays in php
 
Basic R Data Manipulation
Basic R Data ManipulationBasic R Data Manipulation
Basic R Data Manipulation
 
Trie tree
Trie treeTrie tree
Trie tree
 
Coding test review
Coding test reviewCoding test review
Coding test review
 
Trie (1)
Trie (1)Trie (1)
Trie (1)
 

Similar to 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
 
Array
ArrayArray
Array
 
SP-First-Lecture.ppt
SP-First-Lecture.pptSP-First-Lecture.ppt
SP-First-Lecture.ppt
 
Lecture 15 - Array
Lecture 15 - ArrayLecture 15 - Array
Lecture 15 - Array
 
Array
ArrayArray
Array
 
Introduction to Arrays in C
Introduction to Arrays in CIntroduction to Arrays in C
Introduction to Arrays in C
 
Data structure array
Data structure  arrayData structure  array
Data structure array
 
C (PPS)Programming for problem solving.pptx
C (PPS)Programming for problem solving.pptxC (PPS)Programming for problem solving.pptx
C (PPS)Programming for problem solving.pptx
 
Mesics lecture 8 arrays in 'c'
Mesics lecture 8   arrays in 'c'Mesics lecture 8   arrays in 'c'
Mesics lecture 8 arrays in 'c'
 
Arrays in C language
Arrays in C languageArrays in C language
Arrays in C language
 
Arrays
ArraysArrays
Arrays
 
Arrays
ArraysArrays
Arrays
 
Arrays in c
Arrays in cArrays in c
Arrays in c
 
Chapter 13.pptx
Chapter 13.pptxChapter 13.pptx
Chapter 13.pptx
 
Comp102 lec 8
Comp102   lec 8Comp102   lec 8
Comp102 lec 8
 
Arrays and Strings
Arrays and Strings Arrays and Strings
Arrays and Strings
 
Array in C.pdf
Array in C.pdfArray in C.pdf
Array in C.pdf
 
Array.pdf
Array.pdfArray.pdf
Array.pdf
 
Arrays
ArraysArrays
Arrays
 
2DArrays.ppt
2DArrays.ppt2DArrays.ppt
2DArrays.ppt
 

More from Patel Raj

disposal of solid waste
disposal of solid waste disposal of solid waste
disposal of solid waste Patel Raj
 
Inner Product Space
Inner Product SpaceInner Product Space
Inner Product SpacePatel Raj
 
Communication Skill
Communication SkillCommunication Skill
Communication SkillPatel Raj
 
Water resources
Water  resourcesWater  resources
Water resourcesPatel Raj
 
Nanorobotics13.ppt
Nanorobotics13.pptNanorobotics13.ppt
Nanorobotics13.pptPatel Raj
 

More from Patel Raj (6)

disposal of solid waste
disposal of solid waste disposal of solid waste
disposal of solid waste
 
Hydrography
HydrographyHydrography
Hydrography
 
Inner Product Space
Inner Product SpaceInner Product Space
Inner Product Space
 
Communication Skill
Communication SkillCommunication Skill
Communication Skill
 
Water resources
Water  resourcesWater  resources
Water resources
 
Nanorobotics13.ppt
Nanorobotics13.pptNanorobotics13.ppt
Nanorobotics13.ppt
 

Recently uploaded

Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINESIVASHANKAR N
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performancesivaprakash250
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxupamatechverse
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduitsrknatarajan
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...Call Girls in Nagpur High Profile
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...ranjana rawat
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxupamatechverse
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).pptssuser5c9d4b1
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...ranjana rawat
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 

Recently uploaded (20)

Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduits
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 

Array

  • 1. (Managed By Shree Tapi Brahmcharyashram Sabha) Shree Swami AtmanandVidhya Sankul, Kapodra,Varachha Road, Surat, Gujarat, India. 395006 Phone: 0261-2573552 Fax No.: 0261-2573554 Email: ssasit@yahoo.in Web: www.ssasit.ac.in Shree Swami Atmanand Saraswati Institute of Technology, Surat Arrays1
  • 3. Arrays  Suppose, you need to store years of 100 cars. Will you define 100 variables? int y1, y2,…, y100;  An array is an indexed data structure to represent several variables having the same data type: int y[100]; 3 y[0] y[1] y[2] … y[k-1] y[k] y[k+1] … y[98] y[99]
  • 4. One-Dimensional Arrays (cont’d)  An element of an array is accessed using the array name and an index or subscript, for example: y[5] which can be used like a variable  In C, the subscripts always start with 0 and increment by 1, so y[5] is the sixth element  The name of the array is the address of the first element and the subscript is the offset 4 y[0] y[1] y[2] … y[k-1] y[k] y[k+1] … y[98] y[99]
  • 5. Definition and Initialization  An array is defined using a declaration statement. data_type array_name[size]; allocates memory for size elements subscript of first element is 0 subscript of last element is size-1 size must be a constant 5
  • 6. Example int list[5];  allocates memory for 5 integer variables  subscript of first element is 0  subscript of last element is 4  C does not check bounds on arrays  list[6] =5; /* may give segmentation fault or overwrite other memory locations*/ 6 list[0] list[1] list[2] list[3] list[4]
  • 7. Initializing Arrays  Arrays can be initialized at the time they are declared.  Examples: double rate[3] ={0.15, 0.25, 0.3}; char list[5] = {‘h’, ’e’, ’l’, ’l’, ’o’}; double vector[100] = {0.0}; /* assigns zero to all 100 elements */ int s[] = {5,0,-5}; /*the size of s is 3*/ 7
  • 8. Assigning values to an array8 For loops are often used to assign values to an array Example: int list[5], i; for(i=0; i<5; i++){ list[i] = i; } list[0] list[3] list[4] list[1] list[2] 0 1 2 3 4 list[0] list[1] list[2] list[3] list[4] OR for(i=0; i<=4; i++){ list[i] = i; }
  • 9. Matrices (2D-array)  A matrix is a set of numbers arranged in a grid with rows and columns.  A matrix is defined using a type declaration statement. datatype array_name[row_size][column_size]; int matrix[3][4]; 9 in memory Column 0 Column 1 Column 2 Column 3 Row 0 Row 1 Row 2 4 1 0 2 -1 2 4 3 0 -1 3 1 4 1 0 2 -1 2 4 3 0 -1 3 1
  • 10. Accessing Array Elements int matrix[3][4];  matrix has 12 integer elements  matrix[0][0] element in first row, first column  matrix[2][3] element in last row, last column  matrix is the address of the first element  matrix[1] is the address of the Row 1  matrix[1] is a one dimensional array (Row 1) 10
  • 11. Initialization int x[4][4] = { {2, 3, 7, 2}, {7, 4, 5, 9}, {5, 1, 6, -3}, {2, 5, -1, 3}}; int x[][4] = { {2, 3, 7, 2}, {7, 4, 5, 9}, {5, 1, 6, -3}, {2, 5, -1, 3}}; 11
  • 12. Initialization int i, j, matrix[3][4]; for (i=0; i<3; i++) for (j=0; j<4; j++) matrix[i][j] = i; 12 j 0 1 2 3 0 1 2 i 0 0 0 0 1 1 1 1 2 2 2 2 j 0 1 2 3 0 1 2 i 0 1 2 3 0 1 2 3 0 1 2 3 matrix[i][j] = j;
  • 13. Multidimensional Arrays  An array can have many dimensions – if it has more than one dimension, it is called a multidimensional array  Each dimension subdivides the previous one into the specified number of elements  Each dimension has its own length constant  Because each dimension is an array of array references, the arrays within one dimension can be of different lengths these are sometimes called ragged arrays 13
  • 14. Strings  A string is an array of characters char data[10] = “Hello”; char data2[] = {‘H’, ‘e’, ‘l’, ‘l’, ‘o’, ‘0’}  Use printf to print strings printf(“%s”,data);  Can be accessed char by char data[0] is first character 14 H e l l o 0 0 1 2 3 4 5 6 7 8 9 data End of String Symbol
  • 15. Strings  Each character has an integer representation 15 a b c d e z………… 97 98 99 100 101 ………………………112 A B C D E Z………… 65 66 67 68 69 ……………………… 90 0 1 2 3 4 98765 48 49 50 51 52 53 54 55 56 57 0 0 n 10
  • 16. Strings  Characters can be interpreted as integers char c = ‘A’; printf(“%c n”,c); prints A printf(“%d n”,c); prints 65 Printf(“%c n”,65); prints A 16
  • 17. 17