-2

No value is being printed inside if condition block. Where is the logical error? Thanks.

#include<bits/stdc++.h>
using namespace std;

int fx[]= {-1,-1,-1,0,1,1,1,0};
int fy[]= {-1,0,1,1,1,0,-1,-1};

int ar[20][20];
int n;
int v1, v2;

void fun(int a, int b)
{
    for(int i=0; i<8; i++)
    {
        v1 = a+fx[i];
        v2 = b+fy[i];
        //cout<<v1<<" "<<v2<<endl;
        if(v1>=0 && v1<n)
        {
            if(v2>=0 && v2<n)
            {
               // Not executing 
               cout<<"----------"<<endl;
               cout<<v1<<" "<<v2<<endl;
            }
        }

    }
}

int main()
{
    int n;
    cin>> n;
    for(int i=0; i<n; i++)
    {
        for(int j=0; j<n; j++)
            cin>> ar[i][j];
    }
    fun(0,1);
    return 0;
}

SAMPLE INPUT:
4
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
EXPECTED OUTPUT:

---------

0 2
1 2
1 1
1 0
0 0

  • 1
    Unrelated to your problem but please read [Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) and [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) And please try to take some classes or read [good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) instead of learning all the bad habits of "competitive" programming. – Some programmer dude Jan 07 '20 at 10:49
  • @Shuvo mandol Describe what the program is trying to do. – Vlad from Moscow Jan 07 '20 at 10:50
  • Tip: use breakpoints and check the local variables for logic errors – Nathan Kolpa Jan 07 '20 at 10:52
  • 1
    I can't see the relationship between the input `ar[.][.]` and the `fun` function – Damien Jan 07 '20 at 10:53
  • @VladfromMoscow actually I want to know why the logical If statement is not working? There is nothing here without logic. – Shuvo mandol Jan 07 '20 at 10:54
  • @Damien Yes there is no logical relation between fun function and ar[.][.] till now but why the if statement is not getting executed logically? – Shuvo mandol Jan 07 '20 at 10:58
  • Your assumption that there are any values that satisfy the conditions is mistaken (you forgot to check `n` when debugging). – molbdnilo Jan 07 '20 at 10:58
  • https://stackoverflow.com/q/25385173/10147399 – Aykhan Hagverdili Jan 07 '20 at 12:34

1 Answers1

6

You have 2 variables n, one global (with value 0), one in main feed by input.

fun uses the global one.

Avoid (non const) global variables. prefer to pass n to fun:

void fun(int (&arr)[20][20], int n, int x, int y)
{
    const int fx[]= {-1,-1,-1,0,1,1,1,0};
    const int fy[]= {-1,0,1,1,1,0,-1,-1};

    for(int i=0; i<8; i++)
    {
        int v1 = x + fx[i];
        int v2 = y + fy[i];
        if(v1>=0 && v1<n && v2>=0 && v2<n)
        {
               std::cout << "----------" << std::endl;
               std::cout << v1 << " " << v2 << std::endl;
        }
    }
}

int main()
{
    int n;
    std::cin >> n;

    int arr[20][20];

    for(int i=0; i<n; i++) {
        for(int j=0; j<n; j++) {
            std::cin >> arr[i][j];
        }
    }
    fun(arr, n, 0, 1);
}

Demo

Jarod42
  • 203,559
  • 14
  • 181
  • 302