Home > OS >  Check previous value of a 2 dimentional array before adding a new value
Check previous value of a 2 dimentional array before adding a new value

Time:04-18

I've created a function to add new integer values into a two-dimensional (2D) array from user input. And I would like to check if the newly provided integer is larger than the previous one before adding it to the array.

If it was a single-dimensional array that would be easy. I would index the array -1 and check the value.

But now with 2 dimensions, I'm getting confused and I don't know how to check the previous row's value.

Should I create if statements for each row? (Doesn't seem optimal)

What I have done so far:

#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();
}


CodePudding user response:

All elements TABLE[i][0] of the array stay uninitialized.

You need to write

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;
        }
    }
} 

If you also want to compare the first element of an row with the last element of the previous row when you need to write

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;
        }
    }
} 

Another way is to reinterpret the two-dimensional array as a one-dimenisonal. For example

    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;
        }
    }
  • Related