What is the problem with passing 2D array in the below question like this ? Showing an error , use of parameter outside function body before + token

Viewed 39

CODE:-

  int check(int a[] , int n , int sum , bool d[][sum+1]){
        for(int p = 0 ; p<= n ;p++){
            d[p][0] = 1 ;
        }
        for(int j = 1 ; j<=sum ;j ++){
            d[0][j] = 0 ;
        }
        for(int p = 1 ; p<=n ;p ++){
            for(int j = 1 ; j<=sum ;j ++){
                if(a[p-1] > j) d[p][j] = d[p-1][j] ;
                else d[p][j] = d[p-1][j] || d[p-1][j-a[p-1]] ;
            }
        }
        int mini = sum ;
        for(int p = 0 ; p <= total ;p ++){
            if(d[n][p]) mini = min(mini , abs(sum-2*p)) ;
        }
        return mini ;
    } 
    int differ(int a[], int n)  { 
        int sum = 0 ;
        for(int p = 0 ; p < n ;p ++) sum += a[p] ;
        bool d[n+1][sum+1] ;
        return check(a , n , sum , d) ;
    }

This is showing an error: use of parameter outside function body before + token

    int check(int a[] , int n , int sum , bool d[][sum+1]){
                                                          ^
prog.cpp: In member function int Solution::check(...):
prog.cpp:10:28: error: n was not declared in this scope
         for(int p = 0 ; p<=n ;p++){
                            ^
prog.cpp:11:13: error: dp was not declared in this scope
            d[p][0] = 1 ;

I even passed some constant value in the d array as in 2d arrays we cannot pass variable size but still , it is not working and throwing some error. So how should we pass 2d array in c++.

0 Answers
Related