# Iterative Help

• October 6th, 2010, 10:35 AM
Jnoobs
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?
• October 6th, 2010, 11:23 AM
Jared
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.
• October 6th, 2010, 01:17 PM
Jnoobs
Re: Iterative Help
That makes a lot of sense, thank you.

• October 6th, 2010, 02:13 PM
copeg
Re: Iterative Help
Recursively in pseudo-code:
Code java:

```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 }```
• October 6th, 2010, 03:38 PM
Jnoobs
Re: Iterative Help
I ran into a problem with my code. My Iterative works but my Recursion isnt.

Code :

```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
• October 6th, 2010, 03:47 PM
copeg
Re: Iterative Help
Recursive functions call themselves...I don't see where your Recursion() function does this.
• October 6th, 2010, 04:25 PM
Jnoobs
Re: Iterative Help

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

Code :

```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; }```
• October 6th, 2010, 04:51 PM
copeg
Re: Iterative Help
please define doesn't work...eg output, exceptions, compile time errors, etc..
• October 6th, 2010, 05:34 PM
Jnoobs
Re: Iterative Help
My Recursive output needs to be the same as my iterative Output, but it isnt outputting the same way.
• October 6th, 2010, 06:20 PM
copeg
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.
• October 6th, 2010, 06:26 PM
Jnoobs
Re: Iterative Help
I dont understand what you mean
• October 6th, 2010, 07:27 PM
Jnoobs
Re: Iterative Help
I also need to do these but dont understand the question

Quote:

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:
Quote:

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:
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
Code :

```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; }```
• October 6th, 2010, 07:40 PM
copeg
Re: Iterative Help
One thing at a time...

Quote:

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.