I was reading the C-FAQ question no: 20.8 which basically deals with bit arrays:
http://c-faq.com/misc/bitsets.html
One of the macros defined looks something like:
#define BITNSLOTS(nb) ((nb + CHAR_BIT - 1) / CHAR_BIT)
Is this macro meant to calculate the num of elements(or slots) in the char array (each slot = 8 bits) ? I am not sure what this macro is doing, in particular what the purpose of "+CHAR_BIT -1/CHAR_BIT" is. Any clues will be appreciated!
Yes, it calculates how many chars are needed to hold the bits. The addition stuff is to make it round up.
It is a way to round up.
If nb is smaller than CHAR_BIT, you'll still need at least one character.
Remember the division is integer division: there's no "... and three eighths". Suppose you want to group into slots of size 6 (yeah ... I know CHAR_BIT is 8 or more)
1 element: 1 slot: (1 + 6 - 1) / 6 == (6 / 6) == 1
...
5 elements: 1 slot: (5 + 6 - 1) / 6 == (10/6) == 1
6 elements: 1 slot: (6 + 6 - 1) / 6 == (11 / 6) == 1
7 elements: 2 slots: (7 + 6 - 1) / 6 == (12 / 6) == 2
...
Related
How do I create a function (e.g. here, an anonymous one but I don't mind any) to get x elements from vec that are most centered (i.e. around the median)? In essence I want a function with same syntax as Matlab's randsample(n,k), but for non-random, with elements spanning around the center.
cntr=#(vec,x) vec(round(end*.5)+(-floor(x/2):floor(x/2))); %this function in question
cntr(1:10,3) % outputs 3 values around median 5.5 => [4 5 6];
cntr(1:11,5) % outputs => [4 5 6 7 8]
Note that vec is always sorted.
One part that I struggle with is not to output more than the limits of vec. For example, cntr(1:10, 10) should not throw an error.
edit: sorry to answer-ers for many updates of question
It's not a one-line anonymous function, but you can do this pretty simply with a couple calls to sort:
function vec = cntr(vec, x)
[~, index] = sort(abs(vec-median(vec)));
vec = vec(sort(index(1:min(x, end))));
end
The upside: it will still return the same set of values even if vec isn't sorted. Some examples:
>> cntr(1:10, 3)
ans =
4 5 6
>> cntr(1:11, 5)
ans =
4 5 6 7 8
>> cntr(1:10, 10) % No indexing errors
ans =
1 2 3 4 5 6 7 8 9 10
>> cntr([3 10 2 4 1 6 5 8 11 7 9], 5) % Unsorted version of example 2
ans =
4 6 5 8 7 % Same values, in their original order in vec
OLD ANSWER
NOTE: This applied to an earlier version of the question where a range of x values below and x values above the median were desired as output. Leaving it for posterity...
I broke it down into these steps (starting with a sorted vec):
Find the values in vec less than the median, get the last x indices of these, then take the first (smallest) of them. This is the starting index.
Find the values in vec greater than the median, get the first x indices of these, then take the last (largest) of them. This is the ending index.
Use the starting and ending indices to select the center portion of vec.
Here's the implementation of the above, using the functions find, min, and max:
cntr = #(vec, x) vec(min(find(vec < median(vec), x, 'last')):max(find(vec > median(vec), x)));
And a few tests:
>> cntr(1:10, 3) % 3 above and 3 below 5.5
ans =
3 4 5 6 7 8
>> cntr(1:11, 5) % 5 above and 5 below 6 (i.e. all of vec)
ans =
1 2 3 4 5 6 7 8 9 10 11
>> cntr(1:10, 10) % 10 above and 10 below 5.5 (i.e. all of vec, no indexing errors)
ans =
1 2 3 4 5 6 7 8 9 10
median requires sorting the array elements. Might as well sort manually, and pick out the middle block (edit: OP's comment indicates elements are already sorted, more justification for keeping it simple):
function data = cntr(data,x)
x = min(x,numel(data)); % don't pick more elements than exist
data = sort(data);
start = floor((numel(data)-x)/2) + 1;
data = data(start:start+x-1);
You could stick this into a single-line anonymous function with some tricks, but that just makes the code ugly. :)
Note that in the case of an uneven division (when we don't leave an even number of elements out), here we prioritize an element on the left. Here is what I mean:
0 0 0 0 0 0 0 0 0 0 0 => 11 elements, x=4
\_____/
picking these 4 values
This choice could be made more complex, for example shifting the interval left or right depending on which of those values is closest to the mean.
Given data (i.e. vec) is already sorted, the indexing operation can be kept to a single line:
cntr = #(data,x) data( floor((numel(data)-x)/2) + (1:x) );
The thing that is missing in that line is x = min(x,numel(data)), which we need to add twice becuase we can't change a variable in an anonymous function:
cntr = #(data,x) data( floor((numel(data)-min(x,numel(data)))/2) + (1:min(x,numel(data))) );
This we can simplify to:
cntr = #(data,x) data( floor(max(numel(data)-x,0)/2) + (1:min(x,numel(data))) );
I was looking for C code to generate a set of random even number in range [start, end]. I tried,
int random = ((start + rand() % (end - start) / 2)) * 2;
This won't work, for example if the range is [0, 4], both 0 & 4 included
int random = (0 + rand() % (4 - 0) / 2) * 2
=> (rand() % 2) * 2
=> 0, 2, ... (never includes 4) but expectation = 0, 2, 4 ...
On the other hands if I use,
int random = ((start + rand() % (end - start) / 2) + 1) * 2;
This won't work, for example,
int random = (0 + (rand() % (4 - 0) / 2) + 1) * 2
=> ((rand() % 4 / 2) + 1) * 2
=> 2, 4, ... (never includes 0) but expectation = 0, 2, 4 ...
Any clue? how to get rid of this problem?
You complicated it too much. Since you're using rand() and the modulo operator, I'm assuming that you will not be using this for cryptographic or security purposes, but as a simple even number generator.
The formula I have found for generating a random even number in the range of [0, 2n] is to use
s = (rand() % (n + 1)) * 2
An example code:
#include <stdio.h>
int main() {
int i, s;
for(i = 0; i < 100; i++) {
s = (rand() % 3) * 2;
printf("%d ", s);
}
}
And it gave me the following output:
2 2 0 2 4 2 2 0 0 2 4 2 4 2 4 2 0 0 2 2 4 4 0 0 4 4 4 2 2 2 4 0 0 0 4 0 2 2 2 2 0 0 0 4 4 2 4 4 4 0 4 2 2 4 4 0 4 4 2 2 0 0 4 0 4 4 2 0 2 4 0 0 0 0 4 0 4 4 0 4 2 0 0 4 4 0 0 4 4 2 0 0 4 0 2 2 2 0 0 4 0 2 4 2
Best regards!
rand() % x will generate a number in the range [0,x) so if you want the range [0,x] then use rand() % (x+1)
Common notation for ranges is to use [] for inclusive and () for exclusive, so [a,b) would be a range such that a is included but not b.
So in your case, just use (rand() % 3)*2 to get random numbers among {0,2,4}
If you want even numbers in the range [m,n], then use ((m/2) + rand() % ((n-m+2)/2))*2
I do not trust in the mod operator for random numbers. I prefer
start + ((1 + stop - start) * rand())
/ (1 + RAND_MAX)
which only relies on the distribution of rand() in the interval
[0, .. , RAND_MAX] and not on any distribution of rand()%n in the
interval [0, .. , n-1].
Note: If you use this expression you should add appropriate casts to avoid multiplication overflow.
Note also
ISO/IEC 9899:201x (p.346):
There are no guarantees as to the quality of the random sequence produced and some implementations are known to produce sequences with distressingly non-random low-order bits. Applications with particular requirements should use a generator that is known to be sufficient for their needs.
Just and-out the low bit, which makes it even:
n= (rand()%N)&(-2);
and to use a start/stop (a range), the values can be offset:
int n, start= 5, stop= 20+1;
n= ((rand()%(stop-start))+start)&(-2);
The latter calculation generates a random number between 0 and RAND_MAX (this value is library-dependent, but is guaranteed to be at least 32767).
If the stop value must be included in the range of generated numbers, then add 1 to the stop value.
It takes that value modulo the stop value plus the start value, and then adds the start value. The value is now within the range of [start, stop]. As only even numbers are required, the low bit is anded-out because even numbers start at 2.
The anding-out is performed by generating a mask of all 1's, except the lowest bit. As -1 is all 1's (0xFFF...FFFFF), -2 is all 1's except this low bit (0xFFF...FFFFE). Next the bitwise AND operation (&) is perfomed with this mask and the number is now in the range [start,stop]. QED.
I've this problem:
i must develop an algorithm that get an int array and reshuffles the elements respecting these constraints:
For each element, must be lower than its neighbors or greater than its neighbors:
for each x in array a,
( a[x-1]<=a[x] AND a[x+1]<=a[x] )
OR
( a[x-1]>=a[x] AND a[x+1]>=a[x] )
This all in theta(n log n) in the worst case
I've no idea how to do this, my only intuition is that i must do something similar to merge-sort...
Sorry for mi poor English
Sort the array
Cut it in halfs
Put elements from first half in between elements of the second half
If number of elemnts is not even move last element to the front
Example:
2 3 1 5 7 8 6 4 9
1 2 3 4 5 6 7 8 9
1 5 2 6 3 7 4 8 9
9 1 5 2 6 3 7 4 8
As a naive approach, you could perform some sorting first and group the array to groups of equal size. Afterwards, adjacent groups would have to be pairwise switched; if the number of groups is odd, the first group needs to be switched to the back.
An example:
Input: 9 9 8 8 7 7 6 6 5 5 4 4 3 3 2 2 1 1 (already sorted)
Groups: (9 9) (8 8) (7 7) (6 6) (5 5) (4 4) (3 3) (2 2) (1 1)
Groups pairwise switched: (8 8) (9 9) (6 6) (7 7) (4 4) (5 5) (2 2) (3 3) (1 1)
First group moved to the back: (9 9) (6 6) (7 7) (4 4) (5 5) (2 2) (3 3) (1 1) (8 8)
Perhaps the idea can be refined to obtain a better algorithm.
if anyone could answer me why this works, it would be greatly appreciated. The exercise (chapter 4, ex 7 and 8) says that if you have the expression:
9 - ((total - 1) % 10)
then, you could be tempted to simplify it like this:
10 - (total % 10)
But this would not work. Instead he offers the alternative:
(10 - (total % 10)) % 10
Now, I understand how he got to the first simplification, but not why it's wrong, or why does the second one works.
Thanks in advance
x %m has a range of (-m, m) in most C implementations. Mathematically it is generally defined from (0, m). Hence by adding m the modulo again will convert the C to the mathematical one.
Consider the outputs for total = 10 to see that the second expression is not equivalent.
Note also that the third expression is not equivalent to the first expression unless total > 0 (because the behaviour of % is implementation-defined in pre-C99 C, and defined but not what you want in C99).
Assuming that total > 0, the first and third expressions are equivalent due to the following mathematical identity:
(a % b) == (((a + c) % b) - c) % b
To understand why, imagine doing the operations on a clock-face.
This is because modulo in C allows for negative numbers.
so -5 % 10 is -5 instead of 5.
In the first case, the 9 - ((total - 1) % 10) is always positive.
In the second case it can be negative if -10 < total < 0. In the 3rd case it is again wrapped around for negatives back into the positive range.
It is a common thing for modulo because generally you want it for positives only(not sure why they implemented it for negatives).
To show why 9-((total)%10) is wrong, use a contradiction.
Let total = 10.
Then 9-((10-1)%10) ==> 9-(9%10) ==> 9-9 = 0.
But, 10-(10%10) ==> 10 -0 = 10.
Thus, 10-((total)%10) is not equivalent to 9-((total-1)%10)
The alternative is not a simplification and neither expression is equvalent to the first so the premise is flawed from the start:
The following:
int total ;
for( total = -10; total <= 10; total++ )
{
printf( "%d:\t%d\t%d\t%d\n", total,
9 - ((total - 1) % 10),
10 - (total % 10),
(10 - (total % 10)) % 10 ) ;
}
Produces:
-10: 10 10 0
-9: 9 19 9
-8: 18 18 8
-7: 17 17 7
-6: 16 16 6
-5: 15 15 5
-4: 14 14 4
-3: 13 13 3
-2: 12 12 2
-1: 11 11 1
0: 10 10 0
1: 9 9 9
2: 8 8 8
3: 7 7 7
4: 6 6 6
5: 5 5 5
6: 4 4 4
7: 3 3 3
8: 2 2 2
9: 1 1 1
10: 0 10 0
The last is only equvalent for integers greater than zero.
7 % 3 = 1 (remainder 1)
how does
3 % 7 (remainder ?)
work?
remainder of 3/7 is 3..since it went 0 times with 3 remainder so 3%7 = 3
7 goes into 3? zero times with 3 left over.
quotient is zero. Remainder (modulus) is 3.
Conceptually, I think of it this way. By definition, your dividend must be equal to (quotient * divisor) + modulus
Or, solving for modulus: modulus = dividend - (quotient * divisor)
Whenever the dividend is less than the divisor, the quotient is always zero which results in the modulus simply being equal to the dividend.
To illustrate with OP's values:
modulus of 3 and 7 = 3 - (0 * 7) = 3
To illustrate with other values:
1 % 3:
1 - (0 * 3) = 1
2 % 3:
2 - (0 * 3) = 2
The same way. The quotient is 0 (3 / 7 with fractional part discarded). The remainder then satisfies:
(a / b) * b + (a % b) = a
(3 / 7) * 7 + (3 % 7) = 3
0 * 7 + (3 % 7) = 3
(3 % 7) = 3
This is defined in C99 ยง6.5.5, Multiplicative operators.
7 divided by 3 is 2 with a remainder of 1
3 divided by 7 is 0 with a remainder of 3
As long as they're both positive, the remainder will be equal to the dividend. If one or both is negative, then you get reminded that % is really the remainder operator, not the modulus operator. A modulus will always be positive, but a remainder can be negative.
(7 * 0) + 3 = 3; therefore, the remainder is 3.
a % q = r means there is a x so that q * x + r = a.
So, 7 % 3 = 1 because 3 * 2 + 1 = 7,
and 3 % 7 = 3 because 7 * 0 + 3 = 3
It seems you forgot to mention the surprising case, if the divident is smaller and negative:
-3 % 7
result: 4
For my brain to understand this kind of question, I'm always converting it to a real world object, for example if I convert your question 3 % 7. I'm going to represent the "3" as a 3-inch wide metal hole, then the "7" as a 7 inch metal screw. Can you insert the 7 inch metal screw to a 3 inch wide metal hole? Of course not, therefore the answer should be the 3-inch metal hole, it doesn't matter even let say you have a 1000 or a million inch wide screw, it is still 3, because how many times can you insert the 1000 or a million inch wide screw to a 3 inch wide metal hole? Zero times, right?
The most simple and effective catch to remember would be:
Whenever dividend is less than the divisor, modulus is just that dividend.
Let's formulate this:
if x < y, then x % y = x