Order of parenthesis in case of multiplication and division [closed] - c

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 last month.
Improve this question
Consider the following statement in C
j=2 * 3 / 4 + 2.0 / 5 + 8 / 5;
If the parenthesis is done considering BODMAS rule:
j=(2 * (3 / 4)) + (2.0 / 5) + (8 / 5); // then j = 0
If we take the priority of operators in C
j=((2 * 3) / 4) + (2.0 / 5) + (8 / 5); // then j = 2
Which one is the correct order?

You're misunderstanding BODMAS (or for Americans like me, PEMDAS). It's not a strict one at a time, in order application. Parenthesized, it groups as (B)(O)(DM)(AS). Division and multiplication are the same precedence (in both grade school arithmetic and the C operator precedence); similarly, addition and subtraction are the same precedence. You'll note the American acronym even flips the D and the M; it doesn't matter, because they're the same precedence, but trying to render a word with the M and D in the same space would be ugly, so we just fudge it.
Both the grade school and C approach work from left to right when the operators are equal precedence, so the correct evaluation order is:
j=((2 * 3) / 4) + (2.0 / 5) + (8 / 5);

Related

Find the regular expressions of the following?Language of odd length and cannot contain length multiple of 3 over ššŗ={š’‚,š’ƒ} [closed]

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
Language of odd length and cannot contain length multiple of 3 over
ššŗ={š’‚,š’ƒ}
OK to have an odd length that's not a multiple of 3, we can have a "head" part that generates strings of length 6n, and then a "tail" part that generates strings of length 1 and 5 (but not 7). Any odd number that's not a multiple of 3 can be written as 6n + 1 or 6n + 5 for n >= 0. So, let's get crackin.
r = ((a + b)^6)*((a + b) + (a + b)^5)
I have used a shorthand notation here which you can omit; basically, s ^ n stands for the regular expression s repeated n times. As long as n is a definite number this can be written out, just makes the regular expression a little longer.

What is the logic to find out Leonardo number in C programming? [closed]

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 5 years ago.
Improve this question
Leonardo number
L(x)= {
1 if x=0
1 if x=1
L(x-1)+L(x-2)+1 if x>1
}
If x>1, what logic can I apply to find out the Leonardo number?
In your function L(int x)
If x is 0 or 1
return 1;
If x is greater than 1
return L(x-1) + L(x-2) + 1;
What I am doing using L(x - 1) is calling the function for the value x - 1 and the value will be evaluated directly as 1 if it is 0 or 1, otherwise it call again for (x - 1) - 1 i.e. x - 2 and the process repeats.
Same is the case for calling L(x - 2).
This is the logic, without the entire code.
For more information on this kind of logic, try searching for Recursion.

