Download presentation
Presentation is loading. Please wait.
1
1 Array, Pointer and Reference ( IV ) Ying Wu Electrical Engineering & Computer Science Northwestern University yingwu@ece.northwestern.edu EECS 230 Lectures Series
2
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
3 What to learn today? Array of pointers 2D array (or matrix)
4
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
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
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 1 2 3 4 1 0 3 4
7
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
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 } }; 29 30 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
9 2. Call functions minimum, maximum, and average 3. Define functions 34 << "\nHighest grade: " 35 << maximum( studentGrades, students, exams ) << '\n'; 36 37 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; 42 43 return 0; 44} 45 46// Find the minimum grade 47int minimum( int grades[][ exams ], int pupils, int tests ) 48{ 49 int lowGrade = 100; 50 51 for ( int i = 0; i < pupils; i++ ) 52 53 for ( int j = 0; j < tests; j++ ) 54 55 if ( grades[ i ][ j ] < lowGrade ) 56 lowGrade = grades[ i ][ j ]; 57 58 return lowGrade; 59} 60 61// Find the maximum grade 62int maximum( int grades[][ exams ], int pupils, int tests ) 63{ 64 int highGrade = 0; 65 66 for ( int i = 0; i < pupils; i++ )
10
10 67 68 for ( int j = 0; j < tests; j++ ) 69 70 if ( grades[ i ][ j ] > highGrade ) 71 highGrade = grades[ i ][ j ]; 72 73 return highGrade; 74} 75 76// Determine the average grade for a particular student 77double average( int setOfGrades[], int tests ) 78{ 79 int total = 0; 80 81 for ( int i = 0; i < tests; i++ ) 82 total += setOfGrades[ i ]; 83 84 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]"; 91 92 for ( int i = 0; i < pupils; i++ ) { 93 cout << "\nstudentGrades[" << i << "] "; 94 95 for ( int j = 0; j < tests; j++ ) 96 cout << setiosflags( ios::left ) << setw( 5 ) 97 << grades[ i ][ j ]; 98 } 99} 3. Define functions
11
11 Program Output The array is: [0] [1] [2] [3] studentGrades[0] 77 68 86 73 studentGrades[1] 96 87 89 78 studentGrades[2] 70 90 86 81 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
12
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
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
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
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
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
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
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; }
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.