Let's say I have an NSArray of 60 elements. Out of it, I would like to get 4 values at the indexes 15, 30, 45, 60. I was trying to put it into a for loop but it didn't work quite well here:
for (int elementIndex = x_valuesDataArray.count / 4; elementIndex < x_valuesDataArray.count; elementIndex = elementIndex + elementIndex){
NSLog(#"Element index is %d", elementIndex);
NSLog(#"Total values %u", x_valuesDataArray.count);
NSString *firstValue = [x_valuesDataArray objectAtIndex:elementIndex];
[xAxisArray02 addObject:firstValue];
}
But my NSLog output says I'm, doing something wrong:
2013-04-12 13:48:59.267 myApp[44682:907] Element index is 15
2013-04-12 13:48:59.269 myApp[44682:907] Total values 60
2013-04-12 13:48:59.270 myApp[44682:907] Element index is 30
2013-04-12 13:48:59.271 myApp[44682:907] Total values 60
What am I doing wrong here?
for (int i = 1; i <= 4; i++) {
int elementIndex = x_valuesDataArray.count * i / 4 - 1;
// ...
}
should work. Note that an array with 40 elements has indices 0 .. 39. In this case the above
loop gives the indices 9, 19, 29, 39. It is also assumed that the array has at least 4 elements.
A. The x_valuesDataArray has 60 not 40 elements as you predicted.
B. This line: elementIndex = elementIndex + elementIndex should be
elementIndex = elementIndex + 10
If the valuesDataArray was 40.
Related
I am trying to divide Time in float form (hh.mm) by an integer.
For example 1.30 by 2 must give 00.45.
Is there any simple way to do this?
Using a float to express h.mm is a bit unusual. You would typically use strings for formatting.
However, I'd start by extracting hours and minutes from the float value. To do so, I would convert the float to a string using format:
time = 1.3
time_str = format('%.2f', time)
#=> "1.30"
Then I would split the string at . to get the hour part and minutes part and call to_i to convert them to actual integers: (I'm using map here, you could also call h = h.to_i / m = m.to_i afterwards)
h, m = time_str.split('.').map(&:to_i)
h #=> 1
m #=> 30
Now that we have the numbers 1 and 30 as integers, we can easily calculate the total duration in minutes:
duration = h * 60 + m
#=> 90
I would then divide the duration by 2 (or whatever value):
duration /= 2
#=> 45
and convert it back to hours and minutes using divmod: (it returns both values at once)
h, m = duration.divmod(60)
h #=> 0
m #=> 45
We can format these as a string:
format('%02d.%02d', h, m)
#=> "00.45"
or convert it back to a float:
time = h + m.fdiv(100)
#=> 0.45
Which can be formatted like this:
format('%05.2f', time)
#=> "00.45"
time = 1.3
divisor = 2
hr, min = (time.fdiv(divisor)).divmod(1)
#=> [0, 0.65]
min = (60 * min).round
#=> 39
"%02d.%02d" % [hr, min]
#=> "00.39"
Another example.
time = 1005
divisor = 5
hr, min = (time.fdiv(divisor)).divmod(1)
#=> [201, 0.0]
"%02d.%02d" % [hr, (60 * min).round]
#=> "201.00"
See Integer#fdiv, Float#fdiv, Integer#divmod and Integer#round. divmod is an extremely useful method that, for reasons I don't understand, seems to be under-used.
Maybe you can split into an array an then:
n = 2
[1, 30].then { |h, m| [h / n, (m + h % n * 60) / n]}
#=> [0, 45]
For splitting:
num = 1.3
('%.2f' % num).split('.').map(&:to_i) #=> [1, 30]
You can try the following :
num = 1.3
splitted_values = ('%.2f' % num).split('.').map(&:to_i) => [1, 30]
((splitted_values[0] * 60) / 2) + (splitted_values[1] / 2 ) => 45
I have a dynamic array. I need to display tableview as following scenario...
In the First cell i need to display 1 item.
In the second cell i need to display 2 items.
In the third cell i need to display 3 items.
In the forth cell i need to display 1 item.
In the fifth cell i need to display 2 items.
In the sixth cell i need to display 3 items.
and so on...
Could any one please suggest how to return no of rows in a section.
Try this :
int noOfRow = total/2 + ceil((total % 3)/3.0);
Simple logic for this is:
NoOfRows = TotalCount / 2
For e.g.:
If last value is 6 then, total no of rows are (6 / 2) = 3
If last value is 12 then, total no of rows are (12 / 2) = 6
You have to think logical that's it.
Hope this helps.
A faster method might be:
Notice in the divide by 2 method, most numbers work. The ones don't work are:
2, 4, 8, 10... basically, even numbers that aren't divisible by 6.
So we can come up with something like:
int count = array.count;
if (count % 2 == 0 && count % 6 != 0) {
count + 2;
}
int rows = ceilf(count / 2);
Or we can write a for loop:
int counter = array.size;
int rows = 0;
int dec = 1;
while (counter > 0) {
rows++;
counter - dec;
dec = dec % 3 + 1;
}
The for loop is of course, slower.
I was trying to do some math in something other than base 10, and wanted to get some input from the Dart community on possible directions I can take.
So, for example, suppose p = 2. Then, looking at 100 in base 2:
100 = (1 · 26
) + (1 · 25
) + (0 · 24
) + (0 · 23
) + (1 · 22
) + (0 · 21
) + (0 · 20
)
so
100 base 2 = 1100100
Add 1 to each digit and multiply the answers together:
(1 + 1)(1 + 1)(0 + 1)(0 + 1)(1 + 1)(0 + 1)(0 + 1) = 8
Try it for 3:
100 = (1 · 34
) + (0 · 33
) + (2 · 32
) + (0 · 31
) + (1 · 30
)
so
100 base 3 = 10201
(1 + 1)(0 + 1)(2 + 1)(0 + 1)(1 + 1) = 12
In Dart, I came up with the following;
https://gist.github.com/anonymous/11345381
int rows = 1000000000;
int total = 0;
for(int i = 0 ; i < rows ; i++ ){
total += i.toRadixString(7)
.split('')
.map((s) => int.parse(s) + 1)
.reduce((prev,next) => prev*next);
}
print('${total} results');
This does work, and seems to be ok for small number of rows. But for larger numbers, it is really quite slow.
As shown, I am converting an int to a string (resulting in a string representation of a number), splitting it, mapping the split characters back to ints, adding 1 to each int, and then multiplying them all together.
Am I missing something when it comes to working with numbers in Dart, other than base 10?
Going through the string seems inefficient. How about doing it directly?
int digitProduct(int n, int base) { // non-negative n and base only
int product = 1;
while (n > 0) {
int digit = n % base;
n = n ~/ base;
product *= (digit + 1);
}
return product;
}
main() {
int rows = 1000000000;
int total = 0;
for (int i = 0; i < rows; i++) {
total += digitProduct(i, 7);
}
print("${total} results");
}
Also 1000000000 is a pretty big number. If it takes a microsecond per row, it will still take ~16 minutes to complete.
Division by arbitrary bases is slow. If you hardcode the base to be 7, it'll probably be significantly faster (I expect ~90% of the time spent in digitProduct to be used on ~/ and %).
local i = nil
local randNum = nil
local tableSize = nil
local gap = 300
local t = {1, 2, 3, 4, 5}
local tableSize = 5
for i=1,5,1 do
randNum = t[mRandom(tableSize)]
table.remove(t, randNum)
if randNum == 1 then
pencilOne.x = 680 + (gap*i)
pencilTwo.x = 680 + (gap*i)
end
if randNum == 2 then
scissor.x = 680 + (gap*i)
end
if randNum == 3 then
paperClip.x = 680 + (gap*i)
end
if randNum == 4 then
inkPot.x = 680 + (gap*i)
end
if randNum == 5 then
gum.x = 680 + (gap*i)
end
tableSize = tableSize - 1
end
I was trying to get a unique random value after each iteration. I am using the table.remove technique to get it done. I dont know why it is not giving me the unique random value. kindly, help. :)
These lines extract a value from the table, and then wrongly use the value itself as an index to table.remove:
randNum = t[mRandom(tableSize)]
table.remove(t, randNum)
What you really want to write is:
randIndex = mRandom(tableSize)
randNum = t[randIndex]
table.remove(t, randIndex)
The problem is in the line:
randNum = t[mRandom(tableSize)]
Lets suppose on the first iteration you get a random roll of 1.
randNum value gets calculated as:
randNum = t[1]
randNum = 1
Then the next statement removes the 2nd entry from the table. In this case, the table becomes:
t = {2, 3, 4, 5}
On the next iteration, the random function range is from 1-4. Lets assume the new rolled number is 4. Now randNum is calculated as:
randNum = t[4]
randNum = 5
The next statement tried to remove the 5th entry from the table. Which does not exist. So your code won't work properly.
In order to solve the problem, you can change the random number generation with the following code:
randSize = mRandom(tableSize)
randNum = t[randSize]
table.remove(t, randSize)
I want to generate two different random numbers which has 15 digits. How can I do that
Thanks
Most random number generating functions such as arc4random produce only numbers in
the range 0 .. 2^32-1 = 2147483647. For a 15 digit decimal number, you can compute
3 numbers in the range 0 .. 10^5-1 and "concatenate" them:
uint64_t n1 = arc4random_uniform(100000); // 0 .. 99999
uint64_t n2 = arc4random_uniform(100000);
uint64_t n3 = arc4random_uniform(100000);
uint64_t number = ((n1 * 100000ULL) + n2) * 100000ULL + n3; // 0 .. 999999999999999
Or, if you need exactly 15 digits:
uint64_t n1 = 10000 + arc4random_uniform(90000); // 10000 .. 99999
uint64_t n2 = arc4random_uniform(100000); // 0 .. 99999
uint64_t n3 = arc4random_uniform(100000); // 0 .. 99999
uint64_t number = ((n1 * 100000ULL) + n2) * 100000ULL + n3;
Try this::
arc4random() is the standard Objective-C random number generator function. It'll give you a number between zero and... well, more than fifteen! You can generate a number between 0 and 15 (so, 0, 1, 2, ... 15) :
A random number with 6 digits would be:
int number = arc4random_uniform(900000) + 100000;
it will give random numbers from 100000 to 899999.
Hope it Helps!!
using arc4random() functionality you can achieve to generate random numbers.
Here is the link that will give you pretty good idea about arc4random().
hope this will help