SlideShare a Scribd company logo
1 of 29
Array
A Linear Data Structure
SONI GUPTA
ASSISTANT PROFESSOR (NPIU-TEQIP III )
HBTU KANPUR, UP
TEQIP
-III
Topics to be covered
 What is an array?
 Declaration of an array
 Initialization of an array
 Examples
 Representation of an array in memory
 Pointers and array
 2-dimensional array
 Representation in row major order and column major order
 Example: Representation of Lower triangular matrix
 Example: Representation of tri diagonal matrix
 Example: Representation of X-matrix
 Disadvantage
TEQIP
-III
Array
 Array is a collection of similar elements having same data type, accessed using a
common name.
 The elements of an array occupy contiguous memory locations.
100 104 108 112 116 120
TEQIP
-III
Dimension of an array:
 In C language there is no limitation on the dimension on an array
1-Dimensional
• List
• Vector
2-dimensional
• Table
• Matrix
3-dimensional
• 3-d matrix
TEQIP
-III
Declaration of an array
 Type-name variable-name [size];
 The array is indexed from 0 to size-1
 The size (in brackets) must be an integer literal or a constant variable.)
 Based on the size declared the compiler determine how much space to allocate in
memory.
TEQIP
-III
Examples:
 int list[5]; // an array of 5 integers
list
list[0]=4, list[1]=6, list[2]=1, list[4]=50
 char name[5]; // an array of 5 characters
name
TEQIP
-III
4 6 1 100 50
a i o u e
 int table[2][3]; // a two dimensional array of integers
table
table[0][0]=5, table[0][1]=9, table[1][2]=5,
 Int location[5][5][10]: // a three dimensional array of integer
 Int *a[4] // an array of pointer/address
 a
5 9 14
1 3 5
1004 1008 1120 1222
TEQIP
-III
Initializing an Array
 int list[5]={1,2,5,6,1};
 char [20]={a,r,w,q,q,t}; // all uninitialized values will set to zero.
 int table[2][3]={{2,3,8}{3,4,9}};
 int count[ 100 ] = { 1, 2, 3, [10] = 10, 11, 12, [60] = 50, [42] = 500 };
TEQIP
-III
Representation of an array in memory
0 1000 (Base address)
1 1004
2 1008
3 1012
4 1016
IndexValues Address (in Byte)
4
6
1
100
50
TEQIP
-III
Pointers and Array
 An array variable is actually a pointer which hold the address of the first element
of the array.
 We can access array value by array notations or pointer value.
 a[0]=*a
 a[1]=*(a+1)
 a[2]=*(a+2)
TEQIP
-III
 a = a+0 = &a[0]
 a+1 = &a[1]
 a+i = &a[i]
 &(*(a+i)) = &a[i] = a+i
 *(&a[i]) = *(a+i) = a[i]
 Address of an element i of array a = a + i * sizeof(element)
TEQIP
-III
2-dimensional Array
 Suppose a 2-d array A[i][j];
 Row major order
Location of A[i][j] = A + ( Number of rows * i + j )*sizeof(element)
 For Column Major Order
Location of A[i][j] = A + ( Number of Columns * j + i )*sizeof(element)
 A[i][j]= *(*(A + i) + j)= *(A[i] + j) = (*(A+i))[j]
 &A[i][j]= *(A + i) + j = A[i] + j
TEQIP
-III
Row major order
 Row major order𝒂 𝟏𝟏 𝒂 𝟏𝟐 𝒂 𝟏𝟑
𝒂 𝟐𝟏 𝒂 𝟐𝟐 𝒂 𝟐𝟑
𝒂 𝟑𝟏 𝒂 𝟑𝟐 𝒂 𝟑𝟑
𝒂 𝟏𝟏 𝒂 𝟏𝟐 𝒂 𝟏𝟑 𝒂 𝟐𝟏 𝒂 𝟐𝟐 𝒂 𝟐𝟑 𝒂 𝟑𝟏 𝒂 𝟑𝟐 𝒂 𝟑𝟑
TEQIP
-III
TEQIP
-III
Row major order
 for (i=1,i<=3,i++)
 for (j=1,j<=3,j++)
 {
 Printf(“Enter the element”);
 Scanf(“%d”,A[i][j]);
 }
TEQIP
-III
Column major order
 Column major order
