Find an Online Tutor Now

Heth A.

asked • 06/05/20

File compression and multi-dimensional array

Scenario A simple matrix could be used to store data about images by using 1’s and 0’s to represent dark

and light shades of color similar to a bitmap. You will find a file data.txt under your assignment

3 on Moodle. The file looks the image of an X drawn on a canvas.














If we look at the data in the file, there are a lot of repeated digits present in the file. This causes

a lot of overhead in storing the file as you have to store so many numbers. The goal of this

assignment is to use a compression algorithm to compress the file and try to make it smaller

without loosing any information about the data present in the data file.

Compression Algorithm:

One method of compressing a file is to recognize repeated digits and replace the runs of

repetition with a number representing the repetition length. For instance for a line in you file

such as ;


Can be represented with the line:

1 2 10 2 1

Because there is 1 zero, 2 ones, 10 zeros, 2 ones and 1 zero. It is already understood that only

zeros and ones appear and that zeros apper first in a given line. Thus storing the new line of

data is much shorter (5 numbers) than the original line (16 numbers)

To decompress the encoded file you can follow the reverse implementation of the method

stated above, for example for the encoded data:

4 2 4 2 4

The decompressed data is:



You have to complete the following tasks to successfully complete the assignment.

  1. Read the data file data.txt and store its data in a multi-dimentional array
  2. Create a file called compressed.txt
  3. For every row in the multi-dimensional array
  4. a. Compress the data the file data using compression scheme discussed above
  5. b. Take the compressed line and write it to your compresed.txt file
  6. Create a function called decompress. This function should:
  7. a. Read the data from compressed.txt file
  8. b. Decompress it using decompression scheme shown above
  9. c. Print decompressed data to console
  10. Create the main function that carries out all the tasks listed above

1.2.2 Use of Functions in Assignment:

It necessary that you implement this assignment using functions. To obtain the maximum

score you must demonstrate the ability to create and use functions of your own. For

example, you may consider creating and using the following functions in your program:

  1. Encoding function
  2. Decoding function
  3. Printing Welcome Message

1.4 Note:

  1. In all your program constructs you must write comments where necessary. Don’t write

comments for obvious code, but segments of code which seem complex.

  1. All input should be validated wherever necessary


2 Answers By Expert Tutors


Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.


Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.