Results 1 to 14 of 14
  1. #1
    spinter is offline Member
    Join Date
    Oct 2011
    Posts
    18
    Rep Power
    0

    Default 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;
                            }
                        }

  2. #2
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    4

    Default 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);
    }
    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?
    Last edited by tnrh1; 10-28-2011 at 12:46 AM.

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default 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}};
    This is a much neater way to initialise a 2D array.

  4. #4
    spinter is offline Member
    Join Date
    Oct 2011
    Posts
    18
    Rep Power
    0

    Default 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

  5. #5
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    4

    Default Re: dont know how to resolve last function always same stack overflow

    Why dont you use Math.sqrt()?

  6. #6
    DiamondSoul is offline Member
    Join Date
    Oct 2011
    Posts
    83
    Rep Power
    0

    Default 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.

  7. #7
    spinter is offline Member
    Join Date
    Oct 2011
    Posts
    18
    Rep Power
    0

    Default 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

  8. #8
    DiamondSoul is offline Member
    Join Date
    Oct 2011
    Posts
    83
    Rep Power
    0

    Default 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);
    }
    See if you can get just the square root method to work like this, and then we'll talk about the matrix symmetry checker.

  9. #9
    spinter is offline Member
    Join Date
    Oct 2011
    Posts
    18
    Rep Power
    0

    Default 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){

  10. #10
    DiamondSoul is offline Member
    Join Date
    Oct 2011
    Posts
    83
    Rep Power
    0

    Default 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?

  11. #11
    spinter is offline Member
    Join Date
    Oct 2011
    Posts
    18
    Rep Power
    0

    Default Re: dont know how to resolve last function always same stack overflow

    i only can use the 3 methods i show in the problem

  12. #12
    DiamondSoul is offline Member
    Join Date
    Oct 2011
    Posts
    83
    Rep Power
    0

    Default 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...)

  13. #13
    spinter is offline Member
    Join Date
    Oct 2011
    Posts
    18
    Rep Power
    0

    Default 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;
    }
    and i declare before public static int n ,r; its necessary

  14. #14
    spinter is offline Member
    Join Date
    Oct 2011
    Posts
    18
    Rep Power
    0

    Default Re: dont know how to resolve last function always same stack overflow

    it works now XD XD thx to all

Similar Threads

  1. Gui problem and stack overflow error
    By zaricpp in forum Advanced Java
    Replies: 5
    Last Post: 06-03-2011, 09:54 AM
  2. Preventing stack overflow?
    By bobocheez in forum New To Java
    Replies: 11
    Last Post: 01-05-2011, 05:50 PM
  3. Stack Overflow work around?
    By Coukapecker in forum New To Java
    Replies: 2
    Last Post: 03-14-2010, 08:49 PM
  4. Java Stack Overflow?
    By fullmetaljacket in forum New To Java
    Replies: 0
    Last Post: 05-19-2009, 07:49 PM
  5. Graphics2D: stack overflow error
    By rosh72851 in forum New To Java
    Replies: 11
    Last Post: 10-15-2008, 09:01 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •