码迷,mamicode.com
首页 > 其他好文 > 详细

c primer plus 习题答案(3)

时间:2015-05-24 23:35:02      阅读:249      评论:0      收藏:0      [点我收藏+]

标签:

p281.2

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #define SIZE 5
 4 void copy_arr(double ar[], double pr[],int n);
 5 void copy_ptr(double ar[], double pr[], int n);
 6 
 7 int main(void)    
 8 {
 9     double source[5]={1.1, 2.2, 3.3, 4.4, 5.5};
10     double target1[5];
11     double target2[5];
12 
13     copy_arr(source, target1, 5);
14     copy_ptr(source, target2, 5);
15     
16     system("pause");
17     return 0;
18 }
19 
20 void copy_arr(double ar[], double pr[],int n)
21 {
22     int i;
23     for(i=0; i<n; i++)
24     {
25         pr[i]=ar[i];
26         printf(" %lf", pr[i]);
27     }
28     printf("\n");
29 }
30 
31 void copy_ptr(double ar[], double pr[], int n)
32 {
33     int i;
34     for(i=0; i<5; i++)
35     {
36         *(pr+i)=*(ar+i);
37         printf(" %lf", pr[i]);
38     }
39     printf("\n");
40 }

p281.4

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #define n 5
 4 int compare(double [], int);
 5 
 6 
 7 int main(void)    
 8 {
 9     double ar[]={2.1, 3.1, 4.1, 3.2, 6.2};
10     printf("the biggest number index is %d", compare(ar, n));
11 
12     system("pause");
13     return 0;
14 }
15 
16 int compare(double ar[], int num){
17     int i, max=ar[0], j=0;
18 
19     for(i=1; i<num; i++){
20         if(max<ar[i]){
21             max=ar[i];
22             j=i;
23         }
24     }
25     return j;
26 }

p281.5

 1 #includestdio.h
 2 #includestdlib.h
 3 #define n 5
 4 double d_value(double [], int);
 5 
 6 
 7 int main(void)    
 8 {
 9     double ar[]={2.1, 3.1, 4.1, 3.2, 6.2};
10     printf(the difference value is %lf, d_value(ar, n));
11 
12     system(pause);
13     return 0;
14 }
15 
16 double d_value(double ar[], int num){
17     int i; 
18     double max=ar[0], min=ar[0];
19 
20     for(i=1; inum; i++){
21         if(maxar[i])
22             max=ar[i];
23     }
24 
25     for(i=1; inum; i++){
26         if(minar[i])
27             min=ar[i];
28     }
29     return max-min;
30 }

p281.6

A方案

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #define COLS 2
 4 #define ROWS 3
 5 void copy_ptr(double source[ROWS][COLS], double target[ROWS][COLS]);
 6 
 7 int main(void)    
 8 {
 9     int i, j;
10     double source[ROWS][COLS]={{2.1, 1.2}, {3.1, 2.3}, {4.2, 3.5}}; 
11     double target[ROWS][COLS]={0};
12     
13     for(i=0; i<ROWS; i++)
14         for(j=0; j<COLS; j++)
15     printf(" %lf", source[i][j]);
16     printf("\n");
17     copy_ptr(source, target);
18 
19     system("pause");
20     return 0;
21 }
22 
23 void copy_ptr(double source[ROWS][COLS], double target[ROWS][COLS])
24 {
25     int i, j;
26 
27     for(i=0; i<ROWS; i++)
28         for(j=0; j<COLS; j++){
29             target[i][j]=source[i][j];
30             printf(" %lf", target[i][j]);
31         }
32     printf("\n");
33 }

