任务一
#include<iostream>
using namespace std;
int main()
{
double ted=20.2;
double *ptr = &ted;
cout << "ted=" << ted << endl;
cout << "*ptr=" << *ptr << endl;
return 0;
}
任务二
#include<iostream>
using namespace std;
void ptr2()
{
float treacle[10]={1,2,3,4,5,6,7,8,9,0},*ptr;
ptr=&treacle[0];
cout<<"first="<<*ptr<<endl;
cout<<"final="<<*(ptr+9)<<endl;
}
int main()
{
ptr2();
return 0;
}
任务三
#include<iostream>
using namespace std;
void fun1(int a,int b,int c,int *one,int *two,int *three)
{
if(a<b)
if(a<c)
{
*one=a;
if(b<c)
{
*two=b;
*three=c;
}
}
else
{
*one=c;
*two=a;
*three=b;
}
else if(b>c)
{
*one=c;
*two=b;
*three=a;
}
else if(a>c)
{
*one=b;
*two=c;
*three=a;
}
else
{
*one=b;
*two=a;
*three=c;
}
}
int main()
{
int a=10,b=9,c=8,x,y,z;
fun1(a,b,c,&x,&y,&z);
cout<<x<<' '<<y<<' '<<z<<endl;
}
方案二
#include<iostream>
using namespace std;
void fun1(int *a,int *b,int *c)
{
int t;
if (*a > *b)
{
t = *a;
*a = *b;
*b = t;
}
if (*a > *c)
{
t = *a;
*a = *c;
*c = t;
}
if (*c < *b)
{
t = *b;
*b = *c;
*c = t;
}
}
int main()
{
int a = 10, b = 9, c = 8;
fun1(&a, &b, &c);
cout << a << ' ' << b << ' ' << c << endl;
}
任务四
#include<iostream>
using namespace std;
double fun2(int *p,int n)
{
double sum=0;
for(int i=0;i<n;i++)
{
sum+=p[i];
}
return sum/n;
}
int main()
{
int f[10]={1,2,3,4,5,6,7,8,9,10};
cout<<fun2(f,10)<<endl;
return 0;
}