SasseMan 49 Junior Poster

wasn't all variables positive integers? If not then there are an infinite number of solutions.

edit.

"x,y,z,p,q,MAX are all non-negative integers".

SasseMan 49 Junior Poster

Would you like a blueberry pie with that solution?

SasseMan 49 Junior Poster

table is a pointer to a ListNode so you can't write table[hash(val)], you need to dereference the pointer first like this, (*table)[hash(val)]. I think that might be the issue.

SasseMan 49 Junior Poster

This can be solved analytically, why pseudocode? The pseudocode in this case is almost like the mathematical expressions needed to solve it.

s2*t = s1*t + s1*h <=> t(s2-s1) = s1*h <=>

t = s1*h/(s2-s1)

which is the pseudocode and the real code for that matter.

SasseMan 49 Junior Poster

That is the taylor series for sin(x) i.e. sin(x) is approximately x- (x3/3!)+ (x5/5!)- (x7/7!)+.......

What do you want to solve?

SasseMan 49 Junior Poster

Is that all the relations? If y is not 0 can z,p,q be anything, or must they be equal and larger or something?

SasseMan 49 Junior Poster

If you change the order in which you generate the variables you have to take into account the relationship between the variables. Of course there is a way yo choose random variables in any given order, what is stopping you? You just have to make sure you pick values inside the space. Could you give the exact mathematical relation between the variables?

SasseMan 49 Junior Poster

HAHA! Iv'e never heard of this problem before, sounds (hehe) really weird. But if you move the mouse programmatically, how is your program going to detect when you start moving the real mouse? I don't think I can help you with this. Best of luck!

SasseMan 49 Junior Poster

Note. my equation was wrong, should say y(1+z(1+p+q)) = 1.

How about randomizing the order in which you pick the variables to randomize? Take for example a list with 5 elements [1,2,3,4,5] which represent the variables x,y... randomize the ordering of the list, and then generate random numbers for each variable in that order.

If your'e unsure about whether some algorithm truly picks random solutions uniformly you could implement it and run it on a small testset a couple of million times and se if the distribution is uniform or not.

SasseMan 49 Junior Poster

Ok can't you just print some of the content in the array to se whats inside? Do you mean black and white or grayscale when you say black and white, there's a huge difference?

SasseMan 49 Junior Poster

If you want to calculate the height by going down the tree you don't need all that code. Just follow nodes down until you reach the end and thats your height. But the better thing to do is to keep track of the number of nodes in the data structure an just return ceil(log2(n)). Your code look to be running in O(n), I would do it like this...

int height( node *AVLtree ) {
     if(AVLtree == NULL) return 1;
     return 1 + height(AVLtree->left);
}

But of course you can be off by 1 if you use this method, it depends on what the heigh value is going to be used for. If your looking for the most "correct" solution you probably want to go through the entire tree. I'm not sure if your code will work though, it not even returning anything.

SasseMan 49 Junior Poster

Are your sure you can't get a uniform distribution and that variables can go to inf.? The set 0 < x(1+y(1+z(1+p+q))) < MAX, is closed i.e. there are a finite number of integer solution. Take this 2D case as an example...

http://www.wolframalpha.com/input/?i=0+%3C%3D+x%281%2By%29+%3C%3D+100

You can choose a random x and y from that set and you have your answer. The 5D case is exactly the same. You should just be able to randomize a variable at a time, the solution space changes, and then take the next variable, and the next, until you have all variables.

Say you get a random number from 0 to MAX, ex. x=2. Then you have y(1+z(1+p+q)) = MAX/2 left, etc.

Anything wrong with my reasoning?

SasseMan 49 Junior Poster

Yes netbeans can be a pain in situations like this. Save yourself some headache and create a new project.

SasseMan 49 Junior Poster

Ok as i thougt. I am guessing you didnt see the sarcastic tone in my message. Have you made any Effort into solving it yourself, or do you just want the answer?

SasseMan 49 Junior Poster

Could you give some more details? What code are you calling and how are you selecting?

SasseMan 49 Junior Poster

What kind of data do you have in the array now? RGB values maybe? What language do you use?

SasseMan 49 Junior Poster

Do you want to move the mouse programatically to fix the sound issue? It sound more like a hardware problem to me. Does the same thing happen with a regular mouse connected?

