(C语言)数组与指针的使用
1#include <stdio.h>
2#define SIZE 5
3void CopybyArray(double ar[],double target[],int);
4void CopybyPoint(double *ar,double *target,int);
5void DisplayNum(double *array,int);
6double MaxNum(double *array,int);
7int main(void)
8{
9 double source[SIZE]={1.2,3.2,4.5,6.3,8.2};
10 double target1[SIZE],target2[SIZE];
11 double max;
12 printf("The numbers in source:");
13 DisplayNum(source,SIZE);
14 CopybyArray(source,target1,SIZE);
15 CopybyPoint(source,target2,SIZE);
16 printf("\nAfter copy the numbers:");
17 printf("\ntarget1:");
18 DisplayNum(target1,SIZE);
19 printf("\ntarget2:");
20 DisplayNum(target2,SIZE);
21
22 max=MaxNum(source,SIZE);
23 printf("max is %f.",max);
24 //two
25 double source2[3][4]={
26 {2,5,8.9,3.6},
27 {4.5,6.3,12,36},
28 {5.5,8.1,9,98}
29 };
30 double target3[3][4];
31 printf("\nbefore copy,source2:\n");
32 for(int n=0;n<3;n++)
33 {
34 DisplayNum(source2[n],4);
35 printf("\n");
36 }
37 for(int i=0;i<3;i++)
38 CopybyPoint(source2[i],target3[i],4);
39 printf("\ntarget3:\n");
40 for(int m=0;m<3;m++)
41 {
42 DisplayNum(target3[m],4);
43 printf("\n");
44 }
45 //choice
46 double source3[7]={2.3,5.6,8,9,5.8,89,12};
47 double target4[3];
48 printf("\nsource3:\n");
49 DisplayNum(source3,7);
50 CopybyPoint(&source3[2],target4,3);
51 printf("\ntarget4:");
52 DisplayNum(target4,3);
53 return 0;
54}
55void CopybyArray(double ar[],double target[],int size)
56{
57 for(int i=0;i<size;i++)
58 target[i]=ar[i];
59 return ;
60}
61void CopybyPoint(double * ar,double * target,int size)
62{
63 for(int i=0;i<size;i++)
64 {
65 *target=*ar++;
66 target++;
67 }
68 return ;
69}
70void DisplayNum(double *array,int size)
71{
72 for(int i=0;i<size;i++)
73 printf("%f\t",*array++);
74}
75double MaxNum(double *array,int size)
76{
77 double max;
78 max=0.0;
79 int p;
80 for(int i=0;i<size;i++)
81 {
82 if(*array>=max)
83 {
84 max=*array++;
85 p=i;
86 }
87 }
88 printf("number %d\n",p);
89 return max;
90}
91
2#define SIZE 5
3void CopybyArray(double ar[],double target[],int);
4void CopybyPoint(double *ar,double *target,int);
5void DisplayNum(double *array,int);
6double MaxNum(double *array,int);
7int main(void)
8{
9 double source[SIZE]={1.2,3.2,4.5,6.3,8.2};
10 double target1[SIZE],target2[SIZE];
11 double max;
12 printf("The numbers in source:");
13 DisplayNum(source,SIZE);
14 CopybyArray(source,target1,SIZE);
15 CopybyPoint(source,target2,SIZE);
16 printf("\nAfter copy the numbers:");
17 printf("\ntarget1:");
18 DisplayNum(target1,SIZE);
19 printf("\ntarget2:");
20 DisplayNum(target2,SIZE);
21
22 max=MaxNum(source,SIZE);
23 printf("max is %f.",max);
24 //two
25 double source2[3][4]={
26 {2,5,8.9,3.6},
27 {4.5,6.3,12,36},
28 {5.5,8.1,9,98}
29 };
30 double target3[3][4];
31 printf("\nbefore copy,source2:\n");
32 for(int n=0;n<3;n++)
33 {
34 DisplayNum(source2[n],4);
35 printf("\n");
36 }
37 for(int i=0;i<3;i++)
38 CopybyPoint(source2[i],target3[i],4);
39 printf("\ntarget3:\n");
40 for(int m=0;m<3;m++)
41 {
42 DisplayNum(target3[m],4);
43 printf("\n");
44 }
45 //choice
46 double source3[7]={2.3,5.6,8,9,5.8,89,12};
47 double target4[3];
48 printf("\nsource3:\n");
49 DisplayNum(source3,7);
50 CopybyPoint(&source3[2],target4,3);
51 printf("\ntarget4:");
52 DisplayNum(target4,3);
53 return 0;
54}
55void CopybyArray(double ar[],double target[],int size)
56{
57 for(int i=0;i<size;i++)
58 target[i]=ar[i];
59 return ;
60}
61void CopybyPoint(double * ar,double * target,int size)
62{
63 for(int i=0;i<size;i++)
64 {
65 *target=*ar++;
66 target++;
67 }
68 return ;
69}
70void DisplayNum(double *array,int size)
71{
72 for(int i=0;i<size;i++)
73 printf("%f\t",*array++);
74}
75double MaxNum(double *array,int size)
76{
77 double max;
78 max=0.0;
79 int p;
80 for(int i=0;i<size;i++)
81 {
82 if(*array>=max)
83 {
84 max=*array++;
85 p=i;
86 }
87 }
88 printf("number %d\n",p);
89 return max;
90}
91