SlideShare a Scribd company logo
1 of 18
Arrays 
Trupti Agrawal 1
ARRAYS 
 The Array is the most commonly used Data Structure. 
 An array is a collection of data elements that are of the same type (e.g., 
a collection of integers, collection of characters, collection of doubles). 
OR 
 Array is a data structure that represents a collection of the same types 
of data. 
 The values held in an array are called array elements 
 An array stores multiple values of the same type – the element type 
 The element type can be a primitive type or an object reference 
 Therefore, we can create an array of integers, an array of characters, an 
array of String objects, an array of Coin objects, etc. 
Trupti Agrawal 2
Array Applications 
 Given a list of test scores, determine the maximum and minimum scores. 
 Read in a list of student names and rearrange them in alphabetical order 
(sorting). 
 Given the height measurements of students in a class, output the names of 
those students who are taller than average. 
Trupti Agrawal 3
Array Declaration 
 Syntax: 
<type> <arrayName>[<array_size>] 
Ex. int Ar[10]; 
 The array elements are all values of the type <type>. 
 The size of the array is indicated by <array_size>, the number 
of elements in the array. 
 <array_size> must be an int constant or a constant expression. 
Note that an array can have multiple dimensions. 
Trupti Agrawal 4
Array Declaration 
// array of 10 uninitialized ints 
int Ar[10]; 
0 1 2 3 4 5 6 7 8 9 
Ar -- -- -- -- -- -- -- -- -- -- 
0 1 2 3 4 5 
Trupti Agrawal 5
Subscripting 
 Declare an array of 10 integers: 
int Ar[10]; // array of 10 ints 
 To access an individual element we must apply a subscript to array named 
Ar. 
 A subscript is a bracketed expression. 
 The expression in the brackets is known as the index. 
 First element of array has index 0. 
Ar[0] 
 Second element of array has index 1, and so on. 
Ar[1], Ar[2], Ar[3],… 
 Last element has an index one less than the size of the array. 
Ar[9] 
 Incorrect indexing is a common error. 
Trupti Agrawal 6
Subscripting 
// array of 10 uninitialized ints 
int Ar[10]; 
Ar[3] = 1; 
int x = Ar[3]; 
0 1 2 3 4 5 6 7 8 9 
Ar -- -- -- 1 -- -- -- -- -- -- 
Ar[0] Ar[1] Ar[2] Ar[3] Ar[4] Ar[5] Ar[6] Ar[7] Ar[8] Ar[9] 
Trupti Agrawal 7
Subscripting Example 
#include <stdio.h> 
int main () 
{ 
int n[10]; /* n is an array of 10 integers */ 
int i,j; 
/* initialize elements of array n to 0 */ 
for ( i = 0; i < 10; i++ ) 
{ 
n[ i ] = i + 100; /* set element at location i to i + 100 */ 
} 
/* output each array element's value */ 
for (j = 0; j < 10; j++ ) 
{ 
printf("Element[%d] = %dn", j, n[j] ); 
} 
return 0; 
} 
Trupti Agrawal 8
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 
Trupti Agrawal 10
Multidimensional Arrays 
2 Dimensional 3 Dimensional 
Multiple dimensions get difficult to visualize graphically. 
• 
double Coord[100][100][100]; 
Trupti Agrawal 11
Processing Multi-dimensional Arrays 
The key to processing all cells of a multi-dimensional array is nested 
loops. 
[0] [1] [2] [3] 
[0] 
[1] 
[2] 
10 11 12 13 
14 15 16 17 
18 19 20 21 
22 23 24 25 
26 27 28 29 
[3] 
[4] 
for (int row=0; row!=5; row++) { 
for (int col=0; col!=4; col++) { 
System.out.println( myArray[row][col] ); 
} 
} 
for (int col=0; col!=4; col++) { 
for (int row=0; row!=5; row++) { 
System.out.println( myArray[row][col] ); 
} 
} 
Trupti Agrawal 12
Two-Dimensional Arrays 
• A one-dimensional array stores a list of elements 
• A two-dimensional array can be thought of as a table of 
elements, with rows and columns 
two 
dimensions 
one 
dimension 
Trupti Agrawal 13
Two-Dimensional Arrays [Cont…] 
A two-dimensional array consists of a certain number of rows and columns: 
const int NUMROWS = 3; 
const int NUMCOLS = 7; 
int Array[NUMROWS][NUMCOLS]; 
0 1 2 3 4 5 6 
0 4 18 9 3 -4 6 0 
1 12 45 74 15 0 98 0 
2 84 87 75 67 81 85 79 
Array[2][5] 3rd value in 6th column 
Array[0][4] 1st value in 5th column 
The declaration must specify the number of rows and the number of columns, 
and both must be constants. 
Trupti Agrawal 14
Address calculation using column 
and row major ordering 
 In computing, row-major order and column-major order describe methods 
