CAS Community   >   Resources   >  

Demo Program: Run-length encoding of black and white pixels

Encodes strings composed of 'b' and 'w' characters, compressing contiguous blocks

Phil Gardner

Created by Phil Gardner
last edited Mar 12 2021 by Phil Gardner

Solution to the problem described in the Cambridge Elevate GCSE text book.

Used as a demo in class on the board, but also posted on Google Classroom for students to interact with.

Allows you to type in a sequence of characters that represent individual pixels in a bitmap - b for black, w for white. Groups contiguous blocks of pixels that are the same colour. Displays the size of each contiguous colour block.








Paste the C++ code into a new repl on the website.

Alternatively, if you have a Raspberry Pi, you already have the compiler to make the executable. You can load the runlength.cpp file into the Geany editor, then click on Build. Once built successfully, you can click on Execute.

If you are using Ubuntu, if you do not have the compiler, you can install it with…

sudo apt-get install build-essential

To compile from command line using compiler:

g++ runlength.cpp

…then run the executable with:


Deluxe compilation instructions, for an executable with a more descriptive name:

g++ -o runlength runlength.cpp

…run with:


Blog post here.

Downloaded 494 times.


This resource has attached files: to access these files, please tick the box below to assent to the license terms
License: The resources on CAS website are under Creative Commons Attribution-Share Alike 3.0 licence unless otherwise specified by the resource creators.

You must confirm that you have read and agree the licence's ToS before you can download the attachments of this resource.

I have read the licence agreement of this resource and agree to abide by its terms and conditions.

Feedback and Comments

Available when logged in (join via the front page, for free):
  • View 0 comments on this resource.
  • View resource history, links to related resources.
  • Leave feedback for the author(s), or help by editing the resource.

© 2021 BCS, The Chartered Institute for IT Registered charity: No. 292786
Using the websiteDisclaimer of liabilityCookies policyPrivacy notice