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