程序清单10.1,day_mon1.c:
/* day_mon1.c -- 打印每个月的天数 */
#include <stdio.h>
#define MONTHS 12
int main(void)
{
int days[MONTHS] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int index;
for (index = 0; index < MONTHS; index++)
printf("Month %2d has %2d days,\n", index + 1, days[index]);
return 0;
}
输出结果:
程序清单10.2,no_data.c:
/* no_data.c -- 未初始化数组 */
#include <stdio.h>
#define SIZE 4
int main(void)
{
int no_data[SIZE]; /* 未初始化数组 */
int i;
printf("%2s%14s\n", "i", "no_data[i]");
for (i = 0; i < SIZE; i++)
printf("%2d%14d\n", i, no_data[i]);
return 0;
}
输出结果:
程序清单10.3,some_data.c:
/* some_data.c -- 部分初始化数组 */
#include <stdio.h>
#define SIZE 4
int main(void)
{
int some_data[SIZE] = {1492, 1066};
int i;
printf("%2s%14s\n", "i", "some_data[i]");
for (i = 0; i < SIZE; i++)
printf("%2d%14d\n", i, some_data[i]);
return 0;
}
输出结果:
程序清单10.4,day_mon2.c:
/* day_mon2.c -- 让编译器计算元素个数 */
#include <stdio.h>
int main(void)
{
const int days[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31 };
int index;
for (index = 0; index < (int) (sizeof days / sizeof days[0]); index++)
printf("Month %2d has %d days.\n", index + 1, days[index]);
return 0;
}
输出结果:
程序清单10.5,designate.c:
// designate.c -- 使用指定初始化器
#include <stdio.h>
#define MONTHS 12
int main(void)
{
int days[MONTHS] = { 31, 28, [4] = 31, 30, 31, [1] = 29 };
int i;
for (i = 0; i < MONTHS; i++)
printf("%2d %d\n", i + 1, days[i]);
return 0;
}
输出结果:
程序清单10.6,bounds.c:
// bounds.c -- 数组下标越界
#include <stdio.h>
#define SIZE 4
int main(void)
{
int value1 = 44;
int arr[SIZE];
int value2 = 88;
int i;
printf("valuel = %d, value2 = %d\n", value1, value2);
for (i = -1; i <= SIZE; i++)
arr[i] = 2 * i + 1;
for (i = -1; i < 7; i++)
printf("%2d %d\n", i, arr[i]);
printf("value1 = %d, value2 = %d\n", value1, value2);
printf("address of arr[-1]: %p\n", &arr[-1]);
printf("address of arr[4]: %p\n", &arr[4]);
printf("address of value1: %p\n", &value1);
printf("address of value2: %p\n", &value2);
return 0;
}
输出结果:
程序清单10.7,rain.c:
/* rain.c -- 计算每年的总降水量、年平均降水量和 5 年中每月的平均降水量 */
#include <stdio.h>
#define MONTHS 12 // 一年的月份数
#define YEARS 5 // 年数
int main(void)
{
// 用 2020~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 }
};
int year, month;
float subtot, total;
printf(" YEAR RAINFALL (inches)\n");
for (year = 0, total = 0; year < YEARS; year++)
{ // 每一年,各月的降水量总和
for (month = 0, subtot = 0; month < MONTHS; month++)
subtot += rain[year][month];
printf("%5d %15.1f\n", 2010 + year, subtot);
total += subtot; // 5 年的总降水量
}
printf("\nThe yearly average is %.1f inches.\n\n", total / YEARS);
printf("MONTHLY AVERAGES:\n\n");
printf(" Jan Feb Mar Apr May Jun Jul Aug Sep Oct ");
printf(" Nov Dec\n");
for (month = 0; month < MONTHS; month++)
{ // 每个月,5年的总降水量
for (year = 0, subtot = 0; year < YEARS; year++)
subtot += rain[year][month];
printf("%4.1f ", subtot / YEARS);
}
printf("\n");
return 0;
}
输出结果:
程序清单10.8,pnt_add.c:
// pnt_add.c -- 指针地址
#include <stdio.h>
#define SIZE 4
int main(void)
{
short dates[SIZE];
short * pti;
short index;
double bills[SIZE];
double * ptf;
pti = dates; // 把数组地址赋给指针
ptf = bills;
printf("%23s %15s\n", "short", "double");
for (index = 0; index < SIZE; index++)
printf("pointers + %d: %10p %10p\n", index, pti + index, ptf + index);
return 0;
}
输出结果:
程序清单10.8,day_mon3.c:
/* day_mon3.c -- uses pointer notation */
#include <stdio.h>
#define MONTHS 12
int main(void)
{
int days[MONTHS] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int index;
for (index = 0; index < MONTHS; index++)
printf("Month %2d has %d days.\n", index + 1,
*(days + index));
return 0;
}
输出结果:
程序清单10.10,sum_arr1.c:
// sum_arr1.c -- 数组元素之和
// 如果编译器不支持 %zd, 用 %u 或 %lu 替换它
#include <stdio.h>
#include <ctype.h>
#define SIZE 10
int sum(int ar[], int n);
int main(void)
{
int marbles[SIZE] = { 20, 10, 5, 39, 4, 16, 19, 26, 31, 20 };
long answer;
answer = sum(marbles, SIZE);
printf("The total number of marbles is %ld.\n", answer);
printf("The size of marbles is %zd bytes.\n",
sizeof marbles);
return 0;
}
int sum(int ar[], int n) // 这个数组的大小是?
{
int i;
int total = 0;
for (i = 0; i < n; i++)
total += ar[i];
printf("The size of ar is %zd bytes.\n", sizeof ar);
return total;
}
输出结果:
程序清单10.11,sum_arr2.c:
/* sum_arr2.c -- 数组元素之和 */
#include <stdio.h>
#define SIZE 10
int sump(int * start, int * end);
int main(void)
{
int marbles[SIZE] = { 20, 10, 5, 39, 4, 16, 19, 26, 31, 20 };
long answer;
answer = sump(marbles, marbles + SIZE);
printf("The total number of marbles is %ld.\n", answer);
return 0;
}
/* 使用指针算法 */
int sump(int * start, int * end) //或者int sum(int start[], int * end)
{
int total = 0;
while (start < end)
{
total += *start; // 把数组元素的值加起来
start++; // 让指针指向下一个元素
}
return total;
}
输出结果:
程序清单10.12,order.c:
/* order.c -- 指针运算中的优先级 */
#include <stdio.h>
int data[2] = { 100, 200 };
int moredata[2] = { 200, 400 };
int main(void)
{
int * p1, *p2, *p3;
p1 = p2 = data;
p3 = moredata;
printf(" *p1 = %d, *p2 = %d, *p3 = %d\n", *p1, *p2, *p3);
printf("*p1++ = %d, *++p2 = %d, (*p3)++ = %d\n", *p1++, *++p2, (*p3)++);
printf(" *p1 = %d, *p2 = %d, *p3 = %d\n", *p1, *p2, *p3);
return 0;
}
输出结果:
程序清单10.13,ptr_ops.c:
// ptr_ops.c -- 指针操作
#include <stdio.h>
int main(void)
{
int urn[5] = { 100, 200, 300, 400, 500 };
int * ptr1, * ptr2, * ptr3;
ptr1 = urn; //把第一个地址赋给指针
ptr2 = &urn[2]; //把一个地址赋给指针
//解引用指针,以及获得指针的地址
printf("pointer value, dereferenced pointer, poiner adress:\n");
printf("ptr1 = %p, *ptr1 = %d, &ptr1 = %p\n", ptr1, *ptr1, &ptr1);
//指针加法
ptr3 = ptr1 + 4;
printf("\nadding an int to a pointer:\n");
printf("ptr1 + 4 = %p, *(ptr1 + 4) = %d\n", ptr1 + 4, *(ptr1 + 4));
ptr1++; //递增指针
printf("\nvalue after ptr1++:\n");
printf("ptr1 = %p, *ptr1 = %d, &ptr1 = %p\n", ptr1, *ptr1, &ptr1);
ptr2--; //递减指针
printf("\nvalues after ptr--:\n");
printf("ptr2 = %p, *ptr2 = %d, &ptr2 = %p\n", ptr2, *ptr2, &ptr2);
--ptr1; //恢复为初始值
++ptr2; //恢复为初始值
printf("\nPointers reset to original values:\n");
printf("ptr1 = %p, ptr2 = %p\n", ptr1, ptr2);
//一个指针减去另一个指针
printf("\nsubtracting one pointer from another:\n");
printf("ptr2 = %p, ptr1 = %p, ptr2 - ptr1 = %zd\n", ptr2, ptr1, ptr2 - ptr1);
//一个指针减去一个整数
printf("\nsubtracting an int from a pointer:\n");
printf("ptr3 = %p, ptr3 - 2 = %p\n", ptr3, ptr3 - 2);
return 0;
}
输出结果:
程序清单10.14,arf.c:
/* arf.c -- 处理数组的函数 */
#include <stdio.h>
#define SIZE 5
void show_array(const double ar[], int n);
void mult_array(double ar[], int n, double mult); // 将数组所有值乘以mult
int main(void)
{
double dip[SIZE] = { 20.0, 17.66, 8.2, 15.3, 22.22 };
printf("The original dip array:\n");
show_array(dip, SIZE);
mult_array(dip, SIZE, 2.5);
printf("The dip array after calling mult_array():\n");
show_array(dip, SIZE);
return 0;
}
void show_array(const double ar[], int n)
{
int i;
for (i = 0; i < n; i++)
{
printf("%8.3f ", ar[i]);
//printf("%8.3f ", *(ar + i));
}
putchar('\n');
}
void mult_array(double ar[], int n, double mult)
{
int i;
for (i = 0; i < n; i++)
ar[i] *= mult;
}
输出结果:
程序清单10.15,zippol.c:
/* zippol.c -- zippo 的相关信息 */
#include <stdio.h>
int main(void)
{
int zippo[4][2] = { { 2, 4 }, { 6, 8 }, { 1, 3 }, { 5, 7 } };
printf(" zippo = %p, zippo + 1 = %p\n", zippo, zippo + 1);
printf("zippo[0] = %p, zippo[0] + 1 = %p\n", zippo[0], zippo[0] + 1);
printf(" *zippo = %p, *zippo + 1 = %p\n", *zippo, *zippo + 1);
printf("zippo[0][0] = %d\n", zippo[0][0]);
printf(" *zippo[0] = %d\n", *zippo[0]);
printf(" **zippo = %d\n", **zippo);
printf(" zippo[2][1] = %d\n", zippo[2][1]);
printf("*(*(zippo + 2) + 1) = %d\n", *(*(zippo + 2) + 1));
return 0;
}
输出结果:
程序清单10.16,zippo2.c:
/* zippo2.c -- 通过指针获取zippo 的信息 */
#include <stdio.h>
int main(void)
{
int zippo[4][2] = { { 2, 4 }, { 6, 8 }, { 1, 3 }, { 5, 7 } };
int (*pz) [2]; // 数组的指针
pz = zippo;
printf("&zippo = %p\n", &zippo);
printf("&zippo[0] = %p\n", &zippo[0]);
printf(" zippo = %p, zippo + 1 = %p\n", zippo, zippo + 1);
printf("zippo[0] = %p, zippo[0] + 1 = %p\n", zippo[0], zippo[0] + 1);
printf(" *zippo = %p, *zippo + 1 = %p\n", *zippo, *zippo + 1);
printf("zippo[0][0] = %d\n", zippo[0][0]);
printf(" *zippo[0] = %d\n", *zippo[0]);
printf(" **zippo = %d\n", **zippo);
printf(" zippo[2][1] = %d\n", zippo[2][1]);
printf("*(*(zippo + 2) + 1) = %d\n\n\n", *(*(zippo + 2) + 1));
printf("&pz = %p\n", &pz);
printf(" pz = %p, pz + 1 = %p\n", pz, pz + 1);
printf("pz[0] = %p, pz[0] + 1 = %p\n", pz[0], pz[0] + 1);
printf(" *pz = %p, *pz + 1 = %p\n", *pz, *pz + 1);
printf("pz[0][0] = %d\n", pz[0][0]);
printf(" *pz[0] = %d\n", *pz[0]);
printf(" **pz = %d\n", **pz);
printf(" pz[2][1] = %d\n", pz[2][1]);
printf("*(*(pz + 2) + 1) = %d\n", *(*(pz + 2) + 1));
return 0;
}
输出结果:
程序清单10.17,array2d.c:
// array2d.c -- 处理二维数组的函数
#include <stdio.h>
#define ROWS 3
#define COLS 4
void sum_rows(int ar[][COLS], int rows);
void sum_cols(int [][COLS], int); // 省略形参名
int sum2d(int (*ar)[COLS], int rows); // 另一种语法
int main(void)
{
int junk[ROWS][COLS] = {
{ 2, 4, 6, 8 },
{ 3, 5, 7, 9 },
{ 12, 10, 8, 6}
};
sum_rows(junk, ROWS);
sum_cols(junk, ROWS);
printf("Sum of all elements = %d\n", sum2d(junk, ROWS));
return 0;
}
void sum_rows(int ar[][COLS], int rows)
{
int r;
int c;
int tot;
for (r = 0; r < rows; r++)
{
tot = 0;
for (c = 0; c < COLS; c++)
tot += ar[r][c];
printf("row %d: sum = %d\n", r, tot);
}
}
void sum_cols(int ar[][COLS], int rows)
{
int r;
int c;
int tot;
for (c = 0; c < COLS; c++)
{
tot = 0;
for (r = 0; r < rows; r++)
tot += ar[r][c];
printf("col %d: sum = %d\n", c, tot);
}
}
int sum2d(int ar[][COLS], int rows)
{
int r;
int c;
int tot = 0;
for (r = 0; r < rows; r++)
for (c = 0; c < COLS; c++)
tot += ar[r][c];
return tot;
}
输出结果:
程序清单10.18,vararr2d.c:
// vararr2d.c -- 使用变长数组的函数
#include <stdio.h>
#define ROWS 3
#define COLS 4
int sum2d(int rows, int cols, int ar[rows][cols]);
int main(void)
{
int i, j;
int rs = 3;
int cs = 10;
int junk[ROWS][COLS] = {
{ 2, 4, 6, 8 },
{ 3, 5, 7, 9 },
{ 12, 10, 8, 6}
};
int morejunk[ROWS - 1][COLS + 2] = {
{ 20, 30, 40, 50, 60, 70 },
{ 5, 6, 7, 8, 9, 10 }
};
int varr[rs][cs]; // 变长数组(VLA)
for (i = 0; i < rs; i++)
for (j = 0; j <cs; j++)
varr[i][j] = i * j + j;
printf("3x5 array\n");
printf("Sum of all elements = %d\n", sum2d(ROWS, COLS, junk));
printf("2x6 array\n");
printf("Sum of all elements = %d\n", sum2d(ROWS - 1, COLS + 2, morejunk));
printf("3x10 array\n");
printf("Sum of all elements = %d\n", sum2d(rs, cs, varr));
return 0;
}
// 带变长数组形参的函数
int sum2d(int rows, int cols, int ar[rows][cols])
{
int r;
int c;
int tot = 0;
for (r = 0; r < rows; r++)
for (c = 0; c < cols; c++)
tot += ar[r][c];
return tot;
}
输出结果:
程序清单10.19,flc.c:
// flc.c -- 有趣的常量
#include <stdio.h>
#define COLS 4
int sum2d(const int ar[][COLS], int rows);
int sum(const int ar[], int n);
int main(void)
{
int total1, total2, total3;
int * pt1;
const int (*pt2)[COLS];
pt1 = (int[2]) { 10, 20 };
pt2 = (const int[2][COLS]) { { 1, 2, 3, -9 }, { 4, 5, 6, -8 } };
total1 = sum(pt1, 2);
total2 = sum2d(pt2, 2);
total3 = sum((int []){ 4, 4, 4, 5, 5, 5}, 6);
printf("total1 = %d\n", total1);
printf("totll2 = %d\n", total2);
printf("total3 = %d\n", total3);
return 0;
}
int sum(const int ar [], int n)
{
int i;
int total = 0;
for (i = 0; i < n; i++)
total += ar[i];
return total;
}
int sum2d(const int ar [][COLS], int rows)
{
int r;
int c;
int tot = 0;
for (r = 0; r < rows; r++)
for (c = 0; c < COLS; c++)
tot += ar[r][c];
return tot;
}
输出结果:
编程练习
题目1,方法:用指针计算rain.c,示例代码10_1.c:
#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;
printf(" YEAR RAINFALL (inches)\n");
for (year = 0, total = 0; year < YEARS; year++)
{
for (month = 0, subtot = 0; month < MONTHS; month++)
subtot += *(*(rain + year) + month);
printf("%5d %15.1f\n", 2010 + year, subtot);
total += subtot;
}
printf("\nThe yearly average is %.1f inches.\n\n", total / YEARS);
printf("MONTHLY AVERAGES:\n\n");
printf(" Jan Feb Mar Apr May Jun Jul Aug Sep Oct ");
printf(" Nov Dec\n");
for (month = 0; month < MONTHS; month++)
{
for (year = 0, subtot = 0; year < YEARS; year++)
subtot += *(*(rain + year) + month);
printf("%4.1f ", subtot / YEARS);
}
printf("\n");
return 0;
}
输出结果:
题目2,方法:用不同的方法拷贝数组。示例代码10_2.c:
#include <stdio.h>
void copy_arr(double ar[], const double original[], int n);
void copy_ptr(double * ar, const double * original, int n);
void copy_ptrs(double * ar, const double * original, const double * end);
void show_arr(const double [], int n);
int main(void)
{
double source[5] = { 1.1, 2.2, 3.3, 4.4, 5.5 };
double target1[5];
double target2[5];
double target3[5];
printf("Source array:\n");
show_arr(source, 5);
printf("Target1 array:\n");
copy_arr(target1, source, 5);
show_arr(target1, 5);
printf("Target2 array:\n");
copy_ptr(target2, source, 5);
show_arr(target2, 5);
printf("Target3 array:\n");
copy_ptrs(target3, source, source + 5);
show_arr(target3, 5);
return 0;
}
void copy_arr(double ar[], const double original[], int n)
{
int index;
for (index = 0; index < n; index++)
ar[index] = original[index];
}
void copy_ptr(double * ar, const double * original, int n)
{
int index;
for (index = 0; index < n; index++)
*(ar + index) = *(original + index);
}
void copy_ptrs(double * ar, const double * original, const double * end)
{
int i;
for (i = 0; i + original < end; i++)
*(ar + i) = *(original + i);
}
void show_arr(const double ar[], int n)
{
int index;
for (index = 0; index < n; index++)
printf("%-4g", ar[index]);
putchar('\n');
}
输出结果:
题目3,方法:求一个int类型数组中的最大值。示例代码10_3.c:
#include <stdio.h>
int int_max(int * ar, int n);
int main(void)
{
int a[] = { 1, 3, 3, 4, 9 };
int n;
n = sizeof a / sizeof a[0];
printf("The array has %d of elements.\n", n);
printf("The max num in the array is:%d\n", int_max(a, n));
printf("&a = %p\n", &a);
printf("a = %p\n", a);
return 0;
}
int int_max(int * ar, int n)
{
int index, max;
printf("&ar = %p\n", &ar);
printf("ar = %p\n", ar);
max = ar[0];
for (index = 0; index < n; index++)
{
if (ar[index] > max)
max = ar[index];
}
return max;
}
输出结果:
题目4,方法:求出数组中最大值的下标。示例代码10_4.c:
#include <stdio.h>
int subscript(double *, int n);
int main(void)
{
int num;
double a[] = { 1.1, 3.4, 5.3, 2.1 };
num = sizeof a / sizeof a[0];
printf("The subscript of the max number of the array a is %d\n", subscript(a, num));
return 0;
}
int subscript(double * ar, int n)
{
int i, subid;
double max;
max = *ar;
for (i = 0; i < n; i++)
{
if (*(ar + i) > max)
{
max = *(ar + i);
subid = i;
}
}
return subid;
}
输出结果:
题目5,方法:计算数组中最大值和最小值的差值。示例代码10_5.c:
#include <stdio.h>
double diference(double *, int n);
int main(void)
{
int num;
double a[] = { 1.1, 3.4, 5.3, 0.5 };
num = sizeof a / sizeof a[0];
printf("The diference of the max number and min number in the array a is %g\n", diference(a, num));
return 0;
}
double diference(double * ar, int n)
{
int i;
double max, min;
max = min = *ar;
for (i = 0; i < n; i++)
{
if (*(ar + i) > max)
max = *(ar + i);
if (*(ar + i) < min)
min = *(ar + i);
}
return max - min;
}
输出结果:
题目6,方法:倒序排列数组元素。示例代码10_6.c:
#include <stdio.h>
void reverse(double *, int n);
int main(void)
{
int num, i;
double a[] = { 1.1, 2.2, 3.3, 4.4 };
num = sizeof a / sizeof a[0];
reverse(a, num);
printf("After reverse the array is:\n");
for (i = 0; i < num; i++)
printf("%g ", *(a + i));
return 0;
}
void reverse(double * ar, int n)
{
double item;
int i;
for (i = 0; i < n / 2; i++)
{
item = *(ar + i);
*(ar + i) = *(ar + n - 1 - i);
*(ar + n - 1 - i) = item;
}
}
输出结果:
题目7,方法:拷贝二维数组。示例代码10_7.c:
#include <stdio.h>
#define ROWS 3
#define COLS 4
void copy_ptr(double * ar, const double * original, int n);
void show_arr(int m, int n, double (*)[n]);
int main(void)
{
double source[ROWS][COLS] = {
{ 2.3, 4.5, 6.7, 8.6 },
{ 3.3, 5.2, 7.1, 9.9 },
{ 12.3, 10.0, 8.4, 6.5}
};
double target[ROWS][COLS];
int i;
for (i = 0; i < ROWS; i++)
copy_ptr(*(target + i), *(source + i), COLS);
printf("After copy,target is:\n");
show_arr(ROWS, COLS, target);
return 0;
}
void copy_ptr(double * ar, const double * original, int n)
{
int index;
for (index = 0; index < n; index++)
*(ar + index) = *(original + index);
}
void show_arr(int m, int n, double (*ptr)[n])
{
int i, j;
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
printf("%g ", *(*(ptr + i) + j));
putchar('\n');
}
}
输出结果:
题目8,方法:拷贝数字到数组中。示例代码10_8.c:
#include <stdio.h>
void copy_arr(double ar[], const double original[], int n);
void show_arr(const double [], int n);
int main(void)
{
double a[] = { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7 };
double b[3];
copy_arr(b, a + 2, 3);
printf("After copy, b is:\n");
show_arr(b, 3);
return 0;
}
void copy_arr(double * ar, const double * original, int n)
{
int index;
for (index = 0; index < n; index++)
*(ar + index) = *(original + index);
}
void show_arr(const double ar[], int n)
{
int index;
for (index = 0; index < n; index++)
printf("%g ", ar[index]);
putchar('\n');
}
输出结果:
题目9,方法:用变长数组拷贝二维数组。示例代码10_9.c:
#include <stdio.h>
void copy2d(int m, int n, const double (*)[n], double (*)[n]);
void show2d(int m, int n, const double [][n]);
int main(void)
{
const double source[][5] = {
{1.1, 2.2, 3.3, 4.4, 5.5 },
{5.5, 4.4, 3.3, 2.2, 1.1 },
{1.1, 2.2, 3.3, 4.4, 5.5 }
};
double target[3][5];
printf("The source array is:\n");
show2d(3, 5, source);
printf("The target array is:\n");
copy2d(3, 5, source, target);
show2d(3, 5, target);
return 0;
}
void copy2d(int m, int n, const double (* ar)[n], double (* tr)[n])
{
int i, j;
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
*(*(tr + i) + j) = *(*(ar + i) + j);
}
}
void show2d(int m, int n, const double ar[][n])
{
int i, j;
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
printf("%g ", ar[i][j]);
putchar('\n');
}
}
输出结果:
题目10,方法:将两个数组对应元素之和相加并存储到第三个数组中。示例代码10_10.c:
#include <stdio.h>
void add2_ar(const int *, const int *, int [], int n);
void show_ar(const int *, int n);
int main(void)
{
const int a[] = { 2, 4, 5, 8 };
const int b[] = { 1, 0, 4, 6 };
int c[4];
printf("The array a is:\n");
show_ar(a, 4);
printf("The array b is:\n");
show_ar(b, 4);
printf("The array c is:\n");
add2_ar(a, b, c, 4);
show_ar(c, 4);
return 0;
}
void add2_ar(const int * ar, const int * br, int c[], int n)
{
int index;
for (index = 0; index < n; index++)
c[index] = *(ar + index) + *(br + index);
}
void show_ar(const int * ar, int n)
{
int i;
for (i = 0; i < n; i++)
printf("%3d", *(ar + i));
putchar('\n');
}
输出结果:
题目11,方法:将二维数组中的元素的值翻倍。示例代码10_11.c:
#include <stdio.h>
void times2(int m, int n, int (*)[n]);
void show2d(int m, int n, int (*)[n]);
int main(void)
{
int a[][5] = {
{ 1, 2, 3, 4, 5 },
{ 5, 4, 3, 2, 1 },
{ 1, 2, 3, 4, 5 }
};
printf("Before times:\n");
show2d(3, 5, a);
printf("After times:\n");
times2(3, 5, a);
show2d(3, 5, a);
return 0;
}
void times2(int m, int n, int(* ar)[n])
{
int i, j;
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
*(*(ar + i) + j) *= 2;
}
}
void show2d(int m, int n, int(* ar)[n])
{
int i, j;
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
printf("%3d", ar[i][j]);
putchar('\n');
}
}
输出结果:
题目12,方法:用函数改写程序清单10.7.示例代码10_12.c:
#include <stdio.h>
#define MONTHS 12
#define YEARS 5
float get_year(int m, int n, const float (*)[n]);
void get_month(int m, int n, const float (*)[n]);
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 }
};
printf(" YEAR RAINFALL (inches)\n");
printf("\nThe yearly average is %.1f inches.\n\n", get_year(YEARS, MONTHS, rain) / YEARS);
get_month(YEARS, MONTHS, rain);
return 0;
}
float get_year(int m, int n, const float (* ar)[n])
{
int i, j;
float subtot, total;
for (i = 0, total = 0; i < m; i++)
{
for (j = 0, subtot = 0; j < n; j++)
subtot += *(*(ar + i) + j);
printf("%5d %15.1f\n", 2010 + i, subtot);
total += subtot;
}
return total;
}
void get_month(int m, int n, const float (* ar)[n])
{
int i, j;
float subtot;
printf("MONTHLY AVERAGES:\n\n");
printf(" Jan Feb Mar Apr May Jun Jul Aug Sep Oct ");
printf(" Nov Dec\n");
for (i = 0; i < n; i++)
{
for (j = 0, subtot = 0; j < m; j++)
subtot += *(*(ar + j) + i);
printf("%4.1f ", subtot / YEARS);
}
putchar('\n');
}
输出结果:
题目13,14,方法:按要求完成计算(已使用变长数组)。示例代码10_13.c:
#include <stdio.h>
#define ROWS 3
#define COLS 5
void show2d(int m, int n, double [][n]);
void function(int m, int n, double (*)[n]);
int main(void)
{
double a[ROWS][COLS] = { { 0.0 } };
int i, j;
for (i = 0; i < ROWS; i++)
{
printf("Please input %dth of 5 double numbers: ", i + 1);
for (j = 0; j < COLS; j++)
scanf("%lf", *(a + i) + j);
}
printf("The array of a is:\n");
show2d(ROWS, COLS, a);
function(ROWS, COLS, a);
return 0;
}
void show2d(int m, int n, double ar[][n])
{
int i, j;
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
printf("%g ", ar[i][j]);
putchar('\n');
}
}
void function(int m, int n, double (* ar)[n])
{
double average_5, average_all, max;
int i, j;
max = **ar;
for (i = 0, average_all = 0; i < m; i++)
{
for (j = 0, average_5 = 0; j < n; j++)
{
average_5 += *(*(ar + i) + j);
if (ar[i][j] > max)
max = ar[i][j];
}
average_all += average_5;
printf("The average of %dth of 5 numbers is %g\n", i + 1, average_5 / COLS);
}
printf("The average of the array is %g\n", average_all / (ROWS * COLS));
printf("The maximum of the array is %g\n", max);
}
输出结果: