Any good codes for this algorithm?

P

poison.summer

I have a line of binary codes such as 1 1 1 0 0 0 1 1 1
I'd like to change the number of consecutive 0s to be even.
is there any good algorithm in C?

Thanks
 
B

Ben Pfaff

I have a line of binary codes such as 1 1 1 0 0 0 1 1 1
I'd like to change the number of consecutive 0s to be even.
is there any good algorithm in C?

You should probably define the algorithm itself before you try to
implement it. It is not obvious how to change the number of
consecutive 0s to be even. Should it be done by inserting or
deleting 0s? Should it be done by changing 0s to 1s or 1s to 0s?
Will there be more than one sequence of consecutive 0s, or just
one? (Are there guaranteed to be any 0s at all?)
 
K

Keith Thompson

I have a line of binary codes such as 1 1 1 0 0 0 1 1 1
I'd like to change the number of consecutive 0s to be even.
is there any good algorithm in C?

Delete all the 0s, or change them all to 1s.

I doubt that that's what you had in mind, but your question really has
nothing to do with C. You might try comp.programming -- and be more
specific about the problem definition. Once you have an algorithm, if
you have trouble implementing it in C feel free to come back here and
show us what you've written, and we'll help if we can.
 
C

CBFalconer

I have a line of binary codes such as 1 1 1 0 0 0 1 1 1
I'd like to change the number of consecutive 0s to be even.
is there any good algorithm in C?

if (1 & zerobitcount()) setlastbit();
 
E

Emmanuel Delahaye

I have a line of binary codes such as 1 1 1 0 0 0 1 1 1
I'd like to change the number of consecutive 0s to be even.
is there any good algorithm in C?

Thanks

You should cleanup your mind.

- An algorithm belongs to the design (phase #2 of a project life). It
has nothing to do with an implementation language. It can be written in
plain <select your spoken language> or in algorithmic langage (AFAICT,
no real standard, but a kind of Pascal is close enough).

- Once you have an algorithm working on a sheet of paper (could be an
electronic one, let's be modern...), you switch to phase #3 (Coding and
test). You translate it into your implementation language (Asm, C, C++,
X--, Y** whatever).

FYI:

Project life:

#1 Specifications (what ?)
#2 Design (how-to ?)
#3 Coding and unit-test (implementation)
#4 Integration (glue the all thing and make it work)
#5 Validation (Does the thing comply with the specs ?)


--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"C is a sharp tool"
 
J

Jasen Betts

I have a line of binary codes such as 1 1 1 0 0 0 1 1 1
I'd like to change the number of consecutive 0s to be even.
is there any good algorithm in C?

Thanks

Output all 1s then there will be an even number of 0s :)

more information please.
possibly something could be done using a precomputes lookup table.

Bye.
Jasen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top