By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,719 Members | 1,875 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,719 IT Pros & Developers. It's quick & easy.

Any good codes for this algorithm?

P: n/a
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

Nov 15 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
po***********@gmail.com writes:
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?)
--
"It wouldn't be a new C standard if it didn't give a
new meaning to the word `static'."
--Peter Seebach on C99
Nov 15 '05 #2

P: n/a
po***********@gmail.com writes:
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #3

P: n/a
po***********@gmail.com wrote:

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();

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Nov 15 '05 #4

P: n/a
po***********@gmail.com wrote on 30/07/05 :
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"
Nov 15 '05 #5

P: n/a
In article <11*********************@o13g2000cwo.googlegroups. com>, po***********@gmail.com wrote:
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
Nov 15 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.