Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 13 of 13

Thread: Iterative Help

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Iterative Help

    Im writing a program that takes a number inputed and prints the first n positive numbers beginning at 0 by 10's, separated by spaces.

    For example: n=4 prints 0, n=65 prints 0 10 20 30 40 50 60

    Im not understanding how to write this using an iterative method. Any help please?


  2. #2
    Junior Member
    Join Date
    Oct 2010
    Posts
    11
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: Iterative Help

    First you probably want to divide the number by 10, which will give you a number that tells you how many you gotta print. for instance, 76 / 10 = 7. Then toss that into a loop which starts at 0 and goes until your integer, multiplying each by 10 and displaying it.

  3. The Following User Says Thank You to Jared For This Useful Post:

    Jnoobs (October 6th, 2010)

  4. #3
    Junior Member
    Join Date
    Oct 2010
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Iterative Help

    That makes a lot of sense, thank you.

    What about recursively?
    Last edited by Jnoobs; October 6th, 2010 at 01:19 PM.

  5. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Iterative Help

    Recursively in pseudo-code:
    public void printVals(int max, int current){
        //evaluate whether to return. Is current > max?
        //do work - increment and print
        //loop by calling myself with new values determined from work
    }

  6. #5
    Junior Member
    Join Date
    Oct 2010
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Iterative Help

    I ran into a problem with my code. My Iterative works but my Recursion isnt.

    public static int Recursion(int n) {
    		int result = 1, num;
     
    		num = n/10;
     
    		if(n == 0) {
    			result = 1; // Base case
    		}
    		else if(n == 1) {
    			result = 1; // Base case
    		}
    		else {
    			result = n * 10;
    		}
    		System.out.print("Recursive: " + result + " " + "\n");
    		return result;
    	}
     
    	public static void Iterative(int n) {
    		int var, num;
     
    		var = n/10;
     
    		for(int i = 0; i <= var;i++) {
    			num = i*10;
    			System.out.print("Iterative: " + num + " ");
    		}
     
    	}

    The recursive method should print the same output as the iterative method
    Last edited by Jnoobs; October 6th, 2010 at 03:41 PM.

  7. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Iterative Help

    Recursive functions call themselves...I don't see where your Recursion() function does this.

  8. #7
    Junior Member
    Join Date
    Oct 2010
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Iterative Help

    Oh, i forgot about that


    but now that i tried incorporating the call it still doesnt work.

    public static int Recursion(int n) {
    		int result = 1, num;
     
    		num = n/10;
     
    		System.out.print("\nRecursive: ");
    		if(n == 0) {
    			result = 1; // Base case
    		}
    		else if(n == 1) {
    			result = 1; // Base case
    		}
    		else {
    			result = num * Recursion(num - 1);
    		}
    		System.out.print(result + " ");
    		return result;
    	}

  9. #8
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Iterative Help

    please define doesn't work...eg output, exceptions, compile time errors, etc..

  10. #9
    Junior Member
    Join Date
    Oct 2010
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Iterative Help

    My Recursive output needs to be the same as my iterative Output, but it isnt outputting the same way.

  11. #10
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Iterative Help

    Is the parameter to Recusion() on each iteration what you would expect? Add a println in the function to test the value of the parameter to make sure it is what it should be.

  12. #11
    Junior Member
    Join Date
    Oct 2010
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Iterative Help

    I dont understand what you mean

  13. #12
    Junior Member
    Join Date
    Oct 2010
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Iterative Help

    I also need to do these but dont understand the question

    3. Write an iterative Java method that is passed two non–negative integer numbers m and n as parameters,
    and multiplies the two numbers by consecutive additions. The method should return the product m∗n.
    4. Write the same method as above, but use recursion instead of iteration.

    and finally:
    Write a recursive method to solve Ackermann’s Function assuming non–negative integers as input. Ackermann’s function is a that solves the following mathematical recurrence function:
     n + 1 if m = 0 A(m, n) = A(m − 1, 1) if m > 0 and n = 0  A(m−1,A(m,n−1) ifm>0andn>0
    NOTE: This recurrence relation grows extremelly rapidly, so test it using small m and n values (≤ 4).
    Doesnt print results; here is my code:
    System.out.print("\nProblem 6 (Enter a positive number): ");
    		m = nm.nextInt();
    		n = nm.nextInt();
    		result3 = Recurfour(m, n);
    		System.out.println("Problem 6: " + result3);

    AND the method is
    public static int Recurfour(int m, int n) {
    		int result=0;
     
    		if(m == 0) {
    			result = n+1; // Base case
    		}
    		else if(m > 0 && n == 0) {
    			result = Recurfour(m-1,1); // Base case
    		}
    		else if(m > 0 && n > 0){
    			result = Recurfour(m-1,Recurfour(m,n-1));
    		}
    		return result;
    	}

  14. #13
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Iterative Help

    One thing at a time...

    I dont understand what you mean
    I mean check your math. Adding some System.out.println statements to print out the values of variables - calculate what you'd expect, then see what it gives - is an invaluable method in debugging code such as this. Once again check your math...say you first call Recursion with 50, your next call will be with 4. Hmmmm.

Similar Threads

  1. Arrays.sort or iterative search?
    By igniteflow in forum Collections and Generics
    Replies: 1
    Last Post: September 16th, 2009, 02:07 AM

Tags for this Thread