Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have this code which reads from a file:
dummy = fd.readLong();
for(i = 0; i < chunks; i++) {
dummy2=fd.readLong();
chunksizes[i] = dummy2- dummy;
dummy = dummy2;
}
I now have a list of chunk sizes and I want to write them in the format above. I have an array called actualSizes which is the sizes I want to write, I have the amount of chunks. Any psuedocode that could do this? My head is going crazy yet it looks so simple
Seems that the file has a list of offsets and that you load them into an array as deltas (chunk sizes):
0 100 250 420 580 Offsets from file
└──┬──┴──┬──┴──┬──┴──┬──┘
100 150 170 160 Deltas, aka chunk sizes
You then say:
I now have a list of chunk sizes and I want to write them in the format above.
The only "format above" would be the list of offsets, so you create a list of offsets from a list of delta (chunk/actual) sizes, by starting with 0 and writing a running sum:
long offset = 0;
fd.writeLong(offset);
for (long size : actualSizes) {
offset += size;
fd.writeLong(offset);
}
120 140 130 150 actual sizes
┌──┴──┬──┴──┬──┴──┬──┴──┐
0 120 260 390 540 offsets in file (running total)
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
My professor claims my Minheap is incorrect on the following question:
Convert the following numbers to minheap using min heap down and draw the final minheap tree level by level and final array content.
Array given: 100 10 80 30 60 50 40 70 20 90
My answer was as follows:
10
/ \
20 40
/ \ / \
30 60 80 50
/\ /
100 70 90
Sorted Array: 10,20,40,30,60,80,50,100,70,90
Am I incorrect?
A Min-Heap is a complete binary tree in which the value in each internal node is smaller than or equal to the values in the children of that node.
Mapping the elements of a heap into an array is trivial: if a node is stored an index k, then its left child is stored at index 2k + 1 and its right child at index 2k + 2.
your answer is right you miss placed 70 and 100 that's wrong here otherwise its correct
the sorted array will be
10,20,40,30,60,50,80,70,100,90
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I have for loop without printed value , I should put the value which will print in this for loop :
for (int i = 1; i <= 8; i++) {
out.print(_________+" ");
}
The sequence should print : 57 46 35 24 13 2 -9 -20
Any help for solve this
Your sequence should start with 57 and go down by 11 until -20 (if I am right).
So, just start the loop with 57, use >=20 as condition and execute -=11 every time:
for(int i=57;i>=20;i-=11){
out.print(i+" ");
}
In the loop body, i (that changes acvording to your sequence) and a space is being printed every time.
If you want to use the same loop, you can do this:
for (int i = 1; i <= 8; i++) {
out.print(68-11*i+" ");
}
This calculates the sequence values by taking the next higher value (than the first value) and substracted 11 for the first time, 22 for the second time etc.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I want to generate random number between two numbers which are multiples of 10.
For example, between 100 and 500, I want to generate a uniform distribution of 110, 120 ... 490, 500.
I think it is better to use Random.nextInt() as per THIS post. But not sure how to do that.
You can use this function.
public int random()
{
Random r=new Random();
return (r.nextInt(41)+10)*10;
}
Read this Math.random() versus Random.nextInt(int)
Random.nextInt(n) is both more efficient and less biased than
Math.random() * n
(Random.nextInt(41)+10)*10 is the correct answer. The Random.nextInt(41) will generate numbers between 0 to 40. The Random.nextInt(41)+10 will generate number from 0 to 50.
And Hence (Random.nextInt(41)+10)*10 will generate numbers between 100 and 500.Please note that 100 and 500 are also included in the result.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am in 8th grade. I am taking a 12th grade java course. I just started and my home work is to make a Fibonacci series dynamic array program. I don't know where to go. It is online so I can't ask a teacher or something.
How do you make a Fibonacci sequence using a dynamic array in Java?
This was an example of a dynamic array I got:
I don't see how you can make the Fibonacci series out of it! Here is the Fibonacci series example I got.
You can combine the two examples, as such:
Take the DynamicArrayOfInt class, and add the main method of the Fibonacci class.
Insert a new statement at the beginning of the main method instantiating a DynamicArrayOfInt object, as such:
DynamicArrayOfInt arr = new DynamicArrayOfInt();
Replace every instance of numbers[x] with arr.get(x), and instances of numbers[x] = y with arr.put(x, y).
Remove the leftover statements dealing with the numbers array. This will essentially make use of the DynamicArrayOfInt object. A sample output would look like this:
iplante$ java DynamicArrayOfInt
Size of dynamic array increased to: 2
Fibonacci series:
0
1
Size of dynamic array increased to: 4
1
2
Size of dynamic array increased to: 8
3
5
8
13
Size of dynamic array increased to: 16
21
34
55
89
144
233
377
610
Size of dynamic array increased to: 32
987
1597
2584
4181
iplante$
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Regarding the classic problem of putting N identical balls into M distinct bins and printing all the combinations: What if you would want to extend the problem by printing all cases 0< M, N
The brute force method could be done something like this:
for (int i =0; i<M; i++)
{
for (int j =0; j <N; j++)
{
PrintAllCombinations(j,i)
}
}
Now if we study the output of the first couple m and n, we see that the output of each previous iteration is a subset of the next. It seems to me that we can apply a dynamic algorithm to exploit this phenomenon. However, because we still need to partition every n, for example n=3 = 3 +0, 2+1, 1+2. we still need to do alot of redundant combination calculations. Any ideas fir improvments?
Let S[i][j] be the number of combinations for i balls in j bins.
S[0][j] = 1 for all j since the only combination is to have all bins empty.
S[i][1] = 1 for all i since the only combination is to put all the balls in the one bin.
For every other i, j S[i][j] = sum(x = 0 -> i, S[i-x][j-1]). That is for every other position you can compute the number of combinations by assigning every possible number of balls to the last bin and sum the number of combinations you get.
If you want to print out the combinations you can replace the count with the actual combinations and append the value x when you take the internal combinations in the sum. That will take a lot of memory without a lot of gain in speed. Just do the recursion and repeat the computation since you're bound by the number of solutions anyway.