Arrays and Vectors?? I know that an array is like a collection of values. I also know that vectors are used for describing direction and speed in maths. I don’t think that’s the same here.

Well, time to learn some new concepts of the C++ programming language.

If you’re interested in taking the course yourself, you can find it on Udemy.

alternative alternative energy blue eco
This is not the array you’re looking for…

Lecture 53: Section Overview

So just a brief overview of the section again.

Arrays and vectors are compound data types. That is datatypes that are made up of other data types. Vectors are the way to go in modern C++ and the course will dive deeper into this later.

Lecture 54: What is an Array?

As mentioned in the overview, an array is a collection of elements of the same data type.

This means you can for instance make an array of strings, call the array Names and use this to address all the names. You can then go through the list, read each name and then put it into some output. Much better than using say 100 different variables called name_1, name_2 etc.

But arrays also have some limitations. One is that it’s a fixed size. If you define it to have space for 100 names and you need to add name 101, then you’re out of luck and need to change the program.

You also need to keep track of how many elements the array has yourself. If you have 100 elements, but ask to read element 123, the program will read some data from memory and spit it back at you, but it probably won’t be useful.

The worse thing is that you can also change elements that are out of bounds. That means you will write data into memory that might be used by other programs and have the potential to crash the computer.

But an array is very efficient and easy to loop through, so it’s a nice tool to have. Just remember that the first element in the list has address 0, not 1. So an array with size of 100, goes from 0-99. If you go from 1-100, you can and probably will end up in trouble.

Lecture 55: Declaring and Initializing Arrays

Here we go through how to declare an array.

It’s pretty easy and straightforward. Just remember you have to set it’s type and size when declaring it. As always it’s good practice to initialize a list when you declare it.

Actually, as it turns out,  I lie. Just goes to show that a little knowledge can be dangerous. 🙂
If you provide a list of values you can omit setting the size and the compiler will set the size based on the number of elements in the list you provide.

Lecture 56: Accessing and Modifying Array Elements

Here we learn how to access the individual elements of the array to get the value of that element. The instructor goes through how this works in the background and what the compiler does. He then shows you practical examples in CodeLite and also shows how you can easily crash your program.

It’s explained clearly and it’s pretty easy so shouldn’t give you any problems. Just remember that the compiler does NOT check that you are keeping yourself inside the array.

Lecture 57: Multidimensional Arrays

Sometimes you need more dimensions and C++ supports this. You declare, initialize and use it in the same way as normal arrays by using [] and {}.

Really nothing special about this, you just introduce more dimensions into the array.

pexels-photo-285174.jpeg
That is some nice vectors. 🙂

Lecture 58: Declaring and Initializing Vectors

A vector is defined in the C++ Standard Template Library. It has many of the same characteristics as arrays, but with added bonus features. Since it’s part of the library it already has built-in functions that you can use in your code. This can be things like sorting, finding an element and more.

I’m really looking forward to learning more about the STL as that has been lacking in the other courses I have taken. And there’s a whopper of a section on STL later in this course. It’s 28 lectures long, so it’s longer than many full courses on Udemy. 🙂

Vectors can be defined as a dynamic array, where the size can grow or shrink as needed. This is much better than arrays as you are using the exact memory needed to hold the data. With arrays, you had to declare the size up front, so you could up end up wasting memory or find yourself going outside the array with unknown consequences.

After seeing the summary in this lecture I really understand why Frank earlier said that you mostly use vectors in modern C++. They just seem better in every which way.

Lecture 59: Accessing and Modifying Vector Elements

Again we see that arrays and vectors are much alike, as you can use the exact same syntax as with arrays. But then you also have the same limitations, for instance, the possibility to go outside the vector.

And here we get to see the object-oriented part of C++. Vectors have several methods of accessing and modifying it. We also see that if you try to modify something outside the scope of the vector using these methods, the program will now crash with an error message instead of doing something potentially dangerous to your computer.

The error message you get can be pretty daunting, but the instructor explains it in a clear way, showing you what to look for. And that is pretty important. Some people panic when they see an error message because it can be just too much information. Getting shown what to look for is invaluable.

The instructor again uses practical examples in CodeLite to explain and sell vectors over arrays. I now understand even clearer why vectors come with huge benefits over arrays.

Lecture 60: Section Challenge and Quiz

The challenge was again pretty straightforward. The instructor asked us to create 2 vectors, add some data to them and then print them out. We were then going to create a 2D vector and add the first two vectors to that and display the content. Again pretty easy.

But now comes the surprise. Frank asks us to change the first element in the first vector we created and then display the content of the 2D vector again. I thought that we might get a change of value in the 2D vector, but we didn’t. The content was still the same.

That’s because when we added the element, we copied the content from the first vector into the 2D vector. So when we changed the first vector, the copy did not change. Interesting and pretty important to know, so was nice to see how it works in practice.

Later we will learn about vectors of references and pointers, which will behave differently and where we can do what I thought was going to happen.

My solution was pretty close to the instructors. After all, it was pretty straightforward and not terribly advanced. I did throw in some for-loops when I was displaying the data though, just to see if I remember how to do them.

I also did some experimentation with the size()-method on the 2D-vector to find out how you find the sizes of the internal elements. It worked out to be pretty easy. So that’s why my vector1 contains three elements instead of just 2.

And it worked 🙂

As always all my trial code and challenge solution can be found on my GitHub.

The quiz this time was a little more complex and I had to think more. I almost failed one, but with a little bit more thinking I saved my honor and got a 10 out of 10 score. 🙂 But again I miss a little more feedback than just “Good job!” when you answer correctly.

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s