apines 116 Practically a Master Poster Featured Poster

Perhaps a little late, but since it is not marked as solved I will give it a go. In order to swap two variables, denoted 'a' and 'b', using a third variable, denoted 'temp', you need to do the following:
1. place one of the numbers, say the one inside variable 'a', inside 'temp'.
2. then place the value of 'b' inside 'a'.
3. Put previous value of 'a', now placed inside 'temp', into 'a'.

In code it will look like this:

SWAP_NUMBERS(int a, int b) //a and b are given as parameters
{
   int temp;
   temp = a //step 1.
   a = b    //step 2.
   b = temp //step 3.
}

now (even though you didn't ask but you might find it interesting), there is another way to swap to numbers without using the temp variable. With a simple math trick you can do the swap:

SWAP_NUMBERS_2(int a, int b) //a and b are given as parameters
{
   a = a + b; 
   b = a - b; //now b has the value of a.
   a = a - b; //now a has the value of b.
}

Hope that this helps you, and if it does, just mark the thread as solved :)

ddanbe commented: Nice :) +8
apines 116 Practically a Master Poster Featured Poster

I agree with SasseMan, the problem is pure analytic. In addition, if you have to stop for 30min for gas and food, that depends where on the road you have to do it. If the gas station is reached before the t calculated in the previous section, then you have to recalculate the equation with h/2 instead of h. If the gas station is reached after that t, then it doesn't matter, because you have already caught up with your friend.

apines 116 Practically a Master Poster Featured Poster

Sorry, I was looking at the first definition of x,y,z,p,q,Max where you said they were integers, not the later redefinition.

You are going to have to generate all possible solutions for a given MAX value to be able to randomly pick one for an even distribution. I don't see any other way. Which brings up the question: Why does it have to be even?

I am creating a test environment that generates random data at runtime. Given MAX, the random data is generated from the populated variables x,y,z,p,q. In order to cover all possibilities, I want the variables to be generated in a uniform fashion.

apines 116 Practically a Master Poster Featured Poster

Ok, I apologize and will try to clarify: x is a positive integer, MAX also. y,z,p,q are non-negative, meaning integers that can be positive or zero. if y is zero, then z,p,q are zero as well. If z is zero, then p,q are zero. If y is not zero, z,p,q can be in the range of zero and above, complete integers. If z is not zero, then p,q can be zero and above, complete integers.

Hope that clarifies.

apines 116 Practically a Master Poster Featured Poster

You are iterating only once on the entire array, so I can't how your algorithm will work...
I didn't check my code, but I think that should do the trick. It goes around all the subsequences possible and returns the start, end and max value of the subsequence with the highest value. Hope it helps.

MSS(Array[],N)//Where N is the number of elements in array
{
	sum=0; //current sum
	max-sum=0;//Maximum Sum
	seq-start=0;//start of the subsequence
	seq-end=0;//end of the subsequence
	for(i = 0; i < N; ++i)
	{
		for(j = i; j < N; ++j)
		{
			sum = 0;
			for(k = i; k <= j; ++k)
			{
				sum = sum + Array[k];
			}
			if(sum > max-sum)
			{
				max-sum = sum;
				seq-start = i;
				seq-end = j;
			}
		}
	}
}

Edit: I have found another thread on this site: http://www.daniweb.com/forums/thread49177.html discussing the same problem. Rashakil Fol had a nice idea there - a direct link to the post is http://www.daniweb.com/forums/post230344.html#post230344
His idea is better than mine.

apines 116 Practically a Master Poster Featured Poster

if y is not zero, z,p,q can be whatever they want, as long as the main inequality holds.

apines 116 Practically a Master Poster Featured Poster

x has to be strictly positive, y,z,p,q can be zero. If y is zero, then z,p,q must be zero as well. if z equals zero, then p,q are zero as well (think of it as levels in a tree, where x is top, y beneath, z beneath y and p,q are at the same level). You can assume that MAX is strictly positive as well.

apines 116 Practically a Master Poster Featured Poster

I agree with Momerath regarding the order of the generation - after running some test scenarios I've encountered reaching no integer solution when not starting from x. The problem with Momerath's solution though is that for a given MAX choosing uniformly x from [0,MAX], 50% of the solutions, that is [MAX/2,MAX] will result in y=z=p=q=0, meaning that tactic is surely not uniform. On the other hand, not giving x all the possibilities, let's say cutting it's initial distribution to [0,MAX/2], we will clearly not cover the entire wanted solution space...

apines 116 Practically a Master Poster Featured Poster

