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).

Wednesday 28 October 2020

Chapter 17 // Exercise 14 - Principles & Practice Using C++

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

Chapter 17 // Exercise 14

Could the "list of gods" example from Section 17.10.1 have been written using a singly-linked list; that is,could we have left the prev member out of Link? Why might we want to do that? For what kind of examples would it make sense to use a singly-linked list? Re-implement that example using only a singly-linked list.

Github: https://github.com/l-paz91/principles-practice/tree/master/Chapter%2017/Exercise%2014

Eurgh why. Without prev it would be very hard to find the start of the list. The pointer can be changed to point at something else when an element is inserted/removed. We would need to store a pointer to the first value.

Insertion would be trickier as well as you would have to start from a, advance n times to the insertion point so you know the previous then assign the new successors. Just use a vector.

I tried googling best use cases for a Singly linked list and I couldn't really find any.

And with that, another chapter bites the dust. This was a good chapter though. I have so many notes on pointers and memory allocation. I'm also halfway through the book! Just 590 pages out of 1216.

Tuesday 27 October 2020

Chapter 17 // Exercise 13 - Principles & Practice Using C++

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

Chapter 17 // Exercise 13

Modify the Link class from section 17.10.1 to hold a value of a struct God. struct God should have members of type string: name, mythology, vehicle, and weapon. For example, God{"Zeus", "Greek", "", "lightning"} and God{"Odin", "Norse", "Eight-legged flying horse called Sleipner", "Spear and Gungnir"}. Write a print_all() function that lists gods with their attributes one per line. Add a member function add_ordered() that places its new element in its correct lexicographical position. Using the Links with the values of type God, make a list of gods from three mythologies; then move the elements (gods) from that list to three lexicographically ordered lists - one for each mythology.

Github: https://github.com/l-paz91/principles-practice/tree/master/Chapter%2017/Exercise%2013

What in the ever-living christ? I really hated this exercise when I started it. In fact, I procrastinated so much in doing this exercise that I made a slenderman game in Unreal Engine and did the infamous Blender Donut tutorial:

Chapter 17 // Exercise 13  - Principles & Practice Using C++

Anyway, I finally did it and by the end of it, I have to admit, I understood linked lists WAY better than I did before. Don't skip the exercises everyone; they actually work.

Sure, this is not the greatest code ever written but I did it!

Monday 5 October 2020

Chapter 17 // Exercise 11, 12 - Principles & Practice Using C++

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

Chapter 17 // Exercise 11, 12

11. Complete the "list of gods" example from section 17.10.1 and run it.
12. Why did we define two versions of find().

Github: https://github.com/l-paz91/principles-practice/tree/master/Chapter%2017/Exercise%2011

Wow, linked lists are confusing. I never want to read prev->succ = prev if !prev then succ = prev->succ etc....

For exercise 12 I had to go read the small section in Chapter 18. Basically, it's to stop someone from change what Link* is pointing at. In the functions I try to always assign the pointer to a new temp pointer to avoid messing with the original. I feel like this is the same case here.

Friday 2 October 2020

Chapter 17 // Exercise 10 - Principles & Practice Using C++

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

Chapter 17 // Exercise 10

Look at your solution of exercise 7. Is there any way that input could get the array to overflow; that is, is there any way you could enter more characters than you allocated space for (a serious error)? Does anything reasonable happen if you try to enter more characters than you allocated?

Github: https://github.com/l-paz91/principles-practice/blob/master/Chapter%2017/Exercise%207%2C%208

Fortunately, the version of cin.get() that I used only gets up to the specified amount of characters. So I pass it the char pointer then specify 256 (as that is the size of the array) and give it a deliminating character. So it will either stop reading at '!'  or once it reads in 256 characters.

Thursday 1 October 2020

Chapter 17 // Exercise 9 - Principles & Practice Using C++

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

Chapter 17 // Exercise 9

Which way does the stack grow: up (toward higher addresses) or down ( towards lower addresses)? Which way does the free store initially grow (that is, before you use delete)? Write a program to determine the answers.

Github: https://github.com/l-paz91/principles-practice/blob/master/Chapter%2017/Exercise%209

If you're newing via a for loop it seems to pick addresses near to each other? Interestingly, it appears to grow downwards for numbers and upwards for chars. I don't think it's set in stone though as I've seen the free store ints going upwards.