6_array_data_processing

6.数组处理批量数据

6.1 为数组赋值0~9,并将数组元素逆序输出
1
2
3
4
5
6
7
8
9
10
11
12
#include<stdio.h>

int main()
{
int i,a[10]; //定义整型变量i ,整型数组a
for(i=0;i<=9;i++) //循环for为数组元素赋值
a[i]=i;
for(i=9;i>=0;i--) //循环for 将数组元素逆序输出
printf("%d",a[i]);
printf("\n");
return 0;
}
6.2 输出斐波那契数列第2~21项(斐波那契数列:0,1,1,2,3,5…)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<stdio.h>

int main()
{
int i;
int f[20]={1,1}; //定义整型数组,第1,2项均为1

for(i=2;i<20;i++) //循环结构for为整型数组第3至20项赋值
f[i]=f[i-2]+f[i-1];

for(i=0;i<20;i++)
{
if(i%5==0) printf("\n");
printf("%12d",f[i]); //输出数组元素值,每数值占12列且右对齐
}
printf("\n");
return 0;
}

6.3 自定义10个数值,将数值按升序形式输出(冒泡排序法)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<stdio.h>

int main()
{
int a[10];
int i,j,t;
printf("input 10 numbers :\n");
for(i=0;i<10;i++) //循环结构for 读取数据并存储
scanf("%d",&a[i]);
printf("\n");
for(j=0;j<9;j++) //重复趟数总计9次,遍历每趟
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;} //条件判断,若前一项大于后一项,进行数值交换
printf("the sorted numbers :\n");
for(i=0;i<10;i++) //循环结构输出sorted 后的数组元素
printf("%d",a[i]);
printf("\n");
return 0;
}
Author

John Doe

Posted on

2020-10-07

Updated on

2020-10-08

Licensed under

You need to set install_url to use ShareThis. Please set it in _config.yml.
You forgot to set the business or currency_code for Paypal. Please set it in _config.yml.

Comments

You forgot to set the shortname for Disqus. Please set it in _config.yml.
You need to set client_id and slot_id to show this AD unit. Please set it in _config.yml.