Code About Us Tutorial


  Arrays
    What is an Array?
    Array Elements
    Filling an Array
    Initializing An Array
    Multidimensional Arrays


   
 

Home > Arrays > Array Elements

December 1, 2009 12:47 am

   

Array Elements

An array can be set to any valid type in C++. Any name can be chosen as an array name, as long as it follows the rules for valid variable names. Careful attention must be given to the array-size as this is often found to be quite confusing in the beginning. When considering the array-size of an array, you have to remember that its 0 based, what this means is that the index values start off at 0, not 1. So if you had int a[5]; it would consist of the following elements:

 

 
a[0];
a[1];           
a[2];
a[3];
a[4];        //5 elements 0-4

Here is a rule that you can use, that should help you remember how the elements in an array work.

If the name of an array is a, then a[0] is the name of the element that is in position 0, a[1] is the name of the element that is in position 1, etc.

In general, the ith element is in position I-1. So if the array has n elements, their names are a[0], a[1], a[2], …, a[n-1].

Arrays are so useful because they allow a single name with a variable index to be used in place of many different names. In this next example how to “fill” the array elements is illustrated. It will be using a for loop, We aren’t going to talk about the loop in depth in this chapter, right now all you need to know is it fills the array. The for loop checks the condition, “is the counter variable (i) less then 5.” It executes the code in the body over and over again until the condition is true. So if you had this code:

 

 
#include <iostream.h>

void main() { for(int i = 1; i < 11; i++) { cout << i << "n"; }

}

This program prints the counter variable 10 times. The ouput would be

 

 
//program output
1
2
3
4
5
6
7
8
9
10
Please Rate this Code:


    Comments for: Array Elements
Annonymouse User says:
How can I get the array size ?

MoMad [big_mo_mine@yahoo.com] Posted: 31 times. says:
you dont get the array size!!

MoMad [big_mo_mine@yahoo.com] Posted: 31 times. says:
you dont get the array size!! says:
if array contains characters (is a string) you can use strlen(myarray)

MoMad [big_mo_mine@yahoo.com] Posted: 31 times. says:
you dont get the array size!! says:
if array contains characters (is a string) you can use strlen(myarray) says:
hey wouldnt the actual output be 12345678910

because there isnt a endl (endline statement) in the bottom part of the loop.. thus the cout would just keep add the numbers in output to the right.. not making a new line

r3v [reverend_maynard@hotmail.com] Posted: 4 times. says:
that last comment was made by r3v <-- me

reverend_maynard@hotmail.com

i think theres an error in this pages code!! :p

but i did say that up there about the endline statment

r3v [reverend_maynard@hotmail.com] Posted: 4 times. says:
that last comment was made by r3v <-- me

reverend_maynard@hotmail.com

i think theres an error in this pages code!! :p

but i did say that up there about the endline statment

r3v [reverend_maynard@hotmail.com] Posted: 4 times. says:
haha nm.. i was wrong.. "n"

r3v [reverend_maynard@hotmail.com] Posted: 4 times. says:
haha nm.. i was wrong.. "n" says:
The "n" should be "\n" no?

r3v [reverend_maynard@hotmail.com] Posted: 4 times. says:
haha nm.. i was wrong.. "n" says:
The "n" should be "\n" no? says:
The "n" should be a "\n". No?




Add Comment:
Name:
Email:


 «11» THINKQUEST TEAM C0111571 © 2001. All Rights Reserved.