for storing multidimensional arrays in linear memory. 
 By following standard matrix notation, rows are numbered by the first index 
of a two-dimensional array and columns by the second index. 
 Array layout is critical for correctly passing arrays between programs 
written in different languages. It is also important for performance when 
traversing an array because accessing array elements that are contiguous in 
memory is usually faster than accessing elements which are not, due 
to caching. 
Trupti Agrawal 15
Row-major order  In row-major storage, a multidimensional array in linear memory is 
organized such that rows are stored one after the other. It is the 
approach used by the C programming language, among others. 
 For example, consider this 2×3 array: 
1 2 3 
4 5 6 
 An array declared in C as 
int A[2][3] = { {1, 2, 3}, {4, 5, 6} }; 
is laid out contiguously in linear memory as: 
1 2 3 4 5 6 
Trupti Agrawal 16
Row-major order[Cont…] 
 To traverse this array in the order in which it is laid out in memory, one 
would use the following nested loop: 
for (row = 0; row < 2; row++) 
for (column = 0; column < 3; column++) 
printf("%dn", A[row][column]); 
Trupti Agrawal 17
Column-major order 
 Column-major order is a similar method of flattening arrays 
onto linear memory, but the columns are listed in sequence. 
 The scientific programming languages Fortran and Julia, the 
matrix-oriented languages MATLAB and Octave, use column-major 
ordering. The array 
1 2 3 
4 5 6 
 if stored contiguously in linear memory with column-major 
order looks like the following: 
1 4 2 5 3 6 
Trupti Agrawal 18
THANK YOU….. !!! 
Trupti Agrawal 19

More Related Content

What's hot (20)

Array ppt
Array pptArray ppt
Array ppt
 
Two dimensional arrays
Two dimensional arraysTwo dimensional arrays
Two dimensional arrays
 
2D Array
2D Array 2D Array
2D Array
 
Arrays in java
Arrays in javaArrays in java
Arrays in java
 
Array in c++
Array in c++Array in c++
Array in c++
 
Array
ArrayArray
Array
 
Arrays in c
Arrays in cArrays in c
Arrays in c
 
Arrays in java
Arrays in javaArrays in java
Arrays in java
 
Programming in c Arrays
Programming in c ArraysProgramming in c Arrays
Programming in c Arrays
 
Pointers in C Programming
Pointers in C ProgrammingPointers in C Programming
Pointers in C Programming
 
Array
ArrayArray
Array
 
Arrays in c
Arrays in cArrays in c
Arrays in c
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
 
One dimensional 2
One dimensional 2One dimensional 2
One dimensional 2
 
Arrays In C++
Arrays In C++Arrays In C++
Arrays In C++
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
 
Presentation on array
Presentation on array Presentation on array
Presentation on array
 
One Dimensional Array
One Dimensional Array One Dimensional Array
One Dimensional Array
 
C++ Arrays
C++ ArraysC++ Arrays
C++ Arrays
 
sparse matrix in data structure
sparse matrix in data structuresparse matrix in data structure
sparse matrix in data structure
 

Viewers also liked

English vocabulary quiz t5
English vocabulary quiz t5English vocabulary quiz t5
English vocabulary quiz t5jesusingles
 
Homophones
Homophones Homophones
Homophones Cherri21
 
Word Power Made Easy - Chapter 1
Word Power Made Easy - Chapter 1Word Power Made Easy - Chapter 1
Word Power Made Easy - Chapter 1Venkatram Sureddy
 
