c primer plus第十章编程练习答案(纯手写欢迎指错)

10.1

#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",2000+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;

}
10.2

#include<stdio.h>
void copy_arr(double ar[],double br[],int n); 
void copy_ptr(double *ar,double *br,int n);
int main(void)
{
	
	double source[5]={1.1,2.2,3.3,4.4,5.5};        
	double target1[5];           
	double target2[5];
	int i;
	printf("source :");      
	for(i=0;i<5;i++)
	printf("%.1f ",source[i]);
	printf("\n");
	
	copy_arr(source,target1,5);   
	copy_ptr(source,target2,5);  
	
		
		printf("target1:");
		for(i=0;i<5;i++)     
	printf("%.1f ",target1[i]);  
		printf("\n");
		
		
		printf("target2:");
		for(i=0;i<5;i++)
	printf("%.1f ",target2[i]);
}
void copy_arr(double ar[],double br[],int n)
{
	int i;
	for(i=0;i<n;i++)
	br[i]=ar[i];
}
void copy_ptr(double *ar,double *br,int n)
{
	int i;
	for(i=0;i<n;i++)
	  *(br+i)=*(ar+i);
}
10.3

#include<stdio.h>
int max(int *ar,int n);
int main(void)
{
	int test[]={1,2,4,6,1,3,34,5,1,111,23,4,23,5,23,5,23,5,23,5};
	printf("max=%d \n",max(test,20));
	
	
}
int max(int *ar,int n)
{
	int max;
	max=*ar;
	int i;
	for(i=1;i<n;i++)
    {	
        if(max<*(ar+i))
	     max=*(ar+i);
    }
	return max;
}
10.4

#include<stdio.h>
int max(double ar[],int n);
int main(void)
{
	double test[]={12.1,1.2,123.3,5.5};
	printf("最大数值索引为 %d \n",max(test,4));
	
	
}
int max(double ar[],int n)
{
	double max;
	int mark,i=0;
	mark=i;
	max=ar[i];
	
	for(i=1;i<n;i++)
    {	
        if(max<ar[i])
	    {
	    max=ar[i];
	    mark=i;
	    }
	}
	return mark;
}

10.5
#include<stdio.h>
double difference(double *ar,int n);
int main(void)
{
	double test[]={2.5,7.5,8.5,2.1,4.1,41.2,31.12,4.5,123.1,1000.1};
	double result;
	result=difference(test,10);
	printf("max-min=%.1f ",result);
}
double difference(double *ar,int n)
{
	double max,min;
	int i,j;
	max=min=ar[0];
	for(i=1,j=0;i<n,j<n;i++,j++)
	{
		if(max<ar[i])
		max=ar[i];
		if(min>ar[j])
		min=ar[j];

	}
	return max-min;
} 
10.6

#include<stdio.h>  //两种函数都带入了 
void copy_arr(double ar[],double br[],int n); 
void copy_ptr(double *ar,double *br,int n);
int main(void)
{
	double test[2][4]={1,2,3,4,5.7,6,7,8};
	double target1[2][4];
	double target2[2][4];
	
	int i,j;
	
	for(i=0;i<2;i++)
	copy_arr(test[i],target1[i],4);
	for(i=0;i<2;i++)
	copy_ptr(test[i],target2[i],4);
	
	printf("target1     target2\n");
	for(i=0;i<2;i++)
	  for(j=0;j<4;j++)
	    printf("%.1f           %.1f\n",target1[i][j],target2[i][j]); 
	


}
void copy_arr(double ar[],double br[],int n)
{
	int i;
	for(i=0;i<n;i++)
	br[i]=ar[i];
}
void copy_ptr(double *ar,double *br,int n)
{
	int i;
	for(i=0;i<n;i++)
	  *(br+i)=*(ar+i);
}
10.7

