Showing posts with label chapter 18 drills. Show all posts
Showing posts with label chapter 18 drills. Show all posts

Sunday, 1 November 2020

Chapter 18 // Vector Drills - Principles & Practice Using C++

In this exercise I am using Visual Studio 2017 and the std_lib_facilities header provided by Stroustrup.

Chapter 18 // Vector Drills

1 - Define a global vector<int> gv; initialise it with ten ints, 1, 2, 4, 8, 16, etc.
2 - Define a function f() taking a vector<int> argument.
3 - In f():
    a. Define a local vector<int> lv with the same number of elements as the argument vector.
    b. Copy the values from gv into lv.
    c. Print out the elements of lv.
    d. Define a local vector<int> lv2; initialise it to be a copy of the argument vector.
    e. Print out the elements of lv2.
4. In main():
    a. Call f() with gv as its argument.
    b. Define a vector<int> vv, and initialise it with the first ten factorial values (1, 2*1, 3*2*1, 4*3*2*1,             etc.).
    c. Call f() with vv as its argument.

Github: https://github.com/l-paz91/principles-practice/blob/master/Chapter%2018/VectorDrills

Oh I love vector. You can just get on with your life.

Saturday, 31 October 2020

Chapter 18 // Array Drills - Principles & Practice Using C++

In this exercise I am using Visual Studio 2017 and the std_lib_facilities header provided by Stroustrup.

Chapter 18 // Array Drills

1 - Define a global int array ga of ten ints initialised 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():
    a. Define a local int array la of ten ints.
    b. Copy the values from ga into la.
    c.  Print out the elements of la.
    d. Define a pointer p to int  and initialise it with an array allocated on the free store with the same                 number of elements as the argument array.
    e. Copy the values from the argument array into the free-store array.
    f. Print out the elements of the free-store array.
    g. Deallocate the free-store array.
4. In main():
    a. Call f() with ga  as its argument.
    b. Define an array aa with ten elements, and initialise it with the first ten factorial values (1, 2*1,                     3*2*1, 4*3*2*1, etc).
    c. Call f() with aa as its argument;

Github: https://github.com/l-paz91/principles-practice/blob/master/Chapter%2018/ArrayDrills

I will admit the memcpy got me first time as I read it too quickly and was like "ah yes, destination, source, number in array".....it's the number of bytes in the array, so arraySize*sizeof(int).