第六章 利用数组处理批量数据

第六章

6.1 对10个数元素一次赋值给数组,要求输出逆序数

 #include<stdio.h>
 #include<stdlib.h>
 int main()
 {	
 int i,a[10];	
 for(i=0;i<=9;i++)	 
 a[i]=i;	 
 for(i=9;i>=0;i--)	  
 printf("%2d",a[i]);	 
 printf("\n");	 
 system("pause");	 
 return 0;
 }

运行结果如下:在这里插入图片描述

6.2

用数组处理Fibonacci数列问题

#include<stdio.h>
#include<stdlib.h>
int main()
{	
int i;	
int f[20]={1,1};	
for(i=2;i<20;i++)	
{		
f[i]=f[i-2]+f[i-1];	
}	
for(i=0;i<20;i++)	
{		
if(i%5==0)		
printf("\n");		
printf("%12d",f[i]);	
}	
printf("\n");
system("pause");	
return 0;
} 

运行结果如下:在这里插入图片描述# 6.3 有10各地区的面积,要求他们按照由小到大的顺序排列

#include<stdio.h>
#include<stdlib.h>
int main()
{	
int a[10];	
int i,j,t;	
printf("input 10 numbers:\n");
for(i=0;i<10;i++)	
 scanf("%d",&a[i]);	
 printf("\n");	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;	  
 }
printf("the sorted numbers:\n");	
for(i=0;i<10;i++)	 
printf("%3d",a[i]);	
printf("\n");	
system("pause");	
return 0;
} 
```
运行结果如下:![在这里插入图片描述](https://img-blog.csdnimg.cn/20190121162125605.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NDU4MzMwMw==,size_16,color_FFFFFF,t_70)# 6.4 将一个数组的行和列元素互换
```c
#include<stdio.h>
#include<stdlib.h>
int main()
{	
int a[2][3]={{1,2,3},{4,5,6}};	
int i,j;	printf("array a:\n");	
for(i=0;i<=1;i++)	
{		
for(j=0;j<=2;j++)		
{			
printf("%5d",a[i][j]);		
}	printf("\n");	
}	
printf("array b:\n");	
for(i=0;i<=2;i++)	
{		
for(j=0;j<=1;j++)		 
printf("%5d",a[j][i]);		
printf("\n");	}	
system("pause");	return 0;
} 
```
运行结果如下:![在这里插入图片描述](https://img-blog.csdnimg.cn/20190121162300252.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NDU4MzMwMw==,size_16,color_FFFFFF,t_70)
# 6.5 输出矩阵中最大的元素以及行号列号。
```c
#include<stdio.h>
#include<stdlib.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);	
system("pause");	return 0;
}

运行结果如下:在这里插入图片描述# 6.6 输出一个一直字符串

#include<stdio.h>
#include<stdlib.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");	
system("pause");	
return 0;
}

运行结果如下:在这里插入图片描述

6.7 输出一个菱形。

#include<stdio.h>
#include<stdlib.h>
int main()
{
int diamond[][5]={{' ',' ','*'},{' ','*',' ','*',' '},{'*',' ',' ',' ','*'},{' ','*',' ','*'},{' ',' ','*'}};	
int i,j;	
for(i=0;i<=4;i++)	
{
for(j=0;j<=4;j++)
printf("%c",diamond[i][j]);
printf("\n");
}
system("pause");	
return 0;
}

运行结果如下:在这里插入图片描述

6.8 输入一行字符,统计其中有多少单词,单词之间用空格分隔开。

#include<stdio.h>
#include<stdlib.h>
int main()
{	
char string[81];	
int i,num=0,word=0;	
char c;	
gets(string);	
for(i=0;
(c=string[i])!='\0';i++)	 
if(c==' ')word=0;	 
else if(word==0)	 
{
word=1;
num++;	 
}	
 printf("these are %d words in this line.\n",num);	 
 system("pause");	 
 return 0;
 }

运行结果如下:在这里插入图片描述]

6.9 有三个字符,要求输出最大者。

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
char str[3][20];	
char string[20];	
int i;	
for(i=0;i<3;i++)	 
gets(str[i]);	
if(strcmp(str[0],str[1])>0)	 
strcpy(string,str[0]);	
else	 
strcpy(string,str[1]);	
if(strcmp(str[2],string)>0)	 
strcpy(string,str[2]);	
printf("\nthe largest string is:\n%s\n",string);	
system("pause");
return 0;
}

运行结果如下:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值