B方案

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #define COLS 2
 4 #define ROWS 3
 5 void copy_ptr(double source[][COLS], double target[][COLS], int);
 6 void copy_ar(double *a1, double *a2, int n);
 7 
 8 int main(void)    
 9 {
10     int i, j;
11     double source[ROWS][COLS]={{2.1, 1.2}, {3.1, 2.3}, {4.2, 3.5}}; 
12     double target[ROWS][COLS]={0};
13 
14     copy_ptr(source, target, ROWS);
15     for(i=0; i<ROWS; i++)
16         for(j=0; j<COLS; j++)
17             printf(" %lf", target[i][j]);
18     
19     system("pause");
20     return 0;
21 }
22 
23 void copy_ptr(double source[ROWS][COLS], double target[ROWS][COLS], int n)
24 {
25     int i;
26 
27     for(i=0; i<n; i++){
28         copy_ar(source[i], target[i], COLS);
29     }    
30 }
31 
32 void copy_ar(double *a1, double *a2, int n)
33 {
34     int i;
35 
36     for(i=0; i<n; i++)
37         *(a2+i)=*(a1+i);
38 }

p281.10

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #define COLS 5
 4 void dbl(int [][COLS], int [][COLS], int);
 5 void typeout(int[][COLS], int);
 6 
 7 int main(void)
 8 {
 9     int source[3][5]={{1, 2, 7, 4, 1}, {2, 3, 8, 5, 4}, {5, 6, 8, 3, 2}};
10     int ar[6][COLS]={0}, rows=3;
11 
12     dbl(source, ar, rows);
13     typeout(source, rows);
14     typeout(ar, rows);
15 
16     system("pause");
17     return 0;
18 }
19 
20 void dbl(int a1[][COLS], int a2[][COLS], int rows)
21 {
22     int i, j;
23 
24     for(i=0; i<rows; i++)
25         for(j=0; j<COLS; j++)
26             (*(a2+i))[j]=2*(*(a1+i))[j];
27 }
28 
29 void typeout(int b1[][COLS], int rows)
30 {
31     int i, j;
32     for(i=0; i<rows; i++){
33         for(j=0; j<COLS; j++)
34             printf("%d", b1[i][j]);
35     printf("\n");
36     }
37 }

p281.12

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #define COLS 3
 4 void collect(int [][COLS], int);
 5 void mean(int [][COLS], int);
 6 void tot_mean(int [][COLS], int);
 7 void max(int[][COLS], int);
 8 
 9 int main(void)
10 {
11     int rows=3;
12     int num[3][3];
13     collect(num, rows);
14     mean(num, rows);
15     tot_mean(num, rows);
16     max(num, rows);
17 
18     system("pause");
19     return 0;
20 }
21 
22 void collect(int num[][COLS], int rows)
23 {
24     int i, j;
25     for(i=0; i<rows; i++){
26         for(j=0; j<COLS; j++){
27             scanf("%d", &num[i][j]);
28             printf("%5d", num[i][j]);
29         }
30         printf("\n");
31     }
32 }
33 
34 void mean(int num[][COLS], int rows)
35 {
36     int i, j, sum;
37     double mean;
38 
39     for(i=0; i<rows; i++){
40         sum=0;
41         for(j=0; j<COLS; j++)
42             sum+=num[i][j];
43         mean=sum/5.0;
44         printf("the average is:");
45         printf("%.2\n", mean);
46     }
47 }
48 
49 void tot_mean(int num[][COLS], int rows)
50 {
51     int i, j, sum=0;
52     double mean;
53 
54     for(i=0; i<rows; i++)
55         for(j=0; j<COLS; j++)
56             sum+=num[i][j];
57     mean=sum/15.0;
58     printf("the tot_mean is:");
59     printf("%.2lf\n", mean);
60 }
61 
62 void max(int num[][COLS], int rows)
63 {
64     int i, j, max=num[0][0];
65 
66     for(i=0; i<rows; i++)
67         for(j=0; j<COLS; j++)
68             if(max<num[i][j])
69                 max=num[i][j];
70     printf("the largest number is %d\n", max);
71 }

c primer plus 习题答案(3)

标签:

原文地址:http://www.cnblogs.com/coding-time/p/4526621.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!