1 Array, Pointer and Reference ( IV ) Ying Wu Electrical Engineering & Computer Science Northwestern University EECS 230 Lectures.

Slides:



Advertisements
Similar presentations
Introduction to Programming Lecture 34. In Today’s Lecture Arrays of objects Arrays of objects Interaction of Arrays with Free Store Interaction of Arrays.
Advertisements

Pointers and Strings. Introduction Pointers –Powerful, but difficult to master –Simulate call-by-reference –Close relationship with arrays and strings.
Arrays. Introduction Arrays –Structures of related data items –Static entity - same size throughout program A few types –C-like, pointer-based arrays.
4.1Introduction Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based arrays (C-like) –Arrays.
1 Array, Pointer and Reference ( III ) Ying Wu Electrical Engineering & Computer Science Northwestern University ECE230 Lectures.
 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)
Review (Week1) C++_ the unit of programming is the class from which objects are eventually instantiated. C++ classes contain functions that implement class.
Array, Pointer and Reference ( V ) Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series.
1 Dynamic Memory Allocation Ying Wu Electrical Engineering & Computer Science Northwestern University EECS 230 Lectures Series.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays 6.3Declaring Arrays 6.4Examples Using Arrays 6.5Passing.
1 Array, Pointer and Reference ( I ) Ying Wu Electrical Engineering and Computer Science Northwestern University EECS 230 Lectures.
1 11/8/06CS150 Introduction to Computer Science 1 Arrays Chapter 8 page 477 November 13, 2006.
 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.
Multiple-Subscripted Array
 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.
Chapter 7 Arrays C++ Programming, Namiq Sultan1 Namiq Sultan University of Duhok Department of Electrical and Computer Engineering Reference: Starting.
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.
 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 C++ Classes (I) Ying Wu Electrical Engineering & Computer Science Northwestern University ECE230 Lectures Series.
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.
 2000 Deitel & Associates, Inc. All rights reserved Introduction Pointers –Powerful, but difficult to master –Simulate call-by-reference –Close.
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.
1 C++ Classes: Access (II) Ying Wu Electrical Engineering & Computer Science Northwestern University ECE230 Lectures Series.
 2003 Prentice Hall, Inc. All rights reserved. 1 Vectors.
1 Lecture 12 Pointers and Strings Section 5.4, ,
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays Outline Multidimensional Arrays Case Study: Computing Mean, Median and Mode Using 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.
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.
Lecture 14: Arrays Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
1 Lecture 8 Pointers and Strings: Part 2 Section 5.4, ,
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
C++ Programming Lecture 16 Arrays – Part III 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.
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
CSC113: Computer Programming (Theory = 03, Lab = 01)
آرايه ها اصول كامپيوتر 1.
C++ Programming Lecture 15 Arrays – Part II
Multidimensional 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:

1 Array, Pointer and Reference ( IV ) Ying Wu Electrical Engineering & Computer Science Northwestern University EECS 230 Lectures Series

2 We’ve got a lot of stuff! Regular variables and references Pointers Array Function Any combination of these leads to a new stuff! –Pointer + function  passing pointers to a function –Ref. + function  passing references to a function –Array + function  passing an array to a function –Pointer + array  ?? –Array + array  ?? –Pointer + pointer  ??

3 What to learn today? Array of pointers 2D array (or matrix)

4 Arrays of Pointers Arrays can contain pointers –Commonly used to store an array of strings char *suit[ 4 ] = {"Hearts", "Diamonds", "Clubs", "Spades" }; –Each element of suit is a pointer to a char * (a string) –The strings are not in the array, only pointers to the strings are in the array –suit array has a fixed size, but strings can be of any size suit[3] suit[2] suit[1] suit[0]’H’’e’’a’’r’’t’’s’ ’\0’ ’D’’i’’a’’m’’o’’n’’d’’s’ ’\0’ ’C’’l’’u’’b’’s’ ’\0’ ’S’’p’’a’’d’’e’’s’ ’\0’

5 #include using namespace std; void main() { char *month[12] = {“January”, “February”, “March”, “April”, “May”, “June”, “July”, “August”, “September”, “October”, “November”, “December”}; int m; cout << “Please type a number....”; cin >> m; if (m>=1 && m<=12){ cout << “this number “ << m << “refers to “ << month[m-1] << endl; } else{ cout << “out of range!\n”; }