#include<stdio.h>
void copy_arr(const int ar[],int br[],int n); 
void copy_ptr(const int *ar,int *br,int n);
int main(void)
{	int i;
	int test[7]={1,2,3,4,5,6,7};
	int target1[7];
	int target2[3];
	copy_arr(test+2,target1,3);
	copy_ptr(test+2,target2,3);


	for(i=0;i<3;i++) 
	printf("%3.1d%6.1d \n",target2[i],target1[i]); 
}
void copy_arr(const int ar[],int br[],int n)
{
	int i;
	for(i=0;i<n;i++)
	br[i]=ar[i];
}
void copy_ptr(const int *ar,int *br,int n)
{
	int i;
	for(i=0;i<n;i++)
	  *(br+i)=*(ar+i);
}
10.8

#include<stdio.h>
#define ROWS 3
#define COLS 5
void copy(int r,int c,double ar[r][c],double br[r][c]);
void show(int r,int c,double ar[r][c],double br[r][c]);
int main(void)
{
	double test[ROWS][COLS]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
	double target[ROWS][COLS];
	copy(ROWS,COLS,test,target);
	show(ROWS,COLS,test,target);
	
}
void copy(int r,int c,double ar[r][c],double br[r][c])
{
     int row,col;
     for(row=0;row<r;row++)
       for(col=0;col<c;col++)
          br[row][col]=ar[row][col];

}
void show(int r,int c,double ar[r][c],double br[r][c])
{
 int row,col;
 printf("test:");
     for(row=0;row<r;row++)
       for(col=0;col<c;col++)
       printf("%6.1f",ar[row][col]);
 
   printf("\n");
   
   printf("target:");
     for(row=0;row<r;row++)
       for(col=0;col<c;col++)
       printf("%6.1f",br[row][col]);

}

10.9

#include<stdio.h>
void sum(int ar[],int br[],int cr[],int n);
int main(void)
{
	int target1[4]={2,4,5,8};
	int target2[4]={1,0,4,6};
	int target[4];
	sum(target1,target2,target,4);
	int i;
	for(i=0;i<4;i++)
	printf("%4d",target[i]);
	
}
void sum(int ar[],int br[],int cr[],int n)
{
	int i;
	for(i=0;i<n;i++)
	cr[i]=ar[i]+br[i];
}

10.10

#include<stdio.h>
void X2(int ar[][5],int n);
void show(int ar[][5],int n);
int main(void)
{
	int test[3][5]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
	show(test,3);
	puts(" ");
	X2(test,3);
	show(test,3);
} 
void X2(int ar[][5],int n)
{
	int i,j;
	for(i=0;i<n;i++)
	  for(j=0;j<5;j++)
	    ar[i][j]=2*ar[i][j];	
}
void show(int ar[][5],int n)
{
	int i,j;
	for(i=0;i<n;i++)
	  for(j=0;j<5;j++)
	    printf("%4d",ar[i][j]);
}

10.11

