0

Code in C++:

#include <iostream>
#include <cmath>
#include <algorithm>

using namespace std;

void check(int i,int j);

int main()
{
    int n;
    cin >> n;
    int  a[n][n];
    int b[n][n];

    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            b[i][j] = 0;
        }
    }

    b[0][0] = 1;
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; i++)
        {
            cin >> a[i][j];
        }
    }

    check(0, 0);

    if (b[n - 1][n - 1] == 1)
        cout << "yesssss";
    else
        cout << "no!!!!";
}

void check(int i, int j)
{
    if((i - 1) >= 0 &&
       (i - 1) <= (n - 1) &&
       (abs(a[i][j] - a[i - 1][j]) >= 10) &&
       b[i - 1][j] == 0)
    {
        b[i - 1][j] = 1;
        check(i - 1, j);
    }

    if((i + 1) >= 0 &&
       (i + 1) <= (n-1) &&
       (abs(a[i][j] - a[i + 1][j]) >= 10) &&
       b[i + 1][j] == 0)
    {
        b[i + 1][j] = 1;
        check(i + 1, j);
    }

    if((j + 1) >= 0 &&
       (j + 1) <= (n - 1) &&
       (abs(a[i][j] - a[i][j + 1]) >= 10) &&
       b[i][j + 1] == 0)
    {
        b[i][j + 1] = 1;
        check(i, j + 1);
    }

    if((j - 1) >= 0 &&
       (j-1) <= (n - 1) &&
       (abs(a[i][j] - a[i][j - 1]) >= 10) &&
       b[i][j - 1] == 0)
    {
        b[i][j - 1] = 1;
        check(i, j - 1);
    }
}

My code has a function named check inside which integer variable n and arrays a and b. I want them to be made available to this function. If i declare global variable, then I can't use cin outside the main function.

How can I make these variables available to my check function?

Felix Glas
  • 15,065
  • 7
  • 53
  • 82
Aradhya
  • 53
  • 8
  • 2
    Pass them as parameters. Also note that arrays must have a size known at compile-time in standard C++. – chris Dec 27 '13 at 00:48
  • 3
    The code shouldn't compile because the size of a raw array must be known at compile time. Use std::vector instead. – Sceptical Jule Dec 27 '13 at 00:53
  • @chris how can I pass them as paramaters? – Aradhya Dec 27 '13 at 00:55
  • @ScepticalJule, I always use arrays and they seem to work fine, even when the size is taken as input. – Aradhya Dec 27 '13 at 00:57
  • 2
    @Aradhya, You might want to read through [this question](http://stackoverflow.com/questions/4810664/how-do-i-use-arrays-in-c), remembering that multidimensional arrays are just arrays of arrays. The size thing only works due to a compiler extension, unless the compiler is offering the future TS functionality. – chris Dec 27 '13 at 00:57
  • Also a note that at various points you use ‘n-1‘ and similar to index into an array. When ‘n==0‘ you will get undefined behaviour. You also have other cases where you go off the other end of the array. – John5342 Dec 27 '13 at 04:11

1 Answers1

1

Since you do not know the size of the array before you pass it into check, you need to use a pointer to a pointer (and not a 2D array). Here's the modified code (you also had a typo in your nested for-loop where you should have had a j instead of an i):

#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;

void check(int i, int j, int** a, int** b, int n);

int main() {

    int n;
    cin >> n;
    int** a;
    int** b;
    a = new int* [n];
    b = new int* [n];

    for (int i = 0; i < n; i++)
      a[i] = new int[n];

    for (int i = 0; i < n; i++)
      b[i] = new int[n];

    for (int i=0;i<n;i++) {
        for (int j=0;j<n;j++) {
            b[i][j]=0;
        }
    }
    b[0][0]=1;
    for (int i=0;i<n;i++) {
        for (int j=0;j<n;j++) {
            cin >> a[i][j];
        }
    }
    check(0,0,a,b,n);
    if (b[n-1][n-1] == 1)
      cout << "yesssss";
    else
      cout << "no!!!!";
}

void check(int i, int j, int** a, int** b, int n)
{
    if((i-1)>=0 && (i-1)<=(n-1) && (abs(a[i][j]-a[i-1][j])>=10) && b[i-1][j]==0)
    {
        b[i-1][j]=1;
        check(i-1,j,a,b,n);
    }
    if((i+1)>=0 && (i+1)<=(n-1) && (abs(a[i][j]-a[i+1][j])>=10) && b[i+1][j]==0)
    {
        b[i+1][j]=1;
        check(i+1,j,a,b,n);
    }
    if((j+1)>=0 && (j+1)<=(n-1) && (abs(a[i][j]-a[i][j+1])>=10) && b[i][j+1]==0)
    {
        b[i][j+1]=1;
        check(i,j+1,a,b,n);
    }
    if((j-1)>=0 && (j-1)<=(n-1) && (abs(a[i][j]-a[i][j-1])>=10) && b[i][j-1]==0)
    {
        b[i][j-1]=1;
        check(i,j-1,a,b,n);
    } 
}
bunting
  • 687
  • 1
  • 6
  • 11