第五章

  •  实验作业

1.调试分析课本每一个例题,有可能的话更改成2-3个方法的新程序;

2.编程实现课本每一个编程习题。

3. 编程实现输入两个4X5矩阵和5X3矩阵,定义函数并在主函数中调用计算它们的积。

4.编程计算S[n]=1!+21+3!+...n!。要求定义两个函数,一个计算n!,一个计算s[n],在后一个函数中调用前一个函数。然后在主程序中输入数n的值,然后调用定义函数输出结果。

5.编写一个函数,输入一个十六进制数,输出相应的十进制数。

1、

例1

#include<iostream>
using namespace std;

void display()
{
	cout<<"This is an example."<<endl;
}
int main()
{
	display();
	return 0;
}


例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;
}


例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;
}


例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

#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<<endl;
	return 0;
}


例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= "<<b<<endl;
	cout<<"Max is "<<c<<endl;

	return 0;
}


例7

#include<iostream>
using namespace std;

int sqr(int x)
{
	x=x*x;
	return x;
}

int main()
{
	int t=10;
	int s=sqr(t);
	cout<<"t="<<t<<'\t'
		<<"sqr("<<t<<")="<<s<<endl;

	return 0;
}


例8

#include<iostream>
using namespace std;

void swap(int u,int v);

int main()
{
	int a=3;
	int b=4;
	cout<<"a="<<a<<"\tb="<<b<<endl;
	swap(a,b);
	cout<<"a="<<a<<"\tb="<<b<<endl;

	return 0;
}

void swap(int u,int v)
{
	int temp;
	temp=u;
	u=v;
	v=temp;
}


例9

#include<iostream>
using namespace std;

void swap(int &u,int &v);

int main()
{
	int a=3;
	int b=4;
	cout<<"a="<<a<<"\tb="<<b<<endl;
	swap(a,b);
	cout<<"a="<<a<<"\tb="<<b<<endl;

	return 0;
}

void swap(int &u,int &v)
{
	int temp=v;
	v=u;
	u=temp;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值