Monday, November 14, 2011

Coding Lessons: C and Arrays (lesson 9)

Arrays are a pretty important part of the way in which information is stored. This is true of both programming on computers, and (as a hint of things to come) also when programming embedded micro controllers for projects that you may create.

You create arrays when you want to store many items of the same type of data, and you want them to be stored in adjoining memory locations to make locating or sorting them easy.
This could be (for example) a series of temperature measurements.

So we have the idea that an array is a series or collection of data, you can imagine it as a table row.

something like this


and we've covered that the data is of the same type, (all int, or all char, etc) so now lets have a look at how we define and element and how we use an element.

Remember we define a single value like this

int x;

that is I want this type of data (int) stored as this variable name (x)

defining an array is not much different.

int x[8];

I want to store integers (int) as this variable name (x) and I want this many pockets to put data in (8)

When you access an array it is treated like a normal piece of data.

for example.
If I had a program.
int x;
int y;
int z;

I have 3 bits of data.
I can say
x = 1;
y = 2;
z = 3;

and call them like this

printf("x = %d\r\n", x);
printf("y = %d\r\n", y);
printf("z = %d\r\n", z);

but if I use an array I can store all the data in adjoining memory.

now I say

int coordinates[3];

and I can recall the data from it's storage location, in much the same way as using standard data;

printf("x = %d", coordinates[0]);
and so on.

Now this interesting thing for anyone paying attention there is that we're not starting counting from 1, Just as data pins on microchips start counting with pin0, pin1 etc in arrays we count our first data "pocket" as 0

Multi Dimensional Arrays
You may have noticed that a single table row is a little, well limiting, I gave an example of a coordinate above.

Imagine you're drawing a graph (x,y), what are you going to do? declare multiple arrays
int datapoint1[2];
int datapoint2[2];
int datapoint8[2];

or are you going to try to make an array that is:
int datapoints[16];
(where 0 = x1, 1 = y1, 2=x2, 3 = y2, 4 = x3... -far too confusing)
or instead would it make more sense to define your array in multiple dimensions. more like a table?

int datapoints[8][8];
Now you can store your x an y values in a way that makes sense to you.

The thing that you should note is that this is not an array that looks like a table 8 columns wide and 2 rows deep, this table is 8 wide, 8 deep, there are 256 data pockets.
storing 2d co-ordinates like this would be a bit of a waste of memory.

2 dimensional arrays are not the limit, you can create incredibly complex multi dimensional arrays.

int array[10][10][10]; = 1000 array pockets

int array[10][10][10][10]; = 10,000 array pockets

you see how it adds up fast.

On a modern computer with near limitless amounts of memory this is unlikely to be a problem, you can waste memory, like making an 8x8 256 pocket array for storing 16 values, to make 8 graph points on a line graph.

but on a lower spec'd device (like an embedded processor) you might want to be more careful with your memory use.
in this case your 2d array for a graph might be
int points[8][2];

and you'd have data

No comments: