What's the purpose of an array?

  • Thread starter Thread starter realism877
  • Start date Start date
  • Tags Tags
    Array
Click For Summary
Arrays are essential data structures that allow for the storage of multiple items under a single variable name, facilitating efficient data management. They enable operations on collections of data, such as calculating averages, without the need for numerous individual variables, thereby reducing code duplication and complexity. Arrays are compact and easily extensible, making them preferable for handling large datasets, such as measurements in a classroom. Additionally, they can represent various data types, including strings, by treating them as arrays of characters. Overall, arrays significantly enhance programming efficiency and data manipulation capabilities.
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.
 
Technology 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.
 
Just like you would manipulate matrices and vectors mathematically, arrays allow us to manipulate matrices and vectors numerically with the aid of computers.
 
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.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 32 ·
2
Replies
32
Views
3K
  • · Replies 6 ·
Replies
6
Views
1K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
8
Views
2K