Return to Topic Menu | Computer Science Main Page | MathBits.com | Terms of Use | Resource CD

 

Initializing Arrays

Using the assignment operator (=) to initialize an array (the "drudge" method):

// this process could be time consuming (drudgery)
// if a large number of elements are being used

apvector <double> prices(3);
prices[0] = 19.95; 
//filling one element at a time
prices[1] = 29.95;
prices[2] = 42.5;

Note:  Unlike Standard C++ arrays, apvector class does NOT let you initialize the elements with a LIST of different values.

 

Using a for loop and cin to initialize an array:

apvector <int> nums(10);
for(int count = 0; count < 10; count++)
{
     cout<<"Please enter a number.....";
     cin>>nums[count];  
// looping process fills elements
}

 

Declare a vector and establish a "fill" value for all elements:

// declares apvector "values" to contain 100 doubles
// and sets all elements equal to 99.9.

apvector <double> values (100, 99.9);

 

Declare an array of strings:

// be sure all necessary headers are present
apvector <apstring> names(25);

 

 

 

Warning!
Array subscripts start with the
 number 0
 (not 1).

 

 

 

 

<apvector.h>