栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

自学C第16天

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

自学C第16天

C primer plus

第10章

第1题:

#include

#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;

       float(*pr)[MONTHS];

       pr = rain;

       printf("YEAR    RAINFALL  (inches)n");

       for (year = 0, total = 0; year < YEARS; year++)

       {

              for (month = 0, subtot = 0; month < MONTHS; month++)

              {

                     subtot += *(*(pr + year) + month); #注:(pr+year),其中year是数组的列,当year=1时,pr+0表示该数组的第一列, 所以*(pr+0)也就表示rain[0]它是一个地址,表示rain[0][0]的值的地址,也就是&rain[0][0]当让其+month的话就会列遍数组每个月对于的值的地址,之后再用一个解引用,可以表示每个月的值。二维数组的指针名可以记忆为rain[m][n]=*(*(rain+m)+n)

              }

              printf("%5d %15.1fn", 2010 + year, subtot);

              total += subtot;

       }

       printf("nThe yearly average is %.1f inches.nn",total / YEARS);

       printf("MONTHLY AVERAGES:nn");

       printf("JAN FEB MAR APR MAY JUN JUL AUG SEP OCT");

       printf("NOV DECn");

       for (month = 0; month < MONTHS; month++)

       {

              for (year = 0, subtot = 0; year < YEARS; year++)

              {

                     subtot += *(*(pr + year) + month);

              }

                        printf("%4.1f", subtot / YEARS);

       }

       printf("n");

       return 0;

             

}

第2题:

#include

void copy_arr(double x[], double source[], int n);#注:也可以把x[]定义成x*两者没有区别都表示地址。

void copy_ptr(double *y, double source[], int n);

void copy_ptrs(double *z, double source[], double *b);

int main(void)

{

       double target1[5];

       double target2[5];

       double target3[5];

       double source[5] = { 1.1,2.2,3.3,4.4,5.5 };

       copy_arr(target1, source, 5); #注:函数中的前两个实参都相当于指针,也就地址。

       copy_ptr(target2, source, 5);

       copy_ptrs(target3, source, (source + 5));

}

void copy_arr(double x[], double source[], int n)

{

       int i;

       for (i = 0; i < n; i++) {

              x[i] = source[i];

              printf("%g ", x[i]);

       }

}

void copy_ptr(double* y, double source[], int n)

{

       int i;

       putchar('n');

       for (i = 0; i < n; i++)

       {

              *(y + i) = *(source + i);

              printf("%g ", *(y + i));

       }

}

void copy_ptrs(double* z, double source[], double* b)

{

       int i;

       putchar('n');

       for (i = 0; i < b-source; i++) {

              *(z + i) = *(source + i);

              printf("%g ", *(z + i));

       }

}

第3题:

#include

int get_max(int target[], int n);

int main (void){

       int m;

       int source[5] = { 1,2,3,3,3 };

       int size = sizeof source / sizeof source[0]; #注:sizeof运算符给出它运算对象的大小,sizeof source是整个数组的大小也是4*5为20,source[0]是数组中一个元素的大小为4.

       m=get_max(source, size);

       printf("The max number of this list is %d", m);

       return 0;

}

int get_max(int target[],int n) {

       int i;

       int max = target[0];  #注:一开始是尝试直接用sizeof target/sizeof target[0]表示数组长度的,但由于该函数中定义的target其实是一个指针,所以它的sizeof 大小是8字节,而target[0]是一个整数值,是4字节,结果就是2,所以定义数组的长度需要用到数组的名字。所以需要在主函数中计算数组长度。

       for (i = 0; i < n; i++) {

              if (max < target[i]) {

                     max = target[i];

              }

      

       }

       return max;

}

第4题:

#include

int get_max(double target[], int n);

int main(void) {

       int m;

       double source[] = { 2.3,5.6,7.8,9.0,11.2,15.5,10.1,11.2,13.3 };

       int size = sizeof source / sizeof source[0];

       m = get_max(source, size);

       printf("The max number is %g, and it is the %dth of the list",source[m], m);

       return 0;

}

int get_max(double target[], int n) {

       int i;

       int mark;

       double max = target[0];

       for (i = 0; i < n; i++) {

              if (max < target[i] ) {

                     max = target[i];

                     mark = i;

              }

       }

       return mark;

}

