当前课程知识点:C Programming > Chapter 6 Batch Data Processing with Array > 6.1 Definition, Reference and Initialization of One-Dimensional Arrays > 6.1 Definition, Reference of One-Dimensional Arrays.mp4
大家好
我是云南大学信息学院丁海燕老师
欢迎走进C语言程序设计课堂
今天
我们讲解一维数组的定义
引用和初始化
前几章使用的变量都属于基本类型
例如整型
字符型
浮点型数据
这些都是简单的数据类型
对于有些数据
只用简单的数据类型是不够的
难以反映出数据的特点
也难以有效地进行处理
如果有1000名学生
每个学生有一个成绩
需要求这1000名学生的平均成绩
用s1,s2,s3
一直到s1000
表示每个学生的成绩
能体现内在联系
C语言用方括号中的数字表示下标
如用s[15]来表示
s为数组名
数组是一组有序数据的集合
数组中
各数据的排列是有一定规律的
下标代表数据在数组中的序号
用一个数组名和下标
唯一确定数组中的元素
数组中的每一个元素
都属于同一个数据类型
将数组与循环结合起来
可以有效地处理大批量的数据
大大提高了工作效率
十分方便
要使用数组
必须在程序中先定义数组
即通知计算机
由哪些数据组成数组
数组中有哪些元素
属于哪个数据类型
否则
计算机不会自动地把一批数据
作为数组处理
那么
怎样定义一维数组呢
一维数组是数组中最简单的
它的元素只需要用数组名
加一个下标
就能唯一确定
要使用数组
必须在程序中先定义数组
例如
以下是对数组的定义
int a[10];
定义一维数组的一般形式为
类型符 数组名[常量表达式];
数组名的命名规则
和变量名命名规则相同
遵循C语言标识符命名规则
即
必须以字母或下划线开头
其后的字符可以是字母
下划线或数字
在定义数组时
需要指定数组中元素的个数
方括号中的常量表达式
用来表示元素的个数
即数组长度
例如
指定如 a[10]
表示a数组有10个元素
类型符
表示每个数组元素的数据类型
例如
int a[10];
表示定义了一个整型数组
数组名为a
此数组有10个元素
a[0],a[1],a[2],…,a[9]
且每个数组元素
都是int类型
可以看到
用一个int a[10];
就相当于
定义了10个简单的整型变量
显然简捷方便
注意
下标是从0开始的
不存在数组元素a[10]
常量表达式中
可以包含常量和符号常量
如
int a[3+5]是合法的
但不能包含变量
如int a[n];是不合法的
也就是说
C语言不允许对数组的大小
作动态定义
即
数组的大小
不依赖于程序运行过程中
变量的值
例如
以下数组的定义不合法
int n=10;
int a[n];
定义完数组后
如何引用数组中的元素呢?
在定义数组并对其中各元素赋值后
就可以引用数组中的元素
注意
只能引用数组元素
而不能一次整体调用
整个数组全部元素的值
引用数组元素的表示形式为
数组名[下标]
例如
a[0]就是数组a中序号为0的元素
它和一个简单变量的地位和作用相似
下标
可以是整型常量或整型表达式
下面的赋值表达式
包含了对数组元素的引用
a[0]=a[5]+a[7]-a[2*3] 合法
int n=5,a[10];
a[n]=20也是合法
下面来看例6.1
对10个数组元素依次赋值为
0,1,2,3,4,5,6,7,8,9
要求按逆序输出
解题思路为
定义一个长度为10的数组
数组定义为整型
要赋的值是从0到9
可以用循环来赋值
用循环按下标
从大到小输出这10个元素
程序如下
{ int i,a[10];
for (i=0; i<=9;i++)
a[i]=i; //使a[0]~a[9]的值为0~9
for(i=9;i>=0; i--)
输出a[i])
输出回车
for(i=9;i>=0; i--) printf("%d ",a[i])
的作用是先输出a[9]
最后输出a[0]
为了使程序简洁
常在定义数组的同时
给各数组元素赋值
这称为数组的初始化
一
在定义数组的同时
对全部数组元素赋值
例如int a[10]={0,1,2,3,4,5,6,7,8,9};
可以只给数组中的一部分元素赋值
如int a[10]={0,1,2,3,4}
相当于
int a[10]={0,1,2,3,4,0,0,0,0,0};
三
使一个数组中全部元素值为0
int a[10]={0,0,0,0,0,0,0,0,0,0}
相当于int a[10]={0}; //
未赋值的部分元素自动设为0
四
对全部数组元素赋初值时
由于数据的个数已经确定
可以不指定数组长度
int a[5]={1,2,3,4,5}
可写为int a[ ]={1,2,3,4,5};
说明如下
如果在定义数值型数组时
指定了数组的长度
并对之初始化
凡未被“初始化列表”指定初始化的
数组元素
系统会自动把它们初始化为0
如果是字符型数据
则初始化为’\0’
如果是指针型数组
则初始化为null
即空指针
例如
int a[10]={1,2,3,4,5}
定义数组长度为10
只初始化前5个元素
后5个元素为0
好了
同学们
一维数组的定义
引用和初始化
我们就学到这儿
下节课再见
-1.1 The development and characteristics of C language
--1.The development and characteristics of C language .mp4
--1.1 Self test questions
-1.2 A simple C Language program
--1.2 A simple C Language program.mp4
--【Source program】 Example 1.1 output a line of text Hello, world! "
--【Source program】 Example 1.2 program composed of multiple functions, find the larger of two integers
--1.2 Self test questions
-1.3 Program, Programming Language and C Program Running Steps
--1.3 Program, Programming Language and C Program Running Steps.mp4
--Running steps of C source program in CodeBlocks
--1.3 Self test questions
-chapter 1 Self-Test
-2.1 The Concept and Description of Algorithms
--2.1 The Concept and Description of Algorithms.mp4
--2.1 Self test questions
-2.2 Examples of Simple Algorithms, Computational Thinking and Structured Programming
--2.2 Examples of Simple Algorithms, Computational Thinking and Structured Programming.mp4
--2.2 Self test questions
-Chapter 2 Self test questions
-3.1 Simple Structure and Identifier of C Language Program
--3.1 Simple Structure and Identifier of C Language Program.mp4
--【Source program】 Example 3.1 input two integers and output the sum of two numbers.
--【Source program】 Example 3.2 input two integers and output the average value.
--【Source program】 Example 3.3 output the value of the character variable.
--3.1 Self test questions
-3.2 Constants, Variables and Assignments
--3.2 Constants, Variables and Assignments.mp4
--3.2 Self test questions
-3.3 Arithmetic, assignment, increment and decrement operators
--3.3 Arithmetic, assignment, increment and decrement operators.mp4
--3.3 Self test questions
-3.4 Conditions, commas, addresses, byte operators, and mixed operations among various numerical data
--3.4 Conditions, commas, addresses, byte operators, and mixed operations among various numerical data
--【Source program】 example of sizeof
--3.4 Self test questions
-3.5 Input and Output Examples and Character Input and Output
--3.5 Input and Output Examples and Character Input and Output.mp4
--【Source program】 Example. Find the root of quadratic equation of one variable.
--【Source program】 Example 1. Successively output three characters of BOY.
--【Source program】Example 2. Input 3 characters of BOY from the keyboard and output them to the screen
--3.5 Self test questions
-3.6 Formatted output printf function
--3.6 Formatted output printf function.mp4
--【Source program】 Example 3.4 output of integer data.
--【Source program】 Example 3.5 real data output.
--【Source program】 Example 3.6 character data output.
--【Source program】 Example 3.7 uses %s to output a string.
--3.6 Self test questions
-3.7 Formatted input scanf function
--3.7 Formatted input scanf function.mp4
--【Source program】 Example 3.8 Input and output integer data
--【Source program】Example 3.9 Input and output single precision and double precision real data.
--【Source program】Example 3.10 input and output character data.
--【Source program】Example 3.11 input and output string.
--3.7 Self test questions
-3.8 Basic Data Types of C Language
--3.8 Basic Data Types of C Language.mp4
--3.8 Self test questions
-Chapter 3 Self test questions
-Operator and expression self test questions
-4.1 Relational operators, logical operators, and if statements
--4.1 Relational operators, logical operators, and if statements.mp4
--【source example】Example 4.1 Find the root of the quadratic equation of one variable.
--【Source program】Example 4.2 Input two real numbers and output them from small to large.
--4.1 Self test questions
-4.2 Switch Statement
--【Source program】Example 4.3 uses switch statement to implement simple menu program
--【Source program】Example 4.4 converts centesimal score into the corresponding grade system score.
--4.2 Self test questions
-4.3 Examples of Selection Structural Programs
--4.3 Examples of Selection Structural Programs.mp4
--【Source program】 Example 4.5 determines whether a year is a leap year.
--【Source program】 Example 4.6 find the solution of quadratic equation of one variable.
--【Source program】Example 4.7 calculate the transportation cost for the user.
--4.3 Self test questions
-Chapter 4 Self test questions
-5.1 While and Do-While Statement
--5.1 While and Do…while statement.mp4
--【Source program】example 5.1 Use while statement to find 1 + 2 + 3 + +100
--【Source program】example 5.2 using do while statement to find 1 + 2 + 3 + +100
--5.1 Self test questions
-5.2 For Statement
--【Source program】example 5.3 Use for loop to find 1 + 2 + 3 +100
--5.2 Self test questions
-5.3 Change the State of Loop Execution and Nested Loop
--5.3 Change the state of loop execution and nested loop .mp4
--【Source program】example 5.4 collecting charity donations
--【Source program】example 5.5 output a number between 100 and 200 that cannot be divided by 3.
--【Source program】multiplication table
--【Source program】example 5.6 Output the following 4*5 matrix.
--5.3 Self test questions
-5.4 Loop Structure Program example 1
--5.4 Loop structure program example 1.mp4
--5.4 Self test questions
-5.5 Loop Structure Program example 2
--5.5 Loop structure program example 2.mp4
--【Source program】example 5. Output the following figure.
--【Source program】example6. The problem of 100 chickens and 100 coins
--【Source program】example 7. Find the approximate value of PI
--5.5 Self test questions
-Chapter 5 Self test questions
-6.1 Definition, Reference and Initialization of One-Dimensional Arrays
--6.1 Definition, Reference of One-Dimensional Arrays.mp4
--6.1 Self test questions
-6.2 One-Dimensional Array Programming
--6.2 One-Dimensional Array Programming.mp4
--【Source program】example 1. Fibonacci sequence
--【Source program】example 2. Find the maximum value and the minimum value.
--【Source program】example 3 exchanging array elements in reverse order.
--【Source program】example 4. Bubble sorting.
--6.2 Self test questions
-6.3 Definition, Reference and Initialization of Two-Dimensional Arrays
--6.3 Definition, Reference of Two-Dimensional Arrays.mp4
--6.3 Self test questions
-6.4 Two-Dimensional Array Programming
--6.4 Two-Dimensional Array Programming.mp4
--【Source program】example 1. Get the average score of each subject.
--【Source program】Example 2. The elements of row and column of a 2D array are interchanged
--6.4 Self test questions
-6.5 Definition, initialization and input and output of character arrays
--6.5 Definition, initialization and input and output of character arrays .mp4
--【Source program】Example 6.6 Output a known string.
--【Source program】Example 6.7 Output a diamond.
--【Source program】Example 6.10 Sorting of strings.
--6.5 Self test questions
-6.6 String Processing Function
--6.6 String Processing Function.mp4
--6.6 Self test questions
-6.7 Character Array Programming
--6.7 Character Array Programming.mp4
--6.7 Self test questions
-Chapter 6 Self test questions
-7.1 Function Concept and How to Define and Call Functions
--7.1 Function Concept and How to Define and Call Functions.mp4
--7.1 Self test questions
-7.2 Data Transfer in Function Call, Call Procedure and Function Return Value
--7.2 Data Transfer in Function Call, Call Procedure and Function Return Value.mp4
--7.2 Self test questions
-7.3 Declarations of called functions and nested calls to functions
--7.3 Declarations of called functions and nested calls to functions .mp4
--【Source program】Example 7.4 use a function to find the sum of t
--7.3 Self test questions
-7.4 Recursive call to function
--7.4 Recursive call to function.mp4
--【Source program】Example 7.6. How old is the fifth student?
--【Source program】Example 7.7 Use recursion method to find n!
--7.4 Self test questions
-7.5 Array as function parameter (1)
--7.5 Array as function parameter (1).mp4
--【Source program】Example 7.10
--7.5 Self test questions
-7.6 Array as function parameter (2)
--7.6 Array as function parameter (2).mp4
--【Source program】Selection sorting.
--【Source program】Example 7.13 find the maximum of a 3×4 matrix.
--7.6 Self test questions
-7.7 Local and global variables, internal and external functions
--7.7 Local and global variables, internal and external functions.mp4
--【Source program】Example 7.14
--7.7 Self test questions
-7.8 The Survival Period of Variables and the Storage Mode of Local Variables
--7.8 The Survival Period of Variables and the Storage Mode of Local Variables.mp4
--【Source program】Example 7.17
--7.8 Self test questions
-7.9 Storage Categories of Global Variables
--7.9 Storage Categories of Global Variables.mp4
--7.9 Self test questions
-Chapter 7 Self test questions
-8.1 Pointer Concept, Definition and Reference of Pointer Variables
--8.1 Pointer Concept, Definition and Reference of Pointer Variables.mp4
--8.1 Self test questions
-8.2 Pointer variables as function parameters
--8.2 Pointer variables as function parameters.mp4
--【Source program】Example 8.3. Exchange two data with function call.
--8.2 Self test questions
-8.3 Pointer of array element, operation of pointer and reference of array element by pointer
--8.3 Pointer of array element, operation of pointer and reference of array element by pointer.mp4
--【Source program】Point to array elements with pointer variables.
--8.3 Self test questions
-8.4 Using Array Name as Function Parameter
--8.4 Using Array Name as Function Parameter.mp4
--【Source program】Store array elements in reverse order.
--8.4 Self test questions
-8.5 Reference to multidimensional arrays by pointers
--8.5 Reference to multidimensional arrays by pointers.mp4
--【Source program】Pointer variable pointing to one-dimensional array.
--8.5 Self test questions
-8.6 Referencing strings through pointers
--8.6 Referencing strings through pointers.mp4
--【Source program】Reference string by pointer.
--8.6 Self test questions
-8.7 Character pointer as function parameter
--8.7 Character pointer as function parameter.mp4
--【Source program】Character pointer as function parameter.
--8.7 Self test questions
-8.8 Pointer pointing to function
--8.8 Pointer pointing to function.mp4
--【Source program】Pointer variable pointing to the function.
--8.8 Self test questions
-8.9 Functions that return pointer values
--8.9 Functions that return pointer values.mp4
--【Source program】Intercept substring.
--8.9 Self test questions
-8.10 Pointer arrays and multiple pointers
--8.10 Pointer arrays and multiple pointers.mp4
--【Source program】Sorting of strings.
--8.10 Self test questions
-8.11 Dynamic memory allocation and pointer variables pointing to it
--8.11 Dynamic memory allocation and pointer variables pointing to it.mp4
--【Source program】Dynamic memory allocation.
--8.11 Self test questions
-Chapter 8 Self test questions
-9.1 Define and use structural variables
--9.1 Define and use structural variables.mp4
--【Source program】Structure variable
--9.1 Self test questions
-9.2 Using structure arrays
--9.2 Using structure arrays.mp4
--【Source program】Structure array
--9.2 Self test questions
-9.3 Structure pointer
--【Source program】Structure pointer
--9.3 Self test questions
-Chapter 9 Self test questions
-CodeBlocks Baidu online disk download address
-Final Exam
--final exam