Results 1 to 14 of 14
 10272011, 08:13 PM #1Member
 Join Date
 Oct 2011
 Posts
 18
 Rep Power
 0
dont know how to resolve last function always same stack overflow
well I expose th prblem I have a function that resolve the square root of a int on recursive mode , other funtion that says if a matrix is ssymmetric or not in a recursive mode, an the last function must say if the matrix of the square root of the elements is symmetric or not in a recursive mode too, i know i must use the 2 funtion before but always i have the same exception stack overflow i put here the code i have
Java Code:package main; /** * * @author adriandelasmatas */ public class Main { public static final int i = 4; public static final int j = 4; public static int x = 0; public static int y = 0; public static boolean sym = true; public static int m[][] = new int[i][j]; public static int n ; public static int r; /** * @param args the command line arguments */ public static void main(String[] args) { m[0][0]=1; m[1][0]= 4; m[2][0]= 3; m[3][0]= 2; m[0][1]=4; m[1][1]=1; m[2][1]=2; m[3][1]= 4; m[0][2]=3; m[1][2]=2; m[2][2]=1; m[3][2]=3; m[0][3]=2; m[1][3]=4; m[2][3]=3; m[3][3]=1; SqrtMatSym(m); System.out.println(SqrtMatSym(m)); } public static int sqrt(int n) { //pre >= 0; if ( (n >= (r*r)) && (n < (r+1)*(r+1)) ){ return(r); }else { r++; sqrt(n); }return (r); } public boolean matrix(int[][] m) { if ( i == j){ if (sym== true){ if (x != m.length  1) { if (y == m[x].length  1) { if (m[x][y] != m[y][x]) { sym = false; } x++; y = 0; matrix(m); } else { if (m[x][y] != m[y][x]) { sym = false; } else { y++; matrix(m); } } } } }else{ sym = false;} return sym; } public static boolean SqrtMatSym(int[][] m) { //need to do teh sqrt of all elements and know if is sym if ( i == j){ if (sym== true){ if (x != m.length  1) { if (y == m[x].length  1) { if (m[x][y] != m[y][x]) { sym = false; } x++; y = 0; SqrtMatSym(m); } else { if (m[x][y] != m[y][x]) { sym = false; } else { y++; SqrtMatSym(m); } } } } }else{ sym = false;} return sym; } }
 10282011, 12:43 AM #2Senior Member
 Join Date
 Aug 2011
 Posts
 251
 Rep Power
 4
Re: dont know how to resolve last function always same stack overflow
Since I'm hurry I read the code fast and I found somthing wrong:
Java Code:public static int sqrt(int n) { if ( (n >= (r*r)) && (n < (r+1)*(r+1)) ) return(r); r++; sqrt(n); return (r); }
You can't call the method like that: sqrt(n); the method returns an int.
Change it to: return sqrt(n);
But then you have two returns one after one which make no sense, what the method suppose to do?Last edited by tnrh1; 10282011 at 12:46 AM.
 10282011, 01:09 AM #3
Re: dont know how to resolve last function always same stack overflow
Java Code:int[][] matrix = {{1,2,3},{4,5,6},{7,8,9}};
 10282011, 01:31 AM #4Member
 Join Date
 Oct 2011
 Posts
 18
 Rep Power
 0
Re: dont know how to resolve last function always same stack overflow
first funtion must be a square root of n and n is a int but it must be recursive, second must prove if a mtrix is symmetric or not recursive too, third must prove if a matrix wth the square root of a matrix its symmetric
 10282011, 02:19 PM #5Senior Member
 Join Date
 Aug 2011
 Posts
 251
 Rep Power
 4
Re: dont know how to resolve last function always same stack overflow
Why dont you use Math.sqrt()?
 10282011, 03:48 PM #6Member
 Join Date
 Oct 2011
 Posts
 83
 Rep Power
 0
Re: dont know how to resolve last function always same stack overflow
Stack overflow generally means that you are going into infinite recursion. That is, your recursive method keeps calling itself, but never stops. Make sure you have a "base case" or "exit condition" in your recursive methods (a special case where the method does NOT call itself) and make sure that case is always eventually being reached.
 11012011, 04:30 PM #7Member
 Join Date
 Oct 2011
 Posts
 18
 Rep Power
 0
Re: dont know how to resolve last function always same stack overflow
i know it but i dont know how to resolve it men if any can help me saying something in the code i can to do its a important work for the uni
i cant use Math because i must do it in a recursive mode
 11012011, 08:15 PM #8Member
 Join Date
 Oct 2011
 Posts
 83
 Rep Power
 0
Re: dont know how to resolve last function always same stack overflow
Okay, right off the bat, one thing I notice is that, in your square root method, you are doing calculations with a static field (namely r) without initializing it in the method. This is a bad idea, because it means that you have to remember to initialize the value of r every time you call the square root method. I suspect that, if you added an r=0 before every time you call the square root method (except in the square root method itself), it would work the way you want it to. However, this is not the solution I would recommend. Rather than having r be a static field variable declared outside of the square root method, I would have it be local to said method, passed as an argument. Then have an overloaded version of the method without the r parameter, which calls the first one like so:
Java Code:private static int sqrt(int n,int r){ //do the actual calculation here } public static int sqrt(int n){ return sqrt(n,0); }
 11012011, 08:24 PM #9Member
 Join Date
 Oct 2011
 Posts
 18
 Rep Power
 0
Re: dont know how to resolve last function always same stack overflow
but sorry i dont say before i can't use any of Math api an metud must public static int sqrt(int n){
 11012011, 08:28 PM #10Member
 Join Date
 Oct 2011
 Posts
 83
 Rep Power
 0
Re: dont know how to resolve last function always same stack overflow
I'm not asking you to use the Math api, I'm asking you to use method overloading. Are you allowed to do that?
 11012011, 08:37 PM #11Member
 Join Date
 Oct 2011
 Posts
 18
 Rep Power
 0
Re: dont know how to resolve last function always same stack overflow
i only can use the 3 methods i show in the problem
 11012011, 08:46 PM #12Member
 Join Date
 Oct 2011
 Posts
 83
 Rep Power
 0
Re: dont know how to resolve last function always same stack overflow
In that case, do like I said, except don't have the version that just takes the parameter n. Only have the version that takes both n and r. Then, when you call that method from outside itself, say "sqrt(n,0)" where n is the number you want to take the square root of.
On a side note, this sounds like a really silly assignment >_> (I'd be kinda pissed if one of my professors assigned it...)
 11012011, 08:57 PM #13Member
 Join Date
 Oct 2011
 Posts
 18
 Rep Power
 0
Re: dont know how to resolve last function always same stack overflow
well for the root i think i have it know this is the code
Java Code:public static int raizCuadradaEntera (int n) { boolean raiz = false; if (n == 0) { return (n); } if ( (n >= (r*r)) && (n < (r+1)*(r+1))){ raiz = true;} if (raiz == false){ r++; raizCuadradaEntera(n); } return r; }
 11012011, 09:59 PM #14Member
 Join Date
 Oct 2011
 Posts
 18
 Rep Power
 0
Similar Threads

Gui problem and stack overflow error
By zaricpp in forum Advanced JavaReplies: 5Last Post: 06032011, 09:54 AM 
Preventing stack overflow?
By bobocheez in forum New To JavaReplies: 11Last Post: 01052011, 06:50 PM 
Stack Overflow work around?
By Coukapecker in forum New To JavaReplies: 2Last Post: 03142010, 09:49 PM 
Java Stack Overflow?
By fullmetaljacket in forum New To JavaReplies: 0Last Post: 05192009, 07:49 PM 
Graphics2D: stack overflow error
By rosh72851 in forum New To JavaReplies: 11Last Post: 10152008, 09:01 PM
Bookmarks