有输入参数,无返回值 要求:写一个函数,用于将3个数进行排序,将排序结果进行打印,并在主函 数测试它。
时间: 2023-06-15 20:07:11 浏览: 100
下面是一个用于将三个数进行排序并打印的函数示例:
```c++
void sortAndPrint(int a, int b, int c) {
int arr[3] = {a, b, c};
std::sort(arr, arr + 3);
for(int i = 0; i < 3; i++) {
std::cout << arr[i] << " ";
}
std::cout << std::endl;
}
```
该函数接受三个整数作为输入参数,将它们存储在一个数组中,然后使用 C++ STL 中的 std::sort() 函数将它们进行排序。最后,函数遍历排序后的数组,并将其打印到屏幕上。
为了测试该函数,我们可以在主函数中调用它:
```c++
int main() {
sortAndPrint(3, 1, 4); // 打印 1 3 4
sortAndPrint(9, 2, 5); // 打印 2 5 9
sortAndPrint(7, 7, 1); // 打印 1 7 7
return 0;
}
```
该代码将分别调用 sortAndPrint() 函数三次,每次传递不同的参数。输出结果将分别是按从小到大排序后的数字列表。
阅读全文
相关推荐
![c](https://img-home.csdnimg.cn/images/20250102104920.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044901.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)