10.13 编程练习

本文提供了一系列的C语言编程练习,涉及数组和指针的使用。包括但不限于:1. 使用指针计算二维数组的年降水量;2. 实现数组的两种复制方法;3. 找出整数数组的最大值;4. 找出双精度数组的最大值索引;5. 计算双精度数组的最大值和最小值之差;6. 复制二维数组到另一个数组;7. 复制数组的一部分;8. 合并两个数组的元素;9. 将数组元素加倍;10. 重写程序以使用函数处理降水量数据;11. 输入三个数集并计算平均值、最大值等信息。
摘要由CSDN通过智能技术生成
1.修改程序清单10.07中的程序rain,使它不适用数组下标,而是使用指针进行计算(程序中仍然需要声明并初始化数组)
#include <stdio.h>
#define MONTHS 12
#define YEARS 5
int main(void)
{
	const float rain[YEARS][MONTHS]={
		{4.3, 4.3, 4.3, 3.0, 2.0, 1.2, 0.2, 0.2, 0.4, 2.4, 3.5, 6.6,},
		{8.5, 8.2, 1.2, 1.6, 2.4, 0.0, 5.2, 0.9, 0.3, 0.9, 1.4, 7.3,},
		{9.1, 8.5, 6.7, 4.3, 2.1, 0.8, 0.2, 0.2, 1.1, 2.3, 6.1, 8.4,},
		{7.2, 9.9, 8.4, 3.3, 1.2, 0.8, 0.4, 0.0, 0.6, 1.7, 4.3, 6.2,},
		{7.6, 5.6, 3.8, 2.8, 3.8, 0.2, 0.0, 0.0, 0.0, 1.3, 2.6, 5.2,},
	};
	int year, month;
	float subtot,total,sum;
	for(year=0,total=0.0;year<YEARS;year++)
	{
		for(month=0,subtot=0.0;month<MONTHS;month++)
		{
			subtot+=*(*(rain+year)+month);
		}
		printf("%d年降水总量为%0.2f,本年月平均降水量为%0.2f\n",year+1,subtot,subtot/MONTHS);
		total+=subtot;
	}
	printf("%d年的降水总量是%0.2f,五年年降水平均值为%.2f\n",YEARS,total,total/YEARS);
	
	for(month=0;month<MONTHS;month++)
	{
		for(year=0,sum=0.0;year<YEARS;year++)
		{
			sum+=*(*(rain+year)+month);
		}
		printf("每年%d月份的降水平均值为%0.1f\n",month+1,sum/YEARS);	
	} 
	return 0;
}


2.编写一个程序,初始化第一个double数组,然后把数组内容复制到另外两个数组(3个数组都需要在主程序中声明)。制作第一份拷贝的函数使用数组符号。制作第二份拷贝的函数使用指针符号,并使用指针的增量操作。把目标数组名和要复制的元素数目作为参数传递给函数。也就是说,如果给定下列声明,函数调用应该如下面所示。
#include <stdio.h>  
void copy_arr(const double source[],double target1[],int size);  
void copy_ptr(const double *source,double *target2,double *end);  
void print(const double ar[],int size); 
int main(void)  
{  
    double source[5]={1.1,2.2,3.3,4.4,5.5};  
    double target1[5];  
    double target2[5];  
    int size=5;  
              
    printf("source array:\n");  
    print(source,size);  
      
    copy_arr(source,target1,size);  
    printf("target1 array:\n");  
    print(target1,size);      
    copy_ptr(source,target2,source+size);  
    printf("target2 array:\n");  
    print(target2,size);  
    return 0;  
}  
void copy_arr(const double source[],double target1[],int size)  
{  
    int i;  
    for(i=0;i<size;i++)  
        target1[i]=source[i];  
}  
void copy_ptr(const double *source,double *target2,double *end)  
{  
 	while(source<end)
	{
		*target2=*source;
		source++;
		target2++;
	} 
}  
void print(const double ar[],int size)  
{  
    int i;  
    for(i=0;i<size;i++)  
        printf("%.1f  ",ar[i]);  
    printf("\n");  
}  


3.编写一个函数,返回int数组中存储的最大值,并在一个简单的程序中测试这个函数
#include <stdio.h>
void print(int ar[],int *end);  
int max(int ar[],int *end);
int main(void)
{
	const int SIZE=5;
	int array[SIZE]={3, 6, 9, 2, 5, };
	print(array,array+SIZE);
	printf("数组中最大的数是%d\n",max(array,array+SIZE));
	return 0;
	
}
void print(int ar[],int *end) 
{  
	for(;ar<end;ar++)
	{
		printf("%d  ",*ar);
	}
    printf("\n");  
}  


int max(int ar[],int *end)
{
	int max=*ar;
	for(ar+=1;ar<end;ar++)
	{
		if(*ar>max)
			max=*ar;	
	}
	return max;
}


4.编写一个函数,返回一个double数组中存储的最大值的索引,并在一个简单程序中测试这个函数
#include <stdio.h>  
void print(double ar[],double *end);  
int max_index(double ar[],int size);
int main(void)
{
	const int SIZE=5;
	double array[SIZE]={3.0, 6.1, 9.3, 2.7, 5.5, };
	print(array,array+SIZE);
	printf("数组中最大数的下标是%d.\n",max_index(array,SIZE));
	return 0;
	
}
void print(double ar[],double *end) 
{  
	for(;ar<end;ar++)
	{
		printf("%.2f  ",*ar);
	}
    printf("\n
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值