# dont know how to resolve last function always same stack overflow

• 10-27-2011, 08:13 PM
spinter
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
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;                         }                     }```
• 10-28-2011, 12:43 AM
tnrh1
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:
Code:

``` public static int sqrt(int n) {     if ( (n >= (r*r)) && (n < (r+1)*(r+1)) )         return(r);      r++;     sqrt(n);            return (r); }```
I made some little changes in organizing the code (not edits).
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?
• 10-28-2011, 01:09 AM
Junky
Re: dont know how to resolve last function always same stack overflow
Code:

`int[][] matrix = {{1,2,3},{4,5,6},{7,8,9}};`
This is a much neater way to initialise a 2D array.
• 10-28-2011, 01:31 AM
spinter
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
• 10-28-2011, 02:19 PM
tnrh1
Re: dont know how to resolve last function always same stack overflow
Why dont you use Math.sqrt()?
• 10-28-2011, 03:48 PM
DiamondSoul
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.
• 11-01-2011, 04:30 PM
spinter
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
• 11-01-2011, 08:15 PM
DiamondSoul
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:

Code:

```private static int sqrt(int n,int r){     //do the actual calculation here } public static int sqrt(int n){     return sqrt(n,0); }```
See if you can get just the square root method to work like this, and then we'll talk about the matrix symmetry checker.
• 11-01-2011, 08:24 PM
spinter
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){
• 11-01-2011, 08:28 PM
DiamondSoul
Re: dont know how to resolve last function always same stack overflow
• 11-01-2011, 08:37 PM
spinter
Re: dont know how to resolve last function always same stack overflow
i only can use the 3 methods i show in the problem
• 11-01-2011, 08:46 PM
DiamondSoul
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...)
• 11-01-2011, 08:57 PM
spinter
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
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; }```
and i declare before public static int n ,r; its necessary
• 11-01-2011, 09:59 PM
spinter
Re: dont know how to resolve last function always same stack overflow
it works now XD XD thx to all