上海交通大学交大密西根 ·联合学院一 181 UM-SJTU Joint Institute ■ University of Michigan Shanghal Jiao Tong University Vg101:Introduction to Computer and Programming Array and Array Operation
Array and Array Operation Vg101 :Introduction to Introduction to Computer and Programming Computer and Programming
上海交通大学交大密西根 ·联合学院一 : 181 UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University Introduction Today we will start on a major new topic in C++: arrays. Arrays An array is an ordered collection of data values of the same type. Ordered: We can refer to the elements by their position Ist,2nd,... Same type: All ints,all doubles,all strings
Introduction Introduction • Today we will start on a major new topic in C++: arrays
上海交通大学交大密西根 联合学院·一 ◆ 181 UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University Array Definition To declare an array,we specify that type and name,just like any variable,and also the size,which means how many elements the array will have.A square-bracket notation is used to specify the size of an array. Here is how Arrays arrays look in Arrays must be declared,and as with any variable,declaring a stack frame an array sets aside the memory for it. diagram. main main() letters char letters[4]; □ int values[3]; values
Array Definition Array Definition • To declare an array, we specify that type and name, just like any variable, and also the size, which means how many elements the array will have. A square-bracket notation is used to specify the size of an array. • Here is how arrays look in a stack frame diagram
上海交通大学交大密西根 ·联合学院一 181 UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University Array in Memory Space Here is how they look if we show memory as a linear sequence of addresses.The letters array would occupy four bytes of memory;values would occupy 12 since an int requires four bytes. Arrays Arrays must be declared,and as with any variable,declaring an array sets aside the memory for it. 100 letters main() char letters[4]; 104 values int values[3]; }
Array in Memory Space Array in Memory Space • Here is how they look if we show memory as a linear sequence of addresses. The letters array would occupy four bytes of memory; values would occupy 12 since an int requires four bytes
上海交通大学交大密西根 ·联合学院一 181 UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University Element Numbering As with most things in C++,array elements are numbered from zero. Some other languages let the programmer decide what the first index will be,but it is always 0 in C++. Arrays Array elements are numbered from 0. main main() f letters char letters[4]; D int values[3]; 0123 。”。 values 0
Element Numbering Element Numbering • As with most things in C++, array elements are numbered from zero. • Some other languages let the programmer decide what the first index will be, but it is always 0 in C++