 # Programming: Principles and Practice Using C++: Vectors and Arrays

• Print
This chapter is from the book

## Drill In this chapter, we have two drills: one to exercise arrays and one to exercise vectors in roughly the same manner. Do both and compare the effort involved in each.

Array drill:

1. Define a global int array ga of ten ints initialized to 1, 2, 4, 8, 16, etc.
2. Define a function f() taking an int array argument and an int argument indicating the number of elements in the array.
3. In f():

1. Define a local int array la of ten ints.
2. Copy the values from ga into la.
3. Print out the elements of la.
4. Define a pointer p to int and initialize it with an array allocated on the free store with the same number of elements as the argument array.
5. Copy the values from the argument array into the free-store array.
6. Print out the elements of the free-store array.
7. Deallocate the free-store array.
4. In main():

1. Call f() with ga as its argument.
2. Define an array aa with ten elements, and initialize it with the first ten factorial values (1, 2*1, 3*2*1, 4*3*2*1, etc.).
3. Call f() with aa as its argument.

Standard library vector drill:

1. Define a global vector<int> gv; initialize it with ten ints, 1, 2, 4, 8, 16, etc.
2. Define a function f() taking a vector<int> argument.
3. In f():

1. Define a local vector<int> lv with the same number of elements as the argument vector.
2. Copy the values from gv into lv.
3. Print out the elements of lv.
4. Define a local vector<int> lv2; initialize it to be a copy of the argument vector.
5. Print out the elements of lv2.
4. In main():

1. Call f() with gv as its argument.
2. Define a vector<int> vv, and initialize it with the first ten factorial values (1, 2*1, 3*2*1, 4*3*2*1, etc.).
3. Call f() with vv as its argument.