1. 修改程序清单10.7的rain.c程序,用指针进行计算(仍然要声明并初始化数组)
/*
* rain.c
* 计算每年的总降水量、年平均降水量和5年中每月的平均降水量
*/
#include <stdio.h>
#define MONTHS 12
#define YEARS 5
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 } };
void _1(void);
void _2(void);
int main(void) {
_1();
_2();
return 0;
}
void _1(void) {
int year, month;
float subtot, total;
printf(" YEAR RAINFAIL (inches)\n");
for (year = 0, total = 0; year < YEARS; year++) {
for (month = 0, subtot = 0; month < MONTHS; month++) {
subtot += rain[year][month];
}
printf("%6d %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 Fab Mar Apr May Jun Jul Aug Sep Oct Nov Dec\n");
for (month = 0; month < MONTHS; month++) {
for (year = 0, subtot = 0; year < YEARS; year++) {
subtot += rain[year][month];
}
printf("%5.1f ", subtot / YEARS);
}
}
void _2(void) {
int year, month;
float total, subtot;
printf("\n\n YEAR RAINFAIL (inches)\n");
for (year = 0, total = 0; year < YEARS; year++) {
for (month = 0, subtot = 0; month < MONTHS; month++) {
subtot += *(*(rain + year) + month);
}
printf("%6d %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 Fab Mar Apr May Jun Jul Aug Sep Oct Nov Dec\n");
for (month = 0; month < MONTHS; month++) {
for (year = 0, subtot = 0; year < YEARS; year++) {
subtot += *(*(rain + year) + month);
}
printf("%5.1f ", subtot / YEARS);
}
}
2.编写一个程序,初始化一个double类型的数组,然后把该数组的内容拷贝至3个其他数组中(在main()中生命者4个数组)。使用带数组表示法的函数进行第1份拷贝。使用带指针表示法和指针递增的函数进行第2份拷贝。把目标数组名、源数组名和带靠背的元素个数作为前两个函数的参数。第3个函数以目标数组名、源数组名和指向源数组最后一个元素后面的元素的指针。也就是说,给定一下声明,则函数调用如下所示:
double source[5] = {1.1, 2.2, 3.3, 4.4, 5.5};
double target1[5];
double target2[5];
double target3[5];
copy_arr(target1, source, 5);
copy_ptr(target2, source, 5);
copy_ptrs(target3, source, source + 5);
#include <stdio.h>
#define ARR_LEN 5
void copy_arr(double [], double [], int);
void copy_ptr(double [], double [], int);
void copy_ptrs(double [], double [], int *);
int main(void) {
double source[ARR_LEN] = {1.1, 2.2, 3.3, 4.4, 5.5};
double target1[ARR_LEN], target2[ARR_LEN], target3[ARR_LEN];
double source[ARR_LEN] = {1.1, 2.2, 3.3, 4.4, 5.5};
double target1[ARR_LEN];
double target2[ARR_LEN];
double target3[ARR_LEN];
copy_arr(target1, source, ARR_LEN);
copy_ptr(target2, source, ARR_LEN);
copy_ptrs(target3, source, source + ARR_LEN);
return 0;
}
void copy_arr(double target[], double source[], int length) {
int index;
for(index = 0; index < length; index++) {
target[index] = source[index];
printf(" target[%d] = %5.1f", index, target[index]);
}
printf("\n");
}
void copy_ptr(double target[], double source[], int length) {
target = source;
int index;
for(index = 0; index < length; index++){
printf(" target[%d] = %5.1f", index, *(target + index));
}
printf("\n");
}
void copy_ptrs(double target[], double source[], int *ptr) {
for(target = source; target < ptr; target++){
printf(" target[%d] = %5.1f", (target - source), *target);
}
printf("\n");
}
3.编写一个函数,返回储存在int类型数组中的最大值,并在一个简单的程序中测试该函数
#include <stdio.h>
int largestNumberForArr(int [], int);
int largestNumberForPtr(int *, int);
int main(void) {
int source[] = {3, 4, 5, 8, 0, 1, 7, 9, 2, 6};
largestNumberForArr(source, sizeof(source) / sizeof(source[0]));
largestNumberForPtr(source, sizeof(source) / sizeof(source[0]));
return 0;
}
int largestNumberForArr(int source[], int length){
if(length < 1) {
printf("Your Number Array is Null! Return -1!");
return -1;
} else if(length == 1) {
return source[0];
}
int index;
int largestNumber = source[0];
for(index = 1; index < length; index++) {
if(source[index] > largestNumber) {
largestNumber = source[index];
}
}
printf("Calculator The Largest Number By Array Is %d\n", largestNumber);
return largestNumber;
}
int largestNumberForPtr(int *source, int length) {
if(length < 1) {
printf("Your Number Array is Null! Return -1!");
return -1;
} else if(length == 1) {
return *source;
}
int index;
int lm = *source;
for(index = 1; index < length; index++) {
if(*(source + index) > lm) {
lm = *(source + index);
}
}
printf("Calculator The Largest Number By Pointer Is %d\n", lm);
return lm;
}
注:如下的这种方式会出现错误
int largestNumberForError(int source[]) {
int length = sizeof(source) / sizeof(int);
if(length < 1) {
printf("Your Number Array is Null! Return -1!");
return -1;
} else if(length == 1) {
return source[0];
}
int index;
int largestNumber = source[0];
for(index = 1; index < length; index++) {
if(source[index] > largestNumber) {
largestNumber = source[index];
}
}
}
得到的最终结果是返回source[0],原因是,这个方法中的source数组和main函数中的source数组不一样(可以通过打印数组地址来确认),这个函数中的source数组,无法指定长度,定义的是一个指向int型数组的指针,因此,sizeof(source)返回的就是int类型的字节数,因此sizeof(source) / sizeof(int)始终会返回1.
4. 编写一个函数,返回储存在double类型数组中最大值的下标,并在一个简单的程序中测试该函数
#include <stdio.h>
int getLargestIndexForDoubleArray(double [], int);
int getLargestIndexForDoublePointer(double *, int);
int main() {
double source[] = {1.1, 2.3, 4.3, 0.1, 3.7, 8.5, 9.2, 2.2, 3.4};
getLargestIndexForDoubleArray(source, sizeof(source) / sizeof(double));
getLargestIndexForDoublePointer(source, sizeof(source) / sizeof(double));
return 0;
}
int getLargestIndexForDoubleArray(double source[], int length) {
if(length < 1) {
printf("Your Double Array Is Null!Return -1!");
return -1;
} else if(length == 1){
printf("Your Double Array Just Have One Params!");
return 0;
}
int i, index = 0;
double largest = source[0];
for(i = 1; i < length; i++) {
if(largest < source[i]) {
largest = source[i];
index = i;
}
}
printf("The %dth One Is The Largest One!\n", index + 1);
return index;
}
int getLargestIndexForDoublePointer(double *source, int length) {
if(length < 1) {
printf("Your Double Array Is Null!Return -1!");
return -1;
} else if(length == 1){
printf("Your Double Array Just Have One Params!");
return 0;
}
int i, index = 0;
double largest = *source;
for( i = 1; i < length; i++) {
if(largest < *(source + i)) {
largest = *(source + i);
index = i;
}
}
printf("The %dth One Is The Largest One!\n", index + 1);
return index;
}
5. 编写一个函数,返回储存在double类型数组中最大值和最小值的差值,并在一个简单的程序中测试该函数
#include <stdio.h>
double dividerByLargestAndLeast(double [], int);
int main(){
double source[] = {1.1, 2.3, 4.3, 0.1, 3.7, 8.5, 9.2, 2.2, 3.4};
dividerByLargestAndLeast(source, sizeof(source) / sizeof(double));
return 0;
}
double dividerByLargestAndLeast(double source[], int length) {
if(length < 1) {
printf("Your Double Array Is Null!Return -1!");
return -1;
} else if(length == 1){
printf("Your Double Array Just Have One Params!");
return 0;
}
double largest = source[0], least = source[0];
int index;
for(index = 1; index < length; index++) {
if(source[index] > largest) {
largest = source[index];
}
if(least > source[index]){
least = source[index];
}
}
printf("the diminition of maxiuim and miniuim is %f\n", (largest - least));
return (largest - least);
}
6. 编写一个函数,把double类型数组中的数据倒序排列,并在一个简单的程序中测试该函数
#include <stdio.h>
void sort(double [], int);
void printPositive(double [], int);
int main(){
double source[] = {1.1, 2.3, 4.3, 0.1, 3.7, 8.5, 9.2, 2.2, 3.4, 7.3};
printPositive(source, sizeof(source) / sizeof(double));
sort(source, sizeof(source) / sizeof(double));
printPositive(source, sizeof(source) / sizeof(double));
return 0;
}
void printPositive(double * source, int length) {
int index;
for(index = 0; index < length; index++) {
printf(" %.1f ", *(source + index));
}
printf("\n");
}
void sort(double *source, int length) {
int index;
for(index = 0; index < length / 2; index++){
double temp;
temp = *(source + index);
*(source + index) = *(source + length - 1 - index);
*(source + length - 1 - index) = temp;
}
}
7. 编写一个程序,初始化一个double类型的二维数组,使用编程练习2中的一个拷贝函数把该数组中的数据拷贝至另一个二维数组中(因为二维数组是数组的数组,所以可以使用处理一位数组的拷贝函数来处理数组中的每个子数组)
#include <stdio.h>
#define WIDTH 5
#define HEIGHT 5
void copy_arr(double [], double [], int);
void print_arr(int, int, double [*][*]);
void copy_ptr(double *, double *, int);
void print_ptr(int, int height, double (*)[height]);
int main(void) {
double source[WIDTH][HEIGHT] = {
{ 1.1, 2.2, 3.3, 5.2, 4.3},
{ 4.4, 5.5, 6.6, 4.2, 2.4},
{ 7.7, 8.8, 9.9, 5.4, 3.2},
{ 1.4, 4.2, 1.3, 1.2, 4.0},
{ 1.2, 7.2, 9.3, 0.2, 4.8}
};
double target[WIDTH][HEIGHT];
int index;
for(index = 0; index < WIDTH; index++) {
copy_arr(target[index], source[index], HEIGHT);
}
print_arr(WIDTH, HEIGHT, target);
for(index = 0; index < WIDTH; index++) {
copy_ptr(target[index], source[index], HEIGHT);
}
print_ptr(WIDTH, HEIGHT, target);
return 0;
}
void copy_arr(double target[], double source[], int length) {
int index;
for(index = 0; index < length; index++) {
target[index] = source[index];
}
}
void print_arr(int width, int height, double target[width][height]) {
int w, h;
for(w = 0; w < width; w++) {
for(h = 0; h < height; h++) {
printf("target[%d][%d] = %5.1f\n", w, h, target[w][h]);
}
}
printf("\n");
}
void copy_ptr(double *target, double *source, int length) {
int index;
for(index = 0; index < length; index++) {
*(target + index) = *(source + index);
}
}
void print_ptr(int width, int height, double (*target)[height]) {
int w, h;
for(w = 0; w < width; w++) {
for(h = 0; h < height; h++) {
printf("target[%d][%d] = %5.1f\n", w, h, *(*(target + w) + h));
}
}
printf("\n");
}
注:这个问题卡了好久,主要的问题是,定义使用指针函数copy_ptr和print_ptr函数,之前的写法是
copy_ptr(double **target, int width, int height);
print_ptr(double **target, int width, int height);
这种写法是错误的,从网上找的一些资料,然后结合自己的思考得出问题出现的原因为:
定义函数的时候,target为重新定义的二维数组,而定义二维数组的时候,一定要指定其第二个位数,即本例子中的每一行包含的元素个数,而上述错误写法中,并未指定行和列,因此,产生错误
另由于需要定义函数为通用类型,因此需要使用变长数组来处理本例,因此得出如上正确的写法
8.使用编程练习2中的拷贝函数,,把一个内含7个元素的数组中的第3~第5个元素拷贝至内含3个元素的数组中。该函数本身不需要修改,只需要选择合适的实际参数(实际参数不需要是数组名和数组大小,只需要是数组元素的地址和待处理元素的个数)
#include <stdio.h>
#define SOURCE_LENGTH 7
#define TARGET_LENGTH 3
void copy_ptr(double *, double *, int);
void print_ptr(int , double *);
int main(void) {
double source[SOURCE_LENGTH] = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7};
double target[TARGET_LENGTH];
copy_ptr(target, source + 3, TARGET_LENGTH);
print_ptr(TARGET_LENGTH, target);
return 0;
}
void copy_ptr(double *target, double *source, int length) {
int index;
for(index = 0; index < length; index++) {
*(target + index) = *(source + index);
}
}
void print_ptr(int length, double target[]) {
int index;
for(index = 0; index < length; index++) {
printf("target[%d] = %5.1f\n", index, *(target + index));
}
}
9.编写一个程序,初始化一个double类型的3x5二维数组,使用一个处理变长数组的函数将其拷贝至另一个二维数组中。还要编写一个以变长数组为形参的函数以显示两个数组的内容,这两个函数应该能处理任一N x M数组(如果编译器不支持变长数组,就是用传统C函数处理Nx5的数组)
#include <stdio.h>
#define WIDTH 3
#define HEIGHT 5
void copy_arr(int, int, double[*][*], double[*][*]);
void print_arr(int, int, double[*][*], double[*][*]);
void copy_ptr(int, int height, double (*)[height], double (*)[height]);
void print_ptr(int, int height, double (*)[height], double (*)[height]);
int main() {
double source[WIDTH][HEIGHT] = {
{ 1.1, 3.2, 4.3, 5.4, 6.5 },
{ 3.1, 4.2, 5.3, 6.4, 7.5 },
{ 4.1, 5.2, 6.3, 7.4, 8.5 }
};
double target[WIDTH][HEIGHT];
double target2[WIDTH][HEIGHT];
copy_arr(WIDTH, HEIGHT, target, source);
print_arr(WIDTH, HEIGHT, target, source);
copy_ptr(WIDTH, HEIGHT, target2, source);
print_ptr(WIDTH, HEIGHT, target2, source);
return 0;
}
void copy_arr(int width, int height, double target[width][height],
double source[width][height]) {
int w, h;
for (w = 0; w < width; w++) {
for (h = 0; h < height; h++) {
target[w][h] = source[w][h];
}
}
}
void print_arr(int width, int height, double target[width][height],
double source[width][height]) {
int w, h;
for (w = 0; w < width; w++) {
for (h = 0; h < height; h++) {
printf("target[%d][%d] = %5.1f\t\tsource[%d][%d] = %5.1f\n", w, h,
target[w][h], w, h, source[w][h]);
}
}
printf("\n");
}
void copy_ptr(int width, int height, double (*target)[height], double (*source)[height]) {
int w, h;
for (w = 0; w < width; w++) {
for (h = 0; h < height; h++) {
*(*(target + w) + h) = *(*(source + w) + h);
}
}
}
void print_ptr(int width, int height, double (*target)[height], double (*source)[height]) {
int w, h;
for (w = 0; w < width; w++) {
for (h = 0; h < height; h++) {
printf("target[%d][%d] = %5.1f\t\tsource[%d][%d] = %5.1f\n", w, h, *(*(target + w) + h), w, h, *(*(source + w) + h));
}
}
printf("\n");
}
10.编写一个函数,把两个数组中相对应的元素相加,然后把结果储存到第3个数组中。也就是说,如果数组1中包含的值是2、4、5、8,数组2中包含的值是1、0、4、6,那么该函数把3、4、9、14赋给第3个数组。函数接收3个数组名和一个数组大小。在一个简单的程序中测试该函数
#include <stdio.h>
#define LENGTH 5
void sumArr(int [], int [], int [], int);
void sumArrForPtr(int *, int *, int *, int);
int main(void) {
int source1[LENGTH] = {1, 2, 3, 4, 5};
int source2[LENGTH] = {5, 6, 7, 8, 9};
int target1[LENGTH];
int target2[LENGTH];
sumArr(source1, source2, target1, LENGTH);
sumArrForPtr(source1, source2, target2, LENGTH);
return 0;
}
void sumArr(int source1[], int source2[], int target[], int length) {
int index;
for(index = 0; index < length; index++) {
target[index] = source1[index] + source2[index];
printf("target[%d] = %d \n", index, target[index]);
}
printf("\n");
}
void sumArrForPtr(int *source1, int *source2, int *target, int length) {
int index;
for(index = 0; index < length; index++) {
*(target + index) = *(source1 + index) + *(source2 + index);
printf("*(target + %d) = %d\n", index, *(target + index));
}
printf("\n");
}
11.编写一个程序,声明一个int类型的3x5二维数组,并用合适的值初始化它。该程序打印数组中的值,然后各值翻倍(即是原值的2倍),并显示出各元素的新值。编写一个函数显示数组的内容,再编写一个函数把各元素的值翻倍。这两个函数都以函数名和行数作为参数。
#include <stdio.h>
#define WIDTH 3
#define HEIGHT 5
void print_arr(int, int, int [*][*]);
void print_twice_arr(int, int, int [*][*]);
void print_ptr(int, int height, int (*)[height]);
void print_twice_ptr(int, int height, int (*)[height]);
int main(void) {
int source[WIDTH][HEIGHT] = {
{3, 1, 4, 5, 7},
{2, 3, 4, 1, 7},
{5, 8, 0, 2, 6}
};
print_arr(WIDTH, HEIGHT, source);
print_twice_arr(WIDTH, HEIGHT, source);
print_ptr(WIDTH, HEIGHT, source);
print_twice_ptr(WIDTH, HEIGHT, source);
return 0;
}
void print_arr(int width, int height, int source[width][height]) {
int w, h;
for(w = 0; w < width; w++) {
for(h = 0; h < height; h++){
printf("source[%d][%d] = %d\n", w, h, source[w][h]);
}
}
printf("\n");
}
void print_twice_arr(int width, int height, int source[width][height]) {
int w, h;
for(w = 0; w < width; w++) {
for(h = 0; h < height; h++){
printf("source[%d][%d] = %d\n", w, h, source[w][h] * 2);
}
}
printf("\n");
}
void print_ptr(int width, int height, int (*source)[height]) {
int w, h;
for(w = 0; w < width; w++) {
for(h = 0; h < height; h++){
printf("source[%d][%d] = %d\n", w, h, *(*(source + w) + h));
}
}
printf("\n");
}
void print_twice_ptr(int width, int height, int (*source)[height]) {
int w, h;
for(w = 0; w < width; w++) {
for(h = 0; h < height; h++){
printf("source[%d][%d] = %d\n", w, h, (*(*(source + w) + h)) * 2);
}
}
printf("\n");
}
12.重写程序清单10.7的rain.c程序,把main()中的主要任务都改成用函数来完成
参见第1题
13.编写一个程序,提示用户输入3组数,每组数包含5个double类型的数(假设用户都正确地响应,不会输入非数值数据)。该程序应完成下列任务
a.把用户输入的数据储存在3x5的数组中
b.计算每组(5个)数据的平均值
c.计算所有数据的平均值
d.找出这15个数据中的最大值
e.打印结果
每个任务都要用单独的函数来完成(使用传统C处理数组的方式)。完成任务b,要编写一个计算并返回一维数组平均值的函数,利用循环调用该函数3次。对于处理其他任务的函数,应该把整个数组作为参数,完成任务c和d的函数应把结果返回主调函数。
#include <stdio.h>
#define WIDTH 3
#define HEIGHT 5
void scanf_arr(double target[], int height);
double average_line(double target[], int length);
double average_all(int width, int height, double target[width][height]);
int max_all(int width, int height, double target[width][height]);
void print_arr(int width, int height, double target[width][height]);
int main(void) {
double target[WIDTH][HEIGHT];
int width;
for(width = 0; width < WIDTH; width++) {
scanf_arr(target[width], HEIGHT);
}
int index;
double average;
for(index = 0;index < WIDTH; index++) {
average = average_line(target[index], HEIGHT);
printf("The Average Of Line %d is %5.1f\n", index, average);
}
average = average_all(WIDTH, HEIGHT, target);
printf("The Average Of All Array is %5.1f\n", average);
max_all(WIDTH, HEIGHT, target);
print_arr(WIDTH, HEIGHT, target);
}
void scanf_arr(double target[], int height) {
int index;
printf("Please Input 5 Double Numbers:\n");
for(index = 0; index < height; index++){
scanf("%lf", &target[index]);
}
}
double average_line(double target[], int length) {
int index;
double sum, average;
for(index = 0; index < length; index++){
sum += target[index];
}
average = sum / length;
return average;
}
double average_all(int width, int height, double target[width][height]) {
int w, h;
double sum, average;
for(w = 0; w < width; w++) {
for(h = 0; h < height; h++) {
sum += target[w][h];
}
}
average = sum / (width * height);
return average;
}
int max_all(int width, int height, double target[width][height]) {
int w, h;
double maxNumber;
int indexWidth = 0, indexHeight = 0;
maxNumber = target[0][0];
for(w = 0; w < width; w++) {
for(h = 0; h < height; h++) {
if(maxNumber < target[w][h]) {
maxNumber = target[w][h];
indexWidth = w;
indexHeight = h;
}
}
}
printf("The Max Number of This Array is target[%d][%d] = %5.1f\n", indexWidth, indexHeight, maxNumber);
return maxNumber;
}
void print_arr(int width, int height, double target[width][height]) {
int w, h;
for(w = 0; w < width; w++) {
for(h = 0; h < height; h++) {
printf(" target[%d][%d] = %5.1f", w, h, target[w][h]);
}
printf("\n");
}
printf("\n");
}
14.以变长数组作为函数形参,完成编程练习13
参见13题