第5题:

#include

double get_diff(double target[], int n);

int main(void) {

       double d;

       double source[] = { 2.3,5.6,7.8,9.0,11.2,15.5,10.1,11.2,13.3 };

       int size = sizeof source / sizeof source[0];

       d = get_diff(source, size);

       printf("The max number - min number in the list is %g", d);

       return 0;

}

double get_diff(double target[], int n) {

       int i, j;

       double max, min, diff;

       max = target[0];

       min = target[0];

       for (i = 0; i < n; i++) {

              if (max < target[i]) {

                     max = target[i];

              }

       }

       for (j = 0; j < n; j++) {

              if (min > target[j]) {

                     min = target[j];

              }

       }

       diff = max - min;

       return diff;

}

第6题:

#include

#include

void copy_arr(double x[][5], double source[][5], int n); #注:用数组表示法

void copy_ptr(double(*y)[5], double(*source)[5], int n);#注:指针表示法

int main(void)

{

       double target1[2][5];

       double target2[2][5];

       double source[2][5] = { {1.1,2.2,3.3,4.4,5.5},

                                                 {7.7,8.8,9.9,10.1,11.1} };

       copy_arr(target1, source, 5);

       copy_ptr(target2, source, 5);

       return 0;

}

void copy_arr(double x[][5], double source[][5], int n)

{

       int i;

       int j;

       for (i = 0; i < 2; i++) {

              putchar('n');

              for (j = 0; j < n; j++) {

                     x[i][j] = source[i][j];

                     printf("%g ", x[i][j]);

              }

       }

}

void copy_ptr(double(* y)[5], double(*source)[5], int n)

{

       int i,j;

       putchar('n');

       for (i = 0; i < 2; i++){

              putchar('n');

              for (j = 0; j < n; j++) {

                     *(*(y+i) + j) = *(*(source+i) + j);

                     printf("%g ", *(*(y + i) + j));

              }

       }

}

第7题:

#include

void copy_ptr(double* y, double source[], int n);

int main(void)

{

       double target[3];

       double source[] = { 1.1,2.2,3.3,4.4,5.5,6.6,7.7 };

       copy_ptr(target, source, 3);

       return 0;

}

void copy_ptr(double* y, double source[], int n)

{

       int i;

       for (i = 0; i < n; i++) {

              *(y + i) = *(source + 2 + i);

              printf("%g ", *(y + i));

       }

}

第8题:

#include

void copy_ptr(double* y, double source[], int n);

int main(void)

{

       double target[3];

       double source[] = { 1.1,2.2,3.3,4.4,5.5,6.6,7.7 };

       copy_ptr(target, source, 3);

       return 0;

}

void copy_ptr(double* y, double source[], int n)

{

       int i;

       for (i = 0; i < n; i++) {

              *(y + i) = *(source + 2 + i);

              printf("%g ", *(y + i));

       }

}

第9题:#注:VS编译器不支持变长数组,所以这题和第7题类似

#include

void copy_arr(int rows, int cols, double ar[][5],double cr[][5]);

int main(void) {

       double source[][5] = { {1,2,3,4,5},{5,4,3,2,1},{10,9,8,7,6} };

       double target[3][5];

       copy_arr(3, 5, source, target);

       return 0;

}

void copy_arr(int rows, int cols, double ar[][5], double cr[][5]) {

       int i, j;

       for (i = 0; i < rows; i++) {

              putchar('n');

              for (j = 0; j < cols; j++) {

                     cr[i][j] = ar[i][j];

                     printf("%g ", cr[i][j]);

              }

       }

}

第10题:

#include

#define SIZE 4

void get_arr(double ar[], double cr[], int n);

int main(void) {

       double ar[] = { 1,5,4,8 };

       double cr[] = { 5,5.6,8.3,8.8 };

       get_arr(ar, cr, SIZE);

       return 0;

}

void get_arr(double ar[], double cr[], int n) {

       int i;

       double sum_arr[SIZE]; #注:C语言貌似无法直接声明一个空的数组,必须要一个列。

       for (i = 0; i < n; i++) {

              sum_arr[i] = ar[i] + cr[i];

              printf("%g ", sum_arr[i]);

       }

}

加油!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/942930.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号