+ Reply to Thread
Results 1 to 4 of 4

Thread: Recursive Problem

  1. #1
    Code Slinger chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5's Avatar
    Join Date
    Mar 2008
    Posts
    7,023
    Blog Entries
    1

    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);
        }
    }
    "Whenever you remember, I'll be there/
    Remember how we reached that dream together" - Carrie Underwood

  2. #2
    Super Moderator WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther's Avatar
    Join Date
    Jul 2006
    Age
    36
    Posts
    11,698
    Blog Entries
    57

    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.
    CodeCall Blog | CodeCall Wiki | Shareware
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  3. #3
    Code Slinger chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5's Avatar
    Join Date
    Mar 2008
    Posts
    7,023
    Blog Entries
    1

    Re: Recursive Problem

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

    Oh thanks.
    "Whenever you remember, I'll be there/
    Remember how we reached that dream together" - Carrie Underwood

  4. #4
    Super Moderator WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther's Avatar
    Join Date
    Jul 2006
    Age
    36
    Posts
    11,698
    Blog Entries
    57

    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.
    CodeCall Blog | CodeCall Wiki | Shareware
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

     

Similar Threads

  1. i have a problem please help me!!!????
    By stack in forum Java Help
    Replies: 10
    Last Post: 05-26-2009, 08:35 AM
  2. Run Length Encoding problem
    By TriKri in forum General Programming
    Replies: 1
    Last Post: 09-16-2008, 09:08 AM
  3. Peculiar UI Problem Needs Tackling
    By adriyel in forum C# Programming
    Replies: 2
    Last Post: 04-06-2008, 07:46 AM
  4. Problem read pwd protected Access2K dbase - CR9 & VB6
    By mrbar in forum Visual Basic Programming
    Replies: 2
    Last Post: 03-10-2008, 04:50 AM
  5. How to tackle a programming problem?
    By TcM in forum General Programming
    Replies: 10
    Last Post: 01-07-2008, 11:29 AM

Bookmarks

Bookmarks

     
        Algorithms and Data Structures

        Java tutorials

        Algorithms Forum

Posting Permissions

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