第七周作业

5.1

#include<iostream>
using namespace std;
void display()
{
	cout<<"This is an example."<<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(6.0,5.0)<<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;  
    int n=ncomp(k,k++);  
    cout<<n;  
     
    return 0;  
}  


 5.6

#include<iostream>
using namespace std;
int max(int u,int v)
{
	int w;
	w=u>v?u:v;
	return w;
}
int main()
{
	int a,b,c;
	cout<<"please input two numbers:";
	cin>>a>>b;
	c=max(a,b);
	cout<<"a="<<a<<"b="<<endl;
	cout<<"max is"<<c<<endl;
	return 0;
}


5.7

#include<iostream>
using namespace std;
int spr(int x)
{
	x=x*x;
	return x;
}
int main()
{
	int t=10;
	int s=spr(t);
	cout<<"t="<<t<<endl
		<<"spr("<<t<<")="<<s<<endl;
	return 0;
}


书上例题过于繁多,也就不一一打上。

下面是书上练习题

1.说明程序的执行过程和运算结果

#include<iostream>
#include<cmath>
using namespace std;
double squ(double x);
int main()
{
	double x;
	cout<<"please input x:";
	cin>>x;
	cout<<"the spuare root of"<<x<<"is"<<squ(x)<<endl;
	return 0;
}
double squ(double x)
{
	double s1,s2;
	s1=0.5*(1.0+x);
	do{
		s2=s1;
		s1=(s2+x/s2)*0.5;
	}while(fabs(s2-s1)>1.0E-6);
	return s1;
}


2.求整数次幂的函数

#include<iostream>
using namespace std;
long intpower(int base,int exponent);
int main()
{
	long n;
	int base,exponent;
	cout<<"请输入基数及其幂:";
	cin>>base>>exponent;
	n=intpower(base,exponent);
	cout<<base<<"的"<<exponent<<"次幂是"<<n<<endl;
	return 0;
}
long intpower(int base,int exponent)
{
	long result=1;
	int i;
	if(exponent!=0)
		for(i=1;i<=exponent;i++)
			result*=base;
		return result;
}


5.编写一个函数,按照所给的百分制分数,返回与该分数对应的等级代号字符。

#include<iostream>
using namespace std;
char grade(int);
int main()
{
	int score;
	cout<<"请输入百分制成绩(1~100):";
	cin>>score;
	char ch=grade(score);
	cout<<score<<"对应的等级为:"<<ch<<endl;
	return 0;
}
char grade(int n)
{
	char ch;
	if(n>=90&&n<=100)ch='A';
	else if(n>=80)ch='B';
	else if(n>=70)ch='C';
	else if(n>=60)ch='D';
	else ch='E';
	return ch;
}


6.编写一个函数,内放10个学生成绩,求平均成绩。

#include<iostream>
using namespace std;
char grade(int);
int main()
{
	int score;
	cout<<"请输入10个学生的成绩:"<<endl;
	cin>>score;
	char ch=grade(score);
	return 0;
}
char grade(int)
{
	int sum=0,score;
	sum+=score;
	score++;
	cout<<"这10个学生的成绩平均值为:"<<sum/10<<endl;
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值