Mouse events in C++ are a lot more complicated that in java. I'm no expert on that but here a thread where the topic is covered...

http://www.daniweb.com/forums/thread172653.html

SasseMan 49 Junior Poster

OK i get it now. It should be easy to find the solution space for each variable. Then you can just use the built in uniform random number generator in java to pick one solution. Check the java api for java.math and java.math.BigInteger. Theres even a constructor for uniform random number generation for BigInteger.

BigInteger(int numBits, Random rnd)

Constructs a randomly generated BigInteger, uniformly distributed over the range 0 to (2numBits - 1), inclusive.

SasseMan 49 Junior Poster
SasseMan 49 Junior Poster

An avl tree is a self balancing tree which means that there is a mathematical relationship between the number of elements in the tree and the height.

Since a lookup in a AVL tree takes O(log(n)) time, the height should also be O(log(n)) i.e. ceiling(log2(n)). Read about avl trees here...

http://en.wikipedia.org/wiki/AVL_tree

SasseMan 49 Junior Poster

I'm not sure that I understand what you are after. The equation can have many solutions. Do you want to calculate the definition space and randomly select variables x,y... inside that space? What language are you using? It should be easy to find an algorithm that produces random uniform variables.

SasseMan 49 Junior Poster

Ok! When do you want it delivered?

But first. Define even array.

SasseMan 49 Junior Poster

Thats great that you want to study more. I'm just done with masters degree in Engineering and Computer Science and I have no good answer to exactly how and when the errors occur. It is a very specific question. You will maybe have to look into how java works and it may also depend on the underlying architecture. If you really want do get into it, I suggest buying a book about numerical analysis. If you are going to study computer science, the best thing to be ahead of is math. You would really benefit from that.

SasseMan 49 Junior Poster

Yes that it what you want to do. Loop through the scanner and place the seating information into the correct places in the arrays.

I think it will be easier if you construct a 2D array and only store the name of the passenger in each index. If the plane has 3 seats on a row and 7 rows you can create the following array.

String[][] passengers = new String[7][3]; //[rows][leftSeats+rightSeats]

Then passenger C.Lindbergh 1A would be placed in passengers[0][0] (row 0, seat 0), passenger L.Bleriot 1B in passengers[0][1] [row 0, seat 1] and so on. The seat and row name can be derived from the indexes in the array. passengers[0][X] is row 1. passengers[X][1] is row X seat B. You can of course also store the seat information along with the passengers.

All you need to to then is to go trough the file. parse out each passenger and the seat, and place them in the correct place in the 2D-array.

pseudocode:

construct 2D-array passengers[rows][seatsPerRow]

for each passenger p do  //your while loop

      String[] passengerInfo = split p with "-" delimiter.

      row = convert passengerInfo[0] to integer - 1. (-1 since arrays indexes start from 0. Lookup       Integer.parseInt() in java api).

      seat = passengerInfo[1]

      name = passengerInfo[2]

      seatNumber = convert seat string to number // if (seat is "B") seatNumber = 1

      passenger[row][seatNumber] = name.  // place the passengers name in the correct place.

When you have done this you can just loop …

SasseMan 49 Junior Poster

Of course you don't have the real square root, thats what you want to approximate. But you do have the square of the "real" root which is your input data "n". You only need the square and the approximated root to do the error calculation. The "real" root was used just to to make a point. You want itn*itn to be very close to n, hence abs(n-itn*itn) should be a small number of your choice.

Its not that using integers and doubles are imprecise, as I said you can get the precision you want in your calculation by knowing how big the error is. But if you use BigDecimal etc. you can have an arbitrary amount of precision and size, with your computers memory as a limit. For example you can't use integers, floats or doubles if you want to calculate the square root of a huge number, or if you want more precision than 16 decimals. It all depends on what your doing, if there's no reason to use BigInt then you shouldn't, using integers will probably yield faster execution. But in your case I would use BigDecimal for the input value n also. Real numbers have roots to :) Also an integers max value is not very large.

The important thing here, as in all numerical calculations, is to know how large your error is and to know how much error you can afford to have. This is really important as errors accumulate with time and …

Bladtman242 commented: The problem has been solved, and I really have gotten so much more than just the sollution out of theese answers. +2
SasseMan 49 Junior Poster

Since you either should use a 2D array or 3 "parallel" arrays that must mean that you use 3 1D arrays to store each column of seats.

