当前课程知识点:C Programming >  Chapter 6 Batch Data Processing with Array >  6.2 One-Dimensional Array Programming >  6.2 One-Dimensional Array Programming.mp4

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

6.2 One-Dimensional Array Programming.mp4在线视频

下一节:【Source program】example 1. Fibonacci sequence

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

6.2 One-Dimensional Array Programming.mp4课程教案、知识点、字幕

大家好

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

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

今天我们讲解一维数组的编程

下面来看例1

用数组处理求斐波那契数列问题

解题思路

在上一章循环中

是用简单变量处理的

缺点是

不能在内存中保存这些数

假如想直接输出数列中第25个数

是很困难的

如果用数组处理

每一个数组元素

代表数列中的一个数

依次求出各数

并存放在相应的数组元素中

程序如下

定义数组长度为20

对最前面的两个元素f[0]和f[1]

均指定初值为1

根据数列的特点

由前面两个元素的值

可计算出第3个元素的值

f[2]=f[0]+f[1];

在循环中

可以用以下语句依次计算出f[2]~f[19]的值

f[i]=f[i-2]+f[i-1];

if 语句用来控制换行

每行输出5个数据

程序如下

{int i; int f[20]={1,1};

for(i=2;i<20;i++)

f[i]=f[i-2]+f[i-1];

表示依次计算出f[2]~f[19]的值

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

{if(i%5==0) printf(“ ”);

输出

换行

printf

f[i]

输出f[i]的值

运行结果如图所示

下面来看例二

输入5个整数

找出最大数和最小数所在位置

并把二者对调

然后输出

思路是

求最大最小值

采用打擂台的方法

定义一维数组a

存放被比较的数

定义变量max为最大值

min为最小值

k

为最大值下标

j为最小值下标

首先

假定a[0]为最大值和最小值

从a[1]开始

各数依次与擂主进行比较

若a[i]>max

则max=a[i];

k=i;

否则判断

若a[i]

min=a[i];

j=i;

i]当所有的数都比较完之后

当所有的数都比较完之后

将a[j]=max;

a[k]=min;

最后输出a数组

程序如下

{ int a[5],max,min,i,j,k;

定义数组及相关变量

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

用scanf循环输入数组元素的值

min=a[0]; max=a[0];

j=k=0;

for (i=1; i<5; i++)

if (a[i]

else if (a[i]>max) { max=a[i]; k=i ; }

a[j]=max; a[k]=min;

循环输出 a[i]的值

程序运行情况如下

输入

5

7

2

3

1

输出

5

1

2

3

7

下面来看例3

将5个数存放在一维数组中

再将这5个数

逆序存放在同一数组中并输出

解题思路

根据图示

若N=5

则交换5/2=2次

a[0] 和a[4]交换

这时下标0+4=4

a[1]和 a[3]交换

下标1+3=4

则a[i] 和a[j]交换

那么下标i+j=N-1

因此可以推出

j=N-1-i

若N=4

交换 4/2=2次

因此N 个数

交换 N/2 次

a[i]与a[N-1-i]交换

将数组元素逆序存放的程序如下

#define N 5

main()

{int i,t,a[N];

定义变量及数组

for(i=0;i

用scanf输入a[i]的值

for(i=0;i

表示交换了N/2次

{ t=a[i];

a[i]=a[N-1-i];

a[N-1-i]=t; }

这三句实现 a[i]与a[N-1-i]的交换

}

附循环输出a[i]的值

例如输入

2 ,6

3, 1, 8

输出为

8 ,1 ,3 ,6, 2

下面来看例4

有10个地区的面积

要求对它们按由小到大的顺序排列

解题思路

排序的规律有两种

一种是“升序”

从小到大

另一种是“降序”

从大到小

把题目抽象为

“对n个数按升序排序”

采用冒泡法排序

“冒泡法”的基本思路是

每次将相邻两个数比较

将小的调到前头

若有6个数

9

8

5

4

2

0

第1次

先将最前面的两个数8和9对调

第2次

将第2个和第3个数

9和5对调

等等

如此共进行5次

得到

8

5

4

2

0

9的顺序

可以看到

最大的数已经沉底

成为最下面一个数

而小的数上升

最小的数0

已向上浮起一个位置

经过第1趟

共5次比较与交换后

已得到最大数9

然后进行第2趟比较

对余下的前面5个数

8

5

4

2

0

进行新一轮的比较

以便使次大的数“沉底”

按以上方法进行第2趟比较

如图所示

经过这一趟4次比较与交换

得到次大的数8

然后进行第3趟比较

对余下的前面4个数

5

4

2

0

进行新一轮的比较

以便使次大的数“沉底”

按以下方法进行第3趟比较

如图所示

经过这一趟3次比较与交换

得到第3大的数5

然后进行第4趟比较

对余下的前面3个数

4

2

0

进行新一轮的比较

以便使次大的数“沉底”

按以上方法进行第4趟比较

得到第4大的数4

然后进行第5趟比较

对余下的前面2个数

2

0

进行新一轮的比较

以便使次大的数“沉底”

按以上方法进行第5趟比较

如图所示

经过这一趟1次比较与交换

得到第5大的数2

排序结束

按此规律

可以推知

对6个数要比较5趟

才能使6个数按大小顺序排列

在第1趟比较中

要进行两个数之间的比较共5次

在第2趟过程中比较4次

第5趟只须比较1次

如果有n个数

则要进行n-1趟比较

在第1趟比较中

要进行n-1次两两比较

在第j趟比较中

要进行n-j次两两比较

双重循环写为

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

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

if (a[i]>a[i+1])

冒泡法排序算法如图所示

N个数

j控制趟数

i控制每一趟比较的次数

j从0到N-2

比较N-1趟

每一趟i从下标0开始

比较N-j趟

每次比较

若相邻的两个元素后者小于前者

即a[i]>a[i+1]

则交换两个数

程序如下

int a[10]; int i,j,t;

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

scanf("%d",&a[i]);

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

控制比较的趟数

for(i=0;i<9-j;i++)

控制两两比较的次数

if (a[i]>a[i+1])

{t=a[i];a[i]=a[i+1];a[i+1]=t;} //交换两个数

最后通过循环

输出a[i]的值

程序运行结果如图所示

输入10个数

按升序输出这10个数

第四

有十个地区的面积

要求用冒泡法

对他们按从小到大的顺序排序

输入十个面积的值

控制比较的趟数

控制两两比较的次数

若后者小于前者

交换相邻的这两个数

输出排序后的十个数

编译

连接

运行

输入以下十个数

输入十个数为

34

67

90

43

124

87

65

99

132

26

输出排序后的数为

26

34

43

65

67

87

90

99

124

132

好了

同学们

一维数组的编程我们就学习到这儿

下节课再见

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.2 One-Dimensional Array Programming.mp4笔记与讨论

也许你还感兴趣的课程:

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