What's the purpose of an array?

  • Thread starter Thread starter realism877
  • Start date Start date
  • Tags Tags
    Array
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
8 replies · 15K views
realism877
Messages
80
Reaction score
0
I know what an array is and what it does. I'm just having a hard time getting the concpet on how it is useful.
 
Physics news on Phys.org
realism877 said:
I know what an array is and what it does. I'm just having a hard time getting the concpet on how it is useful.

They're useful for the simple fact that they allow you to store multiple items. Can you not see situations in which you'd want that? If you've got a load of items, frequently you'll want to do the same thing with each one. Without arrays, you'd have to use a load of separate variables and would probably have a lot of duplicated code. Arrays allow you to use loops, for one thing.
 
For example, say you took a bunch of measurements, maybe the height of every person in a classroom, so you have a heap of numbers and you want to process them to find the average height. With an array, you might do something like this:

Code:
int arrayLength = 30;
int myArray[arrayLength ]; // an array with 30 "slots", pretend we already put the data in
int total = 0;

for(int i = 0; i < arrayLength; i++)
{
     total = total + myArray[i];
}

average = total / arrayLength;

If you used a unique variable for each height measurement you might have to do something like this:

Code:
int height1, height2, height3, height4, height5, height6 ... height30; // 30 variables!

total = height1 + height2 + height3 + height4 ... + height 30; // !

average = total / 30;

The solution that uses an array is:

a) compact
b) extensible (making the array larger involves changing 1 number)

The solution that doesn't is:

a) large and cumbersome
b) hard to extend (what if you want another 30 measurements? 30 more variables, and then 30 more operations on those variables)

Arrays are great! There are many more data structures available too, Linked Lists, Hash Tables, Trees, and many more.
 
realism877 said:
I know what an array is and what it does. I'm just having a hard time getting the concpet on how it is useful.
Using an array, you can store multiple values of the same time, using essentially a single name. The individual items in the array can be distinguished by an index.
 
Thanks, Guys. Appreciate it.
 
A text string is also a good example.

The above text i just wrote is an array with characters where array[0] = A, array[1] = "space", array[2] = t. and so on.
 
Jaynte said:
A text string is also a good example.
Assuming that we're talking about programming languages that store strings as arrays of characters. That would include C, C++, and several other languages.
Jaynte said:
The above text i just wrote is an array with characters where array[0] = A, array[1] = "space", array[2] = t. and so on.
array[0] == 'A'
array[1] == ' '
array[2] == 't'
etc.