当前位置:网站首页>C语言学习-Day_04
C语言学习-Day_04
2022-06-27 09:49:00 【坏坏-5】
- 学习参考B站郝斌老师的视频,文章内的源码如有需要可以私信联系。
数组
- 作用
- 为了解决大量同类型数据的存储和使用问题
- 对于大量相同类型的数据,不需要重新定义变量名
- 为了模拟现实世界
- 一维数组:
a[x]表示直线 - 二维数组:
a[x][y]表示平面 - 三维数组:
a[x][y][z]表示空间 - 四位数组:
a[x][y][z][h]表示空间 + 时间
- 一维数组:
- 为了解决大量同类型数据的存储和使用问题
- 格式
a是变量名n表示有n个变量- 第一项是
a[0],最后一项是a[n-1]
a[n] = {
1, 2, ...,n};
例:
/*数组*/
# include <stdio.h>
int main(void)
{
int a[5] = {
1, 2, 3, 4, 5}; //a表示数组名,5表示有五个元素
int i;
for (i = 0; i < 5; ++i)
{
printf("%d\n", a[i]);
}
return 0;
}
a[5] = {1, 2, 3, 4, 5}a表示数组的名字,5表示有五个元素,每项分别是a[0]、a[1]...a[4]
/*运行结果*/
1
2
3
4
5
Press any key to continue
一维数组
- 格式:
int a[3];- 表示总共有3个元素
- 3个元素名字分别是
a[0]、a[1]、a[2]
- 定义:
- 为 n 个变量连续分配存储空间
- 所有变量的数据类型必须相同
- 所有变量所占的字节大小必须相同
/*错误写法1*/
int a[5];
a[5] = {
1, 2, 3, 4, 5};
- 只有在定义数组的同时才可以整体赋值
- 定义之后的
a[5]表示的不是数组,而是数组的第6项- 数组
a[5]中,只有a[0]-a[4]项,没有a[5]项
例:a数组的值复制给b数组
/*错误写法2*/
b = a; //a、b表示的是数组中第一个元素的地址
//正确写法
for (i = 0; i < 5; ++i)
b[i] = a[i];
数组的操作
- 初始化
- 赋值
- 排序
- 求最大/小值
- 倒置
- 查找
- 插入
- 删除
初始化
- 完全初始化
int a[5] = {1, 2, 3, 4, 5};
- 不完全初始化
int a[5] = {1, 2, 3};- 未初始化的元素自动为 0
- 不初始化
int a[5];- 所有元素都是垃圾值
- 清零
int a[5] = 0- 所有元素为 0
赋值
/*数组赋值*/
# include <stdio.h>
int main(void)
{
int a[5];
int i;
printf("请输入a[0]的值:");
scanf("%d", &a[0]);
printf("a[0] = %d\n", a[0]);
printf("请输入a[3]的值:");
scanf("%d", &a[3]);
printf("a[3] = %d\n", a[3]);
printf("---------------\n");
for (i = 0; i < 5; ++i)
printf("a[%d] = %d\n", i, a[i]); //循环输出数组各项的值
return 0;
}
/*运行结果*/
请输入a[0]的值: 1
a[0] = 1
请输入a[3]的值:3
a[3] = 3
---------------
a[0] = 1
a[1] = -858993460 //没有被初始化的项使用垃圾数据填充
a[2] = -858993460
a[3] = 3
a[4] = -858993460
Press any key to continue
倒置
例:将一个数组中的元素倒置输出
/*将一个数组中的元素倒置输出*/
# include <stdio.h>
int main(void)
{
int a[5] = {
1, 2, 3, 4, 5};
int i, j;
int t; //用于存放临时数据
i = 0;
j = 4;
while (i < j)
{
t = a[i];
a[i] = a[j];
a[j] = t;
++i; //没有使用到变量自增或自减的整体值,所以i++和++i都可以使用
--j;
}
printf("倒置后的数组输出为:");
for (i = 0; i < 5; ++i)
printf("%d, ", a[i]);
printf("\n");
return 0;
}
/*分析*/
i = 0
j = 4
0 < 4成立,进入循环
t = a0 = 1
a0 = a4 = 5
a4 = t = 1
i = 1
j = 3
1 < 3成立,进入循环
t = a1 = 2
a1 = a3 = 4
a3 = t = 2
i = 2
j = 2
2 < 2不成立,退出循环
a[5] ={
5, 4, 3, 2, 1} //a[2]值不改变
/*运行结果*/
倒置后的数组输出为:5, 4, 3, 2, 1,
Press any key to continue
- 测试偶数是否仍然成立
二维数组
- 格式:
int a[3][4];- 表示总共有12个元素,可以当作3行4列看待
- 12个元素的名字依次是:
a[0][0] a[0][1] a[0][2] a[0][3]
a[1][0] a[1][1] a[1][2] a[1][3]
a[2][0] a[2][1] a[2][2] a[2][3]
a[i][j]表示第i + 1行,第j + 1列
二维数组初始化
- 两种方式是等价的
/*二维数组初始化*/
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}
};
二维数组的操作
- 对二维数组输出
- 对二维数组排序
- 求每一行的最大值
- 判断矩阵是否对称
- 矩阵的相乘
/*二维数组的表示与输出*/
# include <stdio.h>
int main(void)
{
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 i,j;
for (i = 0; i < 3; ++i)
{
printf("第 %d 行:", i + 1);
for (j = 0; j < 4; ++j)
{
printf("%-3d", a[i][j]); //-3d%表示左对齐,中间占3个光标的位置
}
printf("\n");
}
return 0;
}
/*运行结果*/
第 1 行:1 2 3 4
第 2 行:5 6 7 8
第 3 行:9 10 11 12
Press any key to continue
是否存在多维数组
- 不存在多维数组
- 因为内存是线性一维的
n维数组可以当作是n - 1维数组的一维数组int a[3][4];- 该数组是含有3个元素的一维数组
- 每个一维数组还可以包含4个元素
以上内容均属原创,如有不详或错误,敬请指出。
边栏推荐
- Reorganize common shell scripts for operation and maintenance frontline work
- Basic violin plot in R with plot
- 【OpenCV 例程200篇】212. 绘制倾斜的矩形
- [200 opencv routines] 212 Draw a slanted rectangle
- Record in detail the implementation of yolact instance segmentation ncnn
- Bluetooth health management device based on stm32
- BufferedWriter 和 BufferedReader 的使用
- On June 23, the video address of station B in the third episode of rust chat room
- Some exercises about binary tree
- torchvision.models._utils.IntermediateLayerGetter使用教程
猜你喜欢