One array would then contain [ [C.Lindbergh 1A], [H.Quimby 2A], ..., [] ] etc. I would go with a 2D array since that would be more general for amount of rows and columns.

SasseMan 49 Junior Poster

Could you be more specific about what you need help with? What isn't working, and so on.

SasseMan 49 Junior Poster

Yeah I know, but there seem to be some kind of numerical errors along the way, maybe when adding to itn. You should really not get hung up on this. I would be happy with the answer and would just throw away the last 6 decimals. The important thing is that you know that, what you have calculated, has 10 decimals of accuracy and the rest is error.

You could also instead of counting how many decimals you have progressed, just calculate the difference between the numerical value and the "real" value and stop the iteration when it reaches a certain accuracy. Like this...

r = numerical root
R = real root
X = R*R, i.e. your n variable.

error = abs(X-(r*r))

In your while loop you say - while(error<someSmallValue) - where someSmallValue is 0.0000000001 if you want 10 decimals of accuracy - instead of counting decimals, this is usually how you handle this when making numerical approximations.

And yes, you should not use types like integers, floats and doubles if you want to do hardcore math stuff. Use the classes that are made for these kind of calculations like BigDecimal etc. (check the java api, java.math), or GMP if your'e into C/C++.

SasseMan 49 Junior Poster

I think your question is a little poorly stated. You should give some more background on what you are doing. Like how are you connection to the DB, what DB are you using etc.

but heres a link which should be helpful.

http://en.wikipedia.org/wiki/Java_Database_Connectivity

SasseMan 49 Junior Poster

I don't get this about numerical errors and why using 1/i is better. Could you please explain it?
Or provide a link for further reading:)
Why is this?

Im no expert on exactly how numerical error occur, but computers has limited precision and resources. But specifically, numerical errors get larger when calculating stuff that are approaching singularities.

when you write i = 1/10 you get a small error in your result. Then when you divide i by 10 again you already have an error and are adding more because of the singularity ( i -> 0 ). This might not happen when dividing 1/10, but later in when i is small.

My point was that if you multiply i by 10 instead and do the calculation 1/i once you don't get the accumulating error in i since multiplying 10*10 or 100*10 and so on does't give as much numerical errors, if any. Then you only make the division 1/i once which gives you less error.

Try to print the value of i in your loop after you divide it by 10. You will se that the value isn't exactly what you are expecting. And then print 1/i when you multiply it by 10.

And with 1/i it will still calculate 16 decimals you say?

As I said I'm not completely sure myself about how the errors work, but the first 10 decimals should be correct and the last part is crap which you can …

Bladtman242 commented: Provided information which helped cast light over the sollution +2
SasseMan 49 Junior Poster

Oh and I forgot! You should have posted this under computer science.

Hope I was helpful!

SasseMan 49 Junior Poster

Its because of the numerical errors you get when you repeatedly divide i by 10. You should multiply i by 10 and use 1/i instead. You will still get numerical errors and there will be 16 decimals because you are working with doubles and it will prob. not end with zeros, but you will at least not add as much numerical errors as before.

int n; //input number
			double itn = 0; //incremental test number
			double i = 1; //incremental factor
			//n = Integer.valueOf(args[0]);
			n = 10;
			int j = 0;
			int decimals = 0;
	
			while ((itn * itn) != n && decimals < 10) {
	
				if (j > 0) {
					itn = itn - 1/i;
					i = i*10;
					decimals++;
				}

				if (j == 0) {
					j++;
				}
						
				while ((itn * itn) < n) {
					itn = itn + 1/i;
				}
			}
			System.out.println(itn);

I think also it is safe to say that your algoritm is calculating the square root of n. Are you supposed to do it like this? Because there are much faster ways. Google newton-rhapson for example or read this...

http://en.wikipedia.org/wiki/Square_root

SasseMan 49 Junior Poster

Yea i know recursion can be a bitch when your'e not used to it.

SasseMan 49 Junior Poster

I've helped you a little to much now. Make sure you understand the code. It's pretty simple.

public class Main {
	

	public static void main(String [] args) {
		System.out.println( duper("aaaaasssdddddfdeggghhh") );
	}
	
