11 Replies - 817 Views - Last Post: 11 November 2008 - 09:51 PM Rate Topic: -----

#1 webbywebb  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 95
  • Joined: 30-January 07

recirsive and iterator help

Post icon  Posted 05 November 2008 - 05:45 PM

i need to calulate the sum of 1+2+3+4+5+6+7+8+9+10 using a iterator and a recursive to implement it. i am so lost
Is This A Good Question/Topic? 0
  • +

Replies To: recirsive and iterator help

#2 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1385
  • View blog
  • Posts: 10,962
  • Joined: 06-February 08

Re: recirsive and iterator help

Posted 05 November 2008 - 06:04 PM

[rules][/rules]

In your loop:
int total = 0
for loop: i = 1; i <= 10; i++
	total += i


Recursively: Read my tutorial
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,908
  • Joined: 06-March 08

Re: recirsive and iterator help

Posted 05 November 2008 - 07:12 PM

View Postgabehabe, on 5 Nov, 2008 - 05:04 PM, said:

[rules][/rules]

In your loop:
int total = 0
for loop: i = 1; i <= 10; i++
	total += i


Recursively: Read my tutorial


Not really recursive and does not implement the Iterator
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,908
  • Joined: 06-March 08

Re: recirsive and iterator help

Posted 06 November 2008 - 08:35 PM

View Postwebbywebb, on 5 Nov, 2008 - 04:45 PM, said:

i need to calulate the sum of 1+2+3+4+5+6+7+8+9+10 using a iterator and a recursive to implement it. i am so lost

Re thinking of it... it is one or the other
You want to do it by iteration or recursively ? Can't be both unless Baavgay has a solution that satisfy both... this is the kind of challenge he likes
Was This Post Helpful? 0
  • +
  • -

#5 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: recirsive and iterator help

Posted 10 November 2008 - 03:19 PM

I think he was looking for one of each solution.
Was This Post Helpful? 0
  • +
  • -

#6 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: recirsive and iterator help

Posted 10 November 2008 - 03:20 PM

View Postgabehabe, on 5 Nov, 2008 - 05:04 PM, said:

[rules][/rules]

In your loop:
int total = 0
for loop: i = 1; i <= 10; i++
	total += i


Recursively: Read my tutorial


-_-

Don't just give him the answer...-ish.
Was This Post Helpful? 0
  • +
  • -

#7 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1385
  • View blog
  • Posts: 10,962
  • Joined: 06-February 08

Re: recirsive and iterator help

Posted 10 November 2008 - 03:49 PM

@PBL:
Iteratively doesn't just mean by using an iterator. The term can be used to refer to a loop, as I demonstrated.

If I'm not mistaken, an iterator object is merely a tool used to speed up the iterative process.

They're also used to point more specifically to a piece of data. For example, when you use a map in C++ you would do something like this:
map <string, char> _map;
// do some stuff to add values
map <string, char>::iterator it; // create the iterator
for (it = _map.begin(); it != _map.end(); ++it) {
    cout << it->first << '\t' << it->second;
}

In other words, our iterator actually takes the form of the item that it is pointing to. Using a simpler iterative approach, such as that which I demonstrated at the beginning of this thread, requires the use of an array subscript to get the value of that data.
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,908
  • Joined: 06-March 08

Re: recirsive and iterator help

Posted 10 November 2008 - 06:31 PM

View Postgabehabe, on 10 Nov, 2008 - 02:49 PM, said:

If I'm not mistaken, an iterator object is merely a tool used to speed up the iterative process.

To speed up the programmer's writing of an iterative process :D
Was This Post Helpful? 0
  • +
  • -

#9 webbywebb  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 95
  • Joined: 30-January 07

Re: recirsive and iterator help

Posted 11 November 2008 - 02:26 PM

mmmmm i c i am just so confused i though i had it before but i didnt :'(
Was This Post Helpful? 0
  • +
  • -

#10 BigAnt  Icon User is offline

  • May Your Swords Stay Sharp
  • member icon

Reputation: 101
  • View blog
  • Posts: 2,392
  • Joined: 16-August 08

Re: recirsive and iterator help

Posted 11 November 2008 - 02:57 PM

Do you mean this iterator. In the JCF the classes written can implement the iterable interface, which has one method that returns an iterator object. This iterator object has three methods: hasNext which determines if the collection has more elements, next() which returns the next element in the current iteration, and remove() which removes from the collection the last element returned by the current iterator.

If this is the iterator you need to use you will have to write your own collection class that implements the iterable interface. If you do this then you can loop through the collection using the for-each loop, which is like this:
for(<Object/primitivetype> <variableName> : <Collection>){ //etc


The for-each uses the iterator to go through the Collection just like if you use a regular for loop with indexes.
Was This Post Helpful? 0
  • +
  • -

#11 webbywebb  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 95
  • Joined: 30-January 07

Re: recirsive and iterator help

Posted 11 November 2008 - 03:13 PM

oh i c
Was This Post Helpful? 0
  • +
  • -

#12 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,908
  • Joined: 06-March 08

Re: recirsive and iterator help

Posted 11 November 2008 - 09:51 PM

View Postgabehabe, on 10 Nov, 2008 - 02:49 PM, said:

@PBL:
Iteratively doesn't just mean by using an iterator. The term can be used to refer to a loop, as I demonstrated.

If I'm not mistaken, an iterator object is merely a tool used to speed up the iterative process.

They're also used to point more specifically to a piece of data. For example, when you use a map in C++ you would do something like this:
map <string, char> _map;
// do some stuff to add values
map <string, char>::iterator it; // create the iterator
for (it = _map.begin(); it != _map.end(); ++it) {
    cout << it->first << '\t' << it->second;
}

In other words, our iterator actually takes the form of the item that it is pointing to. Using a simpler iterative approach, such as that which I demonstrated at the beginning of this thread, requires the use of an array subscript to get the value of that data.


OK you are 100% right Gabehabe but this what I would called an Iterator in Java

	  int[] list = {1,2,3,4,5,6,7,8,9,10};
	   int sum = 0;
	   for(int x : list)
		   sum += x;
	   System.out.println("Sum: " + sum);
 


but you are right for C++... even Boost library has a FOREACH macro that performs what your code does
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1