This application failed to start because it could not find or load the QT platform plugin
![leetcode:968. Monitor the binary tree [tree DP, maintain the three states of each node's subtree, it is very difficult to think of the right as a learning, analogous to the house raiding 3]](/img/70/3954b0871cc31d24ae016eb99d871e.png)
leetcode:968. Monitor the binary tree [tree DP, maintain the three states of each node's subtree, it is very difficult to think of the right as a learning, analogous to the house raiding 3]
测试同学怎么参与codereview

Installation and usage of source insight tool

通俗易懂理解樸素貝葉斯分類的拉普拉斯平滑

多线程实现 重写run(),怎么注入使用mapper文件操作数据库

Reading and writing Apache poi

1098 insertion or heap sort (PAT class a)

使用aspose-slides将ppt转pdf

Quelques exercices sur les arbres binaires
随机推荐
Bluetooth health management device based on stm32
多线程实现 重写run(),怎么注入使用mapper文件操作数据库
QT运行显示 This application failed to start because it could not find or load the Qt platform plugin
ucore lab5
leetcode:522. Longest special sequence II [greed + subsequence judgment]
隐私计算FATE-离线预测
Pakistani security forces killed 7 terrorists in anti-terrorism operation
导师邀请你继续跟他读博,你会不会立马答应?
Technology is as important as business. It is wrong to favor either side
【面经】云泽科技
js中的数组对象
Hitek power supply maintenance X-ray machine high voltage generator maintenance xr150-603-02
torchvision.models._utils.IntermediateLayerGetter使用教程
ucore lab4
leetcode:522. 最长特殊序列 II【贪心 + 子序列判断】
技术与业务同等重要,偏向任何一方都是错误
vector::data() 指针使用细节
mongodb跨主机数据库拷贝以及常用命令
Use aspese Cells convert Excel to PDF
flutter 微信分享