Alphabetical Order
Alphabetical OrderAlphabetical Order
Alphabetical OrderLee De Groft
 
2014 als a&e test elementary level test passers
2014 als a&e test elementary level test passers2014 als a&e test elementary level test passers
2014 als a&e test elementary level test passersArvic Lasaca
 
Alphabetical order 2 ppt tg 2012
Alphabetical order 2 ppt tg 2012Alphabetical order 2 ppt tg 2012
Alphabetical order 2 ppt tg 2012gavinnancarrow
 
Synonyms and Antonyms (Mashup)
Synonyms and Antonyms (Mashup) Synonyms and Antonyms (Mashup)
Synonyms and Antonyms (Mashup) Carla Meyer
 
Alphabetical order
Alphabetical orderAlphabetical order
Alphabetical orderBevan James
 
Vocabulary- One Word Substitutes
Vocabulary- One Word SubstitutesVocabulary- One Word Substitutes
Vocabulary- One Word Substitutessaraswathi tenneti
 
Odesk english skill test Answer 2014
Odesk english skill  test Answer 2014Odesk english skill  test Answer 2014
Odesk english skill test Answer 2014MD Riyad Rana
 
English Quiz - One word substitutes - Manu Melwin Joy
English Quiz - One word substitutes  - Manu Melwin JoyEnglish Quiz - One word substitutes  - Manu Melwin Joy
English Quiz - One word substitutes - Manu Melwin Joymanumelwin
 

Viewers also liked (20)

English vocabulary quiz t5
English vocabulary quiz t5English vocabulary quiz t5
English vocabulary quiz t5
 
Line basics
Line basicsLine basics
Line basics
 
Homophones
Homophones Homophones
Homophones
 
Word Power Made Easy - Chapter 1
Word Power Made Easy - Chapter 1Word Power Made Easy - Chapter 1
Word Power Made Easy - Chapter 1
 
Synonyms
SynonymsSynonyms
Synonyms
 
Alphabetical Order
Alphabetical OrderAlphabetical Order
Alphabetical Order
 
2014 als a&e test elementary level test passers
2014 als a&e test elementary level test passers2014 als a&e test elementary level test passers
2014 als a&e test elementary level test passers
 
Alphabetical order 2 ppt tg 2012
Alphabetical order 2 ppt tg 2012Alphabetical order 2 ppt tg 2012
Alphabetical order 2 ppt tg 2012
 
Alphabetical order
Alphabetical orderAlphabetical order
Alphabetical order
 
1 alphabetical order
1 alphabetical order1 alphabetical order
1 alphabetical order
 
Synonyms and Antonyms (Mashup)
Synonyms and Antonyms (Mashup) Synonyms and Antonyms (Mashup)
Synonyms and Antonyms (Mashup)
 
Synonyms
SynonymsSynonyms
Synonyms
 
Alphabetical order
Alphabetical orderAlphabetical order
Alphabetical order
 
Antonyms
AntonymsAntonyms
Antonyms
 
Alphabetical order
Alphabetical orderAlphabetical order
Alphabetical order
 
Vocabulary- One Word Substitutes
Vocabulary- One Word SubstitutesVocabulary- One Word Substitutes
Vocabulary- One Word Substitutes
 
Odesk english skill test Answer 2014
Odesk english skill  test Answer 2014Odesk english skill  test Answer 2014
Odesk english skill test Answer 2014
 
Group7 spelling
Group7 spelling Group7 spelling
Group7 spelling
 
English Quiz - One word substitutes - Manu Melwin Joy
English Quiz - One word substitutes  - Manu Melwin JoyEnglish Quiz - One word substitutes  - Manu Melwin Joy
English Quiz - One word substitutes - Manu Melwin Joy
 
Alphabetical order
Alphabetical orderAlphabetical order
Alphabetical order
 

Similar to Arrays (20)

Arrays
ArraysArrays
Arrays
 
Arrays
ArraysArrays
Arrays
 
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
 
Algo>Arrays
Algo>ArraysAlgo>Arrays
Algo>Arrays
 
Array
ArrayArray
Array
 
