当前课程知识点:C Programming >  Chapter 6 Batch Data Processing with Array >  6.3 Definition, Reference and Initialization of Two-Dimensional Arrays >  6.3 Definition, Reference of Two-Dimensional Arrays.mp4

返回《C Programming》慕课在线视频课程列表

6.3 Definition, Reference of Two-Dimensional Arrays.mp4在线视频

下一节:6.4 Two-Dimensional Array Programming.mp4

返回《C Programming》慕课在线视频列表

6.3 Definition, Reference of Two-Dimensional Arrays.mp4课程教案、知识点、字幕

大家好

我是云南大学信息学院丁海燕老师

欢迎走进C语言程序设计课堂

今天

我们讲解二维数组的定义

引用和初始化

有的问题需要用二维数组来处理

例如

有英语

数学

计算机三门课

每门课有5个学生

需要把这些学生的分数

保存起来以备计算

这就需要用到二维数组

如图所示

如果建立一个数组score

它应当是二维的

第一维用来表示第几门课

第二维用来表示第几个学生

例如

用score2,3

表示math第3个学生的分数

它的值是72

可见

一维数组可以用来表示一行数据

如果要表示

由多行多列组成的二维数据

就需要用到二维数组

二维数组常称为矩阵

matrix

把二维数组写成行和列的排列形式

有助于形象化地理解

二维数组的逻辑结构

例如

可以用int score[3][5]

表示3行5列组成的二维数据

那么怎么定义二维数组呢

基本概念与方法和一维数组相似

如int score[3][5]

定义了一个int型的二维数组

第1维有3个元素

第2维有5个元素

每一维的长度分别用一对方括号括起来

二维数组定义的一般形式为

类型符 数组名

[常量表达式][常量表达式]

例如

float a[3][4]

b[5][10]

二维数组可被看作是一种特殊的一维数组

它的元素又是一个一维数组

例如

把a看作是一个一维数组

它有3个元素

a[0]

a[1]

a[2]

每个元素

又是一个包含4个元素的一维数组

a[0]是一个包含a[0][0]

a[0][1]

a[0][2]

a[0][3]

四个元素的一维数组

a[1]是一个包含a[1][0]

a[1][1]

a[1][2]

a[1][3]

四个元素的一维数组

a[2]是一个包含a[2][0]

a[2][1]

a[2][2]

a[2][3]

四个元素的一维数组

可以把a[0]

a[1]

a[2]

看作是3个一维数组的名字

上面定义的二维数组

可以理解为定义了3个一维数组

即相当于float

a[0][4]

a[1][4],a[2][4]

此处把a[0],a[1],a[2]

看作一维数组名

C语言的这种处理方法

在数组初始化

和用指针表示时显得很方便

C语言中

二维数组中元素

排列的顺序是按行存放的

即在内存中

先顺序存放第1行的元素

接着再存放第2行的元素

注意

用矩阵形式

如3行4列形式表示二维数组

是逻辑上的概念

能形象地表示出行列关系

而在内存中

各元素是连续存放的

不是二维的

是线性的

这点务必明确

下面介绍如何引用二维数组的元素

二维数组元素的表示形式为

数组名[下标][下标]

下标应是整型表达式

如a[2-1][2*2-1]

数组元素可以出现在表达式中

也可以被赋值

例如

b[1][2]=a[2][3]/2

合法

注意

在引用数组元素时

下标从0开始

下标值

应在已定义的数组大小的范围内

例如

int a[3][4];

a[3][4]=3;

/* 下标越界 */

定义二维数组的同时

还可以对数组元素赋值

称为数组的初始化

可以用初始化列表

对二维数组初始化

按行赋初值

例如

int a[2][3]={{1,2,3},{4,5,6}};

初始化的结果

第一行1 2 3

第二行4 5 6

按数组元素在内存中排列的顺序

对各元素赋初值

例如int a[2][3]={1,2,3,4,5,6};

用花括号括起来

给部分元素赋初值

例如

int a[2][3]={{1},{4}};

初始化的结果为

第一行为

1 0 0

第二行为

4 0 0

数组初始化时

行长度可省

列长度不能省

例如

int a[][3]={1,2,3,4,5,6,7};

int b[][4]={{1},{4,5}};

初始化的结果如图所示

a的第一行为1 2 3

第二行为4 5 6

第三行为7 0 0

b的第一行为1 0 0 0

第二行为4 5 0 0

例如

int a[3][4]={{1,2,3,4},

{5,6,7,8},

{9,10,11,12}};

int a[3][4]={1,2,3,4,5,6,7,8,9,10,11,12};

int a[3][4]={{1},{5},{9}};

等价于

int a[3][4]

