使用c++语言编写一个函数实现10个数字的最大值函数。要求以传值、传指针、传引用方式分别实现
时间: 2024-05-03 19:23:04 浏览: 23
传值方式:
```c
int max(int a, int b, int c, int d, int e, int f, int g, int h, int i, int j) {
int max_val = a;
if (b > max_val) max_val = b;
if (c > max_val) max_val = c;
if (d > max_val) max_val = d;
if (e > max_val) max_val = e;
if (f > max_val) max_val = f;
if (g > max_val) max_val = g;
if (h > max_val) max_val = h;
if (i > max_val) max_val = i;
if (j > max_val) max_val = j;
return max_val;
}
```
传指针方式:
```c
int max(int *arr, int len) {
int max_val = *arr;
for (int i = 1; i < len; i++) {
if (*(arr + i) > max_val) max_val = *(arr + i);
}
return max_val;
}
```
传引用方式:
```c
int max(int &a, int &b, int &c, int &d, int &e, int &f, int &g, int &h, int &i, int &j) {
int max_val = a;
if (b > max_val) max_val = b;
if (c > max_val) max_val = c;
if (d > max_val) max_val = d;
if (e > max_val) max_val = e;
if (f > max_val) max_val = f;
if (g > max_val) max_val = g;
if (h > max_val) max_val = h;
if (i > max_val) max_val = i;
if (j > max_val) max_val = j;
return max_val;
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)