How to convert a number to Q format [closed]

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 working with XMC1300 MCU series from Infineon. For my application I need to find out the square root of some data.
Following are the math lib api provided by Infineon
int16_t XMC_MATH_CORDIC_Q15_Sqrt(int16_t x)
int32_t XMC_MATH_CORDIC_Q31_Sqrt(int32_t x)
These two API accept and returns data format Q15 and Q31 i.e. It could only represent [-1,1] range.
Suppose I wanted to find out the square root of
144
200
1000
34567
50000
How can I change these numbers in range [-1, 1].
What needs to be done for the normalization of Input and Output.
Regards,
Tinchu
You can't represent 144 or 50000 directly in Q15 or Q31 format. As you mention, those formats are fixed point representations of numbers between -1 and 1.
So the problem you are left with is a basic math problem.
We can use the fact that
sqrt(A/B) = sqrt(A) / sqrt(B)
Let's do the example where your value A = 144:
Set B to the Q31 divisor B = 0x7FFFF = 32768
A/B = 0.00439`
Sqrt(A/B) = 0.0663
Sqrt(B) = Sqrt(32768) = 181.02
Sqrt(A) = Sqrt(A/B)*Sqrt(B) = 0.0663 * 181.02 = 12.
How to do this with Q15 numbers? All numbers are scaled by B.
So Sqrt(A/B) is simply XMC_MATH_CORDIC_Q15_Sqrt(A) //=> 2172
because 0.0663 = 2172/32768.
Sqrt(B) is 181. The final result in integer math is 181 * 2172 / 32768 = 12 *
For larger numbers you need to use the Q31 divisor:
0x7fffFFFF = 2147483647
*note: Integer math will round that answer down to 11. If you want better rounding, you'll need to examine bit 15 before you do the divide.
I usually just develop formulas to convert from machine to engineering units. In this case the input and output would be expected to be Q15. Below I use _eu to designate engineering units, _mu to designate machine units and sqrt() to designate the actual square root. The formula for square root:
output_eu = sqrt(input_eu)
Conversions for input and output:
output_mu = (2^15)*output_eu
input_mu = (2^15)*input_eu
To get the equivalent calculation in machine units, substitute:
output_mu/(2^15) = sqrt(input_mu/(2^15))
output_mu = sqrt((2^15)*input_mu)
It's best to right shift the input in order to optimize precision, so in the case of 144, the number could be left shifted by 7 bits: 144*(2^7) = 18432
So the input is basically Q7.
In other words:
output_mu = sqrt((2^15)*((2^7)*input_eu))
= (2^11)*sqrt(input_eu)
So basically the output here is Q11 and could be shifted right 11 bits to get to the result in engineering units.
So if you were to execute this in code, lets say value 144 is loaded into variable x and we want to put result in variable y:
y = x << 7;
y = XMC_MATH_CORDIC_Q15_Sqrt(y);
y >>= 11;
Walking through the math:
1) y = (144*(2^7)) = 18432
2) y = sqrt((2^15)*18432) = 24576
3) y = 24576/(2^11) = 12

Average distance between two randomly chosen indexes in array [closed]

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 9 years ago.
Improve this question
Interesting thought question for you guys. Given an array of length n, if I were to pick two random indexes in this array, a and b on average how far apart would they be? As in how many steps would I have to take to walk from a to b. There are no restrictions so there's a chance I pick the same index for both, and there's a chance a and b are at opposite ends of the array.
I've thought about this for a while, my initial idea being they're on average n/2(ish) apart, but I think this hunch is incorrect. An index chosen in the center of the array at most would have to walk n/2 places to find its corresponding second choice, whereas only at the ends of the array would the second choice ever be around n distance away.
Thanks!
After scribbling some grids of possible distances for the first few values of n, I think the exact result is in fact given by:
f(n) = (nĀ² - 1) / 3n
Choosing two places in an array is equivalent to splitting the array up into 3 sections. The average size of each of those sections will be n/3 so the average distance between the two points is also n/3.
Using a monte carlo method in python:
from collections import defaultdict
import random
sample = [abs(random.choice(range(0,10)) - random.choice(range(0,10))) for i in range(0,10000)]
avg = float(sum(sample) / len(sample))
print ("Average: %f" % avg)
freq = defaultdict(int)
for s in sample:
freq[s] += 1
scale = 40.0 / max(freq.values())
for i in range(0,10):
print ("%d : %s" % (i, "#" * int(freq[i] * scale)))
Output:
Average: 3.293700
0 : ######################
1 : ########################################
2 : ####################################
3 : ###############################
4 : ##########################
5 : ######################
6 : #################
7 : #############
8 : #########
9 : ####
So, looks like it's n/3 - but it's not evenly distributed.
There is an easy way to know: for all the couples (a, b), computer their distance. Knowing that all the couples (a, b) have the same probability of appearance, you will just need to do the average of those distances in order to answer your question.

Find number of k-permutation with ordering but no repetition [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
Im truggling to find a closed form for the number of k-permutations of a set S of cardinality n.
The combinations should regard ordering, but no repitions.
Example:
|S| = n = 3
S = {a,b,c}
k = 2
{a,b}
{b,a}
{b,c}
{c,b}
{a,c}
{c,a}
Anyone could help me out how to compute the number of viable permutations (and not the permutations itself)?
What I've tried:
I've read through different material and found out, that including repitititions it is
O(n) = n^k
My initial though was, that I need to eliminiate the permutations like
{a,a}
{b,b}
{c,c}
But I struggle finding a closed form for the number of perceivable repititions.
You are looking for the number of k-permutations of a set S of cardinality n.
The formulae is well known : n!/(n-k)!
Pseudo-proof :
for the 1st element, you are able to choose among the n elements of S ;
for the 2nd, only among : n-1, because you don't want doublons ;
...
for the ith, only among : n-(i-1) ;
...
for the kth, only among : n-(k-1) ;
So, finally :
n * (n-1) * ... * (n-i) * ... * (n-k+1)
= n! / (n-k)!

Resources