={{1,0,0,0},

{5,0,0,0},

{9,0,0,0}};

int a[3][4]={{1},{5,6}};

相当于int a[3][4]={{1},

{5,6},

{0}};

int a[3][4]={1,2,3,4,5,6,7,8,9,10,11,12};

等价于

int a[ ]

[4]={1,2,3,4,5,6,7,8,9,10,11,12};

也就是这个地方的行的长度可以省去

int a[][4]={{0,0,3},{ },{0,10}}; 合法

以下对二维数组的定义都是错误的

例如int a[ ][ ],

b[ ][2],c[3][ ];

float x[3][ ]={1.0,2.0,3.0,4.0,5.0,6.0};

省掉了列的长度

这也是错误的

int m[2][4]={1,2,3,4,5,6,7,8,9};

/*编译出错

因为初值个数多于数组元素的个数 */

下面来看例1

有一个5人的学习小组

如下表所示

从stu1到stu5

有三门课

求每科的平均成绩

问题分析

用一个二维数组a[3][5]

来存储5个人的三门课成绩

用一个一维数组

ave[3]存储每科的平均成绩

程序使用双重循环

内循环用于输入每门课程

各个学生的成绩

并把该成绩累加

退出双重循环后

将累加成绩除以5

得到该门科目的平均分

外层循环执行3次

即可得到3门课程的平均成绩

程序如下

{ float a[3][5],

total,

ave[3];

其中ave 用于存放每科的平均成绩

int i,j;

for(i=0;i<3;i++)

{ total=0;

for(j=0;j<5;j++)

用scanf输入a[i][j])的值

total=total+a[i][j]

计算总分

}

ave[i]=total/5;

计算每科的平均分

}

最后用printf

输出每科的平均成绩

ave[0]

ave[1]

和ave[2]

程序结束

下面请完成课堂练习

求每个学生的平均成绩

好了

同学们

二维数组的定义

引用

和初始化我们就学到这儿

下节课再见

C Programming课程列表:

Chapter 1 Introduction

-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

--Discussion unit

--【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

--Discussion unit

--Running steps of C source program in CodeBlocks

--1.3 Self test questions

-Course references

-chapter 1 Self-Test

Chapter 2 Algorithm

-2.1 The Concept and Description of Algorithms

--2.1 The Concept and Description of Algorithms.mp4

--Discussion unit

--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

--Discussion unit

--2.2 Self test questions

-Chapter 2 Self test questions

Chapter 3 Programming in C

-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

--Discussion unit

--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

--Discussion unit

--【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

Chapter 4 Selection Structure

-4.1 Relational operators, logical operators, and if statements

--4.1 Relational operators, logical operators, and if statements.mp4

--Discussion unit

--【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

--4.2 Switch Statement .mp4

--Discussion unit

--【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

Chapter 5 Loop Structure

-5.1 While and Do-While Statement

--5.1 While and Do…while statement.mp4

--Discussion unit

--【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

--5.2 for statement.mp4

--【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

--【Source program】example 1

--【Source program】example 2

--【Source program】example 3

--【Source program】 Example 4

--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

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

--Discussion unit

--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

--Discussion unit

--【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

Chapter 7 Modular Programming with Functions

-7.1 Function Concept and How to Define and Call Functions

--7.1 Function Concept and How to Define and Call Functions.mp4

--【Source program】Example 7.1

--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

--Discussion unit

--【Source program】Example 7.2

--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

--Discussion unit

--【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

Chapter 8 Pointer

-8.1 Pointer Concept, Definition and Reference of Pointer Variables

--8.1 Pointer Concept, Definition and Reference of Pointer Variables.mp4

--【Source program】Example 8.1

--8.1 Self test questions

--Discussion unit

-8.2 Pointer variables as function parameters

--8.2 Pointer variables as function parameters.mp4

--Discussion unit

--【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

--Discussion unit

--【Source program】Dynamic memory allocation.

--8.11 Self test questions

-Chapter 8 Self test questions

Chapter 9 Structure

-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

--Discussion unit

--【Source program】Structure array

--9.2 Self test questions

-9.3 Structure pointer

--9.3 Structure pointer.mp4

--【Source program】Structure pointer

--9.3 Self test questions

-Chapter 9 Self test questions

CodeBlocks Baidu online disk download

-CodeBlocks Baidu online disk download address

Final Exam

-Final Exam

--final exam

6.3 Definition, Reference of Two-Dimensional Arrays.mp4笔记与讨论

也许你还感兴趣的课程:

© 柠檬大学-慕课导航 课程版权归原始院校所有,
本网站仅通过互联网进行慕课课程索引,不提供在线课程学习和视频,请同学们点击报名到课程提供网站进行学习。