Exercise 10:Pointers(修改一次)

站在巅峰才能看到最美的风景! Happy Coding!!!

  闲聊两句。
  也没什么想说的,就不说了。

  1. Write a program that reads 5 integers into an array, and then uses four different methods of accessing the members of an array to print them out in reverse order.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <stdio.h>
int main()
{
int num1[5],num2[5],i;
int *p=num2;
for(i=0;i<5;i++)
scanf("%d",&num1[i]);
for(i=0;i<5;i++)
num2[i]=num1[4-i];

for(i=0;i<5;i++)
printf("%d ",num2[i]);
printf("\n");
for(i=0;i<5;i++)
printf("%d ",*(p+i));
printf("\n");
for(i=0;i<5;i++)
printf("%d ",*(num2+i));
printf("\n");
for(i=0;i<5;i++)
printf("%d ",p[i]);
printf("\n");
return 0;
}

  1. Write a program that reads 8 floats into an array and then prints out the second, fourth, sixth and eighth members of the array, and the sum of the first, third, fifth and seventh, using pointers to access the members of the array.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    #include <stdio.h>
    int main()
    {
    float num[8],*p=num,sum=0;
    int i;
    for(i=0;i<8;i++)
    scanf("%f",&num[i]);
    for(i=1;i<8;i=i+2)
    printf("%f ",*(p+i));
    printf("\n");
    for(i=0;i<8;i=i+2)
    sum+=*(p+i);
    printf("%f\n",sum);
    return 0;
    }

  2. Write a program that use a SINGLE FUNCTION (用一个函数)to find and return simultaneously both the lowest and highest values in an array of type int. Suppose the size of the array is 6.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    #include <stdio.h>
    //哈哈哈,这么写不知道老师会不会打我 ╮(╯▽╰)╭
    int find(int *num,int a);
    int main()
    {
    int num[6],i;
    for(i=0;i<6;i++)
    scanf("%d",&num[i]);
    printf("The lowest values in an array is %d.\n",find(num,1));
    printf("The highest values in an array is %d.\n",find(num,0));
    return 0;
    }

    int find(int *num,int a){
    int max=num[0],min=num[0],i;
    if(a==0)
    {
    for(i=0;i<6;i++)
    {
    if(max<num[i])
    max=num[i];
    }
    return max;
    }
    else
    {
    for(i=0;i<6;i++)
    {
    if(min>num[i])
    min=num[i];
    }
    return min;
    }
    }

    ↓↓下面这个是认真的↓↓
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <stdio.h>
int* find(int* num);
int result[2];
int *p = result;
//int *p and result are globel variable, or it will warning.
int main()
{
int num[6],i,max,min;
for(i=0;i<6;i++)
scanf("%d",&num[i]);
find(num);
printf("The highest values in an array is %d.\n",p[0]);
printf("The lowest values in an array is %d.\n",p[1]);
//The print cannot be p, it should be for(i=0;i<2;i++) {printf("%d ",*(p+i));}
//But you cannot print some different sentence each time.
//Or like what I write.
return 0;
}

int* find(int* num){
int i;
for(i=0;i<2;i++)
result[i]=num[0];
//You must write this, or if you put all positive integers, the lowest number is 0.
for(i=0;i<6;i++)
{
if(result[0]<num[i])
result[0]=num[i]; //result[0] is the highest number.
if(result[1]>num[i])
result[1]=num[i]; //result[1] is the lowest number.
}
return p; // We have known p is pointer, so we don't need write it as *p.
}