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:
- Define a global int array ga of ten ints initialized to 1, 2, 4, 8, 16, etc.
- Define a function f() taking an int array argument and an int argument indicating the number of elements in the array.
In f():
- Define a local int array la of ten ints.
- Copy the values from ga into la.
- Print out the elements of la.
- 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.
- Copy the values from the argument array into the free-store array.
- Print out the elements of the free-store array.
- Deallocate the free-store array.
In main():
- Call f() with ga as its argument.
- 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.).
- Call f() with aa as its argument.
Standard library vector drill:
- Define a global vector<int> gv; initialize it with ten ints, 1, 2, 4, 8, 16, etc.
- Define a function f() taking a vector<int> argument.
In f():
- Define a local vector<int> lv with the same number of elements as the argument vector.
- Copy the values from gv into lv.
- Print out the elements of lv.
- Define a local vector<int> lv2; initialize it to be a copy of the argument vector.
- Print out the elements of lv2.
In main():
- Call f() with gv as its argument.
- 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.).
- Call f() with vv as its argument.