Can someone please explain arrays to me like I'm in kindergarten?

I need someone to help me understand the concept of arrays.

Thanks in advance!
Joanie
take a sheet of paper, and write the numbers 0-9 separated by a comma. This is a basic 1 Dimensional array. If you hand that paper to your parents and ask them to reorganize it, that is like passing the array, your list, to a function. Instead of getting a copy the function gets the address, or in our case the paper on which the array was written.

That was a 1D array. Now take that same piece of paper and get a second one. Write on the second paper the first 10 numbers which come to your heat, and separate them by a comma. Now staple these pieces of paper, both the 0-9 and the random numbers you just wrote. This is the kind of thing a 2 Dimensional array is. Each of the pages represents a row, and if you flip between the two you can tell which ones of these numbers where first on the page and which were second on their respective pages, right? This is similar to the columns.

If you were to pass this page to your parent, you'd need to tell them that it's two pages, so make sure they reorganize both pages! Similarly with functions you must define the second dimension of the array, in our case 2 for the number of pages.

That is the basics of the organization of arrays themselves, the elements are a little different. Where we count starting from 1 the array starts at 0. So if I have an array A of size N, we will assume N=10 for now. The First element in array A is A[0], nor A[1]. And the last element as you might image in is A[9] not A[10].

Does this help? If so I can answer more specific questions.
Just a disclaimer, I'm not the best with analogies. Still, I'll try to explain the concept as well as I can. Also, I'll assume we're talking about arrays declared using the type int[]; notation instead of type* name = new type[]; notation.

Think of a one-dimensional array as a set of round peg holes placed side-by-side, in line. These holes represent the space that's reserved for your values (the pegs). You cannot easily change the number of holes while your program is running*, but you can change the pegs that go in each hole.

You can put a peg of any color into your round holes which are labeled from 0 to the number of holes minus one, that is, you can put any value supported by the type of the array into a space provided by the array, as long as it's of the same type. If you wanted to put a square peg into that hole, that is if you wanted to store, say, a double in an element of your array in place of an int, you would have to do something to the peg before it would fit (type casting).

Does this help? EDIT: Ninja'd Pirate'd. Except that there's a 3 minute difference.

-Albatross


*Unless you use the other notation. That's dynamic allocation, however, and a rather dirty subject. I don't suggest you use it until you learn about its dangers.
Last edited on
Like your in kindergarten.... I can do this!

Think of a house. You could name every house with it's own name, such as "The Johnson house" or "The Smith's house". Instead of having 40 thousand names per town, it's a lot easier to name the street it's on, and then the number it is in order. So you live on main street, the 24th house down. To find a house, you only need to know where the street is, then you know to go 24 houses down.

Now, this makes things easier if you want to do something to everyone of those houses. If you are giving out newspapers, you can go to Main street, and keep going down the street, and giving newspapers to every house until you run out of houses.
Thank you all so much! I loved each of your analogies. :) They help! Okay, below is a copy and paste from my professor's file. I am going to try and bold the parts I don't understand. They all beging with the "for"


Define Storage

int Numbers [5] = {61, 22, 78, 33, 11};

float Values [] = {60.4, 70.77, 80.4, 90.3, 100.55, 110.8};

char First [] = {'R', 'i', 'c', 'k'};

char Middle [30] = "Joseph";

Display arrays in various formats

for (Slot = 0; Slot <= 4; Slot++)
cout << Numbers [Slot]
<< "\n\n";

cout << "\n\n\n";

for (Slot = 0; Slot <= 5; Slot++) cout << Values [Slot];

cout << "\n\n\n";

for (Slot = 0; Slot <= 3; Slot++)
cout << First [Slot];

cout << " "
<< Middle
<< " "
<< Last
<< '\n';

char Last [] = "Gaydos";

int Slot;
I think its loops you don't understand rather than arrays...

This 'for' part is called a loop. It's called a loop because it keeps repeating itself a set number of times.

The stuff after the 'for' and between the brackets is what determines how many times the loop is going to repeat itself.

Let's take this example:

1
2
3
4
for(i=0;i<4;i++)
{
cout<<"P. Sherman 42 wallaby way, Sydney"<<endl;
}


They key here is the 'i' between the brackets. We are going to repeat the loop 'i' number of times, where 'i' starts at 0, 'i' is less than 4, and you +1 to 'i' every time the loop executes.

