Arrays.ppt
《Arrays.ppt》由会员分享,可在线阅读,更多相关《Arrays.ppt(33页珍藏版)》请在麦多课文档分享上搜索。
1、Arrays,Introduction,Arrays Structures of related data items Static entity - same size throughout program A few types C-like, pointer-based arrays C+, arrays as objects,Arrays,Array Consecutive group of memory locations Same name and type To refer to an element, specify Array name and position number
2、 Format: arrayname position number First element at position 0 n element array c: c 0 , c 1 c n - 1 Array elements are like normal variables c 0 = 3; cout c 0 ; Performing operations in subscript. If x = 3, c 5 2 = c 3 = c x ,Arrays,c6,-45,6,0,72,1543,-89,0,62,-3,1,6453,78,Name of array (Note that a
3、ll elements of this array have the same name, c),c0,c1,c2,c3,c11,c10,c9,c8,c7,c5,c4,Position number of the element within array c,Declaring Arrays,Declaring arrays - specify: Name Type of array Number of elements Examplesint c 10 ; float hi 3284 ; Declaring multiple arrays of same type Similar forma
4、t as other variables Example int b 100 , x 27 ;,Examples Using Arrays,Initializers int n 5 = 1, 2, 3, 4, 5 ; If not enough initializers, rightmost elements become 0 If too many initializers, a syntax error is generated int n 5 = 0 Sets all the elements to 0 If size omitted, the initializers determin
5、e it int n = 1, 2, 3, 4, 5 ; 5 initializers, therefore n is a 5 element array,Element Value0 321 272 643 184 955 146 907 708 609 37,Fig04_07.cpp: Error E2304 Fig04_07.cpp 6: Constant variable x must be initialized in function main() Error E2024 Fig04_07.cpp 8: Cannot modify a const object in functio
6、n main() * 2 errors in Compile *,Examples Using Arrays,Strings Arrays of characters All strings end with null (0) Examples: char string1 = “hello“; char string1 = h, e, l, l, o, 0 ; Subscripting is the same as for a normal array String1 0 is h string1 2 is l Input from keyboardchar string2 10 ;cin s
7、tring2; Takes user input Side effect: if too much text entered, data written beyond array,Enter a string: Hello there string1 is: Hello string2 is: string literal string1 with spaces between characters is: H e l l o string1 is: there,Passing Arrays to Functions,Specify the name without any brackets
8、To pass array myArray declared as int myArray 24 ; to function myFunction, a function call would resemble myFunction( myArray, 24 ); Array size is usually passed to function Arrays passed call-by-reference Value of name of array is address of the first element Function knows where the array is store
9、d Modifies original memory locations Individual array elements passed by call-by-value pass subscripted name (i.e., myArray 3 ) to function,Passing Arrays to Functions,Function prototype: void modifyArray( int b, int arraySize ); Parameter names optional in prototype int b could be simply int int ar
- 1.请仔细阅读文档,确保文档完整性,对于不预览、不比对内容而直接下载带来的问题本站不予受理。
- 2.下载的文档,不会出现我们的网址水印。
- 3、该文档所得收入(下载+内容+预览)归上传者、原创作者;如果您是本文档原作者,请点此认领!既往收益都归您。
下载文档到电脑,查找使用更方便
2000 积分 0人已下载
下载 | 加入VIP,交流精品资源 |
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- ARRAYSPPT
