views:

360

answers:

8

I totally understand the purpose of arrays, yet I do not feel I have "mastered" them. Does anyone have some really good problems or readings involving arrays. I program in PHP and C++ so if there are examples with those languages that would be preferable but is not necessary.

+12  A: 

Draw everything out on graph paper.
Memory is just little boxes, it's a lot clearer to see on paper with a few arrows than in some complex markup language

Martin Beckett
+1. I find that it's impossible to be confused by anything once you draw it with little boxes.
mquander
Except when you get into >3 dimensions. :)
Brian MacKay
Didn't you have HyperLego 4d Building Toy when you were a kid?
Adam Jaskiewicz
A: 

You can't.

Greg
If you haven't noticed, sarcastic answers don't do yo well here.
patricksweeney
+2  A: 

Define 'mastered'.

Does anyone have some really good problems or readings involving arrays.

Try array based

  • Linked-list implementation.
  • Implement stacks, queues, etc and then use stack(s) to emulate a queue etc
  • Heaps
dirkgently
+1  A: 

Try manually (no built in methods) sorting with arrays (bubblesort is a good one to get yourself going)

MasterMax1313
+1  A: 

A lot of people seem to struggle with the concept of arrays at first, particularly arrays of >2 dimensions.

It's a little too abstract. However, getting past that initial block just requires a concrete exploration of the mechanics. So, here's an example I've used a lot that shows the basic mechanics in a nerd-friendly way:

Concrete Example (in psuedocode)

Let's say you're building a role playing game and you want to keep track of your character's stats. You could use an array of integers like this:

  • Stats(0) could be strength
  • Stats(1) could be dexterity
  • Stats(2) could be intelligence

...And so on.

Now let's add a level of complexity. Maybe we want to introduce a potion of strength that increases strength by 5 for 10 turns. We could represent the stat side of that by making this into a 2-dimensional array:

  • Stats(0, 0) - this is my current strength.
  • Stats(0, 1) - this is my normal strength.
  • Stats(0, 2) - this is the number of turns until the strength potion wears off.
  • Stats(1, 0) - this is my current dexterity.

...And so on. You get the idea.

So I've added a 2nd dimension to hold details about the 1st dimension. What if I wanted our Stats array to handle statistics for more than one character? I could represent that by making this into a 3-dimensional array:

  • Stats(0, 0, 0) - character 0's current strength.
  • Stats(1, 1, 0) - character 1's current dexterity.

It would be even better to create some constants or enums to eliminate magic numbers from the code:

Const Strength = 0
Const Dexterity = 1
Const Intelligence = 2

Const CurrentValue = 0 
Const NormalValue = 1
Const PotionTurns = 2

Then I could do:

Stats(1, Dexterity, NormalValue) = 5 'For character 1, set the normalvalue of dex to 5.

A few more thoughts about arrays... At least in the .Net world where I live, most of us don't have to use them in our day-to-day lives too often because they're slowly being relegated to underpinnings for more elaborate data structures like collections.

In fact, if I were to implement character stats, realistically I would not use arrays.

However, it's still important to get your head around them because they are rocket-fast and there are definitely cases where they're invaluable.

Brian MacKay
+1  A: 

An array is a contiguous block of memory devoted to N items of the same type, where N is a fixed number indicating the number of items. In order to expand an array (as they are fixed in size), you can use the C function realloc(..). In C++, you can manually re-allocate an array by creating a new, larger array and copying the contents of the old array into the new one (an expensive operation).

Modern languages have options that supplant arrays (so as to overcome the inherent limitations of arrays). In C++, you can use the Standard Template Library (STL) for this purpose; the STL "vector" data type is a typical replacement for standard arrays in C++. Platform-dependent APIs like MFC have built-in constructs like the CArrayList for a richer array usage experience. ;-)

Garrett
+1  A: 

I'm going to try to explain the best i can arrays in their fundamental form.

Ok, so an array is basically a way to store data. For instance if you want a list of shopping items, we would use a one dimensional array:

[0] - "Bread"

[1] - "Milk"

[2] - "Eggs"

[3] - "Butter" . . .

[n] - "Candy"

Each index 0,1,2,3,...n holds a specific data. The data as you can see are represented as Strings. Now i can't use something like :

[n+1] = 1000

because this will put an integer as index n+1, the compiler will tell you that it's no good and you need to fix that issue.

Moving on to matrices or 2-dimensional arrays. Take a piece of squared paper like the ones you use for math and draw a Cartesian system and some dots. Put the coordinates on different piece of paper and next to them put a 1. For example:

[0,0] = 1

[0,1] = 1

[2,3] = 1

What this means is that at index [0,0],[0,1],[2,3] i have 1. A representation would be like so:

Cartesian System in form of a matrices :

  1) 2) 3)

1) 1  1  0

2) 0  0  1

3) 0  0  0

I used just simple arrays to illustrate what are they, for example if you want to go 3D the data structure for that would be a array of matrices aka a list which holds each Cartesian location at a specific height.

If we had 10 as height and the same dots as above it would be something like:

[10,0,0] - 1

[10,0,1] - 1

[10,2,3] - 1

If you want a way to master: grab a simple list of problems and try to implement them using arrays of any kind. There is no quick way to do it, just have patience and practice.

Cristina
A: 

Alright, so a one-dimensional array is just a grouping of variables. Useful if you've got a lot of something, and you like to save time and space. Functionally,

element1:=5;
element2:=6;
element3:=7;
...

is the same as saying

element[1]:=5;
element[2]:=6;
element[3]:=7;
...

except now the computer knows what you're talking about and you can write something like:

for i:=1 to n do
   element[i]:=element[i]+1;

Moving on, a two dimensional array is somewhat more complicated, but can be thought of as an array of arrays. So we could have something like this:

type
  arrayA=array[1..50] of integer;
  arrayB=array[1..50] of arrayA;
  arrayB=array[1..50,1..50] of integer; //an equivalent declaration in Pascal to the above two

More specifically, a two-dimensional array is a table. So for example, if arrayA contains the grades of a student in 50 classes, then arrayB represents the grades of a bunch of students. So, arrayB[3,5] would be the grade of the third student on class number 3.

It's easy to expand the same logic to add another dimension to the array:

arrayC=array[1..50] of arrayB;

We could say that C represents a school, so arrayC[2,4,6] is the grade of the second schools fourth student in class 6.

Now, what are arrays used for? Storing groups of similar information, or information which'll need to be processed in bulk. In practice, though, you'll mostly be using a one-, at most two-, dimensional array. If you can imagine your data as a table, you'll almost definitely want an two-dimensional array, for example. How else would you represent a chess board, if you had to?

Three-dimensional arrays tend to be used less, but what if you had to save some sort of state for each of your table elements? Something like:

Table=array[1..50, 1..50, 0..1] of integer;

Then the third value is 1 if some condition is true for a given element, and 0 otherwise. Of course, a trivial example, but it's another way of understand three-dimensional arrays more easily.

VPeric