从键盘输入 10个整数,调用 fun函数找出其中最小的数,并在 main函数中输出。请编写函数 fun,返回找到的最小值,c语言
时间: 2024-03-15 18:45:57 浏览: 90
以下是找出10个整数中最小值的C语言代码:
```c
#include <stdio.h>
int fun(int arr[], int n) {
int min = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] < min) {
min = arr[i];
}
}
return min;
}
int main() {
int arr[10];
printf("请输入10个整数:\n");
for (int i = 0; i < 10; i++) {
scanf("%d", &arr[i]);
}
int min = fun(arr, 10);
printf("最小值为:%d\n", min);
return 0;
}
```
在函数fun()中,使用一个变量min来记录当前最小值,初始值为数组中的第一个元素,然后遍历数组,如果发现有比min更小的数,就将min更新为该数。最后返回min的值。在主函数中,先输入10个整数,然后调用函数fun(),找出数组中最小的数,最后输出结果。
相关问题
从键盘输入 10个整数,调用 fun函数找出其中最小的数,并在 main函数中输出。请编写函数 fun,返回找到的最小值。
好的,以下是实现这个功能的C语言代码:
```c
#include <stdio.h>
int fun(int arr[], int len) {
int min = arr[0];
for (int i = 1; i < len; i++) {
if (arr[i] < min) {
min = arr[i];
}
}
return min;
}
int main() {
int arr[10];
printf("请输入10个整数:\n");
for (int i = 0; i < 10; i++) {
scanf("%d", &arr[i]);
}
int min = fun(arr, 10);
printf("最小值为:%d\n", min);
return 0;
}
```
在这个代码中,我们首先定义了一个函数`fun`,它的参数`arr`表示要查找最小值的数组,`len`表示数组的长度。在函数中,我们首先将数组的第一个元素设为最小值,然后遍历数组,逐个比较每个元素和当前最小值的大小,如果发现有比当前最小值更小的元素,就更新最小值。最后,返回最小值。
在主函数中,我们首先定义一个长度为10的数组`arr`,然后通过`scanf`函数从键盘读入10个整数。接着,我们调用`fun`函数找到数组中的最小值,并将其存储在变量`min`中。最后,使用`printf`函数输出最小值。
C语言从键盘输入 10个整数,调用 fun函数找出其中最小的数,并在 main函数中输出。请编写函数 fun,返回找到的最小值。
以下是一个可能的实现:
```c
#include <stdio.h>
int fun(int arr[], int n) {
int min = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] < min) {
min = arr[i];
}
}
return min;
}
int main() {
int arr[10];
printf("请输入10个整数:\n");
for (int i = 0; i < 10; i++) {
scanf("%d", &arr[i]);
}
int min = fun(arr, 10);
printf("最小值为:%d\n", min);
return 0;
}
```
这里定义了一个 `fun` 函数,它接受一个整型数组和数组长度作为参数,返回数组中最小的数。在 `main` 函数中,先从键盘读入 10 个整数,然后调用 `fun` 函数找到最小值并输出。
阅读全文
相关推荐
















