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 4 years ago.
Improve this question
I am currently working on a school project that is a circuit simulator. One of the components to this circuit can be a multiplexer, which has n inputs, log2(n) selectors, and 1 output.
The way I determine which output is needed is by doing the following:
Generate (# of selectors) bit gray code table, and loop through the table and compare to the values of the selectors. Whichever row is a match is the output needed.
However, for larger multiplexers (16:1, 32:1), this becomes quite slow. Is there a more efficient way to get the output needed without having to compare every single possible graycode possibility?
So you wish to construct a unique number in the range [0, 2n) (the index of the selected input) from n inputs (the selector signals) which are in the range [0, 1].
That's binary code!
Assign one of the selector signals the value 1, the next signal the value 2, then 4, and so on. Add them together. Select the corresponding numbered input.
unsigned selectedInput = 0;
if (selector1) selectedInput += 1; /* or |= */
if (selector2) selectedInput += 2;
if (selector3) selectedInput += 4;
And so on. In the generic case:
unsigned selectedInput = 0;
for (int i = 0; i < selectorCount; ++i)
if (selectors[i]) selectedInput |= 1u << i;
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 2 months ago.
Improve this question
Good evening. I had a coding interview on Codesignal with the question below, and I got just 14/20 with the test cases. How will you solve it please.
Given an array of nums, add positive and negative in succession and return the sum.
Example : given nums = {2, 3, 4, 5, 7}
Answer = 2-3+4-5+7 = 5.
What's the fastest algorithm for this?
I tried to use a two for loops and input -ve with i+1 for the second loop, but that's just brute force and terribly slow
There is no way to avoid an exhaustive accumulation of the values, but this is terribly... fast. There is no need and no way to accelerate, besides unrolling the loop (which will probably have little effect) and parallelizing (out of the scope of the question).
IMO, the most reasonable way is with
int sum= 0; int i;
for (i= 0; i + 1 < n; i+= 2)
{
sum+= num[i] - num[i+1];
}
if (i < n)
{
sum+= num[i];
}
If the data is int32, positive, and the processor 64 bits, a nasty hack would be to load two values at a time as 64 bits and accumulate this way. In the end, you split back to 32 bits and perform the final subtraction. But again, I doubt the this will yield a significant speedup.
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 4 years ago.
Improve this question
I am relatively new to programming and I want to it to loop in a "do while" loop while the quotient of two variables is not a multiple of 4.
The modulo operator, or % will do exactly as you need. It will return the "remainder" of dividing a number by a given value. Using that logic, if the result is 0, then the specified number was divided evenly by the value.
Therefore:
do
{
// do stuff that changes x and/or y
} while ((x / y) % 4) != 0)
Should accomplish your goals.
You divide your x/y value, and then use % 4 on the result. If the result is zero, then the it was evenly divisible by 4, if it is not zero, there was a remainder and it was not evenly divisible.
As pointed out in a comment below, the do...while syntax first "does", then evaluates, which although not indicated in your question, is unlikely the intended behavior, and what you need is simply a while loop without the do. This first evaluates, and then "does" only if the result was true, otherwise does nothing.
while (y != 0 && (x / y) % 4) != 0)
{
// do stuff that changes x and/or y
}
I'll give you the math. Let Z = X / Y. Then you want Z % 4 to be non-zero.
try like this.You can use z to store result of (x/y)%4.
main(){
int x=0,y=0, z=0;
do{ // do anything
}
while((x/y)%4 != 0);
}
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
I'm having trouble on thinking of away to attack this problem.
X is defined below. For n=1,x=0.5,n=2,x=0.833.As you add more terms, X increases. Calculate n for which X becomes larger than 4. First write the algorithm and then implement the code in C.
x= 1/2+1/3+...1/n+1 answer: n = 83
The only thing I'm sure of is that it uses a for loop.At first I was thinking something like
For(int i = 0; i <= n.....
That doesn't seem close though.I dunno..Can I get a hint on where to start?
You will obviously compute the partial sums X.n and stop when X.n<4 and X.n+1>4.
To compute the partial sums, keep an accumulator variable and add the fractions one after the other
n= 0
S= 0
// Repeat the following instructions
n+= 1
S+= 1/(n+1) // Now, S = X.n
Remains to find the stopping condition. As the value of S goes increasing from 0, we will stop as soon as S exceeds 4. In other words, continue as long as S remains below 4.
n= 0
S= 0
while S < 4
n+= 1
S+= 1/(n+1) // Now, S = X.n
Translate that to C syntax.
Remains to look closer at the possibility that X.n = 4.
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 6 years ago.
Improve this question
I have an input number that it's value is not always consistent, i mean the value change plus 1 or minus 1 and etc. So, i want to compare it with a const but have a small range value, for example a const int Dist that have value between 14 to 16. Is that possible to implement it on C programming? Please help me.
You can set constant for lower bound, and constant for upper bound and check if the value falls within the range.
Pseudocode:
int const LOWER_BOUND = 14;
int const UPPER_BOUND = 16;
if (input <= UPPER_BOUND && input >= LOWER_BOUND)
... logic here ...
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 8 years ago.
Improve this question
Write an algorithm called occurrences that, given an array of numbers A, prints all the distinct values in A each followed by its number of occurrences.
For example, if A = <28, 1, 0, 1, 0, 3, 4, 0, 0, 3>, the algorithm should output the following five lines (here separated by a semicolon 28 1; 1 2; 0 4; 3 2; 4 1.
The algorithm may modify the content of A, but may not use any other memory.
Each distinct value must be printed exactly once.
Values may be printed in any order.
Possible solution will have 2 steps:
Sort your array
Iterate on array storing current value and number of its occurrences and printing current value/count pair when current value changes
This solution does not require extra memory and has complexity O(n*log(n)) as sorting is the "heaviest" part of algorithm