C语言英文课件6:数组
合集下载
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
ex. int a[8]; a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7]
a
In C, each array has two fundamental properties:
•The element type •The array size
6.1 DECLARATION AND CITING OF THE
6.0 Foreward
1. Question:
Input all students’ score,compute:
1.average;
2.the difference between every one score and average
2. Array
An array is a collectionof individual data values with two distinguishing characteristics:
2.explanation: (1)assignment all elements with initial value
ex. int a[5]={3,5,6,8,4}; or int a[ ]={3,5,6,8,4}; (2) assignment part elements with initial value ex. int a[8]={3,5,6,8,4};
An array is ordered
An array is homogeneous(类似的)
a1, a2, a3, ... a50
3. Relation between variables
simpleness variable:no connection
ai
between variables
variable suffix variable:variables are
y1 3 5 7 9
y[i]=2*i+1;
}
Output result:
for(i=0;i<5;d%2d\n",x[i ],y[i]); 2 3
for(i=4;i>=0;i--)
45
printf("%d%d",y[i],x[i]);
67
printf("\n"); }
89 9876543210
SIMPLE ARRAYS
6.1.1 DECLARATION
Arrays, like other variables in C, must be declared before they are used.
1. DECLARATION FORM
datatype name of array[constant expression]; 2. STORAGE FORM
ex.int a[8]; a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7]
a
Each element in the array is identified by a numeric value called its index.In C,the index numbers for an array always begin with zero and run uo to the array size minus one. Thus,in an array with 8 elements, the index numbers are 0,1,2,3,4,5,6 and 7.
【exercise】Reading the program,write out the result
main() { int x[5],y[5],i;
x[0] x[1] x[2] x[3] x[4]
x0 2 4 6 8
for(i=0;i<5;i++) { x[i]=2*i;
y[0] y[1] y[2] y[3] y[4]
6.1.2 CITE ELEMENTS OF ARRAY
Arrays have the following syntax, using square brackets to access each indexed value (called an element).
Format: x[i]
In C, indexed value starts with 0. Assigning values to array elements is done by,
equipollence int a[8]={3,5,6,8,4,0,0,0};
6.1.4 APPLYING EXAMPLES
【ex. 6.1】Compute average and give the level with letter #define N 60 main() { int score[N], i;
a: a[1],a[2],a[3],...a[50]
connected by suffix
a[i]
It is best to think of an array as a sequence of boxes, one box for each data value in the array.Each of the valuesin an array is called an element.
Chapter 6 ARRAYS(数组)
6.0 FORWARD 6.1 DECLARATIONS AND CITING OF ONE DIMENSIONAL ARRAYS 6.2 DECLARATIONS AND CITING OF TWO DIMENSIONAL ARRAYS 6.3 CHARACTER ARRAYS AND STRINGS
x[10] = g;
and assigning array elements to a variable is done by,
g = x[10];
[Return]
6.1.3 INITIALIZATIONS OF THE SIMPLE ARRAYS
1. Initialized format:
Data type name of array[constant expression]= {list of initial value}
a
In C, each array has two fundamental properties:
•The element type •The array size
6.1 DECLARATION AND CITING OF THE
6.0 Foreward
1. Question:
Input all students’ score,compute:
1.average;
2.the difference between every one score and average
2. Array
An array is a collectionof individual data values with two distinguishing characteristics:
2.explanation: (1)assignment all elements with initial value
ex. int a[5]={3,5,6,8,4}; or int a[ ]={3,5,6,8,4}; (2) assignment part elements with initial value ex. int a[8]={3,5,6,8,4};
An array is ordered
An array is homogeneous(类似的)
a1, a2, a3, ... a50
3. Relation between variables
simpleness variable:no connection
ai
between variables
variable suffix variable:variables are
y1 3 5 7 9
y[i]=2*i+1;
}
Output result:
for(i=0;i<5;d%2d\n",x[i ],y[i]); 2 3
for(i=4;i>=0;i--)
45
printf("%d%d",y[i],x[i]);
67
printf("\n"); }
89 9876543210
SIMPLE ARRAYS
6.1.1 DECLARATION
Arrays, like other variables in C, must be declared before they are used.
1. DECLARATION FORM
datatype name of array[constant expression]; 2. STORAGE FORM
ex.int a[8]; a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7]
a
Each element in the array is identified by a numeric value called its index.In C,the index numbers for an array always begin with zero and run uo to the array size minus one. Thus,in an array with 8 elements, the index numbers are 0,1,2,3,4,5,6 and 7.
【exercise】Reading the program,write out the result
main() { int x[5],y[5],i;
x[0] x[1] x[2] x[3] x[4]
x0 2 4 6 8
for(i=0;i<5;i++) { x[i]=2*i;
y[0] y[1] y[2] y[3] y[4]
6.1.2 CITE ELEMENTS OF ARRAY
Arrays have the following syntax, using square brackets to access each indexed value (called an element).
Format: x[i]
In C, indexed value starts with 0. Assigning values to array elements is done by,
equipollence int a[8]={3,5,6,8,4,0,0,0};
6.1.4 APPLYING EXAMPLES
【ex. 6.1】Compute average and give the level with letter #define N 60 main() { int score[N], i;
a: a[1],a[2],a[3],...a[50]
connected by suffix
a[i]
It is best to think of an array as a sequence of boxes, one box for each data value in the array.Each of the valuesin an array is called an element.
Chapter 6 ARRAYS(数组)
6.0 FORWARD 6.1 DECLARATIONS AND CITING OF ONE DIMENSIONAL ARRAYS 6.2 DECLARATIONS AND CITING OF TWO DIMENSIONAL ARRAYS 6.3 CHARACTER ARRAYS AND STRINGS
x[10] = g;
and assigning array elements to a variable is done by,
g = x[10];
[Return]
6.1.3 INITIALIZATIONS OF THE SIMPLE ARRAYS
1. Initialized format:
Data type name of array[constant expression]= {list of initial value}