欢迎来到麦多课文档分享! | 帮助中心 海量文档,免费浏览,给你所需,享你所想!
麦多课文档分享
全部分类
  • 标准规范>
  • 教学课件>
  • 考试资料>
  • 办公文档>
  • 学术论文>
  • 行业资料>
  • 易语言源码>
  • ImageVerifierCode 换一换
    首页 麦多课文档分享 > 资源分类 > PPT文档下载
    分享到微信 分享到微博 分享到QQ空间

    Arrays.ppt

    • 资源ID:378575       资源大小:262KB        全文页数:33页
    • 资源格式: PPT        下载积分:2000积分
    快捷下载 游客一键下载
    账号登录下载
    微信登录下载
    二维码
    微信扫一扫登录
    下载资源需要2000积分(如需开发票,请勿充值!)
    邮箱/手机:
    温馨提示:
    如需开发票,请勿充值!快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。
    如需开发票,请勿充值!如填写123,账号就是123,密码也是123。
    支付方式: 支付宝扫码支付    微信扫码支付   
    验证码:   换一换

    加入VIP,交流精品资源
     
    账号:
    密码:
    验证码:   换一换
      忘记密码?
        
    友情提示
    2、PDF文件下载后,可能会被浏览器默认打开,此种情况可以点击浏览器菜单,保存网页到桌面,就可以正常下载了。
    3、本站不支持迅雷下载,请使用电脑自带的IE浏览器,或者360浏览器、谷歌浏览器下载即可。
    4、本站资源下载后的文档和图纸-无水印,预览文档经过压缩,下载后原文更清晰。
    5、试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。

    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,


    注意事项

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




    关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

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

    收起
    展开