7.编写一个程序,初始化一个double类型的二维数组,使用编程练习2中的一个拷贝函数把该数组中的数据拷贝至另一个double类型的二维数组中(因为二维数组是数组的数组,所以可以使用处理一维数组的拷贝函数来处理数组中的每个子数组)。
void copy_ptr(double* target, double* source, int size)
{
for (int i = 0; i < size; i++)
{
*target++ = *source++;
}
}
int main()
{
double src[][7] = {
{1,2,3,4,5,6,7},
{8,9,10,11,12,13,14}
};
double dst[2][7] = { 0 };
for (int i = 0; i < 2; i++)
{
copy_ptr(dst[i], src[i], 7);
for (int j = 0; j < 7; j++)
{
printf("%f ", dst[i][j]);
}
printf("\n");
}
return 0;
}
8.使用编程练习2中的拷贝函数,把一个有7个元素的数组中第3~第五个元素拷贝至含3个元素的数组中。该函数本身不需要修改,只需要选择合适的实际参数(实际参数不需要是数组名和数组大小,只需要是数组元素的地址和待处理元素的个数)
void copy_arr(double target[], double source[], int size)
{
for (int i = 0; i < size; i++)
{
target[i] = source[i];
}
}
void copy_ptr(double* target, double* source, int size)
{
for (int i = 0; i < size; i++)
{
*target++ = *source++;
}
}
void copy_ptrs(double* target, double* source, double* end)
{
for (double *p = source; source < end; source++)
{
*target++ = *source;
}
}
int main(void)
{
double source[5] = { 1.1, 2.2, 3.3, 4.4, 5.5 };
double target1[3];
double target2[3];
double target3[3];
copy_arr(target1, source + 2, 5);
copy_ptr(target2, source + 2, 5);
copy_ptrs(target3, source + 2, source + 5);
for (int i = 0; i < 3; i++)
{
printf("%f %f %f\n", target1[i], target2[i], target3[i]);
}
return 0;
}
9.编写一个程序,初始化一个double类型的3X5二维数组,使用一个处理变长数组的函数将其拷贝至另一个二维数组中。还要编写一个以变长数组为形参的函数以显示两个数组的内容。这两个函数应该能处理任意N X M数组(如果编译器不支持变长数组,就使用传统C函数处理NX5的数组)。
void copy_arr(const int n, const int m, double target[][5], double source[][5])
{
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
target[i][j] = source[i][j];
}
void print_matrix(int n, int m, double matrix[][5])
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
printf("%f ", matrix[i][j]);
printf("\n");
}
}
int main()
{
double source[3][5] = {
{1, 2, 3, 4, 5},
{6, 7, 8, 9, 10},
{11, 12, 13, 14, 15}
};
double target[3][5];
copy_arr(3, 5, target, source);
print_matrix(3, 5, source);
print_matrix(3, 5, target);
return 0;
}
10.编写一个函数,把两个数组中相对的元素相加,然后把结果存储到第3个数组中。也就是说,如果数组1中包含的值是2、4、5、8,数组2中包含的值是1、0、4、6,那么该函数把3、4、9、14赋给第3个数组。函数接受3个数组名和一个数组大小。在一个简单的程序中测试该函数。
void add_arr(int n, int a[], int b[], int c[])
{
for (int i = 0; i < n; i++)
c[i] = a[i] + b[i];
}
int main()
{
int a[] = { 2, 4, 5, 8 };
int b[] = { 1, 0, 4, 6 };
int c[4];
add_arr(4, a, b, c);
printf("%d %d %d %d\n", c[0], c[1], c[2], c[3]);
return 0;
}
11.编写一个程序,声明一个int类型的3X5二维数组,并用合适的值初始化它。该程序打印数组中的值,然后各值翻倍(即是原值的2倍),并显示出各元素的新值。编写一个函数显示数组的内容,再编写一个函数把数组中的值翻倍。这两个函数都以数组名和行数作为参数。
void print_arr(int a[][5], int n)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < 5; j++)
printf("%d ", a[i][j]);
printf("\n");
}
}
void doulbe_arr(int a[][5], int n)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < 5; j++)
a[i][j] *= 2;
}
}
int main()
{
int a[][5] = {
{1, 2, 3, 4, 5},
{6, 7, 8, 9, 10},
{11, 12, 13, 14, 15}
};
print_arr(a, 3);
doulbe_arr(a, 3);
print_arr(a, 3);
return 0;
}
12.重写程序清单10.7的rain.c程序,把main()中的主要任务都改成用函数来完成。
#define MONTHS 12 //一年的月份数
#define YEARS 5 //年数
void average_of_year(const float rainfall[][MONTHS], int years)
{
float subtot = 0.0, total = 0.0;
printf(" Year RAINFALL(inches)\n");
for (int year = 0; year < years; year++)
{
subtot = 0.0;
for (int month = 0; month < MONTHS; month++)
subtot += *(*(rainfall + year) + month);
printf("%5d %15.1f\n", year + 2010, subtot);
total += subtot;
}
printf("\nThe yearly average is %.1f inches.\n\n", total / years);
}
void average_of_monthly_rainfall(const float rainfall[][MONTHS], int years)
{
for (int month = 0; month < MONTHS; month++)
{
float subtot = 0.0;
for (int year = 0; year < years; year++)
{
subtot += *(*(rainfall + year) + month);
}
printf(" % 4.1f ", subtot / years);
}
printf("\n");
}
int main(void)
{
//用2010~2014年的降水量初始化数组
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 }
};
average_of_year(rain, YEARS);
printf("MONTHLY AVERAGES:\n\n");
printf(" Jan Feb Mar Apr May Jun Jul Aug Sep Oct ");
printf(" Nov Dec\n");
average_of_monthly_rainfall(rain, YEARS);
return 0;
}
13.编写一个程序,提示用户输入3个数组,每组包含5个double类型的数(假设用户都正确地响应,不会输入非数值数据)。该程序完成下列任务。
a.把用户输入的数据存储在3X5的数组中
b.计算每组(5个)数据的平均值
c.计算所有数据的平均值
d.找出这15个数据中的最大值
e.打印结果
每个任务都要单独的函数来完成(使用传统C处理数组的方式)。完成任务b,要写一个计算并返回一维数组平均值的函数,利用循环调用该函数3次。对于其他任务的函数,应该把整个数组作为参数,完成任务c和d的函数应该把结果返回主调函数。
double get_average(double arr[], int len)
{
double sum = 0;
for (int i = 0; i < len; i++)
{
sum += arr[i];
}
return sum / len;
}
double get_all_average(double arr[][5], int len)
{
double sum = 0;
for (int i = 0; i < len; i++)
{
sum += get_average(arr[i], 5);
}
return sum / len;
}
double get_max(double arr[][5], int len)
{
double max = arr[0][0];
for (int i = 0; i < len; i++)
{
for (int j = 0; j < 5; j++)
{
if (arr[i][j] > max)
{
max = arr[i][j];
}
}
}
return max;
}
int main()
{
system("chcp 65001");
double arr[3][5];
for (int i = 0; i < 3; i++)
{
printf("请输入5个doulbe类型的数:\n");
for (int j = 0; j < 5; j++)
{
scanf_s("%lf", &arr[i][j]);
}
}
for (int i = 0; i < 3; i++)
{
printf("第%d组数的平均值为:%f\n", i, get_average(arr[i], 5));
}
printf("平均值为:%f\n", get_all_average(arr, 3));
printf("最大值为:%f\n", get_max(arr, 3));
return 0;
}