Therefore, since i=0 it will execute once, add one to 'i' (making i=1 now), then it will check if 'i' is less than 4 (which it is), and execute again, etc. etc. until i=4. At this point the loop will stop executing because for(i=0;i<4 is no longer true ('i' is not less than 4 anymore)

Namely, for(i=0; i<4; i++) means start at 0, perform whats inside the loop, add 1 everytime you perform whats in the loop, keep going until the number is less than 4. (So it will execute 4 times basically)

It's just a logical way to tell the computer that you want to repeat something 'i' number of times. That should sort you out for now.

EDIT: The output of this program would be:

P. Sherman 42 wallaby way, Sydney
P. Sherman 42 wallaby way, Sydney
P. Sherman 42 wallaby way, Sydney
P. Sherman 42 wallaby way, Sydney
Last edited on
Okay, stridexr, I think you're right...my problem may be with the loops. However, you said with the loop that the "for" executes n number of times. The following is the answer after I compiled it. Why didn't it execute n number of times like it said in the loop?

c
-17
Rick
0xffbffa70
53
-17
99
62
closed account (zb0S216C)
A computer is never wrong; they don't lie either. A computer only does what you tell it to.

Henceforth, I shall be referring to this loop (applies to all loops):
JBabyJ wrote:
for (Slot = 0; Slot <= 4; Slot++) (sic)


Indices of an array range from 0 to n-1. n is the specified size of the array. The loop is overstepping the boundaries of your array by 1. This violation of your array boundaries is caused by the condition of the for loop. The loop iterates through your array until Slot attains the value of 4, not 3. Here's some more detail: Your condition, in human form, says this: While the value within Slot is less than or equal to 4, do such and such. If the value within Slot is in between 0 and 4, the loop continues. However, your upper-bound index is 3.

JBabyJ wrote:
char First [] = {'R', 'i', 'c', 'k'}; (sic)

I would've used this: char First[] = "Rick"; This implicitly adds the null-character. With the original First initialization, no null-character is given, and if you passed that to std::cout, your results would differ.

Wazzak
Last edited on
@Framework: None of the arrays is overstepping its boundaries. The first loop (Numbers) goes from 0 to 4, for size 5, and the others are correct too.

I'm not sure what he's using as a compiler, but the order in which the code is presented is all wrong. 'Slot' is used from the start, but only declared at the bottom. Last is cout'd, but only initialized afterwards.

The order in which the output comes is all wrong as well. If we take loop 3 and output 'Rick' as a point of reference, it would suggest that 'middle' outputs an address, which makes zero sense.
closed account (zb0S216C)
Gaminic wrote:
None of the arrays is overstepping its boundaries. (sic)

It's not polite to correct someone with a wrong answer: http://www.filehosting.org/file/details/278116/Loop.png

Wazzak
Last edited on
I don't really feel like feeding that link my mail info. Anyway, since his post hasn't been edited, either you have received more information through non-public means (in which case you can't blame me for "correcting" you), or one of us is miscounting.

This is what I see, reorganized for clarity:

1
2
3
for (Slot = 0; Slot <= 4; Slot++) // 0->4 = 5
cout << Numbers [Slot]
int Numbers [5] = {61, 22, 78, 33, 11}; // Size = 5 

1
2
for (Slot = 0; Slot <= 5; Slot++) cout << Values [Slot]; // 0->5 = 6
float Values [] = {60.4, 70.77, 80.4, 90.3, 100.55, 110.8}; // Size = 6 

1
2
3
for (Slot = 0; Slot <= 3; Slot++) // 0->3 = 4
cout << First [Slot];
char First [] = {'R', 'i', 'c', 'k'}; // Size = 4 


The only possible problem I see is the space between the variable names and the accessor operator []. I never tried it, but considering you're getting nonsense, it could be that (even though the number of output alone is enough to wonder what the hell went wrong).
JBabyJ wrote:
Okay, stridexr, I think you're right...my problem may be with the loops. However, you said with the loop that the "for" executes n number of times. The following is the answer after I compiled it. Why didn't it execute n number of times like it said in the loop?

c
-17
Rick
0xffbffa70
53
-17
99
62


You're getting garbage output because the program is wrong. Based on your output, the only correct one that I can see is "Rick", so I will try and explain why the loop output "Rick". This is the part of the loop we are interested in:

1
2
for (Slot = 0; Slot <= 3; Slot++) 
cout << First [Slot];


Notice that "First" is defined as char First [] = {'R', 'i', 'c', 'k'};. This means that "First" is an array of characters or collection of the characters R, i, c and k, respectively. If we wanted to print the first letter 'R' we would use cout<<First[0]; because this would call the first possible character in the array. Similarly, if we wanted to call the second letter we would use cout<<First[1]; and so on, so forth.

Now back to the loop. We know that the loop is going to repeat itself Slot number of times, where Slot in this case starts at 0 ( for(Slot = 0 ... ) and the loop keeps on repeating itself until Slot <= 3. So the loop is going to repeat itself 4 times. (Since slot starts at 0 and ends up at 3, the loop will repeat for 0,1,2,3 - a total of 4 times.)

The key to remember here is that every time the loop executes, Slot is increased by 1. Therefore, if we type cout<<First[Slot]; in the body of the loop, it will do this when the program is run:

1
2
3
4
cout<<First[0];
cout<<First[1];
cout<<First[2];
cout<<First[3];


And since we know that these hold the values 'R' 'i' 'c' and 'k' respectively, the output will of course read:
Rick


Hope that helped.
Last edited on
Thank you so much, Stridexr. Your help with the loop made perfect sense. (finally!)
Topic archived. No new replies allowed.