-1

i need to input 6 on 6 matrix and to out put the location of the cell that all the cells around him has the value 0. let's say:

1 1 1 1 1 1 
1 1 1 1 1 1 
0 0 1 1 1 1
2 0 1 1 1 1 
0 0 1 1 1 1 
1 1 1 1 1 1 

the out put will be row 3 col 0

this is the code i made.. how i solve this ? i tried a lot please help me

#include<stdio.h>
#define N 6
int main()
{
int arr[N][N] = { 0 }, i = 0, j = 0, x = 0, y = 0, counter = 0;  

        for (i = 0; i < N; i++)
        {
        for (j = 0; j < N; j++)//input//
        {
            scanf("%5d", &arr[i][j]);
        }
        printf("\n");
    } 

    for (i = 0; i < N; i++)//output//
    {
        for (j = 0; j < N; j++)
        {
            printf("%5d", arr[i][j]);
        }
        printf("\n");
    }


    for (i = 0; i < N; j++)
    {
    for (j = 0; j < N; j++)
    {
        for (x = i - 1; x <= i + 1; x + 2)
        {
            for (y = j - 1; y <= j + 1; y + 2)
            {

                if (x >= 0 && x < N &&y >= 0 && y < N&&arr[x][y] == 0)
                {
                    counter++;
                }

            }
        }

        if (i == 0 && j == 0 || i == 0 && j == N - 1 || i == N - 1 && j == 0 
|| i == N - 1 && j == N - 1)
        {
            if (counter == 3)
            {
                printf("the row is %d the col is %d\n", i, j);
            }
        }
        else if (i == 0 && j >= 1 && j < N - 1 || i == N - 1 && j >= 1 && j 
    < N - 1 && j == 0 && i >= 1 && i <= N - 1 && j == N - 1 && i >= 1 && i<N -1)
        {
            if (counter == 5)
            {
                printf("the row is %d the col is %d\n ", i, j);
            }
        }

        else
        {
            if (counter == 8)
            {
                printf("the row is %d the col is %d\n ", i, j);
            }
        }

        }
    }


    }
Eugene Sh.
  • 17,802
  • 8
  • 40
  • 61
  • 1
    In which way is your code not meeting the expectation? Show examples of sample input and contrast the output to the desired output. Does it already work for some special cases? What did you do for debugging your code? Probably helpful read: https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ https://stackoverflow.com/questions/2069367/how-to-debug-using-gdb – Yunnosch Jan 11 '18 at 16:28
  • MY code does not even make the require output... –  Jan 11 '18 at 16:30
  • 3
    [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) – klutt Jan 11 '18 at 16:30
  • 1
    How not? For what not? What do you think why not? What DOES it output? Please try to give the impression that you spent some effort yourself and that you actually want to make helping you easy. The current impression is that you simply want somebody else to do all the thinking for you. And quite a lot of guessing or finding information which you could have provided with hardly any effort. – Yunnosch Jan 11 '18 at 16:30
  • i have 6 on 6 matrix i need to show the cell location of the cell which all the cell around him with the 0 value –  Jan 11 '18 at 16:33
  • 1
    That is what your homework assignment is - again. It is not a solution for any of the shortcomings of your question. – Yunnosch Jan 11 '18 at 16:34
  • i tried the to my code but he doesn't work –  Jan 11 '18 at 16:35
  • 3
    That is already clear and remains unhelpful. How does it not work? In which way is what it does not what you want? What unwanted things does it do? What did you do in order to find out why it does not act as desired? Did you debug? What was the result of debugging? Show the output. Try to force it to create any output. – Yunnosch Jan 11 '18 at 16:39
  • 4 nested `for` loops seems a bit excessive and IMO you're cramming too much into `main`. You should have 3 distinct functions. 1) Input values of array. 2) Scan all values in array 3) Does the current location fit the criteria? 1 and 2 can probably go into main but I definitely would put 3 in it's own function, probably with the signature `bool surrounded_by_zeros(int* row, int* col);`. It would return `true` with `row` and `col` appropriately filled in if the criteria was met, `false` otherwise. – yano Jan 11 '18 at 16:49
  • 1
    You can test each of these functions independently of the others. For example, once your input function is working, comment that out and hard-code a test array with certain values in order to test your `surrounded_by_zeros` function.. this will save time from having to type 36 values every time you run your program. – yano Jan 11 '18 at 16:52
  • You could make your code much easier if you assume that any field outside of the square is 0. Just increment the `counter` for any field outside the range. Then you will have only 1 condition for a match: `counter == 8` without separate cases for corners or edges. – Gerhardh Jan 11 '18 at 16:57

2 Answers2

1

There are problems with your x and y loops:

for (x = i - 1; x <= i + 1; x + 2)
{
    for (y = j - 1; y <= j + 1; y + 2)
    {
  1. You aren't incrementing x or y in these loops. The expression x + 2 merely evaluates the value x + 2. It doesn't do anything with it. If you want to actually set x to x + 2, then you need to use x = x + 2, or more concisely, x += 2.

  2. Incrementing x and y by 2 is incorrect. It will only examine 4 points: (i-1,i-1), (i-1,i+1), (i+1,i-1), and (i+1,i+1). It will skip the following 4 points: (i-1,i), (i,i-1), (i,i+1), (i+1,i). You need to increment x and y by 1 each time, i.e. use x++ and y++ in the loop incremement instead of adding 2. The, inside the loop, add an additional test for x == i && y == i and skip that point (the center point).

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
Tom Karzes
  • 22,815
  • 2
  • 22
  • 41
0

I believe this is what you want (note that you can do eveything in two loops if you nest the conditionals in a smart way to avoid checking areas outside the matrix):

#include <stdio.h>

#define ROWS 5
#define COLS 5

void printBlockeds(int M[ROWS][COLS])
{
    int i, j;
    for(i = 0; i < ROWS; ++i)
    {
        for(j = 0; j < COLS; ++j)
        {
            int isBlocked = 1;

            if(i > 0)
            {
                                isBlocked = isBlocked && !M[i-1][j];
                if(j > 0)       isBlocked = isBlocked && !M[i-1][j-1];
                if(j < COLS-1)  isBlocked = isBlocked && !M[i-1][j+1];
            }

            if(j > 0)           isBlocked = isBlocked && !M[i][j-1];
            if(j < COLS-1)      isBlocked = isBlocked && !M[i][j+1];

            if(i < ROWS-1)
            {
                                isBlocked = isBlocked && !M[i+1][j];
                if(j > 0)       isBlocked = isBlocked && !M[i+1][j-1];
                if(j < COLS-1)  isBlocked = isBlocked && !M[i+1][j+1];
            }

            if(isBlocked) printf("(%d, %d)\n", i, j);
        }
    }
}

int main()
{
    int M[ROWS][COLS] = {{1, 1, 1, 0, 1},
                         {1, 0, 0, 0, 0},
                         {1, 0, 1, 0, 1},
                         {1, 0, 0, 0, 1},
                         {1, 0, 1, 0, 1}};
    printBlockeds(M);
    return 0;
}

Output for the case above:

(0, 4)
(2, 2)
(4, 2)
Jango
  • 378
  • 4
  • 8