Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC 113: C OMPUTER P ROGRAMMING (T HEORY = 03, L AB = 01) Computer Science Department Bahria University, Islamabad.

Similar presentations


Presentation on theme: "CSC 113: C OMPUTER P ROGRAMMING (T HEORY = 03, L AB = 01) Computer Science Department Bahria University, Islamabad."— Presentation transcript:

1 CSC 113: C OMPUTER P ROGRAMMING (T HEORY = 03, L AB = 01) Computer Science Department Bahria University, Islamabad

2 A RRAYS -II Week # 12

3 3 M ULTIPLE -S UBSCRIPTED A RRAYS Multiple subscripts - tables with rows, columns Like matrices: specify row, then column. Initialize int b[ 2 ][ 2 ] = { { 1, 2 }, { 3, 4 } }; Initializers grouped by row in braces int b[ 2 ][ 2 ] = { { 1 }, { 3, 4 } }; Row 0 Row 1 Row 2 Column 0Column 1Column 2Column 3 a[ 0 ][ 0 ] a[ 1 ][ 0 ] a[ 2 ][ 0 ] a[ 0 ][ 1 ] a[ 1 ][ 1 ] a[ 2 ][ 1 ] a[ 0 ][ 2 ] a[ 1 ][ 2 ] a[ 2 ][ 2 ] a[ 0 ][ 3 ] a[ 1 ][ 3 ] a[ 2 ][ 3 ] Row subscript Array name Column subscript 1 2 3 4 1 0 3 4

4 4 M ULTIPLE -S UBSCRIPTED A RRAYS Referenced like normal cout << b[ 0 ][ 1 ]; Will output the value of 0 Cannot reference with commas cout << b( 0, 1 ); Will try to call function b, causing a syntax error

5 S ELF P RACTICE Declare an array table to be an integer array with 3 rows and 3 columns. How many elements does the array contain? Using a for structure, initialize each element to the sum of its subscripts. Write a piece of code to print each element in tabular format of 3x3.

6 P ASSING A RRAYS AS FUNCTION ARGUMENTS Week # 12

7 7 P ASSING A RRAYS TO F UNCTIONS Specify the name without any brackets To pass array myArray declared as int myArray[ 24 ]; to function myFunction, a function call would resemble myFunction( myArray, 24 ); Array size is usually passed to function Arrays passed call-by-reference 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 passed by call-by-value pass subscripted name (i.e., myArray[ 3 ] ) to function

8 8 P ASSING A RRAYS TO F UNCTIONS Function prototype: void modifyArray( int b[], int arraySize ); Parameter names optional in prototype int b[] could be simply int [] int arraysize could be simply int

9  2000 Prentice Hall, Inc. All rights reserved. Outline 9 1. Define function to take in arrays 1.1 Initialize arrays 2. Modify the array (call by reference) 1// Fig. 4.14: fig04_14.cpp 2// Passing arrays and individual array elements to functions 3#include 4 5using std::cout; 6using std::endl; 8#include 10using std::setw; 11 12void modifyArray( int [], int ); // appears strange 13void modifyElement( int ); 15int main() 16{ 17 const int arraySize = 5; 18 int i, a[ arraySize ] = { 0, 1, 2, 3, 4 }; 20 cout << "Effects of passing entire array call-by-reference:" 21 << "\n\nThe values of the original array are:\n"; 23 for ( i = 0; i < arraySize; i++ ) 24 cout << setw( 3 ) << a[ i ]; 26 cout << endl; 28 // array a passed call-by-reference 29 modifyArray( a, arraySize ); 31 cout << "The values of the modified array are:\n"; Functions can modify entire arrays. Individual array elements are not modified by default. No parameter names in function prototype. The values of the original array are: 0 1 2 3 4 The values of the modified array 0 2 4 6 8

