Array pass by value Pass by Reference pass
โครงสรางขอมลแบบอารเรย (Array) การสงผานคาใหพารามเตอรของฟงก ชน การสงผานโดยคา ( pass by value) การสงผานคาโดยการอางอง Pass by Reference ) // pass by value // pass( by reference void my. Function( int ); main( ) { int size = 4 ; my. Function(size) ; cout << size ; } void my. Function( int { n = n + 10 ; } void my. Function( int&); main( ) { int size = 4 ; my. Function(size) ; cout << size ; } n) 25 June 2006 12. 00 AM void { my. Function( int& n) n = n + 10 ; } Week 03 a - ��������� Array ��� Struct 12
โครงสรางขอมลแบบอารเรยหลายมต ตวอยาง (Array) ( Multiple subscripts ) อารเรย 2 มต a[ i ][ j ] Tables with rows and columns Specify row, then column “Array of arrays” a[0] is an array of 4 elements a[0][0] is the first element of that array Row 0 Column 0 a[ 0 ] Column 1 a[ 0 ][ 1 ] Column 2 a[ 0 ][ 2 ] Column 3 a[ 0 ][ 3 ] Row 1 a[ 1 ][ 0 ] a[ 1 ][ 2 ] a[ 1 ][ 3 ] Row 2 a[ 2 ][ 0 ] a[ 2 ][ 1 ] a[ 2 ][ 3 ] Column subscript Array name Week 03 a - ��������� Array Row subscript 25 June 2006 12. 00 AM ��� Struct 18
โครงสรางขอมลแบบอารเรยหลายมต (Array) ( Multiple subscripts ) การกำหนดคาเบองตน (To initialize ) Default of 0 Initializers grouped by row in braces int b[ 2 ] = { { 1, 2 }, { 3, 4 } }; Row 0 Row 1 1 2 3 4 1 0 3 4 int b[ 2 ] = { { 1 }, { 3, 4 } }; 25 June 2006 12. 00 AM Week 03 a - ��������� Array ��� Struct 19
โครงสรางขอมลแบบอารเรยหลายมต (Array) ( Multiple subscripts ) การอางถงสมาชกของอารเรย cout << b[ 0 ][ 1 ]; 1 0 3 4 ผลลพธคอ 0 มกเขยนผดเปน cout << b[ 0, 1 ]; Syntax error!! Function prototypes Must specify sizes of subscripts First subscript not necessary, as with singlescripted arrays void print. Array( int [][ 3 ] ); 25 June 2006 12. 00 AM Week 03 a - ��������� Array ��� Struct 20
ขอมลแบบโครงสราง (Struct) การประกาศตวแปรชนดโครงสราง (Struct) ตวอยาง struct. Name { Type field. Name 1 ; Type field. Name 2 ; struct Student { string name; int age ; … float score ; }; struct. Name var. Name ; 25 June 2006 12. 00 AM Student s 1; Week 03 a - ��������� Array ��� Struct 30
ขอมลแบบโครงสราง การประกาศตวแปรชนดโครงสราง (Struct) การจำลองรปแบบของขอมลชนดโครงสรางบนหน วยความจำ #include <iostream> #include <string> using namespace std; struct Student { string name; int age; float score; }; main() { Student s 1; } 25 June 2006 12. 00 AM Student String name int age 4 bytes float score 4 bytes Week 03 a - ��������� Array ��� Struct 31
- Slides: 39