C++ Programming Lecture 16 Arrays – Part III The Hashemite University Computer Engineering Department (Adapted from the textbook slides)

Slides:



Advertisements
Similar presentations
Arrays. Introduction Arrays –Structures of related data items –Static entity - same size throughout program A few types –C-like, pointer-based arrays.
Advertisements

4.1Introduction Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based arrays (C-like) –Arrays.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Arrays Outline 4.1Introduction 4.2Arrays 4.3Declaring Arrays 4.4Examples Using Arrays 4.5Passing.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Arrays Outline 4.1Introduction 4.2Arrays 4.3Declaring Arrays 4.4Examples Using Arrays 4.5Passing.
 2003 Prentice Hall, Inc. All rights reserved Introduction Arrays –Structures of related data items –Static entity (same size throughout program)
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
1 Array, Pointer and Reference ( IV ) Ying Wu Electrical Engineering & Computer Science Northwestern University EECS 230 Lectures.
Review (Week1) C++_ the unit of programming is the class from which objects are eventually instantiated. C++ classes contain functions that implement class.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays 6.3Declaring Arrays 6.4Examples Using Arrays 6.5Passing.
 2003 Prentice Hall, Inc. All rights reserved. 1 Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based.
Chapter 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
 2003 Prentice Hall, Inc. All rights reserved Multiple-Subscripted Arrays Multiple subscripts –a[ i ][ j ] –Tables with rows and columns –Specify.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Arrays Dale Roberts, Lecturer
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Arrays Outline 4.1Introduction 4.2Arrays 4.3Declaring Arrays 4.4Examples Using Arrays 4.5Passing.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Programming Languages -1 (Introduction to C) arrays Instructor: M.Fatih AMASYALI
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
1 Lecture 5: Part 1 Searching Arrays Searching Arrays: Linear Search and Binary Search Search array for a key value Linear search  Compare each.
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 19 Thanks for Lecture Slides:
Chapter 6 Arrays Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Algorithm and Programming Array Dr. Ir. Riri Fitri Sari MM MSc International Class Electrical Engineering Dept University of Indonesia 15 March 2009.
 2000 Prentice Hall, Inc. All rights reserved Arrays Array –Group of consecutive memory locations –Same name and type To refer to an element, specify.
Arrays Array –Group of consecutive memory locations –Same name and type To refer to an element, specify –Array name –Position number Format: arrayname.
C Lecture Notes 1 Arrays Lecture 6. C Lecture Notes 2 6.1Introduction Arrays –Structures of related data items –Static entity – same size throughout program.
1 Arrays as Lists with examples. 2 Review Arrays –Structures of related data items of the same type –Data items (called array elements) are stored at.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Arrays Outline 4.1Introduction 4.2Arrays 4.3Declaring Arrays 4.4Examples Using Arrays 4.5Passing.
1 Lecture 8 Arrays Part II Sorting Arrays Sorting data  Important computing application  Virtually every organization must sort some data Massive.
1 Chapter 4 - Arrays Outline 4.1Introduction 4.2Arrays 4.3Declaring Arrays 4.4Examples Using Arrays 4.5Passing Arrays to Functions 4.6Sorting Arrays 4.7Case.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 4 - Arrays Outline 4.1Introduction 4.2Arrays 4.3Declaring Arrays 4.4Examples Using Arrays.
C++ Programming Lecture 11 Functions – Part III By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
 2003 Prentice Hall, Inc. All rights reserved. 1 Vectors.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
C++ Programming Lecture 17 Pointers – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays Outline Multidimensional Arrays Case Study: Computing Mean, Median and Mode Using Arrays.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Arrays Outline 4.1Introduction 4.2Arrays 4.3Declaring Arrays 4.4Examples Using Arrays 4.5Passing.
C Lecture Notes 1 Arrays (...cont.). C Lecture Notes 2 6.6Sorting Arrays Sorting data –Important computing application –Virtually every organization must.
 2003 Prentice Hall, Inc. All rights reserved. Outline 1 fig04_03.cpp (1 of 2) 1 // Fig. 4.3: fig04_03.cpp 2 // Initializing an array. 3 #include 4 5.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Arrays Outline 4.1Introduction 4.2Arrays 4.3Declaring Arrays 4.4Examples Using Arrays 4.5Passing.
CHAPTER 3 ARRAYS Dr. Shady Yehia Elmashad. Outline 1.Introduction 2.Arrays 3.Declaring Arrays 4.Examples Using Arrays 5.Multidimensional Arrays 6.Multidimensional.
EC-111 Algorithms & Computing Lecture #9 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
 2000 Prentice Hall, Inc. All rights reserved Arrays Array –Consecutive group of memory locations –Same name and type To refer to an element, specify.
