Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

What's the purpose of an array?

  1. Aug 16, 2011 #1
    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.
     
  2. jcsd
  3. Aug 16, 2011 #2
  4. Aug 17, 2011 #3
    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.
     
  5. Aug 17, 2011 #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 (Text):
    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 (Text):
    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.
     
  6. Aug 17, 2011 #5

    SteamKing

    User Avatar
    Staff Emeritus
    Science Advisor
    Homework Helper

    Just like you would manipulate matrices and vectors mathematically, arrays allow us to manipulate matrices and vectors numerically with the aid of computers.
     
  7. Aug 17, 2011 #6

    Mark44

    Staff: Mentor

    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.
     
  8. Aug 17, 2011 #7
    Thanks, Guys. Appreciate it.
     
  9. Aug 24, 2011 #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.
     
  10. Aug 24, 2011 #9

    Mark44

    Staff: Mentor

    Assuming that we're talking about programming languages that store strings as arrays of characters. That would include C, C++, and several other languages.
    array[0] == 'A'
    array[1] == ' '
    array[2] == 't'
    etc.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: What's the purpose of an array?
  1. CS Purposes (Replies: 4)

  2. Help with array (Replies: 5)

  3. C++ Array (Replies: 10)

  4. C# arrays (Replies: 1)

  5. Array storage (Replies: 1)

Loading...