c primer plus 习题答案(3)

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 }
时间: 2024-11-05 06:26:23

c primer plus 习题答案(3)的相关文章

c++ primer plus 习题答案(1)

第五版c++ primer plus,IDE是visual studio2013. p180.2 1 #include<iostream> 2 #include<ctime> 3 #include<cstdlib> 4 5 int main(void){ 6 using namespace std; 7 int i, j, count=0, sum = 0, pt[10]; 8 double mean; 9 for (i = 0; i < 10; i++){ 10

c primer plus 习题答案(4)

p319.3 1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<ctype.h> 4 #include<string.h> 5 void deliver(char *a1, char *a2, int n); 6 7 int main(void) 8 { 9 int n; 10 char str1[81], *ptr, ar[81]; 11 ptr=gets(str1); 12 n=strlen(ptr)

c++ primer plus 习题答案(2)

p259.4 1 #include<iostream> 2 #include<cstdlib> 3 #include<cctype> 4 using namespace std; 5 struct stringy{ 6 char* str; 7 int ct; 8 }; 9 void set(stringy &, const char*); 10 void show(const stringy &, int times = 1); 11 void sho

c primer plus 习题答案(6)

p376.7 A方案 1 #include<stdio.h> 2 #include<stdlib.h> 3 #define LEN 40 4 void rank(FILE *, FILE *); 5 int main(void) 6 { 7 FILE *fp, *fc; 8 int ch, bp; 9 char file1[LEN], file2[LEN]; 10 11 puts("enter file1 name"); 12 if((fp=fopen(gets

c primer plus 习题答案(5)

p352.6 1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 5 int main(void) 6 { 7 int i, j, count[10]={0}, seed[10]; 8 9 for(i=0; i<10; i++){ 10 printf("please enter a seed.\n"); 11 scanf("%d", &see

c primer plus 习题答案(7)

p421.5 1 #include<stdio.h> 2 #include<stdlib.h> 3 #define CSIZE 4 4 #define SCORE 3 5 6 void get_info(struct students *p); 7 void get_average(struct students *p); 8 void print_info(struct students *p); 9 void print_class_average(struct student

c++ primer plus 习题答案(8)

p475.2 //头文件: class Cd{ private: char *performers; char *label; int selections; double playtime; public: Cd(char *s1, char *s2, int n, double x); Cd(const Cd & st); Cd(); virtual ~Cd(); virtual void Report()const; Cd & operator = (const Cd & s

《C++primer》v5 第5章 语句 读书笔记 习题答案

5.1 空语句只有一个";".如果什么也不想做可以使用空语句. 5.2 用花括号{}括起来的叫块,也叫复合语句.有多条语句作用在同一个作用域时,需要用花括号括起来. 5.3 降低了. 5.4 (a)每次迭代时候会初始化iter,但是iter缺少初值,所以这段代码根本不会通过编译.另外这里的括号需要一个bool类型的,而定义迭代器根本不会返回一个bool类型.假如上面那些问题都可以通过,每次迭代都会初始化这个iter,会导致死循环. (b)我试了一下编译未通过是因为没找到适合的find函

《C++primer》v5 第3章 字符串、向量和数组 读书笔记 习题答案

3.1略 3.2 string str; //读行 while(getline(cin,str)) cout<<str<<endl; //读单个词 while(cin>>str) cout<<str<<endl; 3.3 输入运算符读到空白符结束 getline读到换行符结束,并丢弃换行符 3.4 比较大小. 比较大小是比较的第一个不相同的字符的大小. int main() { string a,b; cin>>a>>b;