5-1
#include <iostream>
using namespace std;
void display()
{
cout<<"hi,everyone"<<endl;
}
int main()
{
display();
return 0;
}
5-2
#include <iostream>
using namespace std;
double min(double x,double y)
{
return x<y?x:y;
}
int main()
{
cout<<min(2.0,55.3)<<endl;
return 0;
}
5-3
#include <iostream>
using namespace std;
double circleArea(double);
int main()
{
double area=circleArea(5.0);
cout<<"area = "<<area<<endl;
return 0;
}
double circleArea(double r)
{
double pi=3.14;
double area=pi*r*r;
return area;
}
5-4
#include <iostream>
using namespace std;
int sum (int x,int y)
{
int temp;
temp=x+y;
return temp;
}
int main()
{
int a,b,c;
a=10;b=5;
c=sum(a,b);
cout<<a<<" + "<<b<<" = "<<c<<endl;
return 0;
}
5-5
#include <iostream>
using namespace std;
int ncomp(int i,int j)
{
if(i>j)return 1;
if(i==j)return 0;
return -1;
}
int main()
{
int k=2,y=1;
int n=ncomp(y,k);
cout<<n<<endl;
return 0;
}
5-6
#include <iostream>
using namespace std;
int max(int i,int j)
{
int y;
return y=i>j?i:j;
}
int main()
{
int a,b,c;
cout<<"input two numbers: "<<endl;
cin>>a>>b;
c=max(a,b);
cout<<"a= "<<a<<"b= "<<b<<endl;
cout<<"max= "<<c<<endl;
return 0;
}
5-7
#include <iostream>
using namespace std;
int sqrt(int i)
{
i=i*i;
return i;
}
int main()
{
int t=10;
int s=sqrt(t);
cout<<"t= "<<t<<'\t'<<"sqrt("<<t<<")= "<<s<<endl;
return 0;
}
5-8
#include <iostream>
using namespace std;
void swap(int u,int v);
int main()
{
int a=3;
int b=4;
cout<<"a= "<<a<<'\t'<<"b= "<<b<<endl;
swap(a,b);
cout<<"a= "<<a<<'\t'<<"b= "<<b<<endl;
}
void swap(int u,int v)
{
int temp;
temp=v,v=u,u=temp;
}
5-9
#include <iostream>
using namespace std;
void swap(int &u,int &v);
int main()
{
int a=3;
int b=4;
cout<<"a= "<<a<<'\t'<<"b= "<<b<<endl;
swap(a,b);
cout<<"a= "<<a<<'\t'<<"b= "<<b<<endl;
}
void swap(int &u,int &v)
{
int temp;
temp=v,v=u,u=temp;
}
5-10
#include <iostream>
using namespace std;
void display(int u,int v)
{
cout<<u<<" "<<v<<endl;
return;
}
int main()
{
float a;
int b ;
cin>>b>>a;
display(b,a);
return 0;
}
5-13
#include <iostream>
using namespace std;
long f2(int);
long f1(int a)
{
long b,r;
b=a*a;
r=f2(b);
return r;
}
long f2(int c)
{
long fact=1;
for(int i=1;i<=c;i++)
fact*=i;
return fact;
}
int main()
{
int e;
long sum=0;
for(e=2;e<=3;e++)
sum+=f1(e);
cout<<"sum= "<<sum<<endl;
return 0;
}