10  2000 Prentice Hall, Inc. All rights reserved. Outline 10 2.1 Modify an element (call by value) 3. Print changes. 3.1 Function Definitions 33 for ( i = 0; i < arraySize; i++ ) 34 cout << setw( 3 ) << a[ i ]; 36 cout << "\n\n\n" 37 << "Effects of passing array element call-by-value:" 38 << "\n\nThe value of a[3] is " << a[ 3 ] << '\n'; 40 modifyElement( a[ 3 ] ); 42 cout << "The value of a[3] is " << a[ 3 ] << endl; 44 return 0; 45} 49void modifyArray( int b[], int sizeOfArray ) 50{ 51 for ( int j = 0; j < sizeOfArray; j++ ) 52 b[ j ] *= 2; 53} 57void modifyElement( int e ) 58{ 59 cout << "Value in modifyElement is " 60 << ( e *= 2 ) << endl; 61} Parameter names required in function definition Effects of passing array element call-by-value: The value of a[3] is 6 Value in modifyElement is 12 The value of a[3] is 6

11  2000 Prentice Hall, Inc. All rights reserved. Outline 11 Program Output Effects of passing entire array call-by- reference: The values of the original array are: 0 1 2 3 4 The values of the modified array are: 0 2 4 6 8 Effects of passing array element call-by-value: The value of a[3] is 6 Value in modifyElement is 12 The value of a[3] is 6

12 S ORTING A RRAYS Week # 12

13 13 S ORTING A RRAYS Sorting data Important computing application Virtually every organization must sort some data Massive amounts must be sorted 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

14 14 S ORTING A RRAYS Example: Original: 3 4 2 6 7 Pass 1: 3 2 4 6 7 Pass 2: 2 3 4 6 7 Small elements "bubble" to the top

15 E XAMPLE : B UBBLE S ORT //Sorting array in ascending order #include int main() { const int arraySize = 10; int a[arraySize]= {2,4,6,8,10,12,89,68,45,37}; int hold; cout<<“Data in original order”<<endl; for(int i=0; i<arraySize; i++) cout<<a[i]; for(int pass=1; pass<arraySize; pass++) for(i=0; i<arraySize-1; i++) if(a[i]>a[i+1]){ hold=a[i]; a[i]=a[i+1]; a[i+1]=hold;} cout<<endl<<“Data items in ascending order”<<endl; for(i=0; i<arraySize; i++) cout<<a[i]; cout<<endl; return 0; }

16

17 17 E XAMPLES : C OMPUTING M EAN, M EDIAN AND M ODE U SING A RRAYS Mean Average Median Number in middle of sorted list 1, 2, 3, 4, 5 (3 is median) Mode Number that occurs most often 1, 1, 1, 2, 3, 3, 4, 5 (1 is mode)

18 S EARCHING A RRAYS Week # 12

19 19 S EARCHING A RRAYS Search array for a key value Linear Search Binary Search 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

20 E XAMPLE : L INEAR S EARCH //Linear Search of an array #include int linearSearch(int[], int, int); int main() { const int arraySize = 100; int a[arraySize], searchKey, element; for(int i=0; i<arraySize; i++) a[i]=2*i; cout<<“Enter integer search key:” <<endl; cin>>searchKey; element=linearSearch(a, searchKey, arraySize); if(element!=-1) cout<<“Found value in element” <<element<<endl; else cout<<“Value not found”<<endl; return 0; } int linearSearch(int array[], int key, int sizeOfArray) { for(int n=0; n<sizeOfArray; n++) if(a[n]==key) return element; else return -1; }

21 E XAMPLE : B INARY S EARCH //Linear Search of an array #include int binarySearch(int[], int, int, int, int); int main() { const int arraySize = 100; int a[arraySize], searchKey, element; for(int i=0; i<arraySize; i++) a[i]=2*i; cout<<“Enter integer search key:” <<endl; cin>>searchKey; element=binarySearch(a, searchKey, 0, arraySize-1, arraySize); if(element!=-1) cout<<“Found value in element” <<element<<endl; else cout<<“Value not found”<<endl; return 0; } int binarySearch(int array[], int key, int low, int high, int sizeOfArray) { int middle; while(low<=high) { middle=(low+high)/2; if(key==array[middle]) return middle; else if(key<array[middle]) high=middle-1; else low=middle+1; } return -1; }

22 B INARY SEARCH


Download ppt "CSC 113: C OMPUTER P ROGRAMMING (T HEORY = 03, L AB = 01) Computer Science Department Bahria University, Islamabad."

Similar presentations


Ads by Google