练习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()是根据种子生成随机数。