有问题请反馈,反正我也不一定修改,部分原创,部分转载,版本2010,
变长数组没有测试实验,仅仅转载过来,版本不支持好像。
//#define _CRT_SECURE_NO_DEPRECATE //备用
//第十章 第一题
/*
#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(" 年 雨量 (英寸)\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);//每一年的12个月加起来的值
total += subtot;//然后从头开始,total值不变,subtot每次大循环后变为零
}
printf("\n年平均值是%.1f 英寸.\n\n", total/YEARS);
printf("每月平均数:\n\n");
printf(" Jan Feb 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("%4.1f ", subtot/YEARS);
}//打开每一年的一月,然后循环年,再打开二月,以此类推
printf("\n");
return 0;
}*/
//第二题
/*
#include <stdio.h>
#define SIZE 5
void copy_arr(double ar[], double a[],int n);
void copy_ptr(double *a, double *b,int n);
void copy_ptrs(double *a, double *b,double *end);
int main(void)
{
int i;
double source[SIZE]={1.1,2.2,3.3,4.4,5.5};
double target1[SIZE];
double target2[SIZE];
double target3[SIZE];
copy_arr(target1,source,SIZE);
copy_ptr(target2,source,SIZE);
copy_ptrs(target3, source,source+SIZE);
for(i = 0; i < SIZE; i++)
{
printf("target3[%d] = %.2lf", i, target3[i]);
printf("\n");
}
return 0;
}
void copy_arr(double ar[], double a[],int n)
{//带数组表示法函数
int i;
for(i = 0; i< n; i++)
ar[i] = a[i];
for(i = 0; i< n; i++)
{
printf("target1[%d] = %.2lf", i, ar[i]);
printf("\n");
}
}
void copy_ptr(double *a, double *b,int n)
{//指针表示法和指针递增函数
int i;
for(i = 0; i< n; i++)
*(a+i) = *(b+i);//加几就是第几个数组
for(i = 0; i< n; i++)
{ //从头开始打印
printf("target2[%d] = %.2lf", i, a[i]);
printf("\n");
}
}
void copy_ptrs(double *a, double *b,double *end)
{
while(b<end)
{
*a = *b;
b++;
a++;
}
}
*/
//第三题
/*
#include<stdio.h>
#define SIZE 8
int max(int a[], int n);
int main()
{
int a[SIZE];
int i;
printf("请输入%d整数: \n", SIZE);
for(i = 0; i < SIZE; i++)
scanf("%d", &a[i]);
printf("最大的数字就是%d\n", max(a, SIZE));
return 0;
}
int max(int a[], int n)
{
int i;
int max = 0;
for(i = 0; i< n; i++)
if(max < a[i])
max = a[i];
return max;
}
*/
//第四题
/*
#include<stdio.h>
#define SIZE 8
int max(double a[], int n);
int main()
{
int i;
double a[SIZE];
printf("请输入%d个数: \n", SIZE);
for(i = 0; i < SIZE; i++)
scanf("%lf", &a[i]);
printf("最大的数字就是 %d\n", max(a, SIZE));
return 0;
}
int max(double a[], int n)
{
int i,j=0;
double max = 0;
for(i = 0; i< n; i++)
if(max < a[i])
{
max = a[i];
j = i;
}
printf("i = %d, max = %.0f\n", j, max);
return j;
}
*/
//第五题
/*
#include<stdio.h>
#define SIZE 8
double maxmin(double a[], int n);
int main()
{
double a[SIZE];
int i;
printf("请输入%d个数字:\n",SIZE);
for(i=0;i<SIZE;i++)
scanf("%lf",&a[i]);
printf("则数组中最大值和最小值的差值是%.1lf",maxmin(a,SIZE));
return 0;
}
double maxmin(double a[], int n)
{
int i;
double max,min;
max=min=a[0];
for(i=1;i<n;i++)
{
if(max<a[i])
max=a[i];
if(min>a[i])
min=a[i];
}
return (max-min);
}
*/
//第六题
/*
#include <stdio.h>
#define SIZE 5
int main()
{
int i;
double const maa[SIZE]={8,7,6,5,4};
double mbb[SIZE];
for(i=0;i<SIZE;i++)
{
mbb[i]=maa[i];
printf("%.0lf ",maa[i]);
}
printf("\n");
for(i=0;i<SIZE;i++)
printf("%.0lf ",mbb[4-i]);
return 0;
}
*/
//第六题
/*
#include<stdio.h>
#define SIZE 8
void revert_arr(double arr[], int n);
int main()
{
int i;
double arr[SIZE];
printf("请输入 %d 个数字:\n", SIZE);
for(i = 0; i < SIZE; i++)
scanf("%lf", &arr[i]);
printf("在还原之前,数组是:\n");
for (i = 0; i < SIZE; i++)
{
printf(" %.2lf", arr[i]);
}
printf("\n");
revert_arr(arr, SIZE);
printf("恢复后,阵列是:\n");
for (i = 0; i < SIZE; i++)
{
printf(" %.2lf", arr[i]);
}
printf("\n");
return 0;
}
void revert_arr(double arr[], int n)
{
int i;
for (i = 0; i < n / 2; i++)
{ //
double tmp = arr[i];
arr[i] = arr[n - 1 - i];
arr[n - 1 - i] = tmp;
//对半运算,一次完成
}
}
*/
//第七题
/*
#include<stdio.h>
#define ROWS 3 //行数
#define COLS 4 //列数
//ar是内含4个double类型值得数组
void copy_arr(double ar[][COLS], double a[][COLS],int n);
//一个长函数,二维数组
int main()
{
double target[ROWS][COLS];//定义数组一用来接收
double source[ROWS][COLS] =
{
{1.1, 2.2, 3.3, 4.4},
{2.2, 3.3, 4.4, 5.5},
{3.3, 4.4, 5.5, 6.6}
};//定义数组二
copy_arr(target, source, ROWS);//??本身就是数组
return 0;//target source此处是链接ar[]和a[]的地方
}
void copy_arr(double ar[][COLS], double a[][COLS],int n)
{
int i, j;
for(i = 0; i < n; i++)
for(j = 0; j < COLS; j++)
ar[i][j] = a[i][j];
//列传送,行传送
//打印目标数组(拷贝后的)
for(i = 0; i < n; i++)
for(j = 0; j < COLS; j++)
printf("target[%d][%d] = %.2lf \n", i, j, ar[i][j]);
}
*/
//第八题
/*
#include<stdio.h>
#define LEN 7
#define SIZE 3
void copy_ptr(double *a, double *b,int n);
int main()
{
int i;
double source[LEN];
double target[SIZE];
printf("请输入%d个数字:\n", LEN);
for(i = 0; i < LEN; i++)
scanf("%lf", &source[i]);
copy_ptr(target, source, SIZE);
//或者这样做
return 0;
}
void copy_ptr(double *a, double *b,int n)
{
int i;
for(i = 0; i < n; i++)
*(a + i) = *(b + i + 2);
for(i = 0; i < n; i++)
{
printf("target[%d] = %.2lf\n", i, a[i]);
}
}
*/
//第九题
//编写一个程序,初始化一个double类型的3x5二维数组,使用一个处理变长数组的
//函数将其拷贝至另一个二维数组中.还要编写一个变长数组为形参的函数以显示
//两个数组的内容.这两个函数应该能处理任意NxM数组(如果编译器不支持变长数
//组,就使用传统C函数处理Nx5的数组)
//无法制作变长数组https://blog.csdn.net/weixin_44603568/article/details/89481112
//第10题
//编写一个函数, 把两个数组中相对应的元素相加,然后把结果储存到第三个数组
//中.也就是说,如果数组1 中包含的值是2,4,5,8,数组2中包含的值是1,0,4,6,那
//么该函数把3,4,9,14,赋值给第三个数组.函数接受三个数组名和一个数组大小.
//在一个简单的程序中测试该函数
/*
# include <stdio.h>
void print_arr(long ar[] , int n);//是为了把每一个都打印一下
void double_add(long add1[] , long add2[] , long sum[] , int n);
//相加运算在这里进行
int main()
{
long ar1[4] = {2 , 4 , 5 , 8} ;
long ar2[4] = {1 , 0 , 4 , 6} ;
long ar3[4] ;
printf("The first array :\n");
print_arr(ar1 , 4) ;
printf("-----------------------------\n\n");
printf("The second array :\n");
print_arr(ar2 , 4) ;
printf("-----------------------------\n\n");
printf("The last array :\n");
print_arr(ar3 , 4) ;
printf("-----------------------------\n\n");
//运行之前
double_add(ar1 , ar2 , ar3 , 4) ;
print_arr(ar1 , 4) ;
print_arr(ar2 , 4) ;
printf("-----------------------------\n");
print_arr(ar3 , 4) ;
printf("============Bye==============\n\n");
//运行之后
return 0 ;
}
void double_add(long add1[] , long add2[] , long sum[] , int n)
{
int i;
for (i = 0 ; i < n ; i++)
sum[i] = add1[i] + add2[i] ;
}
void print_arr(long ar[] , int n)
{
int i;
for ( i = 0 ; i < n ; i++)
printf("%5ld " ,ar[i] );
printf("\n");
}
*/
//第11题 ,,,和第十题差不多
//编写一个程序,声明一个int类型的3x5二维数组,并用合适的值初始化它.该程序
//打印数组中的值,然后各值翻倍(即原来的2倍),并显示出各元素的新值.编写一个
//函数显示数组的内容,再编写一个函数把各元素的值翻倍.这两个函数都以函数名
//和行数作为参数
/*
# include <stdio.h>
# define COLS 5
void double_arr(double arr[][COLS] , int row);
void print_arr(double array[][COLS] , int row);
int main(void)
{
double ar[3][5] =
{
{1.1 , 2.2 , 3.3 , 4.4 , 5.5} ,
{11.1 , 12.2 , 13.3 , 14.4 , 15.5} ,
{21.1 , 22.2 , 23.3 , 24.4 , 25.5}
};
printf("源数组初始化 :\n");
print_arr(ar , 3) ;
printf("--------------------------------\n\n");
double_arr(ar , 3) ;
printf("源数组初始化:\n");
print_arr(ar , 3) ;
printf("--------------------------------\n\n");
return 0 ;
}
void double_arr(double arr[][COLS] , int row)
{
int i,j;
for ( i = 0 ; i < row ; i++)
for ( j = 0 ; j < COLS ; j++)
arr[i][j] *= 2 ;
}
void print_arr(double array[][COLS] , int row)
{
int i,j;
for (i = 0 ; i < row ; i++)
{
for ( j = 0 ; j < COLS ; j++)
printf("%10.2lf " , array[i][j]);
printf("\n");
}
}
*/
//第十二题
// 重写程序清单10.7的rain.c程序,把main()中的主要任务都改成用函数来完成
/*
# include <stdio.h>
# define MONTHS 12
# define YEARS 5
float sum_year(const float ar[] , int n);
void sum_month(const float ar[][MONTHS] , int year);
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 ;
const float * p ;
for (year = 0 , total = 0 ; year < YEARS ; year++)
{
p = rain[year];//用指针来联系两个数组过度
subtot = sum_year(p , MONTHS);
printf("%5d %15.1f\n" , 2010 + year , subtot);
total += subtot ;
}
printf("\n年平均降水量是%.1f \n\n" , total / YEARS);
sum_month(rain , YEARS) ;
return 0 ;
}
float sum_year(const float ar[] , int n)
{
int i;
float sum = 0 ;
for (i = 0 ; i < n ; i++)
sum += ar[i] ;
return sum ;
}
void sum_month(const float ar[][MONTHS] , int year)
{
float total ;
int y,mon;
printf("每月平均数:\n\n");
printf(" Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec\n");
for ( mon = 0 ; mon < MONTHS ; mon++)
{
total = 0 ;
for( y = 0 ; y < year ; y++)
total += ar[y][mon] ;
printf("%4.1f " , total / YEARS);
}
printf("\n");
}
*/
//第十三题
//编写一个程序,提示用户输入3数组,每组数包含5个double类型的数(假设用户
//都正确的相应,不会输入非数值数据).该程序完成的下列任务
//a. 把用户输入的数据储存在3x5的数组中
//b. 计算每组(5个)数据的平均值
//c. 计算所有数据的平均值
//d. 找出这15个数据中的最大值
//e. 打印结果
//每个任务都要用单独的函数来完成(使用传统C处理数组的方式).
//完成任务b,要编写一个计算并返回一位数组平均值的函数,利用
//循环调用该函数3次.对于处理其他任务的函数,应该把整个数组
//作为参数,完成任务c和d的函数应把结果返回主调函数.
/*
# include <stdio.h>
# define ROW 3
# define COL 5
void input_ar( double * ar , int row , int col);
double ave_row( double a [] , int col );
double average_arr( double * ar , int row , int col);
void ave_arr( double * ar , int row , int col);
void max_arr( double * ar , int row , int col);
void print_arr( double * ar , int row , int col);
int main()
{
double ar[ROW][COL] ;
double total ;
int i ;
// a.
input_ar(ar[0] , ROW , COL) ;
// 输出验证
printf("你的数组:\n");
print_arr(ar[0] ,ROW , COL) ;
printf("--------------------------------------\n\n");
// b.
for ( i = 0 ; i < ROW ; i++)//计算每行的平均值
printf("平均每行%d:%.3lf\n", i + 1 , ave_row(ar[i] , COL));
// c.
printf("所有的数值平均后=%.3lf \n" , average_arr(ar[0] , ROW , COL));
//计算总的平均值
// 我的方法实现b , c但是不符合题目要求 , 此方法简单,可读性高
//ave_arr(ar[0], ROW , COL);
// d.
max_arr(ar[0] , ROW , COL);
// e.
print_arr(ar[0], ROW , COL) ;
return 0 ;
}
void input_ar(double * ar , int row , int col)
{//给二维数组赋值
int i,j;
for ( i = 0 ; i < row ; i++)
for ( j = 0 ; j < col ; j++)
{
printf("请输入 %d:%d 原数组:" , i + 1 , j + 1);
scanf("%lf", (ar + i * col + j));//给第i行,j列赋值
}
}
double ave_row(double a[] , int col)
{
double sum = 0 ;
int i;
for ( i = 0 ; i < col ; i++ )
sum += a[i] ;
return (sum /col) ;
}
double average_arr(double *ar , int row , int col)
{
double sum=0 ;
int i,j;
for ( i = 0 ; i < row ; i++)
for ( j = 0 ; j < col ; j++)
sum += *(ar + i * col + j) ;
return (sum /(row * col)) ;
}
void ave_arr(double * ar , int row , int col)
{
double subto = 0 ;
double total = 0 ;
int i , j ;
for ( i = 0 ; i < row ; i++)
{
subto = 0 ;
for ( j = 0 ; j < col ; j++)
subto += *(ar + i * col + j) ;
printf("平均行 %d : %lf\n" , i + 1 , subto / col);
total += subto ;
}
printf("平均 : %lf\n" , total / (row * col));
}
void max_arr(double * ar , int row , int col)
{
double max=ar[0];
int i,j;
for ( i = 0 ; i < row ; i++)
{
for ( j = 0 ; j < col ; j++)
{
if (max < *(ar + i * col + j))
max = *(ar + i * col + j) ;
}
}
printf("阵列最大 : %lf\n" , max);
}
void print_arr(double * ar , int row , int col)
{//把输入的数据按照一定格式打印下来,提高美观和观察
int i,j;
for ( i = 0 ; i < row ; i++)
{
for ( j = 0 ; j < col ; j++)
printf("%5.1lf " , *(ar + i * col + j));
printf("\n");
}
}
*/
/*
// 以变长数组作为函数形参,完成编程练习13
# include <stdio.h>
# define ROW 3
# define COL 3
void input_ar(int row , int col , double ar[row][col]);
void print_arr(int row , int col , double ar[row][col]);
double ave_row(int col , double ar[col]);
double average_arr(int row , int col , double ar[row][col]);
void max_arr(int row , int col , double ar[row][col]);
int main(void)
{
double ar[ROW][COL] ;
double total ;
// a.
input_ar( ROW , COL ,ar) ;
// 输出验证
printf("Your array :\n");
print_arr(ROW , COL , ar) ;
printf("--------------------------------------\n\n");
// b.
for (int i = 0 ; i < ROW ; i++)
printf("Average of row %d : %lf\n", i + 1 , ave_row(COL , ar[i]));
// c.
printf("Average of all : %lf \n" , average_arr(ROW , COL ,ar)) ;
// d.
max_arr(ROW , COL , ar);
// e.
print_arr(ROW , COL , ar) ;
return 0 ;
}
void input_ar(int row , int col , double ar[row][col])
{
for (int i = 0 ; i < row ; i++)
for (int j = 0 ; j < col ; j++)
{
printf("Please enter %d:%d data:" , i + 1 , j + 1);
scanf("%lf",&ar[i][j]) ;
}
}
double ave_row(int col , double ar[col])
{
double sum = 0 ;
for (int i = 0 ; i < col ; i++ )
sum += ar[i] ;
return sum / col ;
}
double average_arr(int row , int col , double ar[row][col])
{
double sum ;
for (int i = 0 ; i < row ; i++)
for (int j = 0 ; j < col ; j++)
sum += ar[i][j];
return sum / (row * col) ;
}
void max_arr(int row , int col , double ar[row][col])
{
double max = ar[0][0];
for (int i = 0 ; i < row ; i++)
{
for (int j = 0 ; j < col ; j++)
{
if (max < ar[i][j])
max = ar[i][j];
}
}
printf("The max of array : %lf\n" , max);
}
void print_arr(int row , int col , double ar[row][col])
{
for (int i = 0 ; i < row ; i++)
{
for (int j = 0 ; j < col ; j++)
printf("%5.1lf " , ar[i][j]);
printf("\n");
}
}
*/