#include<stdio.h>
#define MONTHS 12          //这题有点凑合
#define YEARS 5
void year_tot_average(double ar[][MONTHS]);
void month_tot(double ar[][MONTHS]);
int main(void)
{
	const double 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;
	


year_tot_average(rain);

printf("MONTHLY AVERAGES:\n\n");
printf("JAN FEB MAR APR MAY JUN JUL AUG SEP OCT ");
printf("Nov DEC \n");

month_tot(rain);

printf("\n");
return 0;

}
void year_tot_average(double ar[][MONTHS])
{
	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+=ar[year][month];
	  printf("%5d %15.1f\n",2000+year,subtot);
	  total+=subtot;
      }
   printf("\nThe yearly average is %.1f inches.\n\n",total/YEARS);
}
void month_tot(double ar[][MONTHS])
{
	double subtot;
	int year,month;
	for(month=0;month<MONTHS;month++)
{
for(year=0,subtot=0;year<YEARS;year++)
subtot+=ar[year][month];
printf("%4.1f",subtot/YEARS);

}
}
10.12
#include<stdio.h>   //普通版
#define ROWS 3
#define COLS 5
void input(double *ar,int n);
void show(double ar[][COLS],int n);
double avg_input(double *ar,int n);
double avg_all(double ar[][COLS],int n);
double max_all(double ar[][COLS],int n);
int main(void)
{
	double test[ROWS][COLS];
	int r;
	for(r=0;r<ROWS;r++)
	{
		printf("Plesase enter five value for row %d:\n",r+1);
		input(test[r],COLS);
	}
	//输入数字 
	printf("\nShow numers now!\n");
	show(test,ROWS);
	puts(" ");
	//显示数组
	for(r=0;r<ROWS;r++)
	printf("The average of %d row is %3.1f\n",r+1,avg_input(test[r],COLS));

	//平均每行平均 
	printf("The total of the average is %5.1f\n",avg_all(test,ROWS));
	//总平均 
	printf("The max number is %5.1f",max_all(test,ROWS));
	//MAX值 
	
	
	
}
void input(double *ar,int n)
{
	int c;
	for(c=0;c<n;c++)
	{
	printf("Please enter value #%d:",c+1);	
	scanf("%lf",&ar[c]);
}
}
void show(double ar[][COLS],int n)
{
	int r,c;
	double tot=0;
	for(r=0;r<n;r++)
	for(c=0;c<COLS;c++)
		printf("%4.1f",ar[r][c]);
}
double avg_input(double *ar,int n)
{
	int c;
	double total=0;
	for(c=0;c<n;c++)
  		total+=ar[c];
		return total/COLS;	
}
double avg_all(double ar[][COLS],int n)
{	
	double total=0;
	int r,c;
	for(r=0;r<n;r++)
	for(c=0;c<COLS;c++)
	total+=ar[r][c];
	
	return total/(n*COLS);
}
double max_all(double ar[][COLS],int n)
{
	int r,c;
	double max;
	max=ar[0][0];
	for(r=0;r<n;r++)
	for(c=0;c<COLS;c++)
       if(max<ar[r][c])
       max=ar[r][c];
   return max;
}

10.13

#include<stdio.h>      //变长数组版(VAL)
#define ROWS 3
#define COLS 5
void input(double *ar,int n);
void show(int row,int col,double ar[row][col]);
double avg_input(double *ar,int n);
double avg_all(int row,int col,double ar[row][col]);
double max_all(int row,int col,double ar[row][col]);
int main(void)
{
	double test[ROWS][COLS];
	int r;
	for(r=0;r<ROWS;r++)
	{
		printf("Plesase enter five value for row %d:\n",r+1);
		input(test[r],COLS);
	}
	//输入数字 
	printf("\nShow numers now!\n");
	show(ROWS,COLS,test);
	puts(" ");
	//显示数组
	for(r=0;r<ROWS;r++)
	printf("The average of %d row is %3.1f\n",r+1,avg_input(test[r],COLS));

	//平均每行平均 
	printf("The total of the average is %5.1f\n",avg_all(ROWS,COLS,test));
	//总平均 
	printf("The max number is %5.1f",max_all(ROWS,COLS,test));
	//MAX值 
	
	
	
}
void input(double *ar,int n)
{
	int c;
	for(c=0;c<n;c++)
	{
	printf("Please enter value #%d:",c+1);	
	scanf("%lf",&ar[c]);
}
}
void show(int row,int col,double ar[row][col])
{
	int r,c;
	double tot=0;
	for(r=0;r<row;r++)
	for(c=0;c<col;c++)
		printf("%4.1f",ar[r][c]);
}
double avg_input(double *ar,int n)
{
	int c;
	double total=0;
	for(c=0;c<n;c++)
  		total+=ar[c];
		return total/COLS;	
}
double avg_all(int row,int col,double ar[row][col])
{	
	double total=0;
	int r,c;
	for(r=0;r<row;r++)
	for(c=0;c<col;c++)
	total+=ar[r][c];
	
	return total/(row*col);
}
double max_all(int row,int col,double ar[row][col])
{
	int r,c;
	double max;
	max=ar[0][0];
	for(r=0;r<row;r++)
	for(c=0;c<col;c++)
       if(max<ar[r][c])
       max=ar[r][c];
   return max;
}







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值