Arrays.ppt

上传人:syndromehi216 文档编号:378575 上传时间:2018-10-09 格式:PPT 页数:33 大小:262KB
下载 相关 举报
Arrays.ppt_第1页
第1页 / 共33页
Arrays.ppt_第2页
第2页 / 共33页
Arrays.ppt_第3页
第3页 / 共33页
Arrays.ppt_第4页
第4页 / 共33页
Arrays.ppt_第5页
第5页 / 共33页
亲,该文档总共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

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