First of all, you can change your default downloads folder to something else, and you'll never know the difference nor miss not using your old downloads folder. If you define the default downloads to "abc\downloads2" you might solve the problem, even though I admit in a stupid way. Since the folder is empty, deleting it and re-creating it does not help?

The best way to do this thing is to manually copy the folders after the format, and check which folder causes the problem, meaning after which folder the bug starts. I know, it's an unbelievably annoying task, but this way you'll know for sure where is the rotten folder located, delete it from the backup, and never have to worry about something like that happening again.

apines 116 Practically a Master Poster Featured Poster

It might sound really stupid, but something like this happened to me once and I simply renamed the folder, and it helped (Windows, go figure). Can't you just copy all the relevant material besides the problematic folder?

apines 116 Practically a Master Poster Featured 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?

Hmmm, the thing is that choosing a uniform distribution for x, any results above MAX/2 ensures that no y,z,p,q will be chosen. Same goes afterwards for y,z etc'. If I fix the range for x to be [1, MAX/2] then all the other possibilities x=[MAX/2,MAX] will not be included, thus I am missing cases from the solution space...

apines 116 Practically a Master Poster Featured 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.

Since I can't see it, I'll be happy if you could elaborate how to find the solution space.

I think I wasn't clear on the parameters though. x,y,z,p,q,MAX are all non-negative integers. x and MAX are positive integers. you can look at it as in a tree, where x is the root and the rest are the children. Thus, if y is zero, it's children z,p and q must be also zero. If z is zero, it means that p and q are zero. I hope that this simplifies the solution space. In the tree analogy, MAX is the total number of assets possible in the tree.

apines 116 Practically a Master Poster Featured 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.

Missed your second question - I am using Java.

apines 116 Practically a Master Poster Featured 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.

You have a large solution space for the inequality, and I want one solution from all the possible solution to be returned - and I want that solution to be randomly picked with a uniform probability. Meaning that if I have for a given MAX n solutions, I want that each solution will have 1/n chance of being returned.

apines 116 Practically a Master Poster Featured Poster

Hello,

I am designing for work a testing environment which uses run-time randomly generated data to test our company's product.

The problem is as follows: I have an inequality of the sort of x(1+y(1+z(1+p+q))) <= MAX, when x,y,z,p,q,MAX are integers, MAX is given as a method parameter and the method needs to find x,y,z,p,q to suffice the inequality. The problem is that I need the solution to be in a uniform probability space, meaning that each solution of the x,y,z,p,q has the same probability to come up when providing the same MAX many times.

If possible, it would be great if you can think of a polynomial solution, if not possible O(n^2) will suffice as well. The algorithm needs to be run with high MAX values and big number of iterations on each test, so good complexity is vital.

Can you think of a way to produce uniform probability space for every type of linear equation?

I would appreciate any guidance on this matter, I am stuck on this algorithm for some time now.

Thank you!

apines 116 Practically a Master Poster Featured Poster

Looks perfect, thanks!

apines 116 Practically a Master Poster Featured Poster

Hi all,
I need some advice in implementing a new ADT that has length of n and the following operations:
1) Init() : Initializes all n elements to 0. Can assume that this is the first one called and called only once.
2) Write(i,x): write value x to position i.
3) Read(i): Read the balue at location i.
4) MultiplyAll(y): Multiply ll elements by y, and this can be done many times to create accumulative effect. Cannot get y=0 (returns error)
5) ZeroAll: Zeroes all values in the ADT to 0.
So far so good - But here is the catch: Except the Init, all operations need to have O(1) complexity!
Any ideas?
Thank You,
Aviad

apines 116 Practically a Master Poster Featured Poster

How about the following psuedo-code, I think it should do the trick:

  1. B[][] = new Array[n][n]
  2. C[] = new Array[n]
  3. for i: 1 to n do
  4. ----C = 0
  5. for j: 1 to n do
  6. ----d=(A[j]/n)+1
  7. ----B[d][C[d]] = A[j]
  8. ----C[d]++
  9. for i: 1 to n do
  10. // Perform a version of counting sort on each sub array (bucket). the total of the actions performed here is n no matter how the buckets are filled. The counting sort should be performed on each array but from 1 to c.
  11. counter = 1
  12. for i: 1 to n
  13. ----for j: 1 to c
  14. --------A[counter] = B[j]
  15. --------counter++

I couldn't indent for some reason so I used ---- instead, I hope the code is readable.
As you see, this is a modified bucket sort using counting sort on each bucket. Hope that was helpful.