在添加新值之前检查二维数组的先前值

Check previous value of a 2 dimentional array before adding a new value

我创建了一个函数,用于根据用户输入将新的 integer 值添加到二维 (2D) 数组中。我想在将它添加到数组之前检查新提供的 integer 是否比前一个大。

如果是一维数组就好了。我会索引数组 -1 并检查值。

但是现在有了 2 个维度,我感到很困惑,我不知道如何检查前一行的值。

我应该为每一行创建 if 语句吗? (似乎不是最佳选择)

到目前为止我做了什么:

#include <stdio.h>

#define N 3
#define M 5

void get_numbers()
{
    int i, j, number;
    int TABLE[N][M];
    
    for (i = 0; i < N; i++)
    {
        for (j = 0; j < M; j++)
        {
            printf("Position: [%d, %d]: ", i+1, j+1);
            scanf("%d", &number);
            
            if (j > 0)
            {
                while (number < TABLE[i][j-1])
                {
                    printf("The new number should be larger than the previous one %d \n", TABLE[i][j-1]);
                    printf("Please try again\n");
                    
                    printf("Position: [%d, %d]: ", i+1, j+1);
                    scanf("%d", &number);
                }
                
                TABLE[i][j] = number;

            }
        }
    }   
}


int main () {
    get_numbers();
}


数组的所有元素 TABLE[i][0] 保持未初始化状态。

你需要写

for (i = 0; i < N; i++)
{
    for (j = 0; j < M; j++)
    {
        printf("Position: [%d, %d]: ", i+1, j+1);
        scanf("%d", &number);
        
        if ( j == 0 )
        {
            TABLE[i][j] = number;
        }
        else
        { 
            while (number <= TABLE[i][j-1])
            {
                printf("The new number should be larger than the previous one %d \n", TABLE[i][j-1]);
                printf("Please try again\n");
                
                printf("Position: [%d, %d]: ", i+1, j+1);
                scanf("%d", &number);
            }
            
            TABLE[i][j] = number;
        }
    }
} 

如果你还想在需要写的时候比较一行的第一个元素和上一行的最后一个元素

for (i = 0; i < N; i++)
{
    for (j = 0; j < M; j++)
    {
        printf("Position: [%d, %d]: ", i+1, j+1);
        scanf("%d", &number);
        
        if ( i == 0 && j == 0 )
        {
            TABLE[i][j] = number;
        }
        else
        { 
            while ( number <= ( j == 0 ? TABLE[i-1][M - 1] : TABLE[i][j-1] ) )
            {
                printf("The new number should be larger than the previous one %d \n", TABLE[i][j-1]);
                printf("Please try again\n");
                
                printf("Position: [%d, %d]: ", i+1, j+1);
                scanf("%d", &number);
            }
            
            TABLE[i][j] = number;
        }
    }
} 

另一种方法是将 two-dimensional 数组重新解释为 one-dimenisonal。例如

    int *p = ( int * )TABLE;

    for ( i = 0; i < N * M; i++ )
    {
        printf("Position: [%d, %d]: ", i / M + 1, i % M + 1 );
        scanf("%d", &number);
        
        if ( i == 0 )
        {
            p[i] = number;
        }
        else
        { 
            while ( number <= p[i-1] )
            {
                printf("The new number should be larger than the previous one %d \n", p[i-1] );
                printf("Please try again\n");
                
                printf("Position: [%d, %d]: ", i / M + 1, i % M + 1 );
                scanf("%d", &number);
            }
            
            p[i] = number;
        }
    }