请编写一个函数void fun(int tt[M][N],int pp[N]),tt指向一个M行N列的二维函数组,求出二维函数组每列中最小元素,并依次放入pp所指定一维数组中。二维数组中的数已在主函数中赋予。

请编写一个函数void fun(int tt[M][N],int pp[N]),tt指向一个M行N列的二维函数组,求出二维函数组每列中最小元素,并依次放入pp所指定一维数组中。二维数组中的数已在主函数中赋予。

#include <stdio.h>
#define M 3
#define N 4
void fun(int tt[M][N], int pp[N]) {
    for (int j = 0; j < N; j++) {
        int min = tt[0][j]; 
        for (int i = 0; i < M; i++) {
            if (tt[i][j] < min) {
                min = tt[i][j];
            }
        }
        pp[j] = min;
    }
}

int main() {
    int tt[M][N] = {{3, 8, 5, 1}, {9, 2, 4, 7}, {6, 0, 2, 3}};
    int pp[N]; 

    fun(tt, pp);

    printf("每列中的最小元素:\n");
    for (int j = 0; j < N; j++) {
        printf("%d ", pp[j]);
    }

    return 0;
}

posted on 2024-06-14 23:42  wessf  阅读(88)  评论(0)    收藏  举报