7 – arrays and matrices

Theory

New library functions

Counting the number of tokens in a cell is such a common task in this level that we have added two functions to the library:

These functions work correctly regardless of the number of tokens that Karel already has in its bag. They are:




We have also added step_back( n ) that has Karel step back a given number of steps, which will be useful in a number of missions in this level:

arrays

We already saw that a variable is a placeholder with a name and a value. For example:

show it to me

An array is a placeholder with a name and multiple values; we tell the values apart using indices. For example:

show it to me

matrices

In the same way in which we can think of an array as a stack of variables, we can think of a matrix as a stack of arrays, i.e., a matrix is an array whose elements are arrays. Hence, arrays can represent values along one dimension while matrices can represent values along two dimensions, arranged in a grid.

show it to me

Since the matrix is a column of arrays, we can print any row with a single print() command, but we cannot print a column with a single print() command.

Training Missions

7.TM1 Strawberries – soil analysis

work it out


Karel got the results of a soil analysis of his strawberry patch. To obtain the best strawberries he needs to plant this many plants in each of its 7-cell garden:

cell 0 1 2 3 4 5 6
plants 8 6 3 5 6 8 5


Strawberry, ''gariguette'' variety Copyright © 2005 David Monniaux

Strawberry, ”gariguette” variety Copyright © 2005 David Monniaux


Answer: show it to me

Programs can handle arrays of any size with ease because we can ‘walk’ along them using a for loop.



ready!

ready!


all done!

all done!


7.TM2 Olympic dreams – Shot put

Work it out


Now that the winter olympics are over, Karel is helping with the summer olympic team. Today he is helping the shot putters. He has to go into the field, look for shots, and alert the judges with the coordinates of the cells that have a shot in it. He does not need to pick the shots; the judges will do that. Easy enough. The field is 3 meters wide and 5 meters long.


"Bundesarchiv Bild 183-44941-0006, Plihan" by Bundesarchiv, Bild 183-44941-0006 / CC-BY-SA. Licensed under CC BY-SA 3.0 de via Wikimedia Commons - http://commons.wikimedia.org/wiki/File:Bundesarchiv_Bild_183-44941-0006,_Plihan.jpg#mediaviewer/File:Bundesarchiv_Bild_183-44941-0006,_Plihan.jpg

“Bundesarchiv Bild 183-44941-0006, Plihan” by Bundesarchiv, Bild 183-44941-0006 / CC-BY-SA. Licensed under CC BY-SA 3.0 de via Wikimedia Commons – http://commons.wikimedia.org/wiki/File:Bundesarchiv_Bild_183-44941-0006,_Plihan.jpg#mediaviewer/File:Bundesarchiv_Bild_183-44941-0006,_Plihan.jpg


Answer: show it to me



Ready..

Ready..

in the middle of it..

in the middle of it..


7.1 Strawberries – Storm

Work it out


A storm is coming! a storm is coming! We do not have time to replant each patch of strawberries individually. Pick them all up on one go but remember the number of plants in each cell and duplicate the patten in the fenced area. Then take Karel home to get some hot cocoa.


"Rolling-thunder-cloud" by Photo by John Kerstholt.original upload by Solitude - From English Wikipedia. Licensed under Creative Commons Attribution-Share Alike 3.0 via Wikimedia Commons

“Rolling-thunder-cloud” by Photo by John Kerstholt.original upload by Solitude – From English Wikipedia. Licensed under Creative Commons Attribution-Share Alike 3.0 via Wikimedia Commons


Answer: show it to me

We have to pick up all the strawberries as we move forward. We can set up an array and store the number of strawberries in each cell. When we replant the strawberries, we retrieve the values from the array.


ready...

ready…


phew!

phew!


7.2 Life in SoCal – the tar pits

★★ Work it out


Karel is volunteering at the Los Angeles Tar pits, an archaeological area full of fossils of sable-tooth tigers and mammoths.

Karel has to move all the bones found in a 3 x 4 rectangular area of the park to its new home inside the walls of the museum. Once Karel enters the building, he cannot go back for more bones so he has to pick them up all of them, at once, and remember the number of bones in each cell. Please help Karel.


« Mammoth-ZOO.Dvur.Kralove » par Mistvan — Travail personnel. Sous licence Creative Commons Attribution-Share Alike 3.0-2.5-2.0-1.0 via Wikimedia Commons

« Mammoth-ZOO.Dvur.Kralove » par Mistvan — Travail personnel. Sous licence Creative Commons Attribution-Share Alike 3.0-2.5-2.0-1.0 via Wikimedia Commons


Answer: Show it to me


Ready

Ready


all done!

all done!


7.3 Life in SoCal – Space shuttle

★★★ Work it out

NASA has finally graduated Karel as an Astro-droid and given him command of a space shuttle.. a little one, though. Please, have Karel load the equipment in the shuttle, in the same order in which it was laid out. As soon as Karel gets in the shuttle, the doors close and he cannot get out so he has to move all the equipment in a single pass.

“Ah… this is the same as the museum mission… piece of cake”. Hum.. is it? Probably not, otherwise it would not be worth 3 stars.

Space Shuttle "Discovery"

Space Shuttle “Discovery”

Answer: Show it to me

Loading now...

Loading now…

BLASTOFF!

BLASTOFF!

Optional Mission – Does not give a star

7.4 The rooms of doom – Angkor Vat

★★★★ Work it out

We are in Cambodia, looking at the beautiful structure of Angkor-Vat, mirrored on a lake. Following the structure, the challenge of the room of Doom is simple: mirror the pattern of tokens of the first room on the second and then you are allowed to go home. Once we go from the first room into the second one, we are not allowed back into the first room.

"Angkor Wat" by Bjørn Christian Tørrissen - Own work. Licensed under Creative Commons Attribution-Share Alike 3.0-2.5-2.0-1.0 via Wikimedia Commons

“Angkor Wat” by Bjørn Christian Tørrissen – Own work. Licensed under Creative Commons Attribution-Share Alike 3.0-2.5-2.0-1.0 via Wikimedia Commons

Answer: Show it to me

ready!

ready!

alright!

alright!