Programming

FORUM Dedicated to Programming
 
HomePortalCalendarFAQSearchMemberlistUsergroupsRegisterLog in

Share | 
 

 Introdutction to Arrays

View previous topic View next topic Go down 
AuthorMessage
Admin
Administrator
Administrator


Number of posts : 22
Age : 26
Registration date : 2008-11-12

PostSubject: Introdutction to Arrays   27/11/08, 09:15 pm

Introdutction to Arrays
An array is a series of elements of the same type placed in contiguous memory locations that can be individually referenced by adding an index to a unique identifier.
That means that, for example, we can store 5 values of type int in an array without having to declare 5 different variables, each one with a different identifier. Instead of that, using an array we can store 5 different values of the same type, int for example, with a unique identifier.

For example, an array to contain 5 integer values of type int called billy could be represented like this:
0 12 34
_ __ __

where each blank panel represents an element of the array, that in this case are integer values of type int. These elements are numbered from 0 to 4 since in arrays the first index is always 0, independently of its length.

Like a regular variable, an array must be declared before it is used. A typical declaration for an array in C++ is:

type name [elements];


Initializing arrays.

When declaring a regular array of local scope (within a function, for example), if we do not specify otherwise, its elements will not be initialized to any value by default, so their content will be undetermined until we store some value in them. The elements of global and static arrays, on the other hand, are automatically initialized with their default values, which for all fundamental types this means they are filled with zeros.
In both cases, local and global, when we declare an array, we have the possibility to assign initial values to each one of its elements by enclosing the values in braces { }. For example:

int myarray [5] = { 16, 2, 77, 40, 12071 };


0 12 34
16 277 4012071


The amount of values between braces { } must not be larger than the number of elements that we declare for the array between square brackets [ ]. For example, in the example of array billy we have declared that it has 5 elements and in the list of initial values within braces { } we have specified 5 values, one for each element.

When an initialization of values is provided for an array, C++ allows the possibility of leaving the square brackets empty [ ]. In this case, the compiler will assume a size for the array that matches the number of values included between braces { }:

int myarray [] = { 16, 2, 77, 40, 12071 };

Accessing the values of an array.

In any point of a program in which an array is visible, we can access the value of any of its elements individually as if it was a normal variable, thus being able to both read and modify its value. The format is as simple as:

name[index]

myarray[2] = 75;
Back to top Go down
View user profile http://programming.6forum.info
assad_0123
Level 0
Level 0


Number of posts : 1
Registration date : 2008-11-14

PostSubject: great job boy   01/12/08, 07:14 pm

great job
Back to top Go down
View user profile
Hafiz Talha
Level 0
Level 0


Number of posts : 2
Registration date : 2008-11-15

PostSubject: great job   05/12/08, 03:24 pm

oh great job asad i was a bit cnfused but it cleard that
thnx
Back to top Go down
View user profile
Sponsored content




PostSubject: Re: Introdutction to Arrays   

Back to top Go down
 
Introdutction to Arrays
View previous topic View next topic Back to top 
Page 1 of 1

Permissions in this forum:You cannot reply to topics in this forum
Programming :: C & C++ Programming :: C & C++ Discussions & Solutions-
Jump to: