What's the purpose of an array?

  • Thread starter Thread starter realism877
  • Start date Start date
  • Tags Tags
    Array
Click For Summary

Discussion Overview

The discussion revolves around the purpose and usefulness of arrays in programming, exploring their role in storing multiple items, facilitating data manipulation, and comparing them to other data structures. The conversation includes examples and explanations of how arrays can simplify coding tasks.

Discussion Character

  • Exploratory
  • Technical explanation
  • Conceptual clarification

Main Points Raised

  • Some participants emphasize that arrays allow for the storage of multiple items, which can reduce code duplication and improve efficiency when performing operations on collections of data.
  • One participant provides an example of using an array to calculate the average height of individuals in a classroom, illustrating how arrays can simplify the process compared to using individual variables.
  • Another participant mentions that arrays enable numerical manipulation of matrices and vectors, highlighting their mathematical applications.
  • Some participants point out that strings can be represented as arrays of characters in certain programming languages, further demonstrating the versatility of arrays.

Areas of Agreement / Disagreement

Participants generally agree on the usefulness of arrays for storing and manipulating data, but there is no consensus on all aspects of their applications or comparisons with other data structures.

Contextual Notes

Some participants express difficulty in grasping the concept of arrays' usefulness, indicating a potential gap in understanding that may depend on prior knowledge of programming concepts.

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 4 ·
Replies
4
Views
7K
  • · Replies 6 ·
Replies
6
Views
1K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 17 ·
Replies
17
Views
4K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
8
Views
2K