This set of code is producing a stack overflow error due to infinite recursion (at least, I think it is). I have been staring at this code for a long time and can't figure out where the error happens to be. If anyone can help point out why I am getting such an error, that would be great.
public void DrawValues(Graphics g, Graphics2D g2, int x, int y, int a, int b){
if (b>8){
b = 0;
a++;
x = 61;
y+=66;
}
if (a==8 && b==8){
g.drawString(String.valueOf(Solver.Rows[a][b]), x, y);
}
else{
g.drawString(String.valueOf(Solver.Rows[a][b]), x, y);
DrawValues(g,g2, x+66, y, a, b++);
}
}
It will probably help to state that Rows is a 9x9 2D Array, a and b start at 0
This is because you are using post-increment(b++) instead of pre-increment(++b) when you make recursive calling of DrawValues method. If you use post-increment, argument will be increment after method has been invoked. Hence, in your case, variable b never will be changed.
So, you should to use pre-increment:
...
DrawValues(g,g2, x+66, y, a, ++b);
...
Related
I'm getting segmentation errors, when I define points in different objects which all point to the same variable. I also tried implementing it with shared pointers but so far it hasn't worked out. For example:
double var; //global var
int main(){
double *point_to_var = &var;
typeA A(point_to_var);
typeB B(point_to_var);
typeC C(point_to_var);
var = 10.;
B.sum(10.);
C.sum(10.);
}
struct typeA{
double *ptv;
A(double *ptvv): ptv(ptvv){}
}
struct typeB{
double *ptv;
B(double *ptvv): ptv(ptvv){}
double sum(double x);
}
struct typeC{
double *ptv;
C(double *ptvv): ptv(ptvv){}
double sum(double x);
}
double typeB::sum(double x){
return x + *ptv;
}
double typeC::sum(double x){
return x + *ptv;
}
I would have expected C.sum(10.) to return a value of 20 in this case, since *ptv points to the address of var which equals 10, however it crashes with a segmentation error. My code is more complicated than what I've shown here, but the idea is the same. It crashes when I try to use *ptv inside functions defined within objects. The code compiles on the command line, but on Xcode, inside of segmentation error I get exc_bad_access.
Using shared pointers (at least the way I did it) didn't seem to fix the problem. Is it possible to fix this without just using a global variable inside the objects?
class Solution{
ArrayList subsetSums(ArrayList arr, int N){
int sum=0;
ArrayList<Integer> temparr = new ArrayList<>();
for(int i=1;i<=arr.size();i++)
{
for(int j = 0; i < arr.size()-i+1 ; j++)
temparr.add(recur(arr,i,j,sum));
}
return temparr;
}
int recur(ArrayList<Integer> arr,int i,int j,int sum)
{
int index = j;
int len = i;
int Sum = sum;
if(len==0)
{
return Sum;
}
Sum += arr.get(index);
return recur(arr,len--,index++,Sum);
}
}
,,,
I'm getting stack overflow error in 'return recur(arr,len--,index++,Sum);'
'''
I think, the main problem here (see comments for potential other problems) is the way you are trying to pass changed arguments to the recursive invocation:
recur(arr,len--,index++,Sum)
Actually this will call recur with the unchanged values of len and index because the operators ++ and -- (when written on the right side of a variable) are defined to return the original value of the variable and then update the variable's value.
Use (I would prefer this)
recur(arr, len-1, index+1, Sum)
or (okay, but the assignment is not needed)
recur(arr, --len, ++index, Sum)
to actually pass the modified value to the function.
Java has a recursion limit. The way to fix this is replace the recursion with a loop. (Or change the function so it does not recur as much. Infinite loops are a problem just as infinite recursion is).
A few tips for the future:
Google the documentation for errors
State the language with a tag in posts
Don't use formatting of line 1
Debug with print statements
I tried memoizing the value of 'x', but it gives wrong answer.
Uncommenting the commented part will give wrong answer.
//vi dp(1000001,-1);
int f(int x,int cnt,const vi &v){
if(x<0)return INT_MAX;
if(x==0)return cnt;
//if(dp[x]!=-1)return dp[x];
int ans=INT_MAX;
for(const int &i:v){
ans=min(ans,f(x-i,cnt+1,v));
}
//dp[x]=ans;
return ans;
}
Without memoization, this is working fine.
Your function has 2 states and you are storing value for just one state. Suppose you want the value of f(2,2,v). Your dp[2] array can contain any values among f(2,x,v) where x can be any value of "cnt".
I know that we can't overload a C++ function in R according to this Q/A. But sometime we need it. The solution proposed to make a dispatcher in the question linked seems a bit overkilled to me (using templates and switches). I'm wondering if there is an elegant solution for simple problems.
So here a simple problem. x is a vector of int or a vector of double. sum(x == y) can be written in a faster and memory optimized way in C++.
int int_count_equal(IntegerVector x, int y)
{
return std::count(x.begin(), x.end(), y);
}
and
int num_count_equal(NumericVector x, double y)
{
return std::count(x.begin(), x.end(), y);
}
The two functions have the same body. It does not make sense and it is not convenient to have two functions. How can we make a dispatcher in a single function count_equal without writing twice the same code?
Alright, so I'm trying to make a Java program to solve a picross board, but I keep getting a Stackoverflow error. I'm currently just teaching myself a little Java, and so I like to use the things I know rather than finding a solution online, although my way is obviously not as efficient. The only way I could think of solving this was through a type of brute force, trying every possibility. The thing is, I know that this function works because it works for smaller sized boards, the only problem is that with larger boards, I tend to get errors before the function finishes.
so char[][] a is just the game board with all the X's and O's. int[][] b is an array with the numbers assigned for the picross board like the numbers on the top and to the left of the game. isDone() just checks if the board matches up with the given numbers, and shift() shifts one column down. I didn't want to paste my entire program, so if you need more information, let me know. Thanks!
I added the code for shift since someone asked. Shift just moves all the chars in one row up one cell.
Update: I'm thinking that maybe my code isn't spinning through every combination, and so it skips over the correct answer. Can anyone verify is this is actually trying every possible combination? Because that would explain why I'm getting stackoverflow errors. On the other hand though, how many iterations can this go through before it's too much?
public static void shifter(char[][] a, int[][] b, int[] clockwork)
{
boolean correct = true;
correct = isDone(a, b);
if(correct)
return;
clockwork[a[0].length - 1]++;
for(int x = a[0].length - 1; x > 0; x--)
{
if(clockwork[x] > a.length)
{
shift(a, x - 1);
clockwork[x - 1]++;
clockwork[x] = 1;
}
correct = isDone(a, b);
if(correct)
return;
}
shift(a, a[0].length - 1);
correct = isDone(a, b);
if(correct)
return;
shifter(a, b, clockwork);
return;
}
public static char[][] shift(char[][] a, int y)
{
char temp = a[0][y];
for(int shifter = 0; shifter < a.length - 1; shifter++)
{
a[shifter][y] = a[shifter + 1][y];
}
a[a.length - 1][y] = temp;
return a;
}
Check Recursive call.and give the termination condition.
if(terminate condition)
{
exit();
}
else
{
call shifter()
}