1.修改程序清单10.07中的程序rain,使它不适用数组下标,而是使用指针进行计算(程序中仍然需要声明并初始化数组)
2.编写一个程序,初始化第一个double数组,然后把数组内容复制到另外两个数组(3个数组都需要在主程序中声明)。制作第一份拷贝的函数使用数组符号。制作第二份拷贝的函数使用指针符号,并使用指针的增量操作。把目标数组名和要复制的元素数目作为参数传递给函数。也就是说,如果给定下列声明,函数调用应该如下面所示。
3.编写一个函数,返回int数组中存储的最大值,并在一个简单的程序中测试这个函数
4.编写一个函数,返回一个double数组中存储的最大值的索引,并在一个简单程序中测试这个函数
#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