SlideShare a Scribd company logo
1 of 41
• An array is a collection of similar data elements.
• These data elements have the same data type.
• Elements of arrays are stored in consecutive memory locations and are referenced
by an index (also known as the subscript).
• Declaring an array means specifying three things:
Data type - what kind of values it can store. For example, int, char, float
Name - to identify the array
Size - the maximum number of values that the array can hold
• Arrays are declared using the following syntax:
type name[size];
1st
element
2nd
element
3rd
element
4th
element
5th
element
6th
element
7th
element
8th
element
9th
element
10th
element
marks[0] marks[1] marks[2] marks[3] marks[4] marks[5] marks[6] marks[7] marks[8] marks[9]
Introduction
Accessing Elements of an Array
• To access all the elements of an array, we must use a loop.
• That is, we can access all the elements of an array by varying
the value of the subscript into the array.
• But note that the subscript must be an integral value or an
expression that evaluates to an integral value.
int i, marks[10];
for(i=0;i<10;i++)
marks[i] = -1;
Calculating the Address of Array
Elements
• Address of data element, A[k] = BA(A) + w( k – lower_bound)
where
A is the array
k is the index of the element whose address we have to calculate
BA is the base address of the array A
w is the word size of one element in memory. For example, size of
int is 2
99 67 78 56 88 90 34 85
marks[0] marks[1] marks[2] marks[3] marks[4] marks[5] marks[6] marks[7]
1000 1002 1004 1006 1008 1010 1012 1014
marks[4] = 1000 + 2(4 – 0)
= 1000 + 2(4) = 1008
Storing Values in Arrays
Store values in the array
Initialize the
elements
Input values for
the elements
Assign values to the
elements
int i, marks[10];
for(i=0;i<10;i++)
scanf(“%d”, &marks[i]);
int i, arr1[10], arr2[10];
for(i=0;i<10;i++)
arr2[i] = arr1[i];
Inputting Values from Keyboard Assigning Values to Individual Elements
Initializing Arrays during declaration
int marks [5] = {90, 98, 78, 56, 23};
Calculating the Length of an Array
Length = upper_bound – lower_bound + 1
where
upper_bound is the index of the last element
lower_bound is the index of the first element in the array
99 67 78 56 88 90 34 85
marks[0] marks[1] marks[2] marks[3] marks[4] marks[5] marks[6 marks[7]]
Here, lower_bound = 0, upper_bound = 7
Therefore, length = 7 – 0 + 1 = 8
WAP to Read and Display N Numbers using
an Array
#include<stdio.h>
#include<conio.h>
int main()
{
int i=0, n, arr[20];
clrscr();
printf(“n Enter the number of elements : ”);
scanf(“%d”, &n);
printf(“n Enter the elements : ”);
WAP to Read and Display N Numbers using
an Array
for(i=0;i<n;i++)
{
printf(“n arr[%d] = ”, i);
scanf(“%d”, &num[i]);
}
printf(“n The array elements are ”);
for(i=0;i<n;i++)
printf(“arr[%d] = %dt”, i, arr[i]);
return 0;
}
Inserting an Element in an Array
Algorithm to insert a new element to the end of an array
Step 1: Set upper_bound = upper_bound + 1
Step 2: Set A[upper_bound] = VAL
Step 3; EXIT
Algorithm INSERT( A, N, POS, VAL) to insert an element VAL at
position POS
Step 1: [INITIALIZATION] SET I = N
Step 2: Repeat Steps 3 and 4 while I >= POS
Step 3: SET A[I + 1] = A[I]
Step 4: SET I = I – 1
[End of Loop]
Step 5: SET N = N + 1
Step 6: SET A[POS] = VAL
Step 7: EXIT
Deleting an Element from an Array
Algorithm to delete an element from the end of the array
Step 1: Set upper_bound = upper_bound - 1
Step 2: EXIT
Algorithm DELETE( A, N, POS) to delete an element at POS
Step 1: [INITIALIZATION] SET I = POS
Step 2: Repeat Steps 3 and 4 while I <= N-1
Step 3: SET A[I] = A[I + 1]
Step 4: SET I = I + 1
[End of Loop]
Step 5: SET N = N - 1
Step 6: EXIT
Pointers and Arrays
•Concept of array is very much bound to the concept of pointer.
•Name of an array is actually a pointer that points to the first
element of the array.
int *ptr;
ptr = &arr[0];
•If pointer variable ptr holds the address of the first element in the
array, then the address of the successive elements can be calculated
by writing ptr++.
int *ptr = &arr[0];
ptr++;
printf (“The value of the second element in the array is %d”, *ptr);
Arrays of Pointers
•An array of pointers can be declared as:
int *ptr[10];
•The above statement declares an array of 10 pointers where each
of the pointer points to an integer variable. For example, look at the
code given below.
int *ptr[10];
int p=1, q=2, r=3, s=4, t=5;
ptr[0]=&p;
ptr[1]=&q;
Arrays of Pointers
ptr[2]=&r;
ptr[3]=&s;
ptr[4]=&t
Can you tell what will be the output of the following statement?
printf(“n %d”, *ptr[3]);
Arrays of Pointers
The output will be 4 because ptr[3] stores the address of integer
variable s and *ptr[3] will therefore print the value of s that is 4.
Two-dimensional Arrays
A two-dimensional array is specified using two subscripts where one
subscript denotes row and the other denotes column.
C looks at a two-dimensional array as an array of one-dimensional
arrays.
A two-dimensional array is declared
as:
data_type
array_name[row_size][column_size];
Second Dimension
Two-dimensional Arrays
Therefore, a two dimensional m×n array is an array that contains m×n
data elements and each element is accessed using two subscripts, i
and j, where i<=m and j<=n
int marks[3][5];
Rows/Columns
Col 0 Col 1 Col2 Col 3 Col 4
Row 0 Marks[0][0] Marks[0][1] Marks[0][2] Marks[0][3] Marks[0][4]
Row 1 Marks[1][0] Marks[1][1] Marks[1][2] Marks[1][3] Marks[1][4]
Row 2 Marks[2][0] Marks[2][1] Marks[2][2] Marks[2][3] Marks[2][4]
Two Dimensional Array
Memory Representation of a 2D Array
• There are two ways of storing a 2-D array in memory. The first
way is row-major order and the second is column-major order.
• In the row-major order the elements of the first row are stored
before the elements of the second and third rows. That is, the
elements of the array are stored row by row where n elements of
the first row will occupy the first nth locations.
(0,0) (0, 1) (0,2) (0,3) (1,0) (1,1) (1,2) (1,3) (2,0) (2,1) (2,2) (2,3)
Memory Representation of a 2D Array
• However, when we store the elements in a column major order,
the elements of the first column are stored before the elements of
the second and third columns. That is, the elements of the array
are stored column by column where n elements of the first column
will occupy the first nth locations.
(0,0) (1,0) (2,0) (3,0) (0,1) (1,1) (2,1) (3,1) (0,2) (1,2) (2,2) (3,2)
Initializing Two-dimensional Arrays
A two-dimensional array is initialized in the same was as a single
dimensional array is initialized. For example,
int marks[2][3]={90, 87, 78, 68, 62, 71};
int marks[2][3]={{90,87,78},{68, 62, 71}};
Calculating the Address of Array
Elements
A is a 2D array of M*N
Row Major Order
LOC(A[J,K])=Base(A)+w[N(J-1)+(K-1)]
Column Major Order
LOC(A[J,K])=Base(A)+w[M(K-1)+(J-1)]
Multi-dimensional Arrays
• A multi-dimensional array is an array of arrays.
• Like we have one index in a single dimensional array, two indices in a
two-dimensional array, in the same way we have n indices in a n-
dimensional array or multi-dimensional array.
• Conversely, an n dimensional array is specified using n indices.
• An n dimensional m1 x m2 x m3 x ….. mn array is a collection of
m1×m2×m3× ….. ×mn elements.
• In a multi-dimensional array, a particular element is specified by using
n subscripts as A[I1][I2][I3]…[In], where
I1<=M1 I2<=M2 I3 <= M3 ……… In <= Mn
Multi-dimensional Arrays
Multi-dimensional Arrays
Suppose the array is of n-dimension having the size/length of each dimension as L1,L2,L3, . .. Ln
.Base Address is BA.Index number of the element to find the address is given as i1, i2, i3, . . . .in
Then the formula for column major order will be:
Address of A[i1][ i2]. . . [ in] = BA(A) + W*[((…ENLN-1+ EN-1 )LN-2 +... E3 )L2+ E2 )L1 +E1 ]
Then the formula for row major order will be:
Address of A[i1][ i2][ i3]. . . .[ in] = BA(A) + W*[(E1L2 + E2 )L3 +E3 )L4 ….. + EN-1 )LN + EN ]
Where E(effective index)
Ei= Ki-(lower bound)i
Example: An int array A[50][40][30] is stored at the address 1000. Find the address of
A[40][20][10].
Sparse Matrix
A matrix is a two-dimensional data object made of m rows and n columns,
therefore having total m x n values. If most of the elements of the matrix have
0 value, then it is called a sparse matrix.
Why to use Sparse Matrix instead of simple matrix ?
Storage: There are lesser non-zero elements than zeros and thus lesser
memory can be used to store only those elements.
Computing time: Computing time can be saved by logically designing a data
structure traversing only non-zero elements..
Example:
0 0 3 0
4 0 0 5
7 0 0 0
0 0 0 0
2 6 0 0
Sparse Matrix
Representing a sparse matrix by a 2D array leads to wastage of lots of memory as
zeroes in the matrix are of no use in most of the cases. So, instead of storing
zeroes with non-zero elements, we only store non-zero elements. This means
storing non-zero elements with triples- (Row, Column, value).
Sparse Matrix Representations can be done in many ways following are two
common representations:
•Array representation
•Linked list representation
Sparse Matrix
Method 1: Using Arrays
2D array is used to represent a sparse matrix in which there are three rows named as
Row: Index of row, where non-zero element is located
Column: Index of column, where non-zero element is located
Value: Value of the non zero element located at index – (row,column)
Sparse Matrix
Representing a sparse matrix by a 2D array leads to wastage of lots of memory as
zeroes in the matrix are of no use in most of the cases. So, instead of storing zeroes
with non-zero elements, we only store non-zero elements. This means storing non-
zero elements with triples- (Row, Column, value).
Sparse Matrix Representations can be done in many ways following are two common
representations:
•Array representation
•Linked list representation
Sparse Matrix
Method 2: Using Linked Lists
In linked list, each node has four fields. These four fields are defined as:
Row: Index of row, where non-zero element is located
Column: Index of column, where non-zero element is located
Value: Value of the non zero element located at index – (row,column)
Next node: Address of the next node
Applications of Arrays
• Arrays are widely used to implement mathematical vectors,
matrices and other kinds of rectangular tables.
• Many databases include one-dimensional arrays whose
elements are records.
• Arrays are also used to implement other data structures like
heaps, hash tables, deques, queues, stacks and string. We will
read about these data structures in the subsequent chapters.
• Arrays can be used for dynamic memory allocation.
Operations On Arrays
1.Traversing
2.Insertion
3.Deletion
4.Searching
a)linear search
b)binary search
5.Sorting
a)bubble sort[O(n2)]
b)merge sort[(O(n)]
c)quick sort etc[(nlog(n)]
Sorting
• Sorting takes an unordered collection and makes
it an ordered one.
5
12
35
42
77 101
1 2 3 4 5 6
5 12 35 42 77 101
1 2 3 4 5 6
Sorting
"Bubbling Up" the Largest Element
• Traverse a collection of elements
– Move from the front to the end
– “Bubble” the largest value to the end using pair-
wise comparisons and swapping
5
12
35
42
77 101
1 2 3 4 5 6
Contd…
"Bubbling Up" the Largest Element
• Traverse a collection of elements
– Move from the front to the end
– “Bubble” the largest value to the end using pair-
wise comparisons and swapping
5
12
35
42
77 101
1 2 3 4 5 6
Swap
42 77
Contd…
"Bubbling Up" the Largest Element
• Traverse a collection of elements
– Move from the front to the end
– “Bubble” the largest value to the end using pair-
wise comparisons and swapping
5
12
35
77
42 101
1 2 3 4 5 6
Swap
35 77
Contd…
"Bubbling Up" the Largest Element
• Traverse a collection of elements
– Move from the front to the end
– “Bubble” the largest value to the end using pair-
wise comparisons and swapping
5
12
77
35
42 101
1 2 3 4 5 6
Swap
12 77
Contd…
"Bubbling Up" the Largest Element
• Traverse a collection of elements
– Move from the front to the end
– “Bubble” the largest value to the end using pair-
wise comparisons and swapping
5
77
12
35
42 101
1 2 3 4 5 6
No need to swap
Contd…
"Bubbling Up" the Largest Element
• Traverse a collection of elements
– Move from the front to the end
– “Bubble” the largest value to the end using pair-
wise comparisons and swapping
5
77
12
35
42 101
1 2 3 4 5 6
Swap
5 101
Contd…
"Bubbling Up" the Largest Element
• Traverse a collection of elements
– Move from the front to the end
– “Bubble” the largest value to the end using pair-
wise comparisons and swapping
77
12
35
42 5
1 2 3 4 5 6
101
Largest value correctly placed
Contd…
Items of Interest
• Notice that only the largest value is correctly
placed
• All other values are still out of order
• So we need to repeat this process
77
12
35
42 5
1 2 3 4 5 6
101
Largest value correctly placed
Contd…
Repeat “Bubble Up” How Many
Times?
• If we have N elements…
• And if each time we bubble an element, we
place it in its correct location…
• Then we repeat the “bubble up” process N –
1 times.
• This guarantees we’ll correctly
place all N elements.
Contd…
“Bubbling” All the Elements
77
12
35
42 5
1 2 3 4 5 6
101
5
42
12
35 77
1 2 3 4 5 6
101
42
5
35
12 77
1 2 3 4 5 6
101
42
35
5
12 77
1 2 3 4 5 6
101
42
35
12
5 77
1 2 3 4 5 6
101
N
-
1
Contd…
Reducing the Number of Comparisons
12
35
42
77 101
1 2 3 4 5 6
5
77
12
35
42 5
1 2 3 4 5 6
101
5
42
12
35 77
1 2 3 4 5 6
101
42
5
35
12 77
1 2 3 4 5 6
101
42
35
5
12 77
1 2 3 4 5 6
101
Contd…

More Related Content

Similar to arrays.pptx

Similar to arrays.pptx (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.pptx
Arrays.pptxArrays.pptx
Arrays.pptx
 
Chapter 13.pptx
Chapter 13.pptxChapter 13.pptx
Chapter 13.pptx
 
Arrays
ArraysArrays
Arrays
 
Unit 6. Arrays
Unit 6. ArraysUnit 6. Arrays
Unit 6. Arrays
 
2. Array in Data Structure
2. Array in Data Structure2. Array in Data Structure
2. Array in Data Structure
 
array-191103180006.pdf
array-191103180006.pdfarray-191103180006.pdf
array-191103180006.pdf
 
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
ArraysArrays
Arrays
 
DS Unit 1.pptx
DS Unit 1.pptxDS Unit 1.pptx
DS Unit 1.pptx
 
Chapter 4 (Part I) - Array and Strings.pdf
Chapter 4 (Part I) - Array and Strings.pdfChapter 4 (Part I) - Array and Strings.pdf
Chapter 4 (Part I) - Array and Strings.pdf
 
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
 
DSA Unit II array.pptx
DSA Unit II array.pptxDSA Unit II array.pptx
DSA Unit II array.pptx
 
ARRAYS
ARRAYSARRAYS
ARRAYS
 
Lecture 15 - Array
Lecture 15 - ArrayLecture 15 - Array
Lecture 15 - Array
 
Algo>Arrays
Algo>ArraysAlgo>Arrays
Algo>Arrays
 
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
 
Array assignment
Array assignmentArray assignment
Array assignment
 
Introduction to Arrays in C
Introduction to Arrays in CIntroduction to Arrays in C
Introduction to Arrays in C
 
Arrays_in_c++.pptx
Arrays_in_c++.pptxArrays_in_c++.pptx
Arrays_in_c++.pptx
 

Recently uploaded

Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
Glass Ceramics: Processing and Properties
Glass Ceramics: Processing and PropertiesGlass Ceramics: Processing and Properties
Glass Ceramics: Processing and PropertiesPrabhanshu Chaturvedi
 
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxBSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxfenichawla
 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...Call Girls in Nagpur High Profile
 
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
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingrknatarajan
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...roncy bisnoi
 
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
 
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
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfKamal Acharya
 
(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
 
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
 
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
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)simmis5
 
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
 
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
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations120cr0395
 
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)

Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
 
Glass Ceramics: Processing and Properties
Glass Ceramics: Processing and PropertiesGlass Ceramics: Processing and Properties
Glass Ceramics: Processing and Properties
 
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxBSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
 
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
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
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
 
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...
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
(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...
 
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...
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
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
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
 
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...
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
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
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
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
 

arrays.pptx

  • 1. • An array is a collection of similar data elements. • These data elements have the same data type. • Elements of arrays are stored in consecutive memory locations and are referenced by an index (also known as the subscript). • Declaring an array means specifying three things: Data type - what kind of values it can store. For example, int, char, float Name - to identify the array Size - the maximum number of values that the array can hold • Arrays are declared using the following syntax: type name[size]; 1st element 2nd element 3rd element 4th element 5th element 6th element 7th element 8th element 9th element 10th element marks[0] marks[1] marks[2] marks[3] marks[4] marks[5] marks[6] marks[7] marks[8] marks[9] Introduction
  • 2. Accessing Elements of an Array • To access all the elements of an array, we must use a loop. • That is, we can access all the elements of an array by varying the value of the subscript into the array. • But note that the subscript must be an integral value or an expression that evaluates to an integral value. int i, marks[10]; for(i=0;i<10;i++) marks[i] = -1;
  • 3. Calculating the Address of Array Elements • Address of data element, A[k] = BA(A) + w( k – lower_bound) where A is the array k is the index of the element whose address we have to calculate BA is the base address of the array A w is the word size of one element in memory. For example, size of int is 2 99 67 78 56 88 90 34 85 marks[0] marks[1] marks[2] marks[3] marks[4] marks[5] marks[6] marks[7] 1000 1002 1004 1006 1008 1010 1012 1014 marks[4] = 1000 + 2(4 – 0) = 1000 + 2(4) = 1008
  • 4. Storing Values in Arrays Store values in the array Initialize the elements Input values for the elements Assign values to the elements int i, marks[10]; for(i=0;i<10;i++) scanf(“%d”, &marks[i]); int i, arr1[10], arr2[10]; for(i=0;i<10;i++) arr2[i] = arr1[i]; Inputting Values from Keyboard Assigning Values to Individual Elements Initializing Arrays during declaration int marks [5] = {90, 98, 78, 56, 23};
  • 5. Calculating the Length of an Array Length = upper_bound – lower_bound + 1 where upper_bound is the index of the last element lower_bound is the index of the first element in the array 99 67 78 56 88 90 34 85 marks[0] marks[1] marks[2] marks[3] marks[4] marks[5] marks[6 marks[7]] Here, lower_bound = 0, upper_bound = 7 Therefore, length = 7 – 0 + 1 = 8
  • 6. WAP to Read and Display N Numbers using an Array #include<stdio.h> #include<conio.h> int main() { int i=0, n, arr[20]; clrscr(); printf(“n Enter the number of elements : ”); scanf(“%d”, &n); printf(“n Enter the elements : ”);
  • 7. WAP to Read and Display N Numbers using an Array for(i=0;i<n;i++) { printf(“n arr[%d] = ”, i); scanf(“%d”, &num[i]); } printf(“n The array elements are ”); for(i=0;i<n;i++) printf(“arr[%d] = %dt”, i, arr[i]); return 0; }
  • 8. Inserting an Element in an Array Algorithm to insert a new element to the end of an array Step 1: Set upper_bound = upper_bound + 1 Step 2: Set A[upper_bound] = VAL Step 3; EXIT Algorithm INSERT( A, N, POS, VAL) to insert an element VAL at position POS Step 1: [INITIALIZATION] SET I = N Step 2: Repeat Steps 3 and 4 while I >= POS Step 3: SET A[I + 1] = A[I] Step 4: SET I = I – 1 [End of Loop] Step 5: SET N = N + 1 Step 6: SET A[POS] = VAL Step 7: EXIT
  • 9. Deleting an Element from an Array Algorithm to delete an element from the end of the array Step 1: Set upper_bound = upper_bound - 1 Step 2: EXIT Algorithm DELETE( A, N, POS) to delete an element at POS Step 1: [INITIALIZATION] SET I = POS Step 2: Repeat Steps 3 and 4 while I <= N-1 Step 3: SET A[I] = A[I + 1] Step 4: SET I = I + 1 [End of Loop] Step 5: SET N = N - 1 Step 6: EXIT
  • 10. Pointers and Arrays •Concept of array is very much bound to the concept of pointer. •Name of an array is actually a pointer that points to the first element of the array. int *ptr; ptr = &arr[0]; •If pointer variable ptr holds the address of the first element in the array, then the address of the successive elements can be calculated by writing ptr++. int *ptr = &arr[0]; ptr++; printf (“The value of the second element in the array is %d”, *ptr);
  • 11. Arrays of Pointers •An array of pointers can be declared as: int *ptr[10]; •The above statement declares an array of 10 pointers where each of the pointer points to an integer variable. For example, look at the code given below. int *ptr[10]; int p=1, q=2, r=3, s=4, t=5; ptr[0]=&p; ptr[1]=&q;
  • 12. Arrays of Pointers ptr[2]=&r; ptr[3]=&s; ptr[4]=&t Can you tell what will be the output of the following statement? printf(“n %d”, *ptr[3]);
  • 13. Arrays of Pointers The output will be 4 because ptr[3] stores the address of integer variable s and *ptr[3] will therefore print the value of s that is 4.
  • 14. Two-dimensional Arrays A two-dimensional array is specified using two subscripts where one subscript denotes row and the other denotes column. C looks at a two-dimensional array as an array of one-dimensional arrays. A two-dimensional array is declared as: data_type array_name[row_size][column_size]; Second Dimension
  • 15. Two-dimensional Arrays Therefore, a two dimensional m×n array is an array that contains m×n data elements and each element is accessed using two subscripts, i and j, where i<=m and j<=n int marks[3][5]; Rows/Columns Col 0 Col 1 Col2 Col 3 Col 4 Row 0 Marks[0][0] Marks[0][1] Marks[0][2] Marks[0][3] Marks[0][4] Row 1 Marks[1][0] Marks[1][1] Marks[1][2] Marks[1][3] Marks[1][4] Row 2 Marks[2][0] Marks[2][1] Marks[2][2] Marks[2][3] Marks[2][4] Two Dimensional Array
  • 16. Memory Representation of a 2D Array • There are two ways of storing a 2-D array in memory. The first way is row-major order and the second is column-major order. • In the row-major order the elements of the first row are stored before the elements of the second and third rows. That is, the elements of the array are stored row by row where n elements of the first row will occupy the first nth locations. (0,0) (0, 1) (0,2) (0,3) (1,0) (1,1) (1,2) (1,3) (2,0) (2,1) (2,2) (2,3)
  • 17. Memory Representation of a 2D Array • However, when we store the elements in a column major order, the elements of the first column are stored before the elements of the second and third columns. That is, the elements of the array are stored column by column where n elements of the first column will occupy the first nth locations. (0,0) (1,0) (2,0) (3,0) (0,1) (1,1) (2,1) (3,1) (0,2) (1,2) (2,2) (3,2)
  • 18. Initializing Two-dimensional Arrays A two-dimensional array is initialized in the same was as a single dimensional array is initialized. For example, int marks[2][3]={90, 87, 78, 68, 62, 71}; int marks[2][3]={{90,87,78},{68, 62, 71}};
  • 19. Calculating the Address of Array Elements A is a 2D array of M*N Row Major Order LOC(A[J,K])=Base(A)+w[N(J-1)+(K-1)] Column Major Order LOC(A[J,K])=Base(A)+w[M(K-1)+(J-1)]
  • 20. Multi-dimensional Arrays • A multi-dimensional array is an array of arrays. • Like we have one index in a single dimensional array, two indices in a two-dimensional array, in the same way we have n indices in a n- dimensional array or multi-dimensional array. • Conversely, an n dimensional array is specified using n indices. • An n dimensional m1 x m2 x m3 x ….. mn array is a collection of m1×m2×m3× ….. ×mn elements. • In a multi-dimensional array, a particular element is specified by using n subscripts as A[I1][I2][I3]…[In], where I1<=M1 I2<=M2 I3 <= M3 ……… In <= Mn
  • 22. Multi-dimensional Arrays Suppose the array is of n-dimension having the size/length of each dimension as L1,L2,L3, . .. Ln .Base Address is BA.Index number of the element to find the address is given as i1, i2, i3, . . . .in Then the formula for column major order will be: Address of A[i1][ i2]. . . [ in] = BA(A) + W*[((…ENLN-1+ EN-1 )LN-2 +... E3 )L2+ E2 )L1 +E1 ] Then the formula for row major order will be: Address of A[i1][ i2][ i3]. . . .[ in] = BA(A) + W*[(E1L2 + E2 )L3 +E3 )L4 ….. + EN-1 )LN + EN ] Where E(effective index) Ei= Ki-(lower bound)i Example: An int array A[50][40][30] is stored at the address 1000. Find the address of A[40][20][10].
  • 23. Sparse Matrix A matrix is a two-dimensional data object made of m rows and n columns, therefore having total m x n values. If most of the elements of the matrix have 0 value, then it is called a sparse matrix. Why to use Sparse Matrix instead of simple matrix ? Storage: There are lesser non-zero elements than zeros and thus lesser memory can be used to store only those elements. Computing time: Computing time can be saved by logically designing a data structure traversing only non-zero elements.. Example: 0 0 3 0 4 0 0 5 7 0 0 0 0 0 0 0 2 6 0 0
  • 24. Sparse Matrix Representing a sparse matrix by a 2D array leads to wastage of lots of memory as zeroes in the matrix are of no use in most of the cases. So, instead of storing zeroes with non-zero elements, we only store non-zero elements. This means storing non-zero elements with triples- (Row, Column, value). Sparse Matrix Representations can be done in many ways following are two common representations: •Array representation •Linked list representation
  • 25. Sparse Matrix Method 1: Using Arrays 2D array is used to represent a sparse matrix in which there are three rows named as Row: Index of row, where non-zero element is located Column: Index of column, where non-zero element is located Value: Value of the non zero element located at index – (row,column)
  • 26. Sparse Matrix Representing a sparse matrix by a 2D array leads to wastage of lots of memory as zeroes in the matrix are of no use in most of the cases. So, instead of storing zeroes with non-zero elements, we only store non-zero elements. This means storing non- zero elements with triples- (Row, Column, value). Sparse Matrix Representations can be done in many ways following are two common representations: •Array representation •Linked list representation
  • 27. Sparse Matrix Method 2: Using Linked Lists In linked list, each node has four fields. These four fields are defined as: Row: Index of row, where non-zero element is located Column: Index of column, where non-zero element is located Value: Value of the non zero element located at index – (row,column) Next node: Address of the next node
  • 28. Applications of Arrays • Arrays are widely used to implement mathematical vectors, matrices and other kinds of rectangular tables. • Many databases include one-dimensional arrays whose elements are records. • Arrays are also used to implement other data structures like heaps, hash tables, deques, queues, stacks and string. We will read about these data structures in the subsequent chapters. • Arrays can be used for dynamic memory allocation.
  • 29. Operations On Arrays 1.Traversing 2.Insertion 3.Deletion 4.Searching a)linear search b)binary search 5.Sorting a)bubble sort[O(n2)] b)merge sort[(O(n)] c)quick sort etc[(nlog(n)]
  • 30. Sorting • Sorting takes an unordered collection and makes it an ordered one. 5 12 35 42 77 101 1 2 3 4 5 6 5 12 35 42 77 101 1 2 3 4 5 6 Sorting
  • 31. "Bubbling Up" the Largest Element • Traverse a collection of elements – Move from the front to the end – “Bubble” the largest value to the end using pair- wise comparisons and swapping 5 12 35 42 77 101 1 2 3 4 5 6 Contd…
  • 32. "Bubbling Up" the Largest Element • Traverse a collection of elements – Move from the front to the end – “Bubble” the largest value to the end using pair- wise comparisons and swapping 5 12 35 42 77 101 1 2 3 4 5 6 Swap 42 77 Contd…
  • 33. "Bubbling Up" the Largest Element • Traverse a collection of elements – Move from the front to the end – “Bubble” the largest value to the end using pair- wise comparisons and swapping 5 12 35 77 42 101 1 2 3 4 5 6 Swap 35 77 Contd…
  • 34. "Bubbling Up" the Largest Element • Traverse a collection of elements – Move from the front to the end – “Bubble” the largest value to the end using pair- wise comparisons and swapping 5 12 77 35 42 101 1 2 3 4 5 6 Swap 12 77 Contd…
  • 35. "Bubbling Up" the Largest Element • Traverse a collection of elements – Move from the front to the end – “Bubble” the largest value to the end using pair- wise comparisons and swapping 5 77 12 35 42 101 1 2 3 4 5 6 No need to swap Contd…
  • 36. "Bubbling Up" the Largest Element • Traverse a collection of elements – Move from the front to the end – “Bubble” the largest value to the end using pair- wise comparisons and swapping 5 77 12 35 42 101 1 2 3 4 5 6 Swap 5 101 Contd…
  • 37. "Bubbling Up" the Largest Element • Traverse a collection of elements – Move from the front to the end – “Bubble” the largest value to the end using pair- wise comparisons and swapping 77 12 35 42 5 1 2 3 4 5 6 101 Largest value correctly placed Contd…
  • 38. Items of Interest • Notice that only the largest value is correctly placed • All other values are still out of order • So we need to repeat this process 77 12 35 42 5 1 2 3 4 5 6 101 Largest value correctly placed Contd…
  • 39. Repeat “Bubble Up” How Many Times? • If we have N elements… • And if each time we bubble an element, we place it in its correct location… • Then we repeat the “bubble up” process N – 1 times. • This guarantees we’ll correctly place all N elements. Contd…
  • 40. “Bubbling” All the Elements 77 12 35 42 5 1 2 3 4 5 6 101 5 42 12 35 77 1 2 3 4 5 6 101 42 5 35 12 77 1 2 3 4 5 6 101 42 35 5 12 77 1 2 3 4 5 6 101 42 35 12 5 77 1 2 3 4 5 6 101 N - 1 Contd…
  • 41. Reducing the Number of Comparisons 12 35 42 77 101 1 2 3 4 5 6 5 77 12 35 42 5 1 2 3 4 5 6 101 5 42 12 35 77 1 2 3 4 5 6 101 42 5 35 12 77 1 2 3 4 5 6 101 42 35 5 12 77 1 2 3 4 5 6 101 Contd…