Find the position of the value in a given matrix

Given a value X, find the position of the value X in the given matrix N x M. The matrix has both positive & negative integers. If the element 'X' is not present in the given matrix, output "-1 -1", else output the in-dices "i j".




Method 1:(Brute Force)



1. Use two for loops, outer for loop to hold the row number 'i' and inner for loop for column number 'j'
2. Check whether the current element is the element to be searched by comparing X with A[i][j]
3. Output ''i j'' if the element matches, else print ''-1 -1''

Implementation:



#include <stdio.h>
#define MAX 1000
#define ARR 1000000
int main()
{
    signed long long int N,M,A[MAX][MAX],X,i,j,k,out[ARR],Q,iloc,jloc;
    scanf("%lld %lld",&N,&M);
    for(i=0;i<N;i++)
    {
        for(j=0;j<M;j++)
        {
            scanf("%lld",&A[i][j]);
        }
    }
    scanf("%lld",&Q);
    while(Q--)
    {
        scanf("%lld",&X);
        iloc=-1;
        jloc=-1;
        for(i=0;i<N;i++)
        {
            for(j=0;j<M;j++)
            {
                if(A[i][j]==X)
                {
                    iloc=i;
                    jloc=j;
                }
            }
        }
        printf("%lld %lld\n",iloc,jloc);
    }
    return 0;
}