Arrays_in_c++.pptx
Arrays_in_c++.pptxArrays_in_c++.pptx
Arrays_in_c++.pptx
 
array-191103180006.pdf
array-191103180006.pdfarray-191103180006.pdf
array-191103180006.pdf
 
Arrays in C++
Arrays in C++Arrays in C++
Arrays in C++
 
Homework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdfHomework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdf
 
Unit 6. Arrays
Unit 6. ArraysUnit 6. Arrays
Unit 6. Arrays
 
Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm
 
Array assignment
Array assignmentArray assignment
Array assignment
 
Arrays
ArraysArrays
Arrays
 
ARRAYS
ARRAYSARRAYS
ARRAYS
 
7.basic array
7.basic array7.basic array
7.basic array
 
arrays.pptx
arrays.pptxarrays.pptx
arrays.pptx
 
Arrays
ArraysArrays
Arrays
 
Array and its types and it's implemented programming Final.pdf
Array and its types and it's implemented programming Final.pdfArray and its types and it's implemented programming Final.pdf
Array and its types and it's implemented programming Final.pdf
 
Lecture 15 - Array
Lecture 15 - ArrayLecture 15 - Array
Lecture 15 - Array
 

More from Trupti Agrawal

More from Trupti Agrawal (7)

Trees (data structure)
Trees (data structure)Trees (data structure)
Trees (data structure)
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
Searching algorithms
Searching algorithmsSearching algorithms
Searching algorithms
 
Searching algorithms
Searching algorithmsSearching algorithms
Searching algorithms
 
Linked list
Linked listLinked list
Linked list
 
Stacks and queues
Stacks and queuesStacks and queues
Stacks and queues
 
Data structure and algorithm
Data structure and algorithmData structure and algorithm
Data structure and algorithm
 

Recently uploaded

On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxPooja Bhuva
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxJisc
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxJisc
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
Philosophy of china and it's charactistics
Philosophy of china and it's charactisticsPhilosophy of china and it's charactistics
Philosophy of china and it's charactisticshameyhk98
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxDr. Sarita Anand
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsSandeep D Chaudhary
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Pooja Bhuva
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfNirmal Dwivedi
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxannathomasp01
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxDr. Ravikiran H M Gowda
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 
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...Poonam Aher Patil
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Pooja Bhuva
 

Recently uploaded (20)

On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Philosophy of china and it's charactistics
Philosophy of china and it's charactisticsPhilosophy of china and it's charactistics
Philosophy of china and it's charactistics
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
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...
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 

