SlideShare a Scribd company logo
Made By: Ms. Archika Bhatia ARRAYS
WHAT IS AN ARRAY An array is a derived data type ( derived from fundamental data type ) It is a collection of variables of the same type that are referenced by a common name. Consist of contiguous memory locations. Lowest address corresponds to first element Highest address corresponds to the last element. Can have data items of type like: int, char, float and also user-defined types like : structures, objects.
NEED FOR AN ARRAY To store large number of variables of same type under a single variable. Easy understanding of the program. E.g. To store Marks of 50 students. Record of sales of 100 salesman.
Single Dimensional Array : Element specified by single subscript Syntax : type array_name [ size ] TYPES OF ARRAYS Base type of array Name of array No. of elements that can be stored: Can be a integer value without the sign
Creating an Array void main( ) { int a[10]; // declaration of an array ‘a’ int n; // input 10 elements in an array for ( n = 0; n < 10 ; n + +) { cin >> a [ n ]; } // display the 10 elements of the array input for ( n = 0 ; n < 10 ; n + + ) { cout << a [ n ] << endl; } }
Memory Representation of Single Dimension Array If the array is float arr [ 5 ]; memory representation would be as follows: Arr [ 0 ]  Arr [ 1 ]  Arr [ 2 ]  Arr [ 3 ]  Arr [ 4 ]  5016  5012 5008  5004  5000  Total Memory requirement is : size of ( type ) * size of array 4 * 5  = 20 bytes
ARRAY INITIALISATION int list [ 5 ] ; // declaration int list [ 5 ] = { 10, 20, 30, 40, 50 } ; //  decalaration & initialisation
UNSIZED ARRAY INITIALISATION Can skip the size of an array in array initialisation Elements of an array can be added or removed without changing array dimensions. E.g. float price [ ] = { 50.5, 63.97, 84.6, 779.8 };
Program to count the no. of employees earning more than Rs. 1 lakh per annum. Monthly salaries of 10 employees are given. void main ( ) { const int size = 10 ; float sal [ size ] , an_sal ; int count  = 0; // loop to accept monthly salaries of 10 employees for ( int j = 0 ; j < size ; j + + ) { cout <<  “ Enter the monthly salary of employee “ << j + 1 ; cin >> sal [ j ]; }
// loop to count employees earning more than Rs. 1 lakh per annum for ( j = 0 ; j < size ; j + + ) { an_sal = sal [ j ]  * 12 ; if ( an_sal > 100000 ) { count ++ ; } } cout << count << “ employees out of “ << size << “ employees are earning more than Rs. 1 lakh per annum “ ; }
Passing arrays in a function Note:  Arrays are always passed as reference Program to create functions for input array and display array void main ( ) { void input ( int a [ 10 ] ); void display ( int a [ 10 ] ); int arr [ 10 ]; input ( arr ); display ( arr ); }
void input ( int a [ 10 ] ) { for ( int x = 1; x < 10; x ++ ) { cout << “ enter the element “ << x + 1; cin >> a [ x ]; } } void display ( int a [ 10 ] ) { for ( int x = 1; x  < 10 ; x ++ ) { cout << “ the element no “ << x + 1 << “ is : “<< a [ x ]; } }
WAP to input 10 numbers in an array and replace all even no.s by 0 and odd no.s by 1 void main ( ) { int a [ 10 ], n; // loop to accept 10 values in an array ‘a’ for ( n = 0; n < 10 ; n + +) { cin >> a [ n ]; } // loop to check if the element of an array is even replace by 0 and if odd replace // by 1 for ( n = 0; n < 10 ; n + +) { if ( ( a [ n ] % 2 ) == 0 ) { a [ n ] = 0; } else { a [ n ] = 1 ; } }
// display the 10 elements of the array for ( n = 0 ; n < 10 ; n + + ) { cout << a [ n ] << endl; } }
WAP to input 10 numbers in an array and replace all even no.s by 0 and odd no.s by 1 using functions void main ( ) { void input ( int a [ 10 ] ); void display ( int a [ 10 ] ); void replace ( int a [10 ] ); int arr [ 10 ] ; input ( arr ); cout<< “ elements before replacement “; display ( arr ); replace ( arr ); cout<< “ elements after replacement “ ; display ( arr ); }
void input ( int a [ 10 ] ) { for ( int x = 0; x < 10; x ++ ) { cout << “ enter the element “ << x + 1; cin >> a [ x ]; } } void display ( int a [ 10 ] ) { for ( int x = 0; x  < 10 ; x ++ ) { cout << “ the element no “ << x + 1 << “ is : “<< a [ x ]; } }
void replace ( int a [ 10 ] ) { for ( int n = 0; n < 10 ; n + +) { if ( ( a [ n ] % 2 ) == 0 ) { a [ n ] = 0; } else { a [ n ] = 1 ; } }
WAP to find the largest and smallest no. in an array of 10 elements // input an array // display the array // to find the largest element largest = a [ 0 ] ; for ( n = 1 ; n < 10 ; n + + ) { if ( a [ n ] > largest ) { largest = a [ n ]; } } cout << “ largest value is : “ << largest ; // to find the lowest element lowest = a [ 0 ]; for ( n = 1 ; n < 10 ; n + + ) { if ( a [ n ] < lowest ) { lowest  = a [ n ]; } } cout << “ lowest value is : “ << lowest ;
// function to find the largest element void largest ( int a [ 10 ] ) { largest = a [ 0 ] ; for ( n = 0; n < 10 ; n + + ) { if ( a [ n ] > largest ) { largest = a [ n ]; } } }
// to find the lowest element void lowest ( int a [ 10 ] ) { lowest = a [ 0 ]; for ( n = 0 ; n < 10 ; n + + ) { if ( a [ n ] < lowest ) { lowest  = a [ n ]; } } cout << “ lowest value is : “ << lowest ; }
PRACTICE QUESTIONS Give the output of the following: void main( ) { int a [ 5 ] , j , p = 10; for ( j = 0; j < 5 ; j + + ) a [ j ] = p * j ; for ( k = 0 ; k < 5 ; k + +) cout << a [ k ]; } OUTPUT: 010203040
Give the output of the following: void main ( ) { int b [ 10 ]; for ( int j = 0 ; j < 3 ; j + +) for ( int k = 0 ; k < 3; k + +) { b [ j ] =  4 * j + k ; cout << “\n “ << b [ j ]; } }
output 0 1 2 4 5 6 8 9 10
Give the output of the following: void main ( ) { int b = 0 ; int c [ 10 ] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; for ( int a = 0; a < 10; a + +) { if ( ( c [ a ] % 2 ) = = 0 ) b + = c [ a ]; } cout << “ b = “ << b ; } Output: 30
Give the output void main ( ) { int arr[10]; cout<<arr; getch( ); }
Find errors in the following program code: main ( ) { int a [ 6 ] = { 0 , 1 ; 2 , 3 , 6, 4 } ; char name [ ] = { ‘A’, “ B “ , ‘ C ‘ }; for ( j = 0 ; j < = 5 ; j + +) cout << a [ j ]; }
Find errors in the following program code: (  Assume header files included ) void main ( ) { const int a = 9 ; int p , q ; cout << “ \n enter the value of p and q  ; p + q = p ; a = a + 5 ; cout << a; }
Find errors in the following program code: (  Assume header files included ) void main ( ) { int arr ( 5 ) ; float function ( int , int ); for ( int j = 0 ; j < 5 ;  j ++ ) cin >> a ( j ); cout << func ( 5 ); } float function ( int x ; int y ) { return ( x – y ); }
Find errors in the following program code: (  Assume header files included ) void main ( ) { int r ; w = 90; while w > 60 { r = w – 50 ; switch [ w ] { 20 : cout << “ lower range “; case 30 : cout << “ higher range “; } }
Find errors in the following program code: (  Assume header files included ) void main ( ) { test ( void ); void testcopy ( void ); int a, b ; cin >> a >>b; for ( int j = a ; j < b ; j + + ) { test  ( ) ; } } char test ( void ) { main ( ); cout << “ hello “ ; testcopy (  ) ; return ( 0 ) ; }
SEARCHING FOR AN ELEMENT IN AN ARRAY There are two ways to search for an element: Linear Search: checks each element of the array . If the match found – element exists, else does not exist. Suitable for small lists since time consuming Suitable for unsorted lists. Binary Search: Most important precondition is that the list should be sorted. Suitable for arrays with long list of elements.
void linear_search( int a [ 10 ], int size, int no ) { for ( int j = 1 ; j < size ; j ++ ) { if ( no == a [ j ] ) { cout << “ element found “; break ; } } Function for Linear Searching
Search for the element using Linear Search and count the no. of times the element occurs in the array. void linear_search( int a [ 10 ], int size, int no ) { int flag = 0, count = 0; for ( int j = 1 ; j < size ; j ++ ) { if ( no == a [ j ] ) { flag=1; count ++ ; } if ( flag  = = 0 ) { cout<< “ element not found “; } else { cout<< “ element found “ << count << “ no. of times “; } }
Binary Search method Element to search : 45 Array Positions: 0 1 2 3 4 5 6 7 8 9 Given array: 2 6 8 9 10 12 16 19 20 55 m f m f l l l f f m l m l Note:  need to check f < = l
Element to search : 1 Array Positions: 0 1 2 3 4 5 6 7 8 9 Given array: 2 6 8 9 10 12 16 19 20 55 m l l f m f l f m l
Element to search : 12 Array Positions: 0 1 2 3 4 5 6 7 8 9 Given array: 2 6 8 9 10 12 16 19 20 55 f m l l f m f l m
INSERTING AN ELEMENT IN AN ARRAY Check for the availability of space. Shift all the elements to right and create space for new element. Insert the new element at the space created. Increase the size of array by 1. Insert new element at the space created here. POS VALUE 12 13 12 10 8 7 6 6 5 4 3 2 1 0
DELETING AN ELEMENT IN AN ARRAY Shift all the elements to LEFT. The element to be deleted will automatically be overwritten. Add 0 at the last position. Decrease the size of the array by 1. Insert 0 at the empty space created here. POS VALUE 6 5 4 3 2 1 0 12 13 12 10 8 7 6
SORTING Means arranging the array elements in either ascending or descending order. Three methods of sorting are: Bubble sort Exchange Selection sort Insertion Sort
BUBBLE SORT S NS NS S S PASS - I 9 5 6 2 4 3 5 9 6 2 4 3 5 6 9 2 4 3 5 6 2 9 4 3 5 6 2 9 4 3 5 6 2 9 4 3
BUBBLE SORT NS S NS S NS PASS - II 9 6 5 4 2 3 9 6 5 4 2 3 9 5 6 4 2 3 9 5 6 4 2 3 9 5 6 2 4 3 9 5 6 2 4 3
BUBBLE SORT NS NS S NS NS PASS - III 9 6 5 4 3 2 9 6 5 4 3 2 9 6 5 4 3 2 9 6 5 4 3 2 9 6 5 4 3 2 9 6 5 4 2 3
BUBBLE SORT NS NS NS NS NS PASS - IV 9 6 5 4 3 2 9 6 5 4 3 2 9 6 5 4 3 2 9 6 5 4 3 2 9 6 5 4 3 2 9 6 5 4 3 2
EXCHANGE SELECTION SORT UNSORTED smallest First element of unsorted SORTED UNSORTED SORTED SORTED UNSORTED UNSORTED SORTED UNSORTED SORTED UNSORTED 9 6 5 4 3 2 9 6 5 4 3 2 9 6 5 4 3 2 5 6 9 4 3 2 5 6 4 9 3 2 5 6 3 9 4 2 5 6 2 9 4 3
INSERTION SORT UNSORTED First element of unsorted SORTED UNSORTED SORTED SORTED UNSORTED UNSORTED SORTED UNSORTED SORTED SORTED Find the correct place for the element and keep shifting the elements to the right. 8 7 5 3 2 2 5 8 7 3 2 2 5 2 8 7 3 2 5 2 2 8 7 3 5 2 2 3 8 7 5 2 2 3 8 7
MERGING OF TWO ARRAYS INTO A SINGLE ARRAY You need to sort the resultant array while merging The given two arrays should be in sorted order. STEP - I 15 9 6 5 3 B  (elements) 4 3 2 1 0 B  (pos) 15 13 12 8 7 3 2 A  (elements) 6 5 4 3 2 1 0 A  (pos) C  (elements) C  (pos) 2 11 10 9 8 7 6 5 4 3 2 1 0
MUTLIDIMENSIONAL ARRAY Has more than one subscript S.D.A. is most suitable for processing lists Two Dimensional array is the simplest form of M.D.A. T.D.A. has 2 subscripts: 1 st  subscript for row and 2 nd  subscript for column. T.D.A. is most suitable for table processing or matrix manipulation.
ARRAY DECLARATION Syntax: type array-name [rows] [columns]; e.g. int mat [ 2 ] [ 3 ] ; i.e. total 2 * 3 = 6 elements rows columns ( 1, 2 ) ( 1, 1 ) ( 1, 0 ) 1 ( 0, 2 ) ( 0 , 1 ) ( 0, 0 ) 0 2 1 0
UNSIZED ARRAY INITIALISATION First subscript of T.D.A. may be skipped. Second subscript must be given. E.g. int mat [ ] [ 2 ] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
ADDRESS CALCULATION OF 1DA Address of element with subscript I = base Address + E.S ( I – L ) E.S.  : size of array element L : Lower bound of array e.g.  address of element checks [2] of following array checks [ -3 , 3 ] with element size 2 bytes and base address as 1000. Address of check [ 2 ] = B.A + E.S. ( I – L ) = 1000 + 2 * ( 2 – ( -3 )  = 1000 + 2 * 5 = 1010
ADDRESS CALCULATION OF 2DA ROW MAJOR IMPLEMENTATION Given Array: AR [ L1 : U1, L2 : U2 ] Address of element AR [ i, j ]  = base Address + E.S ( n ( i – L1 ) + ( j – L2 ) ) i : row position of element whose address is to be  found. j : column position of element whose address is to  be found. E.S.   : size of array element L1 : Lower bound of 1 st  subscript i.e. row L2 : Lower bound of 2 nd  subscript i.e column n : No. of columns =  U2 – L2 + 1
Given Array Row Major Implementation Column Major Implementation 8 7 2 6 5 3 8 7 2 6 5 3 8 6 7 5 2 3
e.g.  Each element of an array A [ -20 … 20, 10…35 ] requires one byte of storage. If the array is stored in row major order beginning location 500, determine the location of A [ 0, 30 ] . A[ 0, 30 ] =  base Address + E.S ( n ( i – L1 ) + ( j – L2 ) ) n = 35 – 10 + 1 =  26 = 500 + 1 ( 26 * ( 0 – ( -20 ) )+ (30 – 10 ) ) = 500 + ( 26 ( 20 ) + 20 ) = 500 + ( 520 + 20 ) = 500 + 540 = 1040
ADDRESS CALCULATION OF 2DA COLUMN MAJOR IMPLEMENTATION Given Array: AR [ L1 : U1, L2 : U2 ] Address of element AR [ I, j ]  = base Address + E.S (  ( i – L1 ) + m ( j – L2 ) ) i : row position of element whose address is to be  found. j : column position of element whose address is to  be found. E.S.   : size of array element L1 : Lower bound of 1 st  subscript i.e. row L2 : Lower bound of 2 nd  subscript i.e column m : No. of rows =  U1 – L1 + 1
e.g.  Each element of an array A [ -20 … 20, 10…35 ] requires one byte of storage. If the array is stored in column major order beginning location 500, determine the location of A [ 0, 30 ] . A[ 0, 30 ] =  base Address + E.S ( ( i – L1 ) +m ( j – L2 ) ) m  = 20 – (-20) + 1 =  41 = 500 + 1 ( ( 0 – ( -20 ) )+ 41 *(30 – 10 ) ) = 500 + ( 20  + (41 * 20 ) ) = 500 + ( 20 + 820 ) = 500 + 840 = 1340
MULTIPLICATION OF TWO MATRICES A[M][N] * B[N][P] = C[M][P] * A[3][2] B[2][4] 3 (2,1) 2 (2,0) 1 (1,1) 4 (1,0) 6 (0,1) 5 (0,0) 1 (1,3) 6 (1,2) 4 (1,1) 5 (1,0) 0 (0,3) 1 (0,2) 3 (0,1) 2 (0,0)
A[M][N] * B[N][P] = C[M][P] * A[3][2] B[2][4] C[3][4] 3 (2,1) 2 (2,0) 1 (1,1) 4 (1,0) 6 (0,1) 5 (0,0) 1 (1,3) 6 (1,2) 4 (1,1) 5 (1,0) 0 (0,3) 1 (0,2) 3 (0,1) 2 (0,0) 2*0 + 3*1 2*1 + 3*6 2*3 + 3*4 2*2 + 3*5 4*0 + 1*1 4*1 + 1*6 4*3 + 1*4 4*2 + 1*5 5*0 + 6*1 5*1 + 6*6 5*3 + 6*4 5*2 + 6*5
TRANSPOSE OF A MATRIX No. of rows and columns may not be same Changes rows to columns and columns to rows E.g transpose is 5 3 4 6 (0,0) (0,1) (0,0) (0,1) (0,2) 4 3 5 3 2 (1,0) (1,1) (1,0) (1,1) (1,2) 6 2 (2,0) (2,1)
LOWER TRIANGLE 3 5 6 12 6 9 89 0 1 1 ST  TRIANGLE 2 ND  TRIANGLE 3 6 12 6 6 9 89 0 1 89 0 1
UPPER TRIANGLE 3 5 6 12 6 9 89 0 1 1ST TRIANGLE 2ND TRIANGLE 3 5 6 3 5 6 12 6 6 9 89 1
WAP to find row and column sum of a matrix. Create input, display and row_col_sum function for the same. IP OP 9 (2,2) 8 (2,1) 7 (2,0) 6 (1,2) 5 (1,1) 4 (1,0) 3 (0,2) 2 (0,1) 1 (0,0) 0 17 14 11 21 9 8 7 15 6 5 4 6 3 2 1

More Related Content

PPTX
STACKS IN DATASTRUCTURE
PPTX
Queue in Data Structure
PPTX
Tree in data structure
PPT
Linked lists
PPTX
Data structure - Graph
PPTX
Linked List
PPTX
Heap_Sort1.pptx
PPT
(Binary tree)
STACKS IN DATASTRUCTURE
Queue in Data Structure
Tree in data structure
Linked lists
Data structure - Graph
Linked List
Heap_Sort1.pptx
(Binary tree)

What's hot (20)

PPTX
Collision in Hashing.pptx
PDF
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
PPTX
linked list.pptx
PPTX
Infix to postfix conversion
PPTX
heap Sort Algorithm
PPTX
Queue - Data Structure - Notes
PPT
PPT
Data Structures- Part7 linked lists
PPTX
Insertion sort
PPTX
Array Introduction One-dimensional array Multidimensional array
PPTX
PPTX
PPT
Two dimensional array
PDF
Applications of stack
PPTX
Linked list
PPTX
Unit 4 python -list methods
PPTX
Threaded binary tree
PDF
UNIT III NON LINEAR DATA STRUCTURES – TREES
PPT
Infix prefix postfix
PDF
Collision in Hashing.pptx
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
linked list.pptx
Infix to postfix conversion
heap Sort Algorithm
Queue - Data Structure - Notes
Data Structures- Part7 linked lists
Insertion sort
Array Introduction One-dimensional array Multidimensional array
Two dimensional array
Applications of stack
Linked list
Unit 4 python -list methods
Threaded binary tree
UNIT III NON LINEAR DATA STRUCTURES – TREES
Infix prefix postfix
Ad

Similar to Arrays (20)

PPT
Arrays
PPTX
Data structure array
DOCX
Array assignment
PDF
05_Arrays C plus Programming language22.pdf
PDF
C++ Course - Lesson 2
PDF
C++ L04-Array+String
PPTX
Arrays matrix 2020 ab
PPT
Fp201 unit4
PPT
Lecture#8 introduction to array with examples c++
PPT
Array Presentation (EngineerBaBu.com)
PPTX
Arrays_in_c++.pptx
PPTX
C_Arrays(3)bzxhgvxgxg.xhjvxugvxuxuxuxvxugvx.pptx
PPT
PPT
2DArrays.ppt
PPT
Lecture#5-Arrays-oral patholohu hfFoP.ppt
PPTX
Array 1D.................................pptx
PPTX
Array programs
DOCX
Lab manual data structure (cs305 rgpv) (usefulsearch.org) (useful search)
PPT
Chapter 10.ppt
Arrays
Data structure array
Array assignment
05_Arrays C plus Programming language22.pdf
C++ Course - Lesson 2
C++ L04-Array+String
Arrays matrix 2020 ab
Fp201 unit4
Lecture#8 introduction to array with examples c++
Array Presentation (EngineerBaBu.com)
Arrays_in_c++.pptx
C_Arrays(3)bzxhgvxgxg.xhjvxugvxuxuxuxvxugvx.pptx
2DArrays.ppt
Lecture#5-Arrays-oral patholohu hfFoP.ppt
Array 1D.................................pptx
Array programs
Lab manual data structure (cs305 rgpv) (usefulsearch.org) (useful search)
Chapter 10.ppt
Ad

More from archikabhatia (10)

PPTX
Computer network
PPT
Structures
PPT
Wild Cards
PPT
Console Io Operations
PPT
Programming Methodology
PPT
Headerfiles
PPT
Computer Fundamentals
PPT
Functions
PPT
PPT
Cso Latest
Computer network
Structures
Wild Cards
Console Io Operations
Programming Methodology
Headerfiles
Computer Fundamentals
Functions
Cso Latest

Recently uploaded (20)

PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Electronic commerce courselecture one. Pdf
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Advanced Soft Computing BINUS July 2025.pdf
PDF
GamePlan Trading System Review: Professional Trader's Honest Take
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PDF
NewMind AI Monthly Chronicles - July 2025
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
GDG Cloud Iasi [PUBLIC] Florian Blaga - Unveiling the Evolution of Cybersecur...
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PDF
Modernizing your data center with Dell and AMD
PPTX
Big Data Technologies - Introduction.pptx
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
The Rise and Fall of 3GPP – Time for a Sabbatical?
Network Security Unit 5.pdf for BCA BBA.
Electronic commerce courselecture one. Pdf
“AI and Expert System Decision Support & Business Intelligence Systems”
Reach Out and Touch Someone: Haptics and Empathic Computing
Advanced methodologies resolving dimensionality complications for autism neur...
Chapter 3 Spatial Domain Image Processing.pdf
Advanced Soft Computing BINUS July 2025.pdf
GamePlan Trading System Review: Professional Trader's Honest Take
CIFDAQ's Market Insight: SEC Turns Pro Crypto
20250228 LYD VKU AI Blended-Learning.pptx
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
NewMind AI Monthly Chronicles - July 2025
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
GDG Cloud Iasi [PUBLIC] Florian Blaga - Unveiling the Evolution of Cybersecur...
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
Modernizing your data center with Dell and AMD
Big Data Technologies - Introduction.pptx
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...

Arrays

  • 1. Made By: Ms. Archika Bhatia ARRAYS
  • 2. WHAT IS AN ARRAY An array is a derived data type ( derived from fundamental data type ) It is a collection of variables of the same type that are referenced by a common name. Consist of contiguous memory locations. Lowest address corresponds to first element Highest address corresponds to the last element. Can have data items of type like: int, char, float and also user-defined types like : structures, objects.
  • 3. NEED FOR AN ARRAY To store large number of variables of same type under a single variable. Easy understanding of the program. E.g. To store Marks of 50 students. Record of sales of 100 salesman.
  • 4. Single Dimensional Array : Element specified by single subscript Syntax : type array_name [ size ] TYPES OF ARRAYS Base type of array Name of array No. of elements that can be stored: Can be a integer value without the sign
  • 5. Creating an Array void main( ) { int a[10]; // declaration of an array ‘a’ int n; // input 10 elements in an array for ( n = 0; n < 10 ; n + +) { cin >> a [ n ]; } // display the 10 elements of the array input for ( n = 0 ; n < 10 ; n + + ) { cout << a [ n ] << endl; } }
  • 6. Memory Representation of Single Dimension Array If the array is float arr [ 5 ]; memory representation would be as follows: Arr [ 0 ] Arr [ 1 ] Arr [ 2 ] Arr [ 3 ] Arr [ 4 ] 5016 5012 5008 5004 5000 Total Memory requirement is : size of ( type ) * size of array 4 * 5 = 20 bytes
  • 7. ARRAY INITIALISATION int list [ 5 ] ; // declaration int list [ 5 ] = { 10, 20, 30, 40, 50 } ; // decalaration & initialisation
  • 8. UNSIZED ARRAY INITIALISATION Can skip the size of an array in array initialisation Elements of an array can be added or removed without changing array dimensions. E.g. float price [ ] = { 50.5, 63.97, 84.6, 779.8 };
  • 9. Program to count the no. of employees earning more than Rs. 1 lakh per annum. Monthly salaries of 10 employees are given. void main ( ) { const int size = 10 ; float sal [ size ] , an_sal ; int count = 0; // loop to accept monthly salaries of 10 employees for ( int j = 0 ; j < size ; j + + ) { cout << “ Enter the monthly salary of employee “ << j + 1 ; cin >> sal [ j ]; }
  • 10. // loop to count employees earning more than Rs. 1 lakh per annum for ( j = 0 ; j < size ; j + + ) { an_sal = sal [ j ] * 12 ; if ( an_sal > 100000 ) { count ++ ; } } cout << count << “ employees out of “ << size << “ employees are earning more than Rs. 1 lakh per annum “ ; }
  • 11. Passing arrays in a function Note: Arrays are always passed as reference Program to create functions for input array and display array void main ( ) { void input ( int a [ 10 ] ); void display ( int a [ 10 ] ); int arr [ 10 ]; input ( arr ); display ( arr ); }
  • 12. void input ( int a [ 10 ] ) { for ( int x = 1; x < 10; x ++ ) { cout << “ enter the element “ << x + 1; cin >> a [ x ]; } } void display ( int a [ 10 ] ) { for ( int x = 1; x < 10 ; x ++ ) { cout << “ the element no “ << x + 1 << “ is : “<< a [ x ]; } }
  • 13. WAP to input 10 numbers in an array and replace all even no.s by 0 and odd no.s by 1 void main ( ) { int a [ 10 ], n; // loop to accept 10 values in an array ‘a’ for ( n = 0; n < 10 ; n + +) { cin >> a [ n ]; } // loop to check if the element of an array is even replace by 0 and if odd replace // by 1 for ( n = 0; n < 10 ; n + +) { if ( ( a [ n ] % 2 ) == 0 ) { a [ n ] = 0; } else { a [ n ] = 1 ; } }
  • 14. // display the 10 elements of the array for ( n = 0 ; n < 10 ; n + + ) { cout << a [ n ] << endl; } }
  • 15. WAP to input 10 numbers in an array and replace all even no.s by 0 and odd no.s by 1 using functions void main ( ) { void input ( int a [ 10 ] ); void display ( int a [ 10 ] ); void replace ( int a [10 ] ); int arr [ 10 ] ; input ( arr ); cout<< “ elements before replacement “; display ( arr ); replace ( arr ); cout<< “ elements after replacement “ ; display ( arr ); }
  • 16. void input ( int a [ 10 ] ) { for ( int x = 0; x < 10; x ++ ) { cout << “ enter the element “ << x + 1; cin >> a [ x ]; } } void display ( int a [ 10 ] ) { for ( int x = 0; x < 10 ; x ++ ) { cout << “ the element no “ << x + 1 << “ is : “<< a [ x ]; } }
  • 17. void replace ( int a [ 10 ] ) { for ( int n = 0; n < 10 ; n + +) { if ( ( a [ n ] % 2 ) == 0 ) { a [ n ] = 0; } else { a [ n ] = 1 ; } }
  • 18. WAP to find the largest and smallest no. in an array of 10 elements // input an array // display the array // to find the largest element largest = a [ 0 ] ; for ( n = 1 ; n < 10 ; n + + ) { if ( a [ n ] > largest ) { largest = a [ n ]; } } cout << “ largest value is : “ << largest ; // to find the lowest element lowest = a [ 0 ]; for ( n = 1 ; n < 10 ; n + + ) { if ( a [ n ] < lowest ) { lowest = a [ n ]; } } cout << “ lowest value is : “ << lowest ;
  • 19. // function to find the largest element void largest ( int a [ 10 ] ) { largest = a [ 0 ] ; for ( n = 0; n < 10 ; n + + ) { if ( a [ n ] > largest ) { largest = a [ n ]; } } }
  • 20. // to find the lowest element void lowest ( int a [ 10 ] ) { lowest = a [ 0 ]; for ( n = 0 ; n < 10 ; n + + ) { if ( a [ n ] < lowest ) { lowest = a [ n ]; } } cout << “ lowest value is : “ << lowest ; }
  • 21. PRACTICE QUESTIONS Give the output of the following: void main( ) { int a [ 5 ] , j , p = 10; for ( j = 0; j < 5 ; j + + ) a [ j ] = p * j ; for ( k = 0 ; k < 5 ; k + +) cout << a [ k ]; } OUTPUT: 010203040
  • 22. Give the output of the following: void main ( ) { int b [ 10 ]; for ( int j = 0 ; j < 3 ; j + +) for ( int k = 0 ; k < 3; k + +) { b [ j ] = 4 * j + k ; cout << “\n “ << b [ j ]; } }
  • 23. output 0 1 2 4 5 6 8 9 10
  • 24. Give the output of the following: void main ( ) { int b = 0 ; int c [ 10 ] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; for ( int a = 0; a < 10; a + +) { if ( ( c [ a ] % 2 ) = = 0 ) b + = c [ a ]; } cout << “ b = “ << b ; } Output: 30
  • 25. Give the output void main ( ) { int arr[10]; cout<<arr; getch( ); }
  • 26. Find errors in the following program code: main ( ) { int a [ 6 ] = { 0 , 1 ; 2 , 3 , 6, 4 } ; char name [ ] = { ‘A’, “ B “ , ‘ C ‘ }; for ( j = 0 ; j < = 5 ; j + +) cout << a [ j ]; }
  • 27. Find errors in the following program code: ( Assume header files included ) void main ( ) { const int a = 9 ; int p , q ; cout << “ \n enter the value of p and q ; p + q = p ; a = a + 5 ; cout << a; }
  • 28. Find errors in the following program code: ( Assume header files included ) void main ( ) { int arr ( 5 ) ; float function ( int , int ); for ( int j = 0 ; j < 5 ; j ++ ) cin >> a ( j ); cout << func ( 5 ); } float function ( int x ; int y ) { return ( x – y ); }
  • 29. Find errors in the following program code: ( Assume header files included ) void main ( ) { int r ; w = 90; while w > 60 { r = w – 50 ; switch [ w ] { 20 : cout << “ lower range “; case 30 : cout << “ higher range “; } }
  • 30. Find errors in the following program code: ( Assume header files included ) void main ( ) { test ( void ); void testcopy ( void ); int a, b ; cin >> a >>b; for ( int j = a ; j < b ; j + + ) { test ( ) ; } } char test ( void ) { main ( ); cout << “ hello “ ; testcopy ( ) ; return ( 0 ) ; }
  • 31. SEARCHING FOR AN ELEMENT IN AN ARRAY There are two ways to search for an element: Linear Search: checks each element of the array . If the match found – element exists, else does not exist. Suitable for small lists since time consuming Suitable for unsorted lists. Binary Search: Most important precondition is that the list should be sorted. Suitable for arrays with long list of elements.
  • 32. void linear_search( int a [ 10 ], int size, int no ) { for ( int j = 1 ; j < size ; j ++ ) { if ( no == a [ j ] ) { cout << “ element found “; break ; } } Function for Linear Searching
  • 33. Search for the element using Linear Search and count the no. of times the element occurs in the array. void linear_search( int a [ 10 ], int size, int no ) { int flag = 0, count = 0; for ( int j = 1 ; j < size ; j ++ ) { if ( no == a [ j ] ) { flag=1; count ++ ; } if ( flag = = 0 ) { cout<< “ element not found “; } else { cout<< “ element found “ << count << “ no. of times “; } }
  • 34. Binary Search method Element to search : 45 Array Positions: 0 1 2 3 4 5 6 7 8 9 Given array: 2 6 8 9 10 12 16 19 20 55 m f m f l l l f f m l m l Note: need to check f < = l
  • 35. Element to search : 1 Array Positions: 0 1 2 3 4 5 6 7 8 9 Given array: 2 6 8 9 10 12 16 19 20 55 m l l f m f l f m l
  • 36. Element to search : 12 Array Positions: 0 1 2 3 4 5 6 7 8 9 Given array: 2 6 8 9 10 12 16 19 20 55 f m l l f m f l m
  • 37. INSERTING AN ELEMENT IN AN ARRAY Check for the availability of space. Shift all the elements to right and create space for new element. Insert the new element at the space created. Increase the size of array by 1. Insert new element at the space created here. POS VALUE 12 13 12 10 8 7 6 6 5 4 3 2 1 0
  • 38. DELETING AN ELEMENT IN AN ARRAY Shift all the elements to LEFT. The element to be deleted will automatically be overwritten. Add 0 at the last position. Decrease the size of the array by 1. Insert 0 at the empty space created here. POS VALUE 6 5 4 3 2 1 0 12 13 12 10 8 7 6
  • 39. SORTING Means arranging the array elements in either ascending or descending order. Three methods of sorting are: Bubble sort Exchange Selection sort Insertion Sort
  • 40. BUBBLE SORT S NS NS S S PASS - I 9 5 6 2 4 3 5 9 6 2 4 3 5 6 9 2 4 3 5 6 2 9 4 3 5 6 2 9 4 3 5 6 2 9 4 3
  • 41. BUBBLE SORT NS S NS S NS PASS - II 9 6 5 4 2 3 9 6 5 4 2 3 9 5 6 4 2 3 9 5 6 4 2 3 9 5 6 2 4 3 9 5 6 2 4 3
  • 42. BUBBLE SORT NS NS S NS NS PASS - III 9 6 5 4 3 2 9 6 5 4 3 2 9 6 5 4 3 2 9 6 5 4 3 2 9 6 5 4 3 2 9 6 5 4 2 3
  • 43. BUBBLE SORT NS NS NS NS NS PASS - IV 9 6 5 4 3 2 9 6 5 4 3 2 9 6 5 4 3 2 9 6 5 4 3 2 9 6 5 4 3 2 9 6 5 4 3 2
  • 44. EXCHANGE SELECTION SORT UNSORTED smallest First element of unsorted SORTED UNSORTED SORTED SORTED UNSORTED UNSORTED SORTED UNSORTED SORTED UNSORTED 9 6 5 4 3 2 9 6 5 4 3 2 9 6 5 4 3 2 5 6 9 4 3 2 5 6 4 9 3 2 5 6 3 9 4 2 5 6 2 9 4 3
  • 45. INSERTION SORT UNSORTED First element of unsorted SORTED UNSORTED SORTED SORTED UNSORTED UNSORTED SORTED UNSORTED SORTED SORTED Find the correct place for the element and keep shifting the elements to the right. 8 7 5 3 2 2 5 8 7 3 2 2 5 2 8 7 3 2 5 2 2 8 7 3 5 2 2 3 8 7 5 2 2 3 8 7
  • 46. MERGING OF TWO ARRAYS INTO A SINGLE ARRAY You need to sort the resultant array while merging The given two arrays should be in sorted order. STEP - I 15 9 6 5 3 B (elements) 4 3 2 1 0 B (pos) 15 13 12 8 7 3 2 A (elements) 6 5 4 3 2 1 0 A (pos) C (elements) C (pos) 2 11 10 9 8 7 6 5 4 3 2 1 0
  • 47. MUTLIDIMENSIONAL ARRAY Has more than one subscript S.D.A. is most suitable for processing lists Two Dimensional array is the simplest form of M.D.A. T.D.A. has 2 subscripts: 1 st subscript for row and 2 nd subscript for column. T.D.A. is most suitable for table processing or matrix manipulation.
  • 48. ARRAY DECLARATION Syntax: type array-name [rows] [columns]; e.g. int mat [ 2 ] [ 3 ] ; i.e. total 2 * 3 = 6 elements rows columns ( 1, 2 ) ( 1, 1 ) ( 1, 0 ) 1 ( 0, 2 ) ( 0 , 1 ) ( 0, 0 ) 0 2 1 0
  • 49. UNSIZED ARRAY INITIALISATION First subscript of T.D.A. may be skipped. Second subscript must be given. E.g. int mat [ ] [ 2 ] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
  • 50. ADDRESS CALCULATION OF 1DA Address of element with subscript I = base Address + E.S ( I – L ) E.S. : size of array element L : Lower bound of array e.g. address of element checks [2] of following array checks [ -3 , 3 ] with element size 2 bytes and base address as 1000. Address of check [ 2 ] = B.A + E.S. ( I – L ) = 1000 + 2 * ( 2 – ( -3 ) = 1000 + 2 * 5 = 1010
  • 51. ADDRESS CALCULATION OF 2DA ROW MAJOR IMPLEMENTATION Given Array: AR [ L1 : U1, L2 : U2 ] Address of element AR [ i, j ] = base Address + E.S ( n ( i – L1 ) + ( j – L2 ) ) i : row position of element whose address is to be found. j : column position of element whose address is to be found. E.S. : size of array element L1 : Lower bound of 1 st subscript i.e. row L2 : Lower bound of 2 nd subscript i.e column n : No. of columns = U2 – L2 + 1
  • 52. Given Array Row Major Implementation Column Major Implementation 8 7 2 6 5 3 8 7 2 6 5 3 8 6 7 5 2 3
  • 53. e.g. Each element of an array A [ -20 … 20, 10…35 ] requires one byte of storage. If the array is stored in row major order beginning location 500, determine the location of A [ 0, 30 ] . A[ 0, 30 ] = base Address + E.S ( n ( i – L1 ) + ( j – L2 ) ) n = 35 – 10 + 1 = 26 = 500 + 1 ( 26 * ( 0 – ( -20 ) )+ (30 – 10 ) ) = 500 + ( 26 ( 20 ) + 20 ) = 500 + ( 520 + 20 ) = 500 + 540 = 1040
  • 54. ADDRESS CALCULATION OF 2DA COLUMN MAJOR IMPLEMENTATION Given Array: AR [ L1 : U1, L2 : U2 ] Address of element AR [ I, j ] = base Address + E.S ( ( i – L1 ) + m ( j – L2 ) ) i : row position of element whose address is to be found. j : column position of element whose address is to be found. E.S. : size of array element L1 : Lower bound of 1 st subscript i.e. row L2 : Lower bound of 2 nd subscript i.e column m : No. of rows = U1 – L1 + 1
  • 55. e.g. Each element of an array A [ -20 … 20, 10…35 ] requires one byte of storage. If the array is stored in column major order beginning location 500, determine the location of A [ 0, 30 ] . A[ 0, 30 ] = base Address + E.S ( ( i – L1 ) +m ( j – L2 ) ) m = 20 – (-20) + 1 = 41 = 500 + 1 ( ( 0 – ( -20 ) )+ 41 *(30 – 10 ) ) = 500 + ( 20 + (41 * 20 ) ) = 500 + ( 20 + 820 ) = 500 + 840 = 1340
  • 56. MULTIPLICATION OF TWO MATRICES A[M][N] * B[N][P] = C[M][P] * A[3][2] B[2][4] 3 (2,1) 2 (2,0) 1 (1,1) 4 (1,0) 6 (0,1) 5 (0,0) 1 (1,3) 6 (1,2) 4 (1,1) 5 (1,0) 0 (0,3) 1 (0,2) 3 (0,1) 2 (0,0)
  • 57. A[M][N] * B[N][P] = C[M][P] * A[3][2] B[2][4] C[3][4] 3 (2,1) 2 (2,0) 1 (1,1) 4 (1,0) 6 (0,1) 5 (0,0) 1 (1,3) 6 (1,2) 4 (1,1) 5 (1,0) 0 (0,3) 1 (0,2) 3 (0,1) 2 (0,0) 2*0 + 3*1 2*1 + 3*6 2*3 + 3*4 2*2 + 3*5 4*0 + 1*1 4*1 + 1*6 4*3 + 1*4 4*2 + 1*5 5*0 + 6*1 5*1 + 6*6 5*3 + 6*4 5*2 + 6*5
  • 58. TRANSPOSE OF A MATRIX No. of rows and columns may not be same Changes rows to columns and columns to rows E.g transpose is 5 3 4 6 (0,0) (0,1) (0,0) (0,1) (0,2) 4 3 5 3 2 (1,0) (1,1) (1,0) (1,1) (1,2) 6 2 (2,0) (2,1)
  • 59. LOWER TRIANGLE 3 5 6 12 6 9 89 0 1 1 ST TRIANGLE 2 ND TRIANGLE 3 6 12 6 6 9 89 0 1 89 0 1
  • 60. UPPER TRIANGLE 3 5 6 12 6 9 89 0 1 1ST TRIANGLE 2ND TRIANGLE 3 5 6 3 5 6 12 6 6 9 89 1
  • 61. WAP to find row and column sum of a matrix. Create input, display and row_col_sum function for the same. IP OP 9 (2,2) 8 (2,1) 7 (2,0) 6 (1,2) 5 (1,1) 4 (1,0) 3 (0,2) 2 (0,1) 1 (0,0) 0 17 14 11 21 9 8 7 15 6 5 4 6 3 2 1