4
Contributors
5
Replies
6
Views
6 Years
Discussion Span
Last Post by aravind326
0

It's a collection of things of the same type all right next to each other. My post, for example, is an array of characters. So let's try again. What exactly don't you understand? Take a description of arrays and tell me which parts are confusing, because saying "I can't understand" doesn't help me to help you at all.

1

Array is a simple concept.

It is just a collection of variables, all of the same type. But it is not just some collection of variables. If it were, you could have used a bunch of variables instead.

So the important advantage of arrays is that, the collection of variables are sitting next to each other. Just take a train example. Consider a train wagon to be a variable. Then the train (a series of wagons arranged so that they are in sequence) becomes an array.

An array has elements arranged like that (in a sequence), in memmory. The memory locations of the variables in an array will be close together, in a sequence.

So, if you know the memory location of one element in an array, you can very well go to the adjacent elements by accessing the adjacent memory locations.

Consider a[10] is an array of 10 elements.

a[0] giving you the first element and a[9] gives you the last element. (In most programming languages)

In an array all elements have a same variable name. Only the index changes. The index denoting the position in the array. Kind of like the wagon position indicator in a train.

Array are particularly useful if you have to do something in a looping manner to a bunch of values. Let it be just printing the values of 10 variables.

You cannot write efficient code to do that in a loop, if you are using 10 different variables.

But if you are using an array, everything becomes simple. You declare an array of 10 variables. And then store the 10 values in them.

Like:
a[0]=<first_value>
a[1]=<second_value>
and so on.

In a loop all you have to do is, make the index move from values 0 to 9. Print the value in the array in the position which is specified by the index.

Example code:

for(i=0;i<10;i++)
{
 cout<<a[i];
}

Life is very simple with arrays.

Hope I made something clear. Read more about this, its simple.

Have a great time coding.

Votes + Comments
nicely explained.
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.