Closed Thread
Results 1 to 4 of 4

Thread: Recursive Problem

  1. #1
    Join Date
    Mar 2008
    Posts
    7,144
    Rep Power
    86

    Recursive Problem

    I'm working on this problem here. I know that it is a recursive problem, and I got my function together, and I think that I am calling the function properly.

    However when I run my program it only outputs 3 0's. Any ideas as to why?

    Code:
    import java.util.*;
    import java.io.*;
    
    
    public class _4 {
        static int nMAXSIZE, nBEST = 0, nNUMTRACKS;
        static int[] arnSize, arnUtil;
        public static void main(String[] args) throws IOException {
            
            Scanner sin = new Scanner(new FileReader("DATA4.txt"));
            PrintWriter fout = new PrintWriter(new FileWriter("OUT4.txt"));
            int nAlbums = 0;
            int nSpace = 0;
            
            while (sin.hasNextInt()) {
                nMAXSIZE = 0;
                nSpace = sin.nextInt();
                nAlbums = sin.nextInt();
                arnSize = new int[nAlbums];
                arnUtil = new int[nAlbums];
                
                
                for (int i=0;i<nAlbums;i++) {
                    arnSize[i] = sin.nextInt();
                    arnUtil[i] = sin.nextInt();
                    recurse(0,0,0);
                }
                System.out.println(nBEST);
            }
            
            
            fout.close();
            sin.close();
        }
    
        public static void recurse(int nAlbum,int nUtil,int nSpace) {
            if (nSpace > nMAXSIZE) {
                return;
            }
            
            if (nAlbum == nNUMTRACKS) {
                return;
            }
            
            
            nUtil += arnUtil[nAlbum];
            nSpace += arnSize[nAlbum];
            
            if (nUtil > nBEST) {
                nBEST = nUtil;
            }
            
            recurse(nAlbum+1,nUtil,nSpace);
        }
    }

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Jul 2006
    Posts
    16,475
    Blog Entries
    75
    Rep Power
    143

    Re: Recursive Problem

    nNUMTRACKS == 0, so on the first call nAlbum == 0 and you exit.

    Also, this isn't a recursion problem, it's a linear programming problem.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  4. #3
    Join Date
    Mar 2008
    Posts
    7,144
    Rep Power
    86

    Re: Recursive Problem

    Well our instructor said it was recursive. :s Why would he say that?

    Oh thanks.

  5. #4
    Join Date
    Jul 2006
    Posts
    16,475
    Blog Entries
    75
    Rep Power
    143

    Re: Recursive Problem

    Probably because linear programming is a senior/graduate level math course in college. You can attempt to solve it with recursion, but it's going to be more of a trial and error type thing.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Recursive Help
    By concept in forum PHP Development
    Replies: 2
    Last Post: 05-17-2010, 04:08 AM
  2. Problem with recursive function.
    By posto2012 in forum C and C++
    Replies: 3
    Last Post: 12-05-2009, 06:19 AM
  3. problem with recursive function
    By reniery in forum Visual Basic Programming
    Replies: 1
    Last Post: 07-10-2009, 04:15 PM
  4. Recursive problem
    By DarkSun in forum General Programming
    Replies: 3
    Last Post: 01-22-2009, 02:59 PM
  5. Recursive problem!Please help me experts!
    By noobsia in forum C and C++
    Replies: 8
    Last Post: 06-26-2008, 05:14 PM

Tags for this Thread

Bookmarks

Posting Permissions

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