2 Replies - 1968 Views - Last Post: 13 October 2009 - 05:53 PM Rate Topic: -----

#1 sub432   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 04-October 09

Random Iterator help

Posted 13 October 2009 - 03:59 PM

Hi guys, we just went over iterators in my lab class but I'm not sure what exactly they are. The explanation my TA gave was pretty confusing but I managed to write some down code. I was wondering if someone could explain to me the general concept of iterators so I can understand it. Our assignment was to create a RandomIterator and "demonstrate a faster convergence", which I don't understand either. So far my code looks like :

import java.util.*;

class it1 implements Iterator<Integer> {
	int v, nn;
	public it1 (int n) {
		v=0;
		nn = (int)Math.pow(2, n);
	}
	public boolean hasNext () {
		if (v < nn) { return true; }
		return false;
	}
	public Integer next () { return v++; }
	public void reset () { v=0; }
	public void remove() {}
}	
public class f10 {
	public void tt (int n) {
		int i, j, b, ibest=0;
		double h, h1, hh, h1best=0;
		double e[] = new double[n];

		for (i=0; i<n; i++) { e[i] = Math.sqrt(i); }
		for (i=0,h=0; i<n; i++) { h += e[i]; } hh = h/2;
	
		it1 ii = new it1 (n);
		while (ii.hasNext()) {
			i=ii.next();
			for (j=0, b=1, h1=0; j<n; j++, b<<=1) {
				if ((i & b) != 0) { h1 += e[j]; }
			}
			if (Math.abs(h1-hh) < Math.abs(h1best-hh)) {
				h1best=h1; ibest=i;
			}
			
		}
		System.out.println ("hh="+hh+" h1best="+h1best+" ibest="+
				Integer.toString(ibest, 2));
	}
	public static void main (String args[]) {
		f10 f = new f10();
		f.tt(Integer.parseInt(args[0]));
	}
}







I would appreciate any help

Is This A Good Question/Topic? 0
  • +

Replies To: Random Iterator help

#2 syfran   User is offline

  • D.I.C Lover
  • member icon

Reputation: 83
  • View blog
  • Posts: 1,103
  • Joined: 12-July 09

Re: Random Iterator help

Posted 13 October 2009 - 05:36 PM

Not sure what is meant by a faster convergence but iterators provide an easier and safer way of traversing a collection.
Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: Random Iterator help

Posted 13 October 2009 - 05:53 PM

I think your TA may have been referring to Convergence of Random Variables. Here is a link that may provide some more insight into the assignment:
http://en.wikipedia....andom_variables
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1