	public static String duper( String str ) {
		
		if ( str.length() <= 1 ) {
			return str;
		}
		
		String current = str.substring(0,1);
		String next = str.substring(1,2);
		String rest = str.substring(1);
		
		if( next.equals(current) ) {
			return duper(rest);	
		}
		else {
			return current + duper(rest);
		}
		
	}
	
}
SasseMan 49 Junior Poster

I mean that it takes time proportional to the amount of characters in str, which is as fast as it can be.

the last character dupe can be handled in the <= 1 case.

if ( str.length() <= 1 ) {
			if ( str.equals(last) ) return ns;
			return ns + str;
		}
SasseMan 49 Junior Poster

I wrote something that works fairly. Linear time Does not handle if the last character is a dupe. That is left for you to fix.

public static void main(String [] args) {
		System.out.println( dupe("assdeegfddaa") );
	}
	
	public static String dupe(String str) {
		return dupeHelper( str, "", "" );
	}
	
	public static String dupeHelper( String str, String last, String ns ) {
		
		if ( str.length() <= 1 ) {
			return ns+str;
		}
		
		String current = str.substring(0,1);
		
		if( current.equals(last) ) {
			return ns + dupeHelper( str.substring(1), current, ns );
		}
		
		else {
			return ns + current + dupeHelper( str.substring(1), current, ns );
		}
		
	}
SasseMan 49 Junior Poster

Are you trying to return all three values in that method? If that is the case, it is not possible. And why are you setting the values to 1 that your are returning? Please explain a little more what you are doing or give some more code.

If you want to return all three values from that method you need to encapsulate them into an object.

It looks like you should make a Date object with day, month, and year as member variables. Then you can return the Date object from that method instead.

The code you have written there will not compile. You can only return 1 object.

SasseMan 49 Junior Poster

I think imshow displays grayscale images. You have to call imshow(RGB) first or something like that. Read up on imshow here...

http://www.mathworks.com/help/toolbox/images/ref/imshow.html

And I believe that method you are using to pixelate the image just resizes the image down to some scale and then up again. Read about resize here...

http://www.mathworks.com/help/toolbox/images/ref/imresize.html

Anyhow. Your problem seems to be imshow.

SasseMan 49 Junior Poster

I assume you are talking about classical AI. I took a classical AI course a long time ago which was mostly about logical and statistical reasoning. We had a project in that course where we in groups of four made a minesweeper playing bot, which is a good example of what kind of problems classic "AI" is used to solve.

But in general i would suggest some kind of game to which you make a bot, whether its minesweeper, chess, poker or something else.

SasseMan 49 Junior Poster

Hi, Im kind of new to prolog and would like som help.

I want to write a rule that only takes integers as input like the following example.

rule( X, [] ).
rule( X "is integer", [Heat|Tail] ) :- rule(X, Tail).
rule( X "is anything else", [Heat|Tail] ) :- rule(X, Tail)

Is this functionality avaliable? I want the rule only to be called if X is and Integer and the other rule otherwise. I don't not want to test if X is an integer like this...

rule(X, [Head|Tail]) :- integer(X), rule(X,Tail).

Anyone who can help?

SasseMan 49 Junior Poster

You can add a MouseListener to a JLabel just as a JButton the difference is that a label will not change its appearance when you move you mouse over it or click, you'll have to do that yourself.

For example...

JLabel label1 = new JLabel("label");
label1.addMouseListener(this);

"this" must then implement the MouseListener interface in order to listen to the events.

You can also do the almost same thing this way.

label1.addMouseListener( new MouseAdapter() {
   public void mousePressed(MouseEvent e) {
      //code to handle mouse pressed.
   }
   public void mouseEntered(MouseEvent e) {
      //code to handle mouse entered,
   }
} );

Check out the java api for MouseListener and MouseAdapters, and google some tutorial if you feel uncertain about how to use it.

SasseMan 49 Junior Poster

You can override the paint method of your canvas, which you have already done and then call g.drawLine(x1,y1,x2,y2). where x1 y1 etc. are the points which the line is drawn between. Look at the Graphics and Graphics2D api for more info.

http://java.sun.com/javase/6/docs/api/java/awt/Graphics.html

SasseMan 49 Junior Poster

just iterate through the list and print the value when you have reached the index that is to be printed.

i = 0
n = 0
for each value in list
if i == index[n]
print list
n = n+1
end
i = i+1
end

this should work since the index list is sorted and should take O(N) w.r.t. list, not index.