CSC 113: C OMPUTER P ROGRAMMING (T HEORY = 03, L AB = 01) Computer Science Department Bahria University, Islamabad.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Chapter 4 - Arrays Outline 4.1 Introduction 4.2 Arrays
Arrays Outline 1 Introduction 2 Arrays 3 Declaring Arrays
C++ Programming Lecture 15 Arrays – Part II
آرايه ها اصول كامپيوتر 1.
C++ Programming Lecture 15 Arrays – Part II
Multidimensional Arrays
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
Arrays as Lists with examples.
Arrays Outline Introduction Arrays Declaring Arrays
CISC181 Introduction to Computer Science Dr
C++ Programming Lecture 16 Arrays – Part III
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
4.9 Multiple-Subscripted Arrays
Arrays Kingdom of Saudi Arabia
Chapter 4 - Arrays Outline 4.1 Introduction 4.2 Arrays
Chapter 4 - Arrays Outline 4.1 Introduction 4.2 Arrays
Chapter 4 - Arrays Outline 4.1 Introduction 4.2 Arrays
Capitolo 4 - Arrays Outline 4.1 Introduction 4.2 Arrays
Arrays Arrays A few types Structures of related data items
Chapter 3 Arrays Dr. A. PHILIP AROKIADOSS Assistant Professor
Dale Roberts, Lecturer IUPUI
Vectors.
4.9 Multiple-Subscripted Arrays
Presentation transcript:

C++ Programming Lecture 16 Arrays – Part III The Hashemite University Computer Engineering Department (Adapted from the textbook slides)

The Hashemite University2 Outline Introduction. Searching arrays: Linear search. Binary search. Multisubscripted arrays. Passing Multisubscripted arrays to functions. Examples.

The Hashemite University3 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 introduces multi- subscripted arrays which correspond to multidimensional matrices. Also, we will see how to define, initialize, and pass such arrays to functions.

The Hashemite University4 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 University5 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 2 n is the smallest number larger than the number of elements in the array. E.g.: 30 element array takes at most 5 steps 2 5 > 30

The Hashemite University6 Linear and Binary Search Code On board. Found in Figure 4.20 in the textbook.

The Hashemite University7 Multiple-Subscripted Arrays I Multiple subscripts - tables with rows, columns Like matrices: specify row, then column. Column subscript 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

The Hashemite University8 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 University9 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 } };

The Hashemite University10 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 University11 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 and const. Again, arrays are passed as call-by-reference by default.

The Hashemite University12 1// Fig. 4.23: fig04_23.cpp 2// Double-subscripted array example 3#include 4 5using std::cout; 6using std::endl; 7using std::ios; 8 9#include 10 11using std::setw; 12using std::setiosflags; 13using std::setprecision; 14 15const int students = 3; // number of students 16const int exams = 4; // number of exams 17 18int minimum( int [][ exams ], int, int ); 19int maximum( int [][ exams ], int, int ); 20double average( int [], int ); 21void printArray( int [][ exams ], int, int ); 22 23int main() 24{ 25 int studentGrades[ students ][ exams ] = 26 { { 77, 68, 86, 73 }, 27 { 96, 87, 89, 78 }, 28 { 70, 90, 86, 81 } }; cout << "The array is:\n"; 31 printArray( studentGrades, students, exams ); 32 cout << "\n\nLowest grade: " 33 << minimum( studentGrades, students, exams ) Each row is a particular student, each column is the grades on the exam.

The Hashemite University13 34 << "\nHighest grade: " 35 << maximum( studentGrades, students, exams ) << '\n'; for ( int person = 0; person < students; person++ ) 38 cout << "The average grade for student " << person << " is " 39 << setiosflags( ios::fixed | ios::showpoint ) 40 << setprecision( 2 ) 41 << average( studentGrades[ person ], exams ) << endl; return 0; 44} 45 46// Find the minimum grade 47int minimum( int grades[][ exams ], int pupils, int tests ) 48{ 49 int lowGrade = 100; for ( int i = 0; i < pupils; i++ ) for ( int j = 0; j < tests; j++ ) if ( grades[ i ][ j ] < lowGrade ) 56 lowGrade = grades[ i ][ j ]; return lowGrade; 59} 60 61// Find the maximum grade 62int maximum( int grades[][ exams ], int pupils, int tests ) 63{ 64 int highGrade = 0; for ( int i = 0; i < pupils; i++ )

The Hashemite University14 3. Define functions for ( int j = 0; j < tests; j++ ) if ( grades[ i ][ j ] > highGrade ) 71 highGrade = grades[ i ][ j ]; return highGrade; 74} 75 76// Determine the average grade for a particular student 77double average( int setOfGrades[], int tests ) 78{ 79 int total = 0; for ( int i = 0; i < tests; i++ ) 82 total += setOfGrades[ i ]; return static_cast ( total ) / tests; 85} 86 87// Print the array 88void printArray( int grades[][ exams ], int pupils, int tests ) 89{ 90 cout << " [0] [1] [2] [3]"; for ( int i = 0; i < pupils; i++ ) { 93 cout << "\nstudentGrades[" << i << "] "; for ( int j = 0; j < tests; j++ ) 96 cout << setiosflags( ios::left ) << setw( 5 ) 97 << grades[ i ][ j ]; 98 } 99}

The Hashemite University15 Program Output 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 The average grade for student 1 is The average grade for student 2 is 81.75

The Hashemite University16 Additional Notes This lecture covers the following material from the textbook: Fourth Edition: Chapter 4: Sections 4.8 and 4.9