在C语言中,可以通过遍历数组的方式找到数组中的最大值和对应的下标。以下是一个示例代码:

#include <stdio.h>

int main() {
    int arr[] = {10, 20, 30, 40, 50};
    int size = sizeof(arr) / sizeof(arr[0]);
    int max = arr[0];
    int max_index = 0;

    for (int i = 1; i < size; i++) {
        if (arr[i] > max) {
            max = arr[i];
            max_index = i;
        }
    }

    printf("最大值为:%d\n", max);
    printf("最大值的下标为:%d\n", max_index);

    return 0;
}

在这个示例代码中,我们定义了一个整型数组 arr,并使用 size 变量存储数组的大小。然后我们初始化 maxmax_index 变量为数组的第一个元素和下标0。

在 for 循环中,我们遍历数组从下标1开始,逐个比较数组元素和 max 变量的大小。如果当前元素大于 max,则更新 maxmax_index 的值。

最后,我们输出最大值和对应的下标。运行以上代码,会输出:

最大值为:50
最大值的下标为:4
``
for循环里面的数组最大值下标怎么求 c语言

原文地址: https://www.cveoy.top/t/topic/hPoh 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录