C++primer习题集

练习6.21:编写一个函数,令其接受两个参数:一个是int型的数,另一个是int 指针。函数比较int的值和指针所指的值,返回较大的那个。在该函数中指针的类型应该是什么?

#include<iostream>
using namespace std;
#include<string>
int myCompare(const int a, const int* p) {
	return(a > *p) ? a : *p;
}
int main() {
	srand((unsigned)time(NULL));//随着系统的时间生成随机数
	int a[10];//定义一个数组接受随机数
	cout << "请输入一个数;";
	int c;
	cin >> c;
	//用c接收随机数,然后修改a[10];
	for (auto& c : a) {
		c = rand() % 100;
	}
	cout<<"您输入的数和数组首个元素中较大的是"<<myCompare(c, a)<<endl;		
	cout << "数组中的全部元素是;";
	for (auto c : a) {
		cout << c << " ";
	}
	
}

本题要点:srand((unsigned))time(NULL)是随着系统的时间设置随机数种子,rand()是根据种子生成随机数。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值