/*
============================================================================
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 (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 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 souurce[5] = {1.1,2.2,3.3,4.4,5.5};
double target1[5];
double target2[5];
copy_arr(source,target1,5);
copy_ptr(source,target1,5);
============================================================================
*/
#include <stdio.h>
void copy_arr(const double source[],double arr[],int n);
void copy_ptr(const double * source,double * arr,int n);
int main(void)
{
double source[5] = {1.1,2.2,3.3,4.4,5.5};
double target1[5];
double target2[5];
printf("target2 output:\n");
copy_ptr(source,target2,5);
for(int i = 0;i< 5;i++)
printf("%g\t",target2[i]);
printf("\ntarget1 output:\n");
copy_ptr(source,target1,5);
for(int i = 0;i< 5;i++)
printf("%g\t",target1[i]);
return 0;
}
void copy_arr(const double source[],double arr[],int n){
for(int i = 0;i<n;i++)
arr[i] = source[i];
}
void copy_ptr(const double * source,double * arr,int n){
for(int i = 0;i<n;i++){
*(arr+i) = *(source +i);
}
}
/*
============================================================================
3、编写一个函数,返回一个int数组中存储的最大数值,并在一个简单的程序中测试这个函数
============================================================================
*/
#include <stdio.h>
int max(int * arr,int size);
int main(void)
{
int arr[7] = {2,4,6,8,0,9,12};
printf("%d",max(arr,7));
return 0;
}
int max(int * arr,int size){
int temp = arr[0];
for(int i= 1;i<size;i++){
if(temp < arr[i])
temp = arr[i];
}
return temp;
}
/*
============================================================================
4、编写一个函数,返回一个double数组中存储的最大数值的索引值,并在一个简单的程序中测试这个函数
============================================================================
*/
#include <stdio.h>
int max(double * arr,int size);
int main(void)
{
double arr[7] = {2,4,6,8,0,9,12};
printf("%d",max(arr,7));
return 0;
}
int max(double * arr,int size){
double temp = arr[0];
int n = 0;
for(int i= 1;i<size;i++){
if(temp < arr[i]){
temp = arr[i];
n = i;
}
}
return n;
}
/*
============================================================================
5、编写一个函数,返回一个double数组中存储的最大数值和最小数值的差值,并在一个简单的程序中测试这个函数
============================================================================
*/
#include <stdio.h>
double maxmin(double * arr,int size);
int main(void)
{
double arr[7] = {2,4,6,8,0,9,12};
printf("%g",maxmin(arr,7));
return 0;
}
double maxmin(double * arr,int size){
double max = arr[0];
double min = arr[0];
for(int i= 1;i<size;i++){
if(max < arr[i]){
max = arr[i];
}else if(min > arr[i){
min = arr[i];
}
}
return (max - min);
}
/*
============================================================================
6、编写一个程序,初始化一个二维double数组,并利用练习2中的任一函数来把这个数组复制到另
一个二维数组中(因为二维数组是数组的数组,所以可以使用处理一维数组的函数来复制数组的每个
子数组。
============================================================================
*/
#include <stdio.h>
void copy_arr(const double source[],double arr[],int n);
void copy_ptr(const double * source,double * arr,int n);
int main(void)
{
double twoarray[2][5] = {{2,3,1,0,5},
{1,5,6,7,8}};
double twoarray2[2][5];
double twoarray3[2][5];
for(int i = 0;i<2;i++){
copy_arr(twoarray[i],twoarray2[i],5);
copy_ptr(*(twoarray+i),*(twoarray3+i),5);
}
printf("打印输出towarray2:\n");
for(int i = 0;i<2;i++){
for(int j = 0;j<5;j++){
printf("%g\t",twoarray2[i][j]);
}
printf("\n");
}
printf("打印输出towarray3:\n");
for(int i = 0;i<2;i++){
for(int j = 0;j<5;j++){
printf("%g\t",twoarray3[i][j]);
}
printf("\n");
}
printf("end\n");
return 0;
}
void copy_arr(const double source[],double arr[],int n){
for(int i = 0;i<n;i++)
arr[i] = source[i];
}
void copy_ptr(const double * source,double * arr,int n){
for(int i = 0;i<n;i++){
*(arr+i) = *(source +i);
}
}
/*
============================================================================
7、利用练习2中的复制函数,把一个包含7个元素的数组内第3到第5元素复制到一个包含 三个元素
的数组中。函数本身不要进行制修改,只需要选择合适参数即可(实际参数不需要数组名和数组大小
,而是只要数组元素的的地址和复制的元素个数
============================================================================
*/
#include <stdio.h>
void copy_arr(const double source[],double arr[],int n);
int main(void)
{
double array7[7] = {2,3,1,0,5,9,4};
double array3[3];
for(int i = 0;i<3;i++){
copy_arr(&array7[2+i],array3,3);
}
printf("打印输出array3:\n");
for(int i = 0;i<3;i++)
printf("%g\t",array3[i]);
printf("\nend\n");
return 0;
}
void copy_arr(const double source[],double arr[],int n){
for(int i = 0;i<n;i++)
arr[i] = source[i];
}
/*
============================================================================
8.编写一个程序,初始化一个3x5的二维double数组,并利用一个基于变长数组的函数把该数组复制
到另一个二维数组中,还要编写一个基于变长数组的函数来显示两个数组的内容。这两个函数应该能
够处理任意的NxM数组(如果没有可以支持变长数组的编译器,就使用传统的C中的Nx5的数组的函数
方法)
============================================================================
*/
#include <stdio.h>
void copy_arr(int n,int m,double source[n][m],double target[n][m]);
void show_arr(int n,int m,double source[n][m]);
int main(void)
{
double arr3x5[3][5] = {{1,2,3,4,5},
{2,3,4,4,5},
{8,7,7,8,6}
};
double tarr[3][5];
printf("显示数组arr3x5:\n");
show_arr(3,5,arr3x5);
copy_arr(3,5,arr3x5,tarr);
printf("显示数组tarr\n");
show_arr(3,5,tarr);
printf("\nend\n");
return 0;
}
void copy_arr(int n,int m,double source[n][m],double target[n][m]){
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
target[i][j] = source[i][j];
}
}
}
void show_arr(int n,int m,double source[n][m]){
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
printf("%g\t",source[i][j]);
}
printf("\n");
}
}
/*
============================================================================
9.编写一个函数,把两个数组内相应的元素相加,结果存储到第三个数组内,并在一个程序中测试
============================================================================
*/
#include <stdio.h>
void add_arr(int a[],int b[],int ab[],int n);
int main(void)
{
int a[4] = {2,3,4,5};
int b[4] = {1,2,3,4};
int c[4];
add_arr(a,b,c,4);
for(int i = 0;i<4;i++){
printf("%d\t",c[i]);
}
return 0;
}
void add_arr(int a[],int b[],int ab[],int n){
for(int i = 0;i<n;i++)
ab[i] = a[i]+b[i];
}
/*
============================================================================
10.编写一个程序,声明一个3x5的数组并初始化,具体数值可以随意。程序打印出数值,然后数值
翻一番,接着再次打印出新值。编写一个函数来显示数组的内容,再编写一个函数来执行翻倍功能。
数组行数作为参数由程序传递给函数。
============================================================================
*/
#include <stdio.h>
void mult_arr(int n,int m,double source[n][m]);
void show_arr(int n,int m,double source[n][m]);
int main(void)
{
double arr3x5[3][5] = {{1,2,3,4,5},
{2,3,4,4,5},
{8,7,7,8,6}
};
printf("显示数组arr3x5:\n");
show_arr(3,5,arr3x5);
mult_arr(3,5,arr3x5);
printf("显示数组tarr\n");
show_arr(3,5,arr3x5);
printf("\nend\n");
return 0;
}
void mult_arr(int n,int m,double source[n][m]){
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
source[i][j] = source[i][j]*2;
}
}
}
void show_arr(int n,int m,double source[n][m]){
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
printf("%g\t",source[i][j]);
}
printf("\n");
}
}
/*
============================================================================
11.重写程序清单10.7的程序rain,main()的主要功能由函数执行
============================================================================
*/
#include <stdio.h>
#define MONTHS 12
#define YEARS 5
float eachyears(int m,int n,int s,const float rain[m][n]); //返回二维数组中第s行的数据的总和
float eachmoths(int m,int n,int s,const float rain[m][n]); //返回二维数组中第s列的数据的平均数
int main(void)
{
// 初始化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(" 年\t降雨总量\n");
for (year = 0, total = 0; year < YEARS; year++)
{
subtot = eachyears(YEARS,MONTHS,year,rain);
printf("%d\t%.1f\n",year+2000,subtot);
total += subtot; // 所有年的总降水量
}
printf("\n平均每年的降水量为: %.1f\n\n",total/YEARS);
printf("每月的平均降水量\n");
printf(" 一月\t二月\t三月\t四月\t五月\t六月\t七月\t八月\t九月\t十月\t十一月\t十二月\n");
for (month = 0; month < MONTHS; month++)
{
subtot = eachmoths(YEARS,MONTHS,month,rain);
printf("%.1f\t",subtot/YEARS);
}
printf("\n");
return 0;
}
float eachyears(int m,int n,int s,const float rain[m][n]){
float sums = 0;
for(int i=0;i<n;i++){
sums += rain[s][i];
}
return sums;
}
float eachmoths(int m,int n,int s,const float rain[m][n]){
float sums = 0;
for(int i = 0;i<m;i++){
sums += rain[i][s];
}
return sums;
}
/*
============================================================================
12.编写一个程序,提示用户输入3个数集,每个数集包括5个double值,程序应当实现下列所有功能
a.把输入的信息存储到一个3x5的数组中
b.计算出每个数集(包含5个数值)平均值
c.计算所有数值的平均值
d.找出这15个数中最大值
e.打印出结果
每个任务需要一个单独函数来实现(使用传统C处理数组的方法),对于任务b,需要编写计算并返回
一维数组的平均值函数,循环3次调用该函数来实现任务b,对于其他任务,函数应当把整个数组做为参
数,并且完成任务c和d的函数应当向它的调用函数返回答守案。
============================================================================
*/
#include <stdio.h>
double lines(int m,int n,int s,const double (* rain)[5]); //返回二维数组中第s行的数据平均值
void inputnum(int m,int n,double (* rain)[5]); //向rain[m][n]输入数值
double getnum(void); //从键盘获取一个double数
double allnum(int m,int n,const double (* rain)[5]); //计算二维数组中所有数据的平均值
double allmax(int m,int n,const double (* rain)[5]); //返回二维数中最大的数值
int main(void)
{
double rain[3][5];
inputnum(3,5,rain);
for(int i=0;i<3;i++){
printf("第%d个数集的平均值:%g\n",i+1,lines(3,5,i,rain));
}
printf("数组的平均值为:%g \n",allnum(3,5,rain));
printf("数组中最大数值为:%g \n",allmax(3,5,rain));
return 0;
}
double lines(int m,int n,int s,const double (* rain)[5]){
double sums = 0;
for(int i=0;i<n;i++){
sums += rain[s][i];
}
return sums/n;
}
void inputnum(int m,int n,double (* rain)[5]){
printf("输入%dx%d二维数组的数据:\n",m,n);
for(int i=0;i<m;i++){
printf("开始输入第%d数集:\n",i+1);
for(int j=0;j<n;j++){
printf("第%d个数:",j+1);
rain[i][j] = getnum();
}
}
printf("数据输入完成,如下所示:\n");
for(int i=0;i<m;i++){
printf("第%d数集: ",i+1);
for(int j=0;j<n;j++){
printf("%g\t",rain[i][j]);
}
printf("\n");
}
}
double getnum(void){
double i;
char ch;
while(scanf("%lf",&i) != 1){
while((ch=getchar())!='\n'){
putchar(ch);
}
printf("输入错误,请重新输入\n");
}
return i;
}
double allnum(int m,int n,const double (* rain)[5]){
double total = 0;
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
total += rain[i][j];
}
}
return total / (double)(3*5);
}
double allmax(int m,int n,const double (* rain)[5]){
double tmax = rain[0][0];
for(int i=1;i<m;i++){
for(int j = 1;j<n;j++){
if(tmax < rain[i][j]){
tmax = rain[i][j];
}
}
}
return tmax;
}
/*
============================================================================
13.利用变长数组做为函数参量重做练习12
============================================================================
*/
#include <stdio.h>
double lines(int m,int n,int s,const double rain[m][n]); //返回二维数组中第s行的数据平均值
void inputnum(int m,int n,double rain[m][n]); //向rain[m][n]输入数值
double getnum(void); //从键盘获取一个double数
double allnum(int m,int n,const double rain[m][n]); //计算二维数组中所有数据的平均值
double allmax(int m,int n,const double rain[m][n]); //返回二维数中最大的数值
int main(void)
{
double rain[3][5];
inputnum(3,5,rain);
for(int i=0;i<3;i++){
printf("第%d个数集的平均值:%g\n",i+1,lines(3,5,i,rain));
}
printf("数组的平均值为:%g \n",allnum(3,5,rain));
printf("数组中最大数值为:%g \n",allmax(3,5,rain));
return 0;
}
double lines(int m,int n,int s,const double rain[m][n]){
double sums = 0;
for(int i=0;i<n;i++){
sums += rain[s][i];
}
return sums/n;
}
void inputnum(int m,int n,double rain[m][n]){
printf("输入%dx%d二维数组的数据:\n",m,n);
for(int i=0;i<m;i++){
printf("开始输入第%d数集:\n",i+1);
for(int j=0;j<n;j++){
printf("第%d个数:",j+1);
rain[i][j] = getnum();
}
}
printf("数据输入完成,如下所示:\n");
for(int i=0;i<m;i++){
printf("第%d数集: ",i+1);
for(int j=0;j<n;j++){
printf("%g\t",rain[i][j]);
}
printf("\n");
}
}
double getnum(void){
double i;
char ch;
while(scanf("%lf",&i) != 1){
while((ch=getchar())!='\n'){
putchar(ch);
}
printf("输入错误,请重新输入\n");
}
return i;
}
double allnum(int m,int n,const double rain[m][n]){
double total = 0;
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
total += rain[i][j];
}
}
return total / (double)(3*5);
}
double allmax(int m,int n,const double rain[m][n]){
double tmax = rain[0][0];
for(int i=1;i<m;i++){
for(int j = 1;j<n;j++){
if(tmax < rain[i][j]){
tmax = rain[i][j];
}
}
}
return tmax;
}