What's the purpose of an array?

  • Thread starter realism877
  • Start date
  • Tags
    Array
In summary, arrays are data structures that allow for the storage of multiple items under a single name. They are useful for situations where the same operations need to be performed on a set of data, such as finding the average of a list of heights. Without arrays, this would require a lot of separate variables and duplicated code. Arrays also allow for easy looping and expansion, making them a compact and extensible solution. They are commonly used in programming languages for manipulating strings, which are essentially arrays of characters.
  • #1
realism877
80
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
  • #3
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.
 
  • #4
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.
 
  • #5
Just like you would manipulate matrices and vectors mathematically, arrays allow us to manipulate matrices and vectors numerically with the aid of computers.
 
  • #6
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.
 
  • #7
Thanks, Guys. Appreciate it.
 
  • #8
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.
 
  • #9
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.
 

Related to What's the purpose of an array?

1. What is an array?

An array is a data structure used to store a collection of elements of the same data type. It allows for easy access and manipulation of the data by indexing each element in the array.

2. Why do we use arrays?

Arrays are used to store and organize large amounts of data in a structured and efficient manner. They also allow for easy access and manipulation of the data, making it useful for tasks such as sorting and searching.

3. What is the purpose of an array?

The purpose of an array is to provide a way to store and access a collection of data in a sequential manner. It allows for efficient data retrieval and manipulation, making it a useful tool for solving various programming problems.

4. What are the benefits of using arrays?

Arrays offer several benefits, including efficient data storage and retrieval, easy access to specific elements, and the ability to perform common operations such as sorting and searching. They also allow for code reusability, as the same array structure can be used for different sets of data.

5. Can arrays hold different data types?

In most programming languages, arrays can only hold elements of the same data type. However, some languages allow for arrays to hold different data types by using a data structure called a "variant array" or "heterogeneous array". These types of arrays are less efficient and are not commonly used.

Similar threads

  • Programming and Computer Science
Replies
1
Views
721
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
32
Views
2K
  • Programming and Computer Science
Replies
6
Views
862
  • Programming and Computer Science
Replies
4
Views
5K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
13
Views
1K
  • Programming and Computer Science
Replies
17
Views
2K
  • Programming and Computer Science
Replies
8
Views
1K
Back
Top