Exercise 10:Pointers(修改一次)
闲聊两句。
也没什么想说的,就不说了。
- 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 |
|
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;
}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 |
|
本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!