如何获取每列中的最小元素并将它们分配到C中的新数组?

时间:2023-01-09 19:13:52

I have a C program which generates random numbers and fills array 5X5. How I can get the smallest element form each column and fill new array? My program is finding only the smallest number from the whole array matrix.

我有一个C程序,它生成随机数并填充数组5X5。如何从每列中获取最小元素并填充新数组?我的程序只找到整个数组矩阵中的最小数字。

#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#define M 5

void print(int (*array)[M]);
int smallest(int array[M][M]);

int main()
{
    srand(time(NULL));
    int array[M][M], minScore;
    for(int i=0; i<M; i++)
    {
        for(int k = 0; k<M; k++)
        {
            array[i][k] =-10 + rand() % 21;
        }
    }
    print(array);
    minScore = smallest(array);
    printf("Minimum is: %i\n", minScore);
    system("pause");
    return 0;
}

void print(int (*array)[M])
{
    for(int i=0;i<M;i++)
    {
        for(int k=0; k<M; k++)
        {
            printf("%d ",array[i][k]);
        }
        printf("\n");
    }
}

int smallest(int array[M][M])
{
    int minValue;

    minValue = array[0][0];

    for(int i = 0; i < M; i++)
    {
        for(int k = 0; k < M; k++)
        {
            if ( array[i][k] < minValue)
                minValue = array [i][k];
        }
    }
    return minValue;

}

3 个解决方案

#1


1  

You need to refresh the minValue after traversing each and every column, and assign them to the array after every column. Since, this has to be done for every column.

遍历每一列后,您需要刷新minValue,并在每列之后将它们分配给数组。因为,必须为每一列完成此操作。

for(int i = 0; i < M; i++)
{
    minValue = array[0][i];
    for(int k = 0; k < M; k++)
    {
        if ( array[k][i] < minValue) 
            //here we are using array[k][i] and not array[i][k] since you need to traverse column wise
            minValue = array [k][i];
    }
    //--> here assign the minValue to the array you want
}

#2


1  

Write a function int smallestInColumn(int array[M][M], int k) where k is the column you want to inspect. Within that, just run the i loop.

写一个函数int smallestInColumn(int array [M] [M],int k)其中k是你要检查的列。在其中,只需运行i循环。

(Then refactor int smallest to call that function when looping just over k).

(然后重构int最小,以便在循环刚好超过k时调用该函数)。

By the way, using % 21 to generate a random number will introduce statistical bias, unless your random number generator's periodicity is a multiple of 21, which is unlikely.

顺便说一下,使用%21生成一个随机数会引入统计偏差,除非你的随机数发生器的周期是21的倍数,这是不太可能的。

#3


0  

You can do this something like this

你可以这样做

//...

int smallest( int a[M][M], int column )
{
    int minValue = a[0][column];

    for ( int i = 1; i < M; i++ )
    {
        if ( a[i][column] < minValue ) minValue = a[i][column];
    }

    return minValue;
}

int main( void )
{
    int a[M][M];
    int b[M];

    //...

    for ( int i = 0; i < M; i++ ) b[i] = smallest( a, i );

    //...
} 

#1


1  

You need to refresh the minValue after traversing each and every column, and assign them to the array after every column. Since, this has to be done for every column.

遍历每一列后,您需要刷新minValue,并在每列之后将它们分配给数组。因为,必须为每一列完成此操作。

for(int i = 0; i < M; i++)
{
    minValue = array[0][i];
    for(int k = 0; k < M; k++)
    {
        if ( array[k][i] < minValue) 
            //here we are using array[k][i] and not array[i][k] since you need to traverse column wise
            minValue = array [k][i];
    }
    //--> here assign the minValue to the array you want
}

#2


1  

Write a function int smallestInColumn(int array[M][M], int k) where k is the column you want to inspect. Within that, just run the i loop.

写一个函数int smallestInColumn(int array [M] [M],int k)其中k是你要检查的列。在其中,只需运行i循环。

(Then refactor int smallest to call that function when looping just over k).

(然后重构int最小,以便在循环刚好超过k时调用该函数)。

By the way, using % 21 to generate a random number will introduce statistical bias, unless your random number generator's periodicity is a multiple of 21, which is unlikely.

顺便说一下,使用%21生成一个随机数会引入统计偏差,除非你的随机数发生器的周期是21的倍数,这是不太可能的。

#3


0  

You can do this something like this

你可以这样做

//...

int smallest( int a[M][M], int column )
{
    int minValue = a[0][column];

    for ( int i = 1; i < M; i++ )
    {
        if ( a[i][column] < minValue ) minValue = a[i][column];
    }

    return minValue;
}

int main( void )
{
    int a[M][M];
    int b[M];

    //...

    for ( int i = 0; i < M; i++ ) b[i] = smallest( a, i );

    //...
}