SasseMan 49 Junior Poster

hmm I dont se why this shouldn't work, try changing the "=" to "is" in the query and see if it helps.

SasseMan 49 Junior Poster

You could use a try catch statement and an if statement to solve it. Something like this...

public boolean isNaturalNumber( String input ) {
   boolean correct = false;
   try {
      int i = Integer.parseInt( input );
      if ( i > 0 ) correct = true;
   }
   catch(Exception e) {}
   return correct;
}

Just call this method from where you want to check for natural numbers and ask for the input again if it returns false;

SasseMan 49 Junior Poster

You need to be more specific about your problem.

SasseMan 49 Junior Poster

Since the try statement might fail your variables will not be initialized, just as the compiler states. either you can place the code that uses firstArg and secondArg in the try statement or you can initialize the variables to something.

You could also set firstArg and secondArg to some default value in the catch statement. I know that you call system.exit(1) in the catch statement but the compiler probably doesn't care about that.

Java is quite good at catching errors like this, but try to always initialize variables when you create them to save yourself from headache in the future.

Bladtman242 commented: Even though it might be basic stuff to you, it was great help to me :) +2
SasseMan 49 Junior Poster

What i meant was, if you would like to not use a JButton as the representation for your days in the calendar, you could make a class that extends for instance a JPanel and then add whatever components and use whatever layouts that are necessary and customize the appearance to your liking.

What i always do is to draw an image of what I want a GUI component to look like, and then i implements it. You can also make your new class listen to mouse event just like a button to change its appearance and act on events accordingly.

But your calendar doesn't in my opinion look bad now. If you want a more paper calendar look to it, you could create class that extends JPanel, set its background to white or whatever, add a simple black border to it, and a centered label showing the day number. You can then make the constructors take in the same thing as a JButton does, i.e. a string, or whatever information you want to display in that component.

If you want to show an image in the button on mouse over event just add a MouseListener to the buttons and show your image when the mouse has entered and remove it when the exits.

http://java.sun.com/javase/6/docs/api/java/awt/event/MouseListener.html

SasseMan 49 Junior Poster

Well i don't think it looks so bad, but if you would like the buttons to be closer to each other, i.e. no gap between them you could try to set the vgap and hgap for the your layout. I suppose you are using gridlayout for the calendar, just class gridlayout.setHgap(0), and the same for setVgap.

I see now that the weekdays are all mixed up, you should try to make a timeline for weekdays on the top, monday to sunday and then for every month put the days in the right place. Should be to hard, just calc. what day the first of the month begins with and start with adding buttons in the grid from there, add empty buttons or buttons from the previous month before that. The width of your grid should apparently be the same as number of weekdays.

You maybe also want to make the date picker a bit more separated from the calendar. Right now it looks a little mixed up. There are also lots of free date pickers to download if you don't want to make it yourself, just google java date picker or something.

You could also maybe write your own component for the days instead of using a jbutton. That way you can make it look less like a button and also put more information in it. Maybe show the day of month in the top right or left corner and then bullet point information about its content.

SasseMan 49 Junior Poster

Wrong, it's "super easy" for anyone who knows a tiny bit about Java.
No Hibernate or other persistence framework needed, just common sense.

In fact it's so trivial the only way to be more specific than I already was would be to give him all the code to actually do it, and I'm not going to do that.

Yours and mine definition of what is trivial and super easy could be discussed, obviously not trivial or super easy to some. And I don't see why using JDBC would be much simpler than hibernate. With hibernate you will not have to handle how objects are persisted, retrieved etc. With JDBC you will.

Anyways the solution to this isn't a couple of lines of code (maybe just a bit more) if thats what your after. If you want to use JDBC java has a great getting started guide (link follows) with very good examples which should get you going pretty fast if you are familiar enough with java.

JDBC
http://java.sun.com/javase/6/docs/technotes/guides/jdbc/getstart/GettingStartedTOC.fm.html

Hibernate
http://docs.jboss.org/hibernate/core/3.3/reference/en/html/tutorial.html

Most of chapter 1 in the hibernate tutorial can be done with wizards included in IDEs like netbeans, but it's good if you get a grasp of how config files and mapping files works, and the basics of how hibernate operates, sessions, transactions, hibernate query language (similar to sql but works with java objects instead of tables and columns) etc.

Which ever method you choose you will most definitely end …