Arrays

  • 2. ARRAYS  The Array is the most commonly used Data Structure.  An array is a collection of data elements that are of the same type (e.g., a collection of integers, collection of characters, collection of doubles). OR  Array is a data structure that represents a collection of the same types of data.  The values held in an array are called array elements  An array stores multiple values of the same type – the element type  The element type can be a primitive type or an object reference  Therefore, we can create an array of integers, an array of characters, an array of String objects, an array of Coin objects, etc. Trupti Agrawal 2
  • 3. Array Applications  Given a list of test scores, determine the maximum and minimum scores.  Read in a list of student names and rearrange them in alphabetical order (sorting).  Given the height measurements of students in a class, output the names of those students who are taller than average. Trupti Agrawal 3
  • 4. Array Declaration  Syntax: <type> <arrayName>[<array_size>] Ex. int Ar[10];  The array elements are all values of the type <type>.  The size of the array is indicated by <array_size>, the number of elements in the array.  <array_size> must be an int constant or a constant expression. Note that an array can have multiple dimensions. Trupti Agrawal 4
  • 5. Array Declaration // array of 10 uninitialized ints int Ar[10]; 0 1 2 3 4 5 6 7 8 9 Ar -- -- -- -- -- -- -- -- -- -- 0 1 2 3 4 5 Trupti Agrawal 5
  • 6. Subscripting  Declare an array of 10 integers: int Ar[10]; // array of 10 ints  To access an individual element we must apply a subscript to array named Ar.  A subscript is a bracketed expression.  The expression in the brackets is known as the index.  First element of array has index 0. Ar[0]  Second element of array has index 1, and so on. Ar[1], Ar[2], Ar[3],…  Last element has an index one less than the size of the array. Ar[9]  Incorrect indexing is a common error. Trupti Agrawal 6
  • 7. Subscripting // array of 10 uninitialized ints int Ar[10]; Ar[3] = 1; int x = Ar[3]; 0 1 2 3 4 5 6 7 8 9 Ar -- -- -- 1 -- -- -- -- -- -- Ar[0] Ar[1] Ar[2] Ar[3] Ar[4] Ar[5] Ar[6] Ar[7] Ar[8] Ar[9] Trupti Agrawal 7
  • 8. Subscripting Example #include <stdio.h> int main () { int n[10]; /* n is an array of 10 integers */ int i,j; /* initialize elements of array n to 0 */ for ( i = 0; i < 10; i++ ) { n[ i ] = i + 100; /* set element at location i to i + 100 */ } /* output each array element's value */ for (j = 0; j < 10; j++ ) { printf("Element[%d] = %dn", j, n[j] ); } return 0; } Trupti Agrawal 8
  • 9. 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 Trupti Agrawal 10
  • 10. Multidimensional Arrays 2 Dimensional 3 Dimensional Multiple dimensions get difficult to visualize graphically. • double Coord[100][100][100]; Trupti Agrawal 11
  • 11. Processing Multi-dimensional Arrays The key to processing all cells of a multi-dimensional array is nested loops. [0] [1] [2] [3] [0] [1] [2] 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 [3] [4] for (int row=0; row!=5; row++) { for (int col=0; col!=4; col++) { System.out.println( myArray[row][col] ); } } for (int col=0; col!=4; col++) { for (int row=0; row!=5; row++) { System.out.println( myArray[row][col] ); } } Trupti Agrawal 12
  • 12. Two-Dimensional Arrays • A one-dimensional array stores a list of elements • A two-dimensional array can be thought of as a table of elements, with rows and columns two dimensions one dimension Trupti Agrawal 13
  • 13. Two-Dimensional Arrays [Cont…] A two-dimensional array consists of a certain number of rows and columns: const int NUMROWS = 3; const int NUMCOLS = 7; int Array[NUMROWS][NUMCOLS]; 0 1 2 3 4 5 6 0 4 18 9 3 -4 6 0 1 12 45 74 15 0 98 0 2 84 87 75 67 81 85 79 Array[2][5] 3rd value in 6th column Array[0][4] 1st value in 5th column The declaration must specify the number of rows and the number of columns, and both must be constants. Trupti Agrawal 14
  • 14. Address calculation using column and row major ordering  In computing, row-major order and column-major order describe methods for storing multidimensional arrays in linear memory.  By following standard matrix notation, rows are numbered by the first index of a two-dimensional array and columns by the second index.  Array layout is critical for correctly passing arrays between programs written in different languages. It is also important for performance when traversing an array because accessing array elements that are contiguous in memory is usually faster than accessing elements which are not, due to caching. Trupti Agrawal 15
  • 15. Row-major order  In row-major storage, a multidimensional array in linear memory is organized such that rows are stored one after the other. It is the approach used by the C programming language, among others.  For example, consider this 2×3 array: 1 2 3 4 5 6  An array declared in C as int A[2][3] = { {1, 2, 3}, {4, 5, 6} }; is laid out contiguously in linear memory as: 1 2 3 4 5 6 Trupti Agrawal 16
  • 16. Row-major order[Cont…]  To traverse this array in the order in which it is laid out in memory, one would use the following nested loop: for (row = 0; row < 2; row++) for (column = 0; column < 3; column++) printf("%dn", A[row][column]); Trupti Agrawal 17
  • 17. Column-major order  Column-major order is a similar method of flattening arrays onto linear memory, but the columns are listed in sequence.  The scientific programming languages Fortran and Julia, the matrix-oriented languages MATLAB and Octave, use column-major ordering. The array 1 2 3 4 5 6  if stored contiguously in linear memory with column-major order looks like the following: 1 4 2 5 3 6 Trupti Agrawal 18
  • 18. THANK YOU….. !!! Trupti Agrawal 19

Editor's Notes

  1. Collection of same type of data elements. Contiguous memory location.