ECE 114-9 Arrays -II Dr. Z. Aliyazicioglu Electrical & Computer Engineering Electrical & Computer Engineering 1 Outline Introduction Arrays Declaring and Allocation Arrays Examples Using Arrays Passing Arrays to Functions Sorting Arrays Searching Arrays Multidimensional Arrays 2 1
Passing Arrays to Functions To pass array to function, specify the name without any brackets in call line To pass array hourlytemperatures declared as int hourlytemperatures gc[] = new int gc[ 24 ]; then the function call would be ModifyArray (hourlytemperatures); passes array hourlytemperatures (by reference) to function ModifyArray Every array object knows its own size, don t need to pass the size Value of name of array is address of the first element Function knows where the array is stored» Modifies original memory locations Individual array elements pass by call-by-value pass subscripted name (i.e., myarray[ 3 ]) to function 3 Passing Arrays to Functions Function prototype: void ModifyArray( int b gc[] ); Indicates that ModifyArray expect to receive an integer array in parameter b. Arrays are passes by reference A function can also return an array by defining as int FunctionName (parameter-list) gc[]; The gc [ ] indicates that function returns an integer array. optional int FunctionName (parameter-list) []; 4 2
Example #include "stdafx.h" #using <mscorlib.dll> using namespace System; //Function prototype void ModifyArray ( int gc[] ); void ModifyElement ( int ); int _tmain() int array1 gc[] = 1, 2, 3, 4, 5; Console::WriteLine( S"Effects of passing entire array call-by-reference: " S"\n\nThe values of the original array are:" ); for ( int i = 0; i < array1 ->Length; i++ ) Console::Write ( S" 0", array1[ i].tostring() ); ModifyArray(array1); //array is passed by reference Console::WriteLine( S"\nModified array's values:" ); for ( int i = 0; i < array1 ->Length; i++ ) Console::Write ( S" 0", array1[ i].tostring() ); 5 Example(cont) Console::Write ( S" \n\neffects of passing array elements" S"pass-by-value:\n\narray1[3] before" ); Console::Write ( S" ModifyElement : 0", array1[ 3].ToString() ); ModifyElement( array1[ 3 ] ); Console::WriteLine ( S"\narray [3] after ModifyElement : 0", array1[ 3].ToString() ); return 0; // Function modify by array it receives void ModifyArray( int array2 gc[ ]) for ( int j = 0; j < array2 -> Length; j++ ) array2 [ j ] *= 2; 6 3
Example (cont) // Function, local copy of array element a[ 3 ] passed from main. void ModifyElement( int element ) Console::WriteLine ( S"\nValue recived in ModifyElement : 0", element.tostring() ); element *=2; Console::WriteLine ( S" Value calculated in ModifyElement : 0", element.tostring() ); 7 Example Output 8 4
Sorting Array Sorting data Important computing application Virtually every organization must sort some data» Massive amounts must be sorted Ascending or descending order Bubble sort (sinking sort) Several passes through the array Successive pairs of elements are compared» If increasing order (or identical), no change» If decreasing order, elements exchanged Repeat these steps for every element 9 Example Using Arrays Example: a[4]=3,4,2,6,1 Original: 3 4 2 6 1 a[0] a[1] a[2] a[3] a[4] Pass 1: 3 4 2 6 1 3 2 4 6 1 3 2 4 1 6 Pass 2: 3 2 4 1 6 2 3 4 1 6 2 3 1 4 6 Pass 3: 2 3 1 4 6 2 1 3 4 6 Pass 4: 2 1 3 4 6 1 2 3 4 6 Small elements "bubble" to the top To swap a[i] with a[i+1] if ( a[i] > a[i+1]) hold=a[i]; a[i]=a[i+1]; a[i+1]=hold; 10 5
9 5 7 22 a[0] a[1] a[2] a[3] Example for (pass = 1; pass < 3; pass ++) for ( i = 0; i < 3-1; i++) if(a[i] > a[i+1]) temp = a[i]; a[i]=a[i+1]; a[i+1]=temp; 11 Example #include "stdafx.h" #using <mscorlib.dll> using namespace System; void BubleSort ( int gc[] ); void Swap ( int gc[], int ); int _tmain() int a gc[] = 2, 6, 4, 8, 10, 12, 89, 68, 45, 37; Console::WriteLine( S"Data items in original order " ); for ( int i = 0; i < a -> Length; i++ ) Console::Write ( S" 0", a[ i].tostring() ); BubleSort( a ); //Sort element of array Console::WriteLine( S"\nData item in ascending order" ); for ( int i = 0; i < a -> Length; i++ ) Console::Write ( S" 0", a[ i].tostring() ); Console::WriteLine ( ); return 0; 12 6
Example (cont) // Function BubleSort void BubleSort( int b gc[ ]) for ( int pass = 1; pass < b -> Length; pass++ ) for ( int i = 0; i < b -> Length -1; i++ ) if (b[i] > b[i+1]) Swap(b,i); // Function Swap void Swap( int c gc[], int first ) int hold; hold = c[ first ]; c[ first ]=c[ first + 1 ]; c[ first+1 ]=hold; 13 Different sorting idea for (i=0;i<n-1;i=++) for ( j=i+1;j<n;j++) if(a[i] > a[j]) temp = a[i]; a[i]=a[j]; a[j]=temp; 2 15 9 22 a[0] a[1] a[2] a[3] temp = a[1] a[1]=a[2] a[2]=temp 14 7
Searching Arrays: Linear Search and Binary Search Search array for a key value Linear search Compare each element of array with key value Useful for small and unsorted arrays Binary search Can only be used on sorted arrays Compares middle element with key» If equal, match found» If key < middle, repeat search through the first half of the array» If key > middle, repeat search through the last half of the array Very fast; at most n steps, where 2 n > # of elements» 30 element array takes at most 5 steps 2 5 > 30 15 #include "stdafx.h" #using <mscorlib.dll> using namespace System; //Function prototype int LinearSearch( int gc[], int); Example of Linear search int _tmain() int a gc[] = 2, 6, 4, 8, 10, 12, 14, 16, 18, 20, 22,24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50; Console::Write( S"Please enter the search key :" ); int searchkey = Int32::Parse( Console::ReadLine() ); int elementindex = LinearSearch (a, searchkey); if (elementindex!= -1) Console::WriteLine ( S" Found value in element 0", elementindex.tostring() ); else Console::WriteLine ( S" Value not found"); return 0; 16 8
Example (cont) // Function LienarSearch int LinearSearch( int array gc[], int key) for ( int n = 0; n < array -> Length; n++ ) if (array [n] == key) return n; return -1; 17 Example of binary search: Min=0 a[14]=0, 2,4,6,8,10,12,14,16,18,20,22,24,26,28 15 elements Search key =20 mid=(min+max)/2=7 If (20 ==a[mid]), If (20 > a[mid]) 0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 Max=14 min max if (20 >a[mid]) min 16 18 20 22 24 26 28 16 18 20 20 max mid=(min+max)/2=(8+14)/2=11 18 9
Example of binary search #include "stdafx.h" #using <mscorlib.dll> using namespace System; //Function prototype int BinarySearch( int gc[], y); void ShowOutput( int gc[], int, int, int); int _tmain() int a gc[] = 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22,24, 26, 28; Console::Write( S"Please enter the search key :" ); int searchkey = Int32::Parse( Console::ReadLine() ); Console::WriteLine ( S"\nPortion of array searched"); int element = BinarySearch (a, searchkey); if (element!= -1) Console::WriteLine ( S" Found value in element 0", element.tostring() ); else Console::WriteLine ( S" Value not found"); return 0; 19 Example Cont. // Function BinarySearch int BinarySearch( int array gc[], int key) int low = 0; //low index int high = array ->Length -1; //high index int middle; //middel index while ( low <= high) middle=(low + high) / 2; ShowOutput (array, low, middle, high); if (key == array[middle]) return middle; else if (key < array[ middle ]) high=middle-1; else low = middle + 1; return -1; 20 10
Example cont. //Function ShowOutput void ShowOutput (int array gc[], int low, int mid, int high) for (int i=0; i <array ->Length; i++) if (i < low i > high) Console :: Write ( S" "); else if (i == mid) Console::Write( S"0* ", array[i].tostring("00") ); else Console::Write( S"0 ", array[i].tostring("00") ); Console::WriteLine(); 21 Output 22 11