𝒂 𝟏𝟏 𝒂 𝟏𝟐 𝒂 𝟏𝟑
𝒂 𝟐𝟏 𝒂 𝟐𝟐 𝒂 𝟐𝟑
𝒂 𝟑𝟏 𝒂 𝟑𝟐 𝒂 𝟑𝟑
𝒂 𝟏𝟏 𝒂 𝟐𝟏 𝒂 𝟑𝟏 𝒂 𝟏𝟐 𝒂 𝟐𝟐 𝒂 𝟑𝟐 𝒂 𝟏𝟑 𝒂 𝟐𝟑 𝒂 𝟑𝟑
TEQIP
-III
TEQIP
-III
Column major order
 for (j=1,j<=3,j++)
 for (i=1,i<=3,i++)
 {
 Printf(“Enter the element”);
 Scanf(“%d”,A[i][j]);
 }
TEQIP
-III
 Row major order
 for (i=1,i<=3,i++)
 for (j=1,j<=3,j++)
 {
 Printf(“Enter the element”);
 Scanf(“%d”,A[i][j]);
 }
TEQIP
-III
Character Strings as Arrays of Characters
 Strings are stored in an array of character (char) type along with the null
terminating character "0" at the end
 char name[ ] = { ‘I', ‘N', ‘D',‘I', ‘A', 'O'};
OR char name[ ] = “INDIA";
'0' is not necessary. C inserts the null character automatically.
length of string= 5
size of string=6
TEQIP
-III
Example : Lower Triangular Matrix
0 0 0 0 0
0 0 0 0 𝒂 𝟐𝟓
0 0 0 𝒂 𝟑𝟒 𝒂 𝟑𝟓
0 0 𝒂 𝟒𝟑 𝒂 𝟒𝟒 𝒂 𝟒𝟓
0 𝒂 𝟓𝟐 𝒂 𝟓𝟑 𝒂 𝟓𝟒 𝒂 𝟓𝟓
TEQIP
-III
Column major order
TEQIP
-III
Column major order
𝒂 𝟓𝟐 𝒂 𝟒𝟑 𝒂 𝟓𝟑 𝒂 𝟑𝟒 𝒂 𝟒𝟒 𝒂 𝟓𝟒 𝒂 𝟐𝟓 𝒂 𝟑𝟓 𝒂 𝟒𝟓 𝒂 𝟓𝟓
0-element 1-element 3=1+2
element
6=1+2+3
element
TEQIP
-III
Colum major order
𝒂 𝟓𝟐 𝒂 𝟒𝟑 𝒂 𝟓𝟑 𝒂 𝟑𝟒 𝒂 𝟒𝟒 𝒂 𝟓𝟒 𝒂 𝟐𝟓 𝒂 𝟑𝟓 𝒂 𝟒𝟓 𝒂 𝟓𝟓
0-element 1-element 3=1+2
element
6=1+2+3
element
(𝑗−2)(𝑗−1)
2
+ 𝑖 + 𝑗 − 𝑁 − 2 th element
Where N is number of row/column
0 2 81 7 93 4 5 6
TEQIP
-III
Row major Order
TEQIP
-III
Row major order
𝒂 𝟐𝟓 𝒂 𝟑𝟒 𝒂 𝟑𝟓 𝒂 𝟒𝟑 𝒂 𝟒𝟒 𝒂 𝟒𝟓 𝒂 𝟓𝟐 𝒂 𝟓𝟑 𝒂 𝟓𝟒 𝒂 𝟓𝟓
1-element 3=1+2
element
6=1+2+3
element
(𝑖−2)(𝑖−1)
2
+ 𝑖 + 𝑗 − 𝑁 − 2 th element
Where N is number of row/column
0 2 81 7 93 4 5 6
TEQIP
-III
Example: Tri-diagonal matrix
𝒂 𝟏𝟏 𝒂 𝟏𝟐 𝟎 0
𝒂 𝟐𝟏 𝒂 𝟐𝟐 𝒂 𝟐𝟑 0
0 𝒂 𝟑𝟐 𝒂 𝟑𝟑 𝒂 𝟑𝟒
0 0 𝒂 𝟒𝟑 𝒂 𝟒𝟒
TEQIP
-III
Row major order
 0 1 2 3 4 5 6 7 8 9
𝒂 𝟏𝟏 𝒂 𝟏𝟐 𝒂 𝟐𝟏 𝒂 𝟐𝟐 𝒂 𝟐𝟑 𝒂 𝟑𝟐 𝒂 𝟑𝟑 𝒂 𝟑𝟒 𝒂 𝟒𝟑 𝒂 𝟒𝟒
TEQIP
-III
Example: Diagonal Matrix
𝒂 𝟏𝟏 𝟎 𝟎 0 𝒂 𝟏𝟓
𝟎 𝒂 𝟐𝟐 𝟎 𝒂 𝟐𝟒 0
𝟎 𝟎 𝒂 𝟑𝟑 𝟎 0
𝟎 𝒂 𝟒𝟐 𝟎 𝒂 𝟒𝟒 0
𝒂 𝟓𝟏 0 0 0 𝒂 𝟓𝟓
TEQIP
-III
Example: Diagonal Matrix
𝒂 𝟏𝟏 𝟎 𝟎 0 𝒂 𝟏𝟓
𝟎 𝒂 𝟐𝟐 𝟎 𝒂 𝟐𝟒 0
𝟎 𝟎 𝒂 𝟑𝟑 𝟎 0
𝟎 𝒂 𝟒𝟐 𝟎 𝒂 𝟒𝟒 0
𝒂 𝟓𝟏 0 0 0 𝒂 𝟓𝟓
TEQIP
-III
Disadvantage
 The number of element must be known in advance.
 Array is static in nature i.e array size is fixed and it is allocated at compile time. The
memory which is allocated to array can not be increased or decreased.
 If we will allocate memory more than required then space will be wasted.
 The elements of an array are stored in consecutive memory locations. Therefore
insertion and deletion is difficult and time consuming.
TEQIP
-III

More Related Content

What's hot (20)

C Programming : Arrays
C Programming : ArraysC Programming : Arrays
C Programming : Arrays
 
DATASTRUCTURES UNIT-1
DATASTRUCTURES UNIT-1DATASTRUCTURES UNIT-1
DATASTRUCTURES UNIT-1
 
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
 
C programming slide c05
C programming slide c05C programming slide c05
C programming slide c05
 
Pf presntation
Pf presntationPf presntation
Pf presntation
 
Chapter 5 ds
Chapter 5 dsChapter 5 ds
Chapter 5 ds
 
Numpy
NumpyNumpy
Numpy
 
2- Dimensional Arrays
2- Dimensional Arrays2- Dimensional Arrays
2- Dimensional Arrays
 
Arrays
ArraysArrays
Arrays
 
2D Array
2D Array 2D Array
2D Array
 
Introduction to Arrays in C
Introduction to Arrays in CIntroduction to Arrays in C
Introduction to Arrays in C
 
Lecture17 arrays.ppt
Lecture17 arrays.pptLecture17 arrays.ppt
Lecture17 arrays.ppt
 
Lecture 16 - Multi dimensional Array
Lecture 16 - Multi dimensional ArrayLecture 16 - Multi dimensional Array
Lecture 16 - Multi dimensional Array
 
Two dimensional array
Two dimensional arrayTwo dimensional array
Two dimensional array
 
Multi-Dimensional Lists
Multi-Dimensional ListsMulti-Dimensional Lists
Multi-Dimensional Lists
 
Preparation Data Structures 06 arrays representation
Preparation Data Structures 06 arrays representationPreparation Data Structures 06 arrays representation
Preparation Data Structures 06 arrays representation
 
Multidimensional array in C
Multidimensional array in CMultidimensional array in C
Multidimensional array in C
 
Comp102 lec 8
Comp102   lec 8Comp102   lec 8
Comp102 lec 8
 
Two dimensional arrays
Two dimensional arraysTwo dimensional arrays
Two dimensional arrays
 
Two dimensional array
Two dimensional arrayTwo dimensional array
Two dimensional array
 

Similar to Array Data Structures (20)

Arrays
ArraysArrays
Arrays
 
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
 
Lecture 15 - Array
Lecture 15 - ArrayLecture 15 - Array
Lecture 15 - Array
 
Arrays
ArraysArrays
Arrays
 
STRING LIST TUPLE DICTIONARY FILE.pdf
STRING LIST TUPLE DICTIONARY FILE.pdfSTRING LIST TUPLE DICTIONARY FILE.pdf
STRING LIST TUPLE DICTIONARY FILE.pdf
 
Arrays and library functions
Arrays and library functionsArrays and library functions
Arrays and library functions
 
Arrays
ArraysArrays
Arrays
 
Arrays in C language
Arrays in C languageArrays in C language
Arrays in C language
 
cluod.pdf
cluod.pdfcluod.pdf
cluod.pdf
 
arrays-130116232821-phpapp02.pdf
arrays-130116232821-phpapp02.pdfarrays-130116232821-phpapp02.pdf
arrays-130116232821-phpapp02.pdf
 
Session 7 En
Session 7 EnSession 7 En
Session 7 En
 
Session 7 En
Session 7 EnSession 7 En
Session 7 En
 
Arrays
ArraysArrays
Arrays
 
Array assignment
Array assignmentArray assignment
Array assignment
 
Algo>Arrays
Algo>ArraysAlgo>Arrays
Algo>Arrays
 
Unit 6. Arrays
Unit 6. ArraysUnit 6. Arrays
Unit 6. Arrays
 
Topic20Arrays_Part2.ppt
Topic20Arrays_Part2.pptTopic20Arrays_Part2.ppt
Topic20Arrays_Part2.ppt
 
Matrices
MatricesMatrices
Matrices
 
19-Lec - Multidimensional Arrays.ppt
19-Lec - Multidimensional Arrays.ppt19-Lec - Multidimensional Arrays.ppt
19-Lec - Multidimensional Arrays.ppt
 
02 arrays
02 arrays02 arrays
02 arrays
 

Recently uploaded

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
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerAnamika Sarkar
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSSIVASHANKAR N
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxpranjaldaimarysona
 
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
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSCAESB
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
High Profile Call Girls Nashik Megha 7001305949 Independent Escort Service Na...
High Profile Call Girls Nashik Megha 7001305949 Independent Escort Service Na...High Profile Call Girls Nashik Megha 7001305949 Independent Escort Service Na...
High Profile Call Girls Nashik Megha 7001305949 Independent Escort Service Na...Call Girls in Nagpur High Profile
 
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
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineeringmalavadedarshan25
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLDeelipZope
 
Analog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAnalog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAbhinavSharma374939
 

Recently uploaded (20)

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
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 
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...
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentation
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
High Profile Call Girls Nashik Megha 7001305949 Independent Escort Service Na...
High Profile Call Girls Nashik Megha 7001305949 Independent Escort Service Na...High Profile Call Girls Nashik Megha 7001305949 Independent Escort Service Na...
High Profile Call Girls Nashik Megha 7001305949 Independent Escort Service Na...
 
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
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineering
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCL
 
Analog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAnalog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog Converter
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 

Array Data Structures

  • 1. Array A Linear Data Structure SONI GUPTA ASSISTANT PROFESSOR (NPIU-TEQIP III ) HBTU KANPUR, UP TEQIP -III
  • 2. Topics to be covered  What is an array?  Declaration of an array  Initialization of an array  Examples  Representation of an array in memory  Pointers and array  2-dimensional array  Representation in row major order and column major order  Example: Representation of Lower triangular matrix  Example: Representation of tri diagonal matrix  Example: Representation of X-matrix  Disadvantage TEQIP -III
  • 3. Array  Array is a collection of similar elements having same data type, accessed using a common name.  The elements of an array occupy contiguous memory locations. 100 104 108 112 116 120 TEQIP -III
  • 4. Dimension of an array:  In C language there is no limitation on the dimension on an array 1-Dimensional • List • Vector 2-dimensional • Table • Matrix 3-dimensional • 3-d matrix TEQIP -III
  • 5. Declaration of an array  Type-name variable-name [size];  The array is indexed from 0 to size-1  The size (in brackets) must be an integer literal or a constant variable.)  Based on the size declared the compiler determine how much space to allocate in memory. TEQIP -III
  • 6. Examples:  int list[5]; // an array of 5 integers list list[0]=4, list[1]=6, list[2]=1, list[4]=50  char name[5]; // an array of 5 characters name TEQIP -III 4 6 1 100 50 a i o u e
  • 7.  int table[2][3]; // a two dimensional array of integers table table[0][0]=5, table[0][1]=9, table[1][2]=5,  Int location[5][5][10]: // a three dimensional array of integer  Int *a[4] // an array of pointer/address  a 5 9 14 1 3 5 1004 1008 1120 1222 TEQIP -III
  • 8. Initializing an Array  int list[5]={1,2,5,6,1};  char [20]={a,r,w,q,q,t}; // all uninitialized values will set to zero.  int table[2][3]={{2,3,8}{3,4,9}};  int count[ 100 ] = { 1, 2, 3, [10] = 10, 11, 12, [60] = 50, [42] = 500 }; TEQIP -III
  • 9. Representation of an array in memory 0 1000 (Base address) 1 1004 2 1008 3 1012 4 1016 IndexValues Address (in Byte) 4 6 1 100 50 TEQIP -III
  • 10. Pointers and Array  An array variable is actually a pointer which hold the address of the first element of the array.  We can access array value by array notations or pointer value.  a[0]=*a  a[1]=*(a+1)  a[2]=*(a+2) TEQIP -III
  • 11.  a = a+0 = &a[0]  a+1 = &a[1]  a+i = &a[i]  &(*(a+i)) = &a[i] = a+i  *(&a[i]) = *(a+i) = a[i]  Address of an element i of array a = a + i * sizeof(element) TEQIP -III
  • 12. 2-dimensional Array  Suppose a 2-d array A[i][j];  Row major order Location of A[i][j] = A + ( Number of rows * i + j )*sizeof(element)  For Column Major Order Location of A[i][j] = A + ( Number of Columns * j + i )*sizeof(element)  A[i][j]= *(*(A + i) + j)= *(A[i] + j) = (*(A+i))[j]  &A[i][j]= *(A + i) + j = A[i] + j TEQIP -III
  • 13. Row major order  Row major order𝒂 𝟏𝟏 𝒂 𝟏𝟐 𝒂 𝟏𝟑 𝒂 𝟐𝟏 𝒂 𝟐𝟐 𝒂 𝟐𝟑 𝒂 𝟑𝟏 𝒂 𝟑𝟐 𝒂 𝟑𝟑 𝒂 𝟏𝟏 𝒂 𝟏𝟐 𝒂 𝟏𝟑 𝒂 𝟐𝟏 𝒂 𝟐𝟐 𝒂 𝟐𝟑 𝒂 𝟑𝟏 𝒂 𝟑𝟐 𝒂 𝟑𝟑 TEQIP -III TEQIP -III
  • 14. Row major order  for (i=1,i<=3,i++)  for (j=1,j<=3,j++)  {  Printf(“Enter the element”);  Scanf(“%d”,A[i][j]);  } TEQIP -III
  • 15. Column major order  Column major order 𝒂 𝟏𝟏 𝒂 𝟏𝟐 𝒂 𝟏𝟑 𝒂 𝟐𝟏 𝒂 𝟐𝟐 𝒂 𝟐𝟑 𝒂 𝟑𝟏 𝒂 𝟑𝟐 𝒂 𝟑𝟑 𝒂 𝟏𝟏 𝒂 𝟐𝟏 𝒂 𝟑𝟏 𝒂 𝟏𝟐 𝒂 𝟐𝟐 𝒂 𝟑𝟐 𝒂 𝟏𝟑 𝒂 𝟐𝟑 𝒂 𝟑𝟑 TEQIP -III TEQIP -III
  • 16. Column major order  for (j=1,j<=3,j++)  for (i=1,i<=3,i++)  {  Printf(“Enter the element”);  Scanf(“%d”,A[i][j]);  } TEQIP -III
  • 17.  Row major order  for (i=1,i<=3,i++)  for (j=1,j<=3,j++)  {  Printf(“Enter the element”);  Scanf(“%d”,A[i][j]);  } TEQIP -III
  • 18. Character Strings as Arrays of Characters  Strings are stored in an array of character (char) type along with the null terminating character "0" at the end  char name[ ] = { ‘I', ‘N', ‘D',‘I', ‘A', 'O'}; OR char name[ ] = “INDIA"; '0' is not necessary. C inserts the null character automatically. length of string= 5 size of string=6 TEQIP -III
  • 19. Example : Lower Triangular Matrix 0 0 0 0 0 0 0 0 0 𝒂 𝟐𝟓 0 0 0 𝒂 𝟑𝟒 𝒂 𝟑𝟓 0 0 𝒂 𝟒𝟑 𝒂 𝟒𝟒 𝒂 𝟒𝟓 0 𝒂 𝟓𝟐 𝒂 𝟓𝟑 𝒂 𝟓𝟒 𝒂 𝟓𝟓 TEQIP -III
  • 21. Column major order 𝒂 𝟓𝟐 𝒂 𝟒𝟑 𝒂 𝟓𝟑 𝒂 𝟑𝟒 𝒂 𝟒𝟒 𝒂 𝟓𝟒 𝒂 𝟐𝟓 𝒂 𝟑𝟓 𝒂 𝟒𝟓 𝒂 𝟓𝟓 0-element 1-element 3=1+2 element 6=1+2+3 element TEQIP -III
  • 22. Colum major order 𝒂 𝟓𝟐 𝒂 𝟒𝟑 𝒂 𝟓𝟑 𝒂 𝟑𝟒 𝒂 𝟒𝟒 𝒂 𝟓𝟒 𝒂 𝟐𝟓 𝒂 𝟑𝟓 𝒂 𝟒𝟓 𝒂 𝟓𝟓 0-element 1-element 3=1+2 element 6=1+2+3 element (𝑗−2)(𝑗−1) 2 + 𝑖 + 𝑗 − 𝑁 − 2 th element Where N is number of row/column 0 2 81 7 93 4 5 6 TEQIP -III
  • 24. Row major order 𝒂 𝟐𝟓 𝒂 𝟑𝟒 𝒂 𝟑𝟓 𝒂 𝟒𝟑 𝒂 𝟒𝟒 𝒂 𝟒𝟓 𝒂 𝟓𝟐 𝒂 𝟓𝟑 𝒂 𝟓𝟒 𝒂 𝟓𝟓 1-element 3=1+2 element 6=1+2+3 element (𝑖−2)(𝑖−1) 2 + 𝑖 + 𝑗 − 𝑁 − 2 th element Where N is number of row/column 0 2 81 7 93 4 5 6 TEQIP -III
  • 25. Example: Tri-diagonal matrix 𝒂 𝟏𝟏 𝒂 𝟏𝟐 𝟎 0 𝒂 𝟐𝟏 𝒂 𝟐𝟐 𝒂 𝟐𝟑 0 0 𝒂 𝟑𝟐 𝒂 𝟑𝟑 𝒂 𝟑𝟒 0 0 𝒂 𝟒𝟑 𝒂 𝟒𝟒 TEQIP -III
  • 26. Row major order  0 1 2 3 4 5 6 7 8 9 𝒂 𝟏𝟏 𝒂 𝟏𝟐 𝒂 𝟐𝟏 𝒂 𝟐𝟐 𝒂 𝟐𝟑 𝒂 𝟑𝟐 𝒂 𝟑𝟑 𝒂 𝟑𝟒 𝒂 𝟒𝟑 𝒂 𝟒𝟒 TEQIP -III
  • 27. Example: Diagonal Matrix 𝒂 𝟏𝟏 𝟎 𝟎 0 𝒂 𝟏𝟓 𝟎 𝒂 𝟐𝟐 𝟎 𝒂 𝟐𝟒 0 𝟎 𝟎 𝒂 𝟑𝟑 𝟎 0 𝟎 𝒂 𝟒𝟐 𝟎 𝒂 𝟒𝟒 0 𝒂 𝟓𝟏 0 0 0 𝒂 𝟓𝟓 TEQIP -III
  • 28. Example: Diagonal Matrix 𝒂 𝟏𝟏 𝟎 𝟎 0 𝒂 𝟏𝟓 𝟎 𝒂 𝟐𝟐 𝟎 𝒂 𝟐𝟒 0 𝟎 𝟎 𝒂 𝟑𝟑 𝟎 0 𝟎 𝒂 𝟒𝟐 𝟎 𝒂 𝟒𝟒 0 𝒂 𝟓𝟏 0 0 0 𝒂 𝟓𝟓 TEQIP -III
  • 29. Disadvantage  The number of element must be known in advance.  Array is static in nature i.e array size is fixed and it is allocated at compile time. The memory which is allocated to array can not be increased or decreased.  If we will allocate memory more than required then space will be wasted.  The elements of an array are stored in consecutive memory locations. Therefore insertion and deletion is difficult and time consuming. TEQIP -III

Editor's Notes

  1. Array variables are declared identically to variables of their data type, except that the variable name is followed by one pair of square [ ] brackets for each dimension of the array. Space is allocated only once, at the time the array is declared. The array does NOT change sizes later if the variable used to declare it changes.
  2. We can declare an array as we declare a variable. Simply list the array values in set notation { } after the declaration If an array is to be completely initialized, the dimension of the array is not required.  The compiler will automatically size the array to fit the initialized data.  If the size of the array is not given, then the largest initialized position determines the size of the array.