使用c语言 (2)使用指针设计一个函数fun的功能是:把主函数中输入的3个数,最大的放在a中,最小的放在。 中。例如,输入的数为:55 12 34,输出结果应当是:a=55.0,b=34.0,c=12.0
时间: 2023-03-22 18:01:03 浏览: 288
好的,这是一个关于使用C语言指针的问题。下面是一个可能的解答:
```c
#include <stdio.h>
void fun(float *a, float *b, float *c) {
float tmp;
if (*a < *b) {
tmp = *a;
*a = *b;
*b = tmp;
}
if (*a < *c) {
tmp = *a;
*a = *c;
*c = tmp;
}
if (*b < *c) {
tmp = *b;
*b = *c;
*c = tmp;
}
}
int main() {
float a, b, c;
printf("请输入三个数:");
scanf("%f%f%f", &a, &b, &c);
fun(&a, &b, &c);
printf("最大值:%f,中间值:%f,最小值:%f\n", a, b, c);
return 0;
}
```
上述代码定义了一个函数`fun`,该函数使用指针参数,可以将输入的三个数按照大小顺序存储到`a`、`b`、`c`三个变量中。在`main`函数中,首先输入三个数,然后调用`fun`函数,最后输出结果。
相关问题
生成c语言使用指针设计一个函数fun的功能是:把主函数中输入的3个数,最大的放在a中,最小的放在c中。例如输入的数为55 12 34, 输出结果应当是:a=55.0, b=34.0, c=12.0。
以下是函数fun的代码实现:
```
void fun(float *a, float *b, float *c) {
float temp;
if (*a < *b) {
temp = *a;
*a = *b;
*b = temp;
}
if (*a < *c) {
temp = *a;
*a = *c;
*c = temp;
}
if (*b > *c) {
temp = *b;
*b = *c;
*c = temp;
}
}
```
在主函数中,你可以这样调用函数fun:
```
float a, b, c;
scanf("%f %f %f", &a, &b, &c);
fun(&a, &b, &c);
printf("a=%.1f, b=%.1f, c=%.1f\n", a, b, c);
```
这样就可以得到正确的输出结果了。
c语言 通过函数fun使用指针计算并返回数组最大值的位置。在主函数中输出最大值及其位置
可以使用如下的代码实现:
```c
#include <stdio.h>
int *fun(int arr[], int n) {
int max_index = 0;
for (int i = 1; i < n; i++) {
if (arr[i] > arr[max_index]) {
max_index = i;
}
}
return &arr[max_index];
}
int main() {
int arr[] = {3, 8, 2, 5, 1, 7, 9, 4, 6};
int n = sizeof(arr) / sizeof(int);
int *max_ptr = fun(arr, n);
printf("最大值为:%d,位置为:%ld\n", *max_ptr, max_ptr - arr);
return 0;
}
```
首先,在 `fun` 函数中通过循环遍历整个数组,找到最大值的位置,然后返回该位置对应的指针。
在主函数中,调用 `fun` 函数得到最大值的指针,再通过指针减去数组首地址得到最大值的位置,最后输出即可。
相关推荐
![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_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)