ImageVerifierCode 换一换
你正在下载:

Arrays.ppt

[预览]
格式:PPT , 页数:33 ,大小:262KB ,
资源ID:378575      下载积分:2000 积分
快捷下载
登录下载
邮箱/手机:
温馨提示:
如需开发票,请勿充值!快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。
如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝扫码支付 微信扫码支付   
注意:如需开发票,请勿充值!
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【http://www.mydoc123.com/d-378575.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(Arrays.ppt)为本站会员(syndromehi216)主动上传,麦多课文库仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知麦多课文库(发送邮件至master@mydoc123.com或直接QQ联系客服),我们立即给予删除!

Arrays.ppt

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

10、raysize could be simply int,Program Output,Effects of passing entire array call-by-reference:The values of the original array are:0 1 2 3 4 The values of the modified array are:0 2 4 6 8Effects of passing array element call-by-value:The value of a3 is 6 Value in modifyElement is 12 The value of a3 i

11、s 6,Sorting Arrays,Sorting data Important computing application Virtually every organization must sort some data Massive amounts must be sorted Bubble sort (sinking sort) Several passes through the array Successive pairs of elements are compared If increasing order (or identical), no change If decre

12、asing order, elements exchanged Repeat these steps for every element,Sorting Arrays,Example: Original: 3 4 2 6 7 Pass 1: 3 2 4 6 7 Pass 2: 2 3 4 6 7 Small elements “bubble“ to the top,Computing Mean, Median and Mode Using Arrays,Mean Average Median Number in middle of sorted list 1, 2, 3, 4, 5 (3 is

13、 median) Mode Number that occurs most often 1, 1, 1, 2, 3, 3, 4, 5 (1 is mode),3.3 Define bubbleSort,4. Program Output,*Mean * The mean is the average value of the data items. The mean is equal to the total of all the data items divided by the number of data items (99). The mean value for this run i

14、s: 681 / 99 = 6.8788*Median * The unsorted array of responses is6 7 8 9 8 7 8 9 8 9 7 8 9 5 9 8 7 8 7 86 7 8 9 3 9 8 7 8 7 7 8 9 8 9 8 9 7 8 96 7 8 7 8 7 9 8 9 2 7 8 9 8 9 8 9 7 5 35 6 7 2 5 3 9 4 6 4 7 8 9 6 8 7 8 9 7 87 4 4 2 5 3 8 7 5 6 4 5 6 1 6 5 7 8 7The sorted array is1 2 2 2 3 3 3 3 4 4 4 4

15、4 5 5 5 5 5 5 55 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 77 7 7 7 7 7 7 7 7 7 7 7 7 8 8 8 8 8 8 88 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 89 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9The median is element 49 of the sorted 99 element array. For this run the median is 7,Program Output,*Mode * Response Frequency His

16、togram1 1 2 25 0 5 0 51 1 *2 3 *3 4 *4 5 *5 8 *6 9 *7 23 *8 27 *9 19 * The mode is the most frequent value. For this run the mode is 8 which occurred 27 times.,Searching Arrays: Linear Search and Binary Search,Search array for a key value Linear search Compare each element of array with key value Us

17、eful for small and unsorted arrays Binary search Can only be used on sorted arrays Compares middle element with key If equal, match found If key middle, repeat search through the last half of the array Very fast; at most n steps, where 2n # of elements 30 element array takes at most 5 steps 2 30,n,5

18、,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 ;,Row subscript,Array name,Column subscript,Multiple-Subscripted Arrays,Referenced like no

19、rmal cout b 0 1 ; Will output the value of 0 Cannot reference with commas cout b( 0, 1 ); Will try to call function b, causing a syntax error,3. Define functions,Program Output,The array is:0 1 2 3 studentGrades0 77 68 86 73 studentGrades1 96 87 89 78 studentGrades2 70 90 86 81Lowest 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,

copyright@ 2008-2019 麦多课文库(www.mydoc123.com)网站版权所有
备案/许可证编号:苏ICP备17064731号-1