Download presentation
Presentation is loading. Please wait.
Published byΠαρθενιά Καλλιγάς Modified over 6 years ago
1
C++ Programming Lecture 16 Arrays – Part III
By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department
2
The Hashemite University
Outline Introduction. Searching arrays: Linear search. Binary search. Multisubscripted arrays. Passing Multisubscripted arrays to functions. Examples. The Hashemite University
3
The Hashemite University
Introduction In this lecture we will continue with specific topics related to arrays. We will explore how to search an array for some value. The result of the search is the location (i.e. index or subscript) of the element that has an equal value to the one that we are searching for. The second part of our lecture introduce multi-subscripted arrays which correspond to multidimensional matrices. We will se how to define, initialize, and pass such arrays to functions. The Hashemite University
4
The Hashemite University
Searching Arrays I Search array for a key value and return the subscript of the location in the array. Two types: Linear search. Binary search. Linear search Compare each element of array with key value till you find the key. Useful for small and unsorted arrays. The best case is to find the key in the first element of the array. So, you need 1 comparison or one step. The worst case is to find the key in the last element of the array. So, you need arraySize comparisons or steps. The Hashemite University
5
The Hashemite University
Searching Arrays II 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 2n is the smallest number larger than the number of elements in the array. E.g.: 30 element array takes at most 5 steps 25 > 30 The Hashemite University
6
Linear and Binary Search Code
On board. Found in Figure 4.20 in the textbook. The Hashemite University
7
Multiple-Subscripted Arrays I
Column subscript Row 0 Row 1 Row 2 Column 0 Column 1 Column 2 Column 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 Multiple subscripts - tables with rows, columns Like matrices: specify row, then column. The Hashemite University
8
Multiple-Subscripted Arrays II
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 The Hashemite University
9
Multiple-Subscripted Arrays III
Initialization: Using two nested for loops. Using cin to get values from the keyboard. Using initializers list: Initializers grouped by row in braces int b[ 2 ][ 2 ] = { { 1, 2 }, { 3, 4 } }; int b[ 2 ][ 2 ] = { { 1 }, { 3, 4 } }; 1 2 3 4 1 0 3 4 The Hashemite University
10
Multiple-Subscripted Arrays IV
Try the following on your computer and see what is the results of the following initializations and whether there is a syntax error or not. int b[ 2 ][ 2 ] = { 1, 2, 5 }; //Correct int b[][] = {{1,3},{5},{6,7,8}}; //Syntax Error int b[ 2 ][] = { 1, 2, 5 };//Syntax Error int b[][ 2 ] = { 1, 2, 5 }; //Correct int b[][ 2 ][4] = { 1, 2, 5 }; //Correct int b[][ 2 ][] = { 1, 2, 5 }; // Syntax Error int b[][] = { 1, 2, 5 };//Syntax Error Note that only the first subscript (or dimension size) is allowed to be empty. The Hashemite University
11
Passing Multiple Subscripted Arrays to Functions
The same as one-dimensional array with the exception that you are allowed to have the first dimension only empty, the rest of the dimensions must have their actual size in its square brackets in both the prototype and the function definition. If you use a variable to denote the size of a dimension in a prototype, you must have this variable as global. Again, arrays are passed as call-by-reference by default. The Hashemite University
12
The Hashemite University
1 // Fig. 4.23: fig04_23.cpp 2 // Double-subscripted array example 3 #include <iostream> 4 5 using std::cout; 6 using std::endl; 7 using std::ios; 8 9 #include <iomanip> 10 11 using std::setw; 12 using std::setiosflags; 13 using std::setprecision; 14 15 const int students = 3; // number of students 16 const int exams = 4; // number of exams 17 18 int minimum( int [][ exams ], int, int ); 19 int maximum( int [][ exams ], int, int ); 20 double average( int [], int ); 21 void printArray( int [][ exams ], int, int ); 22 23 int main() 24 { 25 int studentGrades[ students ][ exams ] = { { 77, 68, 86, 73 }, { 96, 87, 89, 78 }, { 70, 90, 86, 81 } }; 29 30 cout << "The array is:\n"; 31 printArray( studentGrades, students, exams ); 32 cout << "\n\nLowest grade: " << minimum( studentGrades, students, exams ) Each row is a particular student, each column is the grades on the exam. The Hashemite University
13
The Hashemite University
<< "\nHighest grade: " << maximum( studentGrades, students, exams ) << '\n'; 36 37 for ( int person = 0; person < students; person++ ) cout << "The average grade for student " << person << " is " << setiosflags( ios::fixed | ios::showpoint ) << setprecision( 2 ) << average( studentGrades[ person ], exams ) << endl; 42 43 return 0; 44 } 45 46 // Find the minimum grade 47 int minimum( int grades[][ exams ], int pupils, int tests ) 48 { 49 int lowGrade = 100; 50 51 for ( int i = 0; i < pupils; i++ ) 52 for ( int j = 0; j < tests; j++ ) 54 if ( grades[ i ][ j ] < lowGrade ) lowGrade = grades[ i ][ j ]; 57 58 return lowGrade; 59 } 60 61 // Find the maximum grade 62 int maximum( int grades[][ exams ], int pupils, int tests ) 63 { 64 int highGrade = 0; 65 66 for ( int i = 0; i < pupils; i++ ) The Hashemite University
14
The Hashemite University
67 for ( int j = 0; j < tests; j++ ) 69 if ( grades[ i ][ j ] > highGrade ) highGrade = grades[ i ][ j ]; 72 73 return highGrade; 74 } 75 76 // Determine the average grade for a particular student 77 double average( int setOfGrades[], int tests ) 78 { 79 int total = 0; 80 81 for ( int i = 0; i < tests; i++ ) total += setOfGrades[ i ]; 83 84 return static_cast< double >( total ) / tests; 85 } 86 87 // Print the array 88 void printArray( int grades[][ exams ], int pupils, int tests ) 89 { 90 cout << " [0] [1] [2] [3]"; 91 92 for ( int i = 0; i < pupils; i++ ) { cout << "\nstudentGrades[" << i << "] "; 94 for ( int j = 0; j < tests; j++ ) cout << setiosflags( ios::left ) << setw( 5 ) << grades[ i ][ j ]; 98 } 99 } 3. Define functions The Hashemite University
15
The Hashemite University
The array is: [0] [1] [2] [3] studentGrades[0] studentGrades[1] studentGrades[2] Lowest grade: 68 Highest grade: 96 The average grade for student 0 is 76.00 The average grade for student 1 is 87.50 The average grade for student 2 is 81.75 Program Output The Hashemite University
16
The Hashemite University
Additional Notes This lecture covers the following material from the textbook: Fourth Edition: Chapter 4: Sections 4.8 and 4.9 The Hashemite University
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.