Generate all Binary Combinations C++ [closed] - c++

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I want to write a program that you give an input (number of digits n) and you get as an output a vector of all possible binary grey code generated.
For example,
if n=2, results should be:
V= {00, 01, 10,11}
Actually I am interested in having them bit by bit and not whole integer. Meaning I want to have a 2 D array of integers where I have each word in rows and bits (as int) in cols

Hint: Gray code of num is (num>>1) ^ num. Go through all numbers of the 0..2^N-1 range, and compute their Gray code representation using this simple formula.
EDIT Another hint: the simplest way to convert an integer to binary is using bitset:
bitset<N>((num>>1) ^ num).to_string()

The set V equals { 0, ..., 2^n-1 }. Just compute this set.

Related

c++ array min max range fraction [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I have an array which fluctuates about between 0.1429 and 0.1428 it doesn't seem to have a real top or bottom though so those numbers could vary.
if(myarray[N-1]<myarry[N]){/*always happens*/}
if(myarray[N-1]>myarry[N]){/*never happens*/}
the numbers are fractional so there must be smaller fraction in the numbers to show curves on my chart eg: 0.14285216
I am having real trouble with 'greater than' 'smaller than' < > I think it's because i've not got numbers bigger than 1 (myarray[N-1] shows 0 always)
can I do something to my data like increase the range or use another method to '<>'?
really stuck
I'm guessing that what you want to do is display the numbers in an array so as to see the differences between them? The reason for your always/never situation is that the array is sorted, which is probably a good thing. Anyway, to display a greater number of digits, you can use format specifiers, such as
printf ("my ith number: %.10f", myarray[i]);
This will give you myarray[i] with 10 decimal places.
what is:
myarray versus myarry (missing the a between the second r and the y)
Direct from your code given above:
if(myarray[N-1]<myarry[N]){/*always happens*/}
if(myarray[N-1]>myarry[N]){/*never happens*/}
Shouldnt it be:
if(myarray[N-1]<myarray[N]){/*always happens*/}
if(myarray[N-1]>myarray[N]){/*never happens*/}
Also I hope you arent ever using N = 0 as an input to this set of if statements.
You might want to multiply all numbers by 7 and subtract 1 - that will make the relative differences larger.

Fastest way to input integers [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
What is the fastest way(code) to take integer input from user via terminal (not file...:P).
P.S 1: Integers are of small size( within size of int) but the total number of intergers is very large.
P.S 2: Scanf toooo... slow
P.S 3: Forget the human limits ,talk technical...plz
I think an approach based on scanf will be hard to beat. In any case, it will be easy to implement. So I'd start with that, if it's not sufficient, benchmark before trying anything else.
If the input consists of whitespace-separated integers:
scanf("%d ", &input)
for continuous input processing you can try this
while( scanf("%d ", &val) == 1)
{
// processing : do what you want
}
also you can use this for file inputs reading (fscanf)

How do I make a smaller range with two given integers? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
For example,
I have a range 14269-14274.
To conserve space on the screen my users want to have it display in the format 14269-74.
Another example would be a range of 14269-14529 which should output as 14269-529.
How would I achieve this?
Something like this should do the trick:
int a = 14269;
int b = 14529;
int endrange = b % pow(10, floor(log10(b - a) + 1));
You need to make sure that a < b though.
You can check the first digit that differs, output the first number and then the second one, starting at the first different digit.
This of course only makes sense if the two numbers have the same length.
Were you expecting the implementation?

Given integer n decide if it is possible to represent it as a sum of two squares of integers [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 12 years ago.
INPUT SPECIFICATION:
First line of input contains one integer t <= 10000: number of test cases.
T lines follow, each of them consisting of exactly one integer 0 <= n <= 10^8.
OUTPUT SPECIFICATION:
For each test case output Yes if it is possible to represent given number as a sum of two squares and No if it is not possible.
Hint: A number N is expressible as a sum of 2 squares iff in the prime factorization of N, every prime of the form (4k+3) occurs an even number of times!

Why aren't my "double" values printed the way I expected them? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
Can I represent the following co-ordinate triplets using variable type "double"?
512685.93 5403444.22 305.63,
512685.91 5403445.72 305.55,
512685.90 5403447.12 305.54, ...
Those values correspond to x, y & z in my input file. After doing some process by my program, I got the output file with following values as:
512668 5.40345e+006 321.39,
512667 5.40345e+006 321.57,
512666 5.40345e+006 321.89,
512666 5.40344e+006 321.32,
512665 5.40344e+006 321.64, ...
I have used double x, y, z to represent those values. Why aren't they printed with a format more similar to the first example?
Can you? Well, yes, if you can live with the inherent inexactness in all floating point computations.
Of course, there's not enough detail in your question to determine whether your problems are due to roundoff, or due to some error in your program, or even if there is a problem in the first place!
Look at how you print the values, likely the values are still fine but your output mechanism limits precision.