Arrays

17,870 views

Published on

presentation on arrays, definition, need, types of array, writing simple programs on array.

Published in: Technology
9 Comments
67 Likes
Statistics
Notes
No Downloads
Views
Total views
17,870
On SlideShare
0
From Embeds
0
Number of Embeds
1,518
Actions
Shares
0
Downloads
12
Comments
9
Likes
67
Embeds 0
No embeds

No notes for slide

Arrays

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

×