Skip to main content

Interactive demo! 2D Array - Flipping bits

Downloaded: 13

Last edit: 15 August 2022

English Key Stage 14-16 years (KS4), 16-18 Years (KS5)
Scottish Curriculum Levels S6 Advanced Higher (17-18 years)
Resource Type Worksheets and Activities, Teaching Resources, Example Programs and Live Lessons
Curriculum Topic Programming, Data & Information

Used as an interactive demo for GCSE students, allowing them to experiment with 2D arrays of values. Teacher can display on board or students can run the code themselves.

Can compile and run using C++ on Raspberry Pi, Apple Mac, Linux PC or Windows PC (latter used CodeBlocks). Alternatively can paste code into replit.com

Source code allows you to choose size of array - change the values for NUM_ROWS or NUM_COLS to alter the size of the array. These constants are used throughout the program. Students are able to change these.

File 1 is the simpler version. File 2 has extra validation check to make sure that row and column numbers that you type in are valid integers. Uses a do-while loop to insist upon this until you give it a valid integer.

Array will be filled with 0 values (all bool values set to false).

Enters an infinite loop, displaying that array and asking the user for a row and column.

If the user enters row and column within range then that particular bit gets flipped using NOT.

Program counts how many bits are set to 1 and detects whether the array is all 0s, all 1s or a mixture of 1s and 0s.

More programs on blog here.