当前位置:   article > 正文

C++primer习题集

c++primer习题集

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

  1. #include<iostream>
  2. using namespace std;
  3. #include<string>
  4. int myCompare(const int a, const int* p) {
  5. return(a > *p) ? a : *p;
  6. }
  7. int main() {
  8. srand((unsigned)time(NULL));//随着系统的时间生成随机数
  9. int a[10];//定义一个数组接受随机数
  10. cout << "请输入一个数;";
  11. int c;
  12. cin >> c;
  13. //用c接收随机数,然后修改a[10];
  14. for (auto& c : a) {
  15. c = rand() % 100;
  16. }
  17. cout<<"您输入的数和数组首个元素中较大的是"<<myCompare(c, a)<<endl;
  18. cout << "数组中的全部元素是;";
  19. for (auto c : a) {
  20. cout << c << " ";
  21. }
  22. }

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

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号