当前位置:网站首页>C语言数组处理批量数据
C语言数组处理批量数据
2022-04-23 05:48:00 【Chshyz】
一维数组、二维数组、字符串数组
环境:CentOS7
PS:数组是有序数据的集合(每个元素都属于同一个数据类型,用一个统一的数组名和下标来唯一的确定数组中的元素。)
- 一维数组(类型符 数组名 [常量表达式])
例子:先用循环结构给数组元素a[0]~a[9]赋值0到9,然后再按照9-0输出
#include <stdio.h>
int main() {
int i, a[10]; #定义整型变量i和整型数组a
for (i = 0; i <= 9; i ++) #先后对10个元素赋值
a[i] = i;
for (i = 9; i >= 0; i --) #按照反顺序输出
printf ("%d\n", a[i]);
printf("\n");
return 0;
}
[root@chenshuyi c]# gcc -o for2 for2.c
[root@chenshuyi c]# ./for2
9
8
7
6
5
4
3
2
1
0
- 二维数组(类型名 数组名[常量表达式] [常量表达式])
例子:行列转置
#include <stdio.h>
int main() {
int a[2] [3] = {
{
1, 2, 3}, {
4,5,6}
};
int b[3] [2], i, j;
printf ("array a:\n");
for (i = 0; i < 2; i ++) {
for (j = 0; j < 3; j ++) {
printf ("%5d", a[i] [j]);
b[j] [i] = a [i] [j];
}
printf ("\n");
}
printf ("array b:\n");
for (i = 0; i < 3; i ++) {
for (j = 0; j < 2; j ++)
printf ("%5d", b[i] [j]);
printf ("\n");
}
return 0;
}
[root@chenshuyi c]# gcc -o for3 for3.c
[root@chenshuyi c]# ./for3
array a:
1 2 3
4 5 6
array b:
1 4
2 5
3 6
- 字符数组
例子:菱形
#include <stdio.h>
int main() {
char a[][5] = {
{
' ',' ','*'},
{
' ','*',' ','*'},
{
'*',' ',' ',' ','*'},
{
' ','*',' ','*'},
{
' ',' ','*'}
};
int i, j;
for (i = 0; i < 5; i ++) {
for (j = 0; j < 5; j ++)
printf ("%c", a[i][j]);
printf ("\n");
}
return 0;
}
[root@chenshuyi c]# gcc -o for4 for4.c
[root@chenshuyi c]# ./for4
*
* *
* *
* *
*
- 统计单词数量
#include <stdio.h>
int main() {
char b[100];
int i, num = 0, word = 0;
char c;
gets (b);
for (i = 0; (c = b[i]) != '\0'; i ++)
if (c == ' ') word = 0;
else if (word == 0) {
word = 1;
num ++;
}
printf ("you have %d words in this line.\n", num);
return 0;
}
[root@chenshuyi c]# ./for5
huang hong zhang
you have 3 words in this line.
版权声明
本文为[Chshyz]所创,转载请带上原文链接,感谢
https://blog.csdn.net/HelloWorld_4396/article/details/120156191
边栏推荐
猜你喜欢
随机推荐
SQL -- data definition
Log4j2跨线程打印traceId
[leetcode 54] spiral matrix
Feign请求日志统一打印
Rust:如何 match 匹配 String 字符串?
Export of data
[leetcode 19] delete the penultimate node of the linked list
Busybox initrd and initialization process
Detailed arrangement of knowledge points of University probability theory and mathematical statistics
grub boot. S code analysis
相机标定:关键点法 vs 直接法
Flask - 中间件
爬虫效率提升方法
代理服务器
NVIDIA Jetson: GStreamer 和 openMAX(gst-omx) 插件
搭建jpress个人博客
[leetcode 6] zigzag transformation
Basic knowledge of network in cloud computing
【UDS统一诊断服务】一、诊断概述(4)— 基本概念和术语
Cf515b drazil and his happy friends