1、修改程序清单10.7中的程序rain,使它不使用数组下标,而是使用指针进行计算(程序中仍然需要声明并初始化数组)。
#include <stdio.h>
#define MONTHS 12 // number of months in a year
#define YEARS 5 // number of years of data
int main(void)
{
// initializing rainfall data for 2000 - 2004
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 each year, sum rainfall for each month
for (month = 0, subtot = 0; month < MONTHS; month++)
subtot += *( *(rain + year) + month );
printf("%5d %15.1f\n", 2000 + year, subtot);
total += subtot; // total for all years
}
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 each month, sum rainfall over years
for (year = 0, subtot =0; year < YEARS; year++)
subtot += *( *(rain + year) + month );
printf("%4.1f ", subtot/YEARS);
}
printf("\n");
return 0;
}
2、编写一个程序,初始化一个double数组,然后把数组内容复制到另外两个数组(3个数组都需要在主程序中声明)。制作第一份拷贝的函数使用数组符号。制作第二份拷贝的函数使用指针符号,并使用指针的增量操作。把目标数组名和要复制的元素数目做为参数传递给函数。也就是说,如果给定了下列声明,函数调用应该如下面所示:
double source [5]={1.1, 2.2, 3.3, 4.4, 5.5};
double targetl[5];
double target2 [5];
copy_arr (source, target1, 5);
copy_ptr (source, target1,5);
#include <stdio.h>
void copy_arr(double [], double [], int );
void copy_ptr(double *, double *, int );
int main(void)
{
double source[] = {1.1, 2.2, 3.3, 4.4, 5.5};
double target1[5] = {0};
double target2[5] = {0};
printf("Before operation:\n");
printf("source :\t%g\t%g\t%g\t%g\t%g\n",source[0],source[1],source[2],source[3],source[4]);
printf("target1:\t%g\t%g\t%g\t%g\t%g\n",target1[0],target1[1],target1[2],target1[3],target1[4]);
printf("target2:\t%g\t%g\t%g\t%g\t%g\n",target2[0],target2[1],target2[2],target2[3],target2[4]);
copy_arr(source, target1, 5);
copy_ptr(source, target2, 5);
printf("After operation:\n");
printf("source :\t%g\t%g\t%g\t%g\t%g\n",source[0],source[1],source[2],source[3],source[4]);
printf("target1:\t%g\t%g\t%g\t%g\t%g\n",target1[0],target1[1],target1[2],target1[3],target1[4]);
printf("target2:\t%g\t%g\t%g\t%g\t%g\n",target2[0],target2[1],target2[2],target2[3],target2[4]);
return 0;
}
void copy_arr(double a1[], double a2[], int n)
{
int i;
for (i=0; i<n; i++)
a2[i] = a1[i];
}
void copy_ptr(double *p1, double *p2, int n)
{
int i;
for (i=0; i<n; i++)
*( p2 + i ) = *( p1 + i );
}
3、编写一个函数,返回一个int数组中存储的最大数值,并在一个简单的程序中测试这个函数。
#include <stdio.h>
#define WIDTH 6
int max(int [], int );
int main(void)
{
int array[] = {4,3,6,2,8,6};
printf("The max is: %d\n",max( array, WIDTH));
return 0;
}
int max(int a[], int n)
{
int i,max;
for (i = 1, max = a[0]; i < n; i++)
if (max < a[i])
max = a[i];
return max;
}
4、编写一个函数,返回一个double数组中存储的最大数值的索引,并在一个简单程序中测试这个函数。
#include <stdio.h>
#define WIDTH 6
int max(float [], int );
int main(void)
{
float array[] = {4.3, 5.3, 2.6, 9.2, 2.8, 3.6};
printf("The max numnber's index is: %d\n",max( array, WIDTH));
return 0;
}
int max(float a[], int n)
{
int i,max;
for (i = 1, max = 0; i < n; i++)
if (a[max] < a[i])
max = i;
return max;
}
/5、编写一个函数,返回一个double数组中最大的和最小的数之间的差值,并在一个简单的程序中测试这个函数。
#include <stdio.h>
#define WIDTH 6
float gap(float [], int );
int main(void)
{
float array[] = {4.3, 5.3, 2.6, 9.2, 2.8, 3.6};
printf("The gap between max and min is: %g\n",gap( array, WIDTH));
return 0;
}
float gap(float a[], int n)
{
int i;
float max,min;
for (i = 1, max = a[0], min = a[0]; i < n; i++)
{
if (max < a[i]) max = a[i];
if (min > a[i]) min = a[i];
}
return max - min ;
}
/6、编写一个程序,初始化一个二维double数组,并利用练习2中的任一函数来把这个数组复制到另一个二维数组(因为二维数组是数组的数组,所以可以使用处理一维数组的函数来复制数组的每个子数组)。
#include <stdio.h>
#define ROWS 2
#define COLS 3
void copy_2d(double source[][COLS], double target[][COLS], int );
void copy_1d(double a1[], double a2[], int n);
int main(void)
{
int i,j;
double source[ROWS][COLS] = {1, 2, 3, 4, 5, 6};
double target[ROWS][COLS] = {0};
copy_2d(source, target, ROWS);
for (i=0; i<ROWS; i++)
{
for (j=0; j<COLS; j++)
printf("%g\t",target[i][j]);
printf("\n");
}
return 0;
}
void copy_2d(double ( *source )[COLS], double target[][COLS], int n)
{
int i;
for(i=0;i<n;i++)
copy_1d( *(source+i),target[i],COLS);
}
void copy_1d(double a1[], double *a2, int n)
{
int i;
for (i=0; i<n; i++)
a2[i] = a1[i];
}
/7、利用练习2中的复制函数,把—个包含7个元素的数组内第3到第5元素复制到一个包含3个元素的数组中。函数本身不需要修改,只需要选择合适的实际参数(实际参数不需要是数组名和数组大小,而只须是数组元素的地址和需要复制的元素数目)。
#include <stdio.h>
void copy(double *, double *, int );
int main(void)
{
double source[] = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7};
double target[7] = {0};
printf("Before operation:\n");
printf("source:\t%g\t%g\t%g\t%g\t%g\t%g\t%g\n",source[0],source[1],source[2],source[3],source[4],source[5],source[6]);
printf("target:\t%g\t%g\t%g\t%g\t%g\t%g\t%g\n",target[0],target[1],target[2],target[3],target[4],target[5],target[6]);
copy(source+2, target, 3);
printf("After operation:\n");
printf("source:\t%g\t%g\t%g\t%g\t%g\t%g\t%g\n",source[0],source[1],source[2],source[3],source[4],source[5],source[6]);
printf("target:\t%g\t%g\t%g\t%g\t%g\t%g\t%g\n",target[0],target[1],target[2],target[3],target[4],target[5],target[6]);
return 0;
}
void copy(double *p1, double *p2, int n)
{
int i;
for (i=0; i<n; i++)
*( p2 + i ) = *( p1 + i );
}
/8、编写一个程序,初始化一个3x5的二维double数组,并利用一个基于变长数组的函数把该数组复制到另一个二维数组。还要编写。个基于变长数组的函数来显示两个数组的内容。这两个函数应该能够处理任意的NxM数组(如果没有可以支持变长数组的编译器,就使用传统C中处理Nx5数组的函数方法)。
#include <stdio.h>
#define COLS 5
void copy(double (*)[COLS], double (*)[COLS], int );
void display(double (*)[COLS], int );
int main(void)
{
double source[3][COLS] = { {1.1, 2.2, 3.3, 4.4, 5.5},
{6.6, 7.7, 8.8, 9.9, 10.10},
{ 11.11, 12.12, 13.13, 14.14, 15.15} };
double target[6][COLS] = {0};
copy(source, target+1, 2);
puts("source:");
display(source,3);
puts("\ntarget:");
display(target,5);
return 0;
}
void copy(double ( *source )[COLS], double target[][COLS], int rows)
{
int i,j;
for(i=0;i<rows;i++)
for (j=0; j<COLS; j++)
target[i][j] = source[i][j];
}
void display(double ( *p )[COLS], int rows)
{
int i,j;
for(i=0;i<rows;i++)
{
for (j=0; j<COLS; j++)
printf("%g\t",p[i][j]);
printf("\n");
}
}
/9、编写一个函数,把两个数组内的相应元素相加,结果存储到第3个数组内。也就是说,如果数组l具有值2、4、5、8,数组2具有值1、0、4、6,则函数对数组3赋值为3、4、9、140函数的参数包括3个数组名和数组大小。并在一个简单的程序中测试这个函数。
#include <stdio.h>
#define WIDTH 5
void add(double [], double *, double [],int n);
int main(void)
{
double source1[5] = { 1.1, 2.2, 3.3, 4.4, 5.5};
double source2[5] = { 6.6, 7.7, 8.8, 9.9, 10.10};
double target[5] = {0};
add(source1, source2, target, WIDTH);
printf("target:\t%g\t%g\t%g\t%g\t%g\n",target[0],target[1],target[2],target[3],target[4]);
return 0;
}
void add(double source1[], double source2[], double target[],int n)
{
int i;
for (i=0; i<n; i++)
target[i] = source1[i] + source2[i];
}
/10、编写…个程序,声明一个3x5的数组并初始化,具体数值可以随意。程序打印出数值,然后数值翻1番,接着再次打印出新值。编写一个函数来显示数组的内容,再编写另一个函数执行翻倍功能。数组名和数组行数作为参数由程序传递给函数。
#include <stdio.h>
#define COLS 5
void twice(double ( * )[COLS], int );
void display(double (*)[COLS], int );
int main(void)
{
double array[3][COLS] = { {1.1, 2.2, 3.3, 4.4, 5.5},
{6.6, 7.7, 8.8, 9.9, 10.10},
{ 11.11, 12.12, 13.13, 14.14, 15.15} };
puts("before:");
display(array, 3);
twice(array, 3);
puts("\nafter:");
display(array, 3);
return 0;
}
void twice(double ( *p )[COLS], int rows)
{
int i,j;
for(i=0;i<rows;i++)
for (j=0; j<COLS; j++)
p[i][j] = 2* p[i][j];
}
void display(double ( *p )[COLS], int rows)
{
int i,j;
for(i=0;i<rows;i++)
{
for (j=0; j<COLS; j++)
printf("%g\t",p[i][j]);
printf("\n");
}
}
/11、重写程序清单10.7的程序rain,main()中的主要功能改为由函数来执行。
#include <stdio.h>
#define MONTHS 12 // number of months in a year
#define YEARS 5 // number of years of data
void display(float rain[][MONTHS], int years);
int main(void)
{
// initializing rainfall data for 2000 - 2004
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}
};
display(rain, YEARS);
return 0;
}
void display( float rain[][MONTHS], int years)
{
int year, month;
float subtot, total;
printf(" YEAR RAINFALL (inches)\n");
for (year = 0, total = 0; year < years; year++)
{ // for each year, sum rainfall for each month
for (month = 0, subtot = 0; month < MONTHS; month++)
subtot += rain[year][month];
printf("%5d %15.1f\n", 2000 + year, subtot);
total += subtot; // total for all years
}
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 each month, sum rainfall over years
for (year = 0, subtot =0; year < years; year++)
subtot += rain[year][month];
printf("%4.1f ", subtot/years);
}
printf("\n");
}
/12、编写…个程序,提示用户输入3个数集,每个数集包括5个double值。程序应当实现下列所有功能:
a.把输入信息存储到一个3x5的数组中
b.计算出每个数集(包含5个数值)的平均值
c.计算所有数值的平均数
d.找出这15个数中的最大值.
e.打印出结果
每个任务需要用一个单独的函数来实现(使用传统C处理数组的方法)。对于任务b,需要编写计算并返回一维数组平均值的函数,循环3次调用该函数来实现任务b。对于其他任务,函数应当把整个数组做为参数,并且完成任务c和d的函数应该向它的调用函数返回答案。
#include <stdio.h>
#define COLS 5
void store(double p[][COLS], int row);
void average_row(double p[][COLS], double *p_average0,double *p_average1,double *p_average2);
double f_average_total(double p[][COLS], int row);
double f_max(double p[][COLS], int row);
void display(double p[][COLS], int rows, double average0, double average1, double average2, double average_total,double max);
int main(void)
{
double array[3][COLS];
double average0, average1, average2, average_total, max;
store(array,3);
average_row(array, &average0, &average1, &average2);
average_total = f_average_total(array,3);
max = f_max(array,3);
display(array, 3, average0, average1, average2, average_total, max);
return 0;
}
void store(double p[][COLS], int row)
{
int i,j;
printf("please input 3 arrays of 5 numbers:");
for (i=0; i<row; i++)
for(j=0; j<COLS; j++)
scanf("%lf",&p[i][j]);
}
void average_row(double p[][COLS], double *p_average0,double *p_average1,double *p_average2)
{
int j;
for (j=0, *p_average0=0; j<COLS; j++)
*p_average0 += p[0][j];
*p_average0 /= COLS;
for (j=0, *p_average1=0; j<COLS; j++)
*p_average1 += p[1][j];
*p_average1 /= COLS;
for (j=0, *p_average2=0; j<COLS; j++)
*p_average2 += p[2][j];
*p_average2 /= COLS;
}
double f_average_total(double p[][COLS], int row)
{
int i,j;
double average=0;
for (i=0; i<row; i++)
for(j=0; j<COLS; j++)
average += p[i][j];
average /= row * COLS;
return average;
}
double f_max(double p[][COLS], int row)
{
int i,j;
double max=p[0][0];
for (i=0; i<row; i++)
for(j=0; j<COLS; j++)
if (max < p[i][j]) max = p[i][j];
return max;
}
void display(double p[][COLS], int rows, double average0, double average1, double average2, double average_total,double max)
{
int i,j;
printf("a.\narray = \n");
for(i=0;i<rows;i++)
{
for (j=0; j<COLS; j++)
printf("%g\t",p[i][j]);
printf("\n");
}
printf("b.\n");
printf("average0 = %g\n",average0);
printf("average1 = %g\n",average1);
printf("average2 = %g\n",average2);
printf("c.\n");
printf("total average = %g\n",average_total);
printf("d.\n");
printf("max = %g\n",max);
}