C语言学习第六章(英文版)课件

上传人:我*** 文档编号:147864201 上传时间:2020-10-14 格式:PPT 页数:81 大小:962KB
返回 下载 相关 举报
C语言学习第六章(英文版)课件_第1页
第1页 / 共81页
C语言学习第六章(英文版)课件_第2页
第2页 / 共81页
C语言学习第六章(英文版)课件_第3页
第3页 / 共81页
C语言学习第六章(英文版)课件_第4页
第4页 / 共81页
C语言学习第六章(英文版)课件_第5页
第5页 / 共81页
点击查看更多>>
资源描述

《C语言学习第六章(英文版)课件》由会员分享,可在线阅读,更多相关《C语言学习第六章(英文版)课件(81页珍藏版)》请在金锄头文库上搜索。

1、Chapter 6 Arrays,6.1What are Arrays 6.2One Dimensional Array 6.3Declaring Array 6.4Array Initialisation and Subscripting 6.5Examples Using One Dimensional Array 6.6Passing 1-D Array to a Function 6.7Two Dimensional Array 6.8Initialising a 2-D Array 6.9Examples Using Two Dimensional Array 6.10 Passin

2、g 2-D Array to a Function,Outline,One Dimensional Array Declaring Array Array Initialisation and Subscripting Examples Using One Dimensional Array Passing 1-D Array to a Function Two Dimensional Array Initialising a 2-D Array Examples Using Two Dimensional Array Passing 2-D Array to a Function,Key P

3、oints,#include void main() int mark1,mark2,mark3,mark4,mark5; int mark6,mark7,mark8,mark9,mark10; int sum; float aver; printf(“please input ten marks”): scanf(“%d, %d, %d, %d, %d”, ,Think about this question:,Input those marks obtained by 10 students in a test, write a program to find the average an

4、d total marks.,Is that troublesome? How about 50 students?,Any better way to solve it ?,6.1 What are Arrays,编程用数组实现上例。 编程统计6位候选人的选票。 编程用数组记录50个随机数,并输出。,6.1 What are Arrays,Arrays Structures of related data items Static entity same size throughout program Dynamic data structures discussed in other ch

5、apter,6.1 What are Arrays,The C language provides a capability that enables the user to design a set of similar data types, called array. Ordinary variables can only hold one value at a time, however, an array can store more than one value at a time. An array is a collective name given to a group of

6、 similar quantities.,6.1 What are Arrays,An array is a collection of similar elements. These similar elements could be all ints, or floats, or chars etc. For example, assume the following group of numbers represent marks obtained by five students. per = 48, 88, 34, 23, 96 If we refer to the second n

7、umber of the group, the usual notation used is per2. In C, the second number is referred as per1.,The counting of elements begin with 0, not 1,6.2 One Dimensional Array,Array Group of consecutive memory locations A block of many variables of the same type、 Same name can be declared for any type Exam

8、ples list of students marks series of numbers entered by user Vectors matrices To refer to an element, specify Array name Position number Format: arrayname position number First element at position 0 n element array named c: c 0 , c 1 .c n 1 ,6.2 One Dimensional Array,Array elements are like normal

9、variables c 0 = 3; printf( %d, c 0 ); Perform operations in subscript. If x equals 3 c 5 - 2 = c 3 = c x ,6.3 Declaring array,When declaring arrays, specify Name Type of array Number of elements arrayType arrayName numberOfElements ; Examples: int c 10 ; float myArray 3284 ; Declaring multiple array

10、s of same type Format similar to regular variables Example: int b 100 , x 27 ;,6.4 Array Initialisation and Subscripting,Array Initialisation int n 5 = 1, 2, 3, 4, 5 ; If not enough initializers, rightmost elements become 0 int n 5 = 0 If too many, a syntax error is produced C arrays have no bounds

11、checking If size omitted, initializers determine it int n = 1, 2, 3, 4, 5 ; 5 initializers, therefore 5 element array,6.4 Array Initialisation and Subscripting,If a is an array, we can write aexpr, where expr is an integral expression, to access an element of the array. We call expr a subscript, or

12、index of a. The expression aican be made to refer to any element of the array by assignment of an appropriate value to the subscript i. eg. ai+j, ai-j, ai+2,6.4 Array Initialisation and Subscripting,#include void main() int n 5 = 1, 2, 3, 4, 5 ; int m5,i; for(i=0;i4;i+) mi = ni; for(i=0;i5/2;i+) n4-

13、i = ni; for(i=0;i4;i+) printf(“%d %dn”,mi,ni); ,refer to the element of array m by mi,refer to the element of array n by ni,n4-i,6.4 Array Initialisation and Subscripting,A single array element ai is accessed when i has a value greater than or equal to 0 and less than or equal to N 1. If i has a val

14、ue outside this range, a run-time error will occur when ai is accessed. Overrunning the bounds of an array is a common programming error.,6.4 Array Initialisation and Subscripting,In c, there is no check to see if the subscript used for an array exceeds the size of the array. Example for bounds chec

15、king (on textbook Page 176) #include void main() int num6,i; for(i=0;i6;i+) numi=i; for(i=0;i10;i+) printf(“%d ”,numi); ,What is the output ?,6.5 Examples Using One Dimensional Array,Entering data into an array Int marks30; for (i=0;i=29;i+) printf(“n enter marks”); scanf(“%d”, ,Example 1: write a p

16、rogram to find average mark of 30 students in a test. On Textbook P174. #include void main() int avg,sum=0; int i; int marks30; for(i=0;i=29;i+) printf(“nEnter marks”); scanf(“%d”, ,Example 2: check the output of the program . #include #define SIZE 10 int main() int n SIZE = 19, 3, 15, 7, 11, 9, 13, 5, 17, 1 ; int i, j; printf( %s%13s%17sn, Element, Value, Histogram ); for ( i = 0; i = SIZE - 1; i+ ) printf( %7d%13d , i, n i ) ; for ( j = 1; j = n i ; j+ ) /*

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 办公文档 > PPT模板库 > PPT素材/模板

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号