//编写一个函数,令其接受两个参数:一个是int类型的数,另一个是int类型的指针。函数比较int的值和int类型指针所指的值,返回较大的那个。
/*1.写一个myCompare()函数
2.随机产生一组100以内的数放在一个数组a[]里,输入要比较的数j
3.调用myCompare()函数输出j和数组a[]首元素中最大者
4.利用范围for循环输出数组中所有元素*/
#include<iostream>
#include<string>
#include<ctime>
#include<cstdlib>
using namespace std;
int myCompare(const int val,const int *p)
{
return(val > *p) ? val : *p;
}
int main()
{
srand((unsigned)time(NULL));
int a[10];
for (auto &i : a)
i = rand() % 100;
cout << "请输入一个数:" << endl;
int j;
cin >> j;
cout << "你输入的数和数组首元素中较大的是:" << myCompare(j, a) << endl;
cout << "数组的全部元素是:" << endl;
for (auto i : a)
cout << i << " ";
cout << endl;
return 0;
}
函数比较int的值和int类型指针所指的值,返回较大的那个
