6_array_data_processing_2

6.数组处理批量数据

6.4 将整型数组元素行列位置对调
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#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<=1;i++) //遍历二维数组a 元素并赋值于b;遍历行
{
for (j=0;j<=2;j++) //遍历数组a 列
{
printf("%5d",a[i][j]);
b[j][i]=a[i][j]; //数组a中元素与数组b中 元素对调(行、列)
}
printf("\n");
}
printf("array b:\n");
for(i=0;i<=2;i++) //遍历数组b,循环结构for遍历行
{
for(j=0;j<=1;j++) //遍历列
printf("%5d",b[i][j]);
printf("\n");
}
return 0;
}
6.5 将整型数组中最大值元素及相应行列位置输出
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<stdio.h>
int main()
{
int i,j,row=0,colum=0,max;
int a[3][4]={{1,2,3,4},{9,8,7,6},{-10,10,-5,2}};
max=a[0][0];
for(i=0;i<=2;i++) //数组遍历,行遍历
for(j=0;j<=3;j++) // 数组遍历,列遍历
if(a[i][j]>max) //“打擂台”算法
{max=a[i][j];
row=i;
colum=j;
}
printf("max=%d\nrow=%d\ncolum=%d\n",max,row,colum); //最大值输出、行/列号输出
return 0;
}
6.6 字符数组输出特定字符
1
2
3
4
5
6
7
8
9
10
#include<stdio.h>
int main()
{
char c[15]={'I',' ','a','m',' ','a',' ','s','t','u','d','e','n','t','.'};
int i;
for(i=0;i<15;i++)
printf("%c",c[i]);
printf("\n");
return 0;
}
6.7 用星号(*)输出钻石图案
1
2
3
4
5
6
7
8
9
10
11
12
13
#include<stdio.h>
int main()
{
char diamond[][5]={{' ',' ','*'},{' ','*',' ','*',' '},{'*',' ',' ',' ','*'},{' ','*',' ','*',' '},{' ',' ','*',' ',' '}}; //定义字符数组元素初始值,5行5列
int i,j;
for(i=0;i<5;i++) //遍历数组元素,行遍历
{
for(j=0;j<5;j++) //遍历数组元素,列遍历
printf("%c",diamond[i][j]);
printf("\n");
}
return 0;
}
Author

John Doe

Posted on

2020-10-08

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.