6 Multiple-Subscripted Arrays 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 } }; How to reference an element –b[row][col], e.g., b[0][1] 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

7 Multiple-Subscripted Arrays How is a 2D array stored in memory? –int a[4][3]; –Row-first order –a is an array of pointers, *a[4], –**a = ? a + 1 = ? –a[0] + 1 = ? –*(a[0] + 1) = ? –Initialization int b[2][2] = {(1,2},{3,4}}; int b[2][2] = {1,2,3,4}; Int b[2][2] = {0}; How to pass a 2-D array to a function? –int myfun(int a[][3], int row, int col); –Why? a[0][0] a[0][1] a[0][2] a[1][0] a[2][0] a[1][2] a[1][1] a[2][2] a[2][1] a[3][2] a[3][1] a[3][0] a[0] a[1] a[2] a[3] a

8 1. Initialize variables 1.1 Define functions to take double scripted arrays 1.2 Initialize studentgrades[][] 2. Call functions minimum, maximum, and average 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.

9 2. Call functions minimum, maximum, and average 3. Define functions 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++ )

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} 3. Define functions

11 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

12 Name Database Task: to create a name database, in which each entry is a name of a variable, e.g., “var_1”, “a”, … How to search an entry given a name?

13 A Toy Example bool Search(const char*, int&); char *g_nameDB[4]; void main() { g_nameDB[0] = “a”; g_nameDB[1] = “var_1”; g_nameDB[2] = “xyz”; g_nameDB[4] = “temp”; char query[100]; cin.getline(query,100); int ind; if(Search(query, ind)){ cout << “Found “ << g_nameDB[ind]); else cout << “was not found!” << endl; } bool Search(const char* q, int& index) { bool isFound = false; for(int k=0;k<4;k++){ if( !strcmp(g_nameDB[k], q)){ index = k; isFound = true; break; } return isFound; }

14 Question to think about? To create a Variable DB –To hold the names/values of variables –Given the name of a variable, search the DB to find the corresponding value?

15 Real stuff: VarDB #defineSIZE_DB 100 double gVarDB_Value[SIZE_DB]; char* gVarDB_Name[SIZE_DB]; int gVarDB_Size;// the size of the DB // Initialize the system variable DB void VarDB_Init(); // set name to one in the DB bool VarDB_SetVarName(const int& index, const char* name); // search variable according to its name bool VarDB_Search(const char* name, int& index); // create a new varible given a name bool VarDB_CreateANewVar(const char*name, int& index); // dump the DB void VarDB_Dump();

16 void VarDB_Init() { // the first one is reserved for "ans" gVarDB_Size = 1; for(int i=0; i<SIZE_DB; i++){ gVarDB_Value[i] = 0.0; gVarDB_Name[i] = NULL; } VarDB_SetVarName(0,"ans"); } bool VarDB_SetVarName(const int& index, const char* name) { assert(index < gVarDB_Size); boolcode = true; if(gVarDB_Name[index]!=NULL){ if(strlen(gVarDB_Name[index])!=0) { delete [] gVarDB_Name[index]; } gVarDB_Name[index] = new char [strlen(name) + 1]; if(gVarDB_Name[index]){ strcpy(gVarDB_Name[index], name); } elsecode = false; return code; }

17 bool VarDB_Search(const char* name, int& index) { boolcode = false; for(int i=0; i<gVarDB_Size; i++){ if(!strcmp(gVarDB_Name[i], name)){ index = i; code = true; break; } return code; } bool VarDB_CreateANewVar(const char*name, int& index) { boolcode = false; if(gVarDB_Size < SIZE_DB){ gVarDB_Size ++; VarDB_SetVarName(gVarDB_Size-1,name); gVarDB_Value[gVarDB_Size-1] = 0.0; index = gVarDB_Size -1; code = true; } return code; }

18 void VarDB_Dump() { cout.setf(ios::left, ios::adjustfield); for(int i=0; i<gVarDB_Size; i++){ cout << " " << setw(20) << gVarDB_Name[i] << setw(15) << gVarDB_Value[i] << endl; }