Learn C++ With Me #9 - Arrays
Key Takeaways
Teaches arrays in C++
Full Transcript
hello everybody and welcome to another c plus plus tutorial for beginners now in this video i'm going to be discussing arrays now an array is a way to store multiple elements in the same variable or in the same kind of container or memory location they are extremely important and well you will learn more about them in this video so let's dive in [Music] so the first thing i want to say as we get into this video is that a list is not the same as an array so if you're coming from another programming language and you've used lists before really you've used anything that has the dot append method on it or dot push method or something like that please make sure you are not mixing up what you know previously from other programming languages with what i'm going to show you right here which is an array now they are very similar a list in an array but they also have some very important differences and i just want to make sure that's very clear now if you've never programmed before or you've i guess never gotten this far in programming for this is your first series that you're following along with you've never heard of an array or a list before don't worry i will explain them to you here and you can kind of ignore what i just said but just for anyone coming from another language please make sure you really pay attention here because an array is different from a list so anyways what is an array well an array is really a data structure and that's a really a fancy word of something that allows us to store multiple values that allows us to store multiple values that's what an array does so rather than having to define say five different variables if we wanted to store five numbers we can actually define an array and this array can store all of those five numbers for us so an array kind of looks like this it's squiggly brackets at least in c plus and other programming languages it's square brackets and inside of these squiggly brackets you have a series of elements now an element is really just a value of some data type so it could be an int it could be a float it could be a bool it could be a string it could be a char but in an array all of your elements all of the things inside of them are going to be of the same types they're all going to be int they're all going to be float all going to be bool whatever it may be but here's an example of a really straightforward array we have 1 2 3 as the elements of our array so what this allows us to do is store these three numbers in the order that we have them in so we can maintain that 1 comes before 2 2 comes before 3 and all of that and then what we can actually do here with this array and i'll show you how we do all this obviously is we can access each of these individual values or each of these individual elements so let's say i want to change this value right here i can fully do that that's totally fine i can also change all of the values in the array the one thing that i cannot do though is once i define an array i cannot change the number of elements that are inside of it so i'm going to show you all this i know this is i'm getting a little bit ahead of myself here but whenever you create an array you need to decide the size of the array that you are going to create and once you define that size once you say this array is going to have four elements five elements 27 elements whatever it may be you cannot change that size if you wanted to say increase the size of the array what you would actually need to do is make a new array and you would need to copy the values from your previous array into the new array that then has a larger or smaller size depending on what you're doing anyways hopefully this isn't confusing you too bad i'm just giving you a high level overview before we jump in here i want to show you now how we create an array so the first thing we need to do when we are defining an array is we need to pick what type the array is going to store so this is a little bit confusing because an array itself is a data type like an array is a valid data type but inside of an array we have elements in those elements of our a type so we need to pick what type this array is going to be in this case i'm going to say int so just like you would declare any other variable you just define the type of it so in this case it's going to be int then the name of it and then when you want to make this an array data type you put two square brackets after the name of the variable so in this case i have int arr standing for array and then two square brackets and then i can do an equal sign and i'll kind of code out the rest of this in a second so this is saying we're defining an array this array is going to store ins now if i change this to char what i've done is define a char array this is because i have char here this is an array and so this array is going to store characters if i change this to string same thing this is now a string array so let's make it ins because this is the most straightforward and now what i need to do is inside of these square brackets i need to define the size of my array so i need to pick in one way or another i'll show you the other ways we can do this how large my array is going to be is it going to have 10 elements 5 elements 4 elements how many is going to be inside of it so in this case i'll just say five you need to decide for yourself and note that once you decide this you cannot change it later on it's not a huge deal there's ways around this that i'll show you in later videos but just want to be very clear you cannot increase or decrease the size of an array now it's also worth noting that if i had say some value here like in x equals 5 i could fully put x inside of here and that would mean that my array is now going to be size 5 because the variable x stores the value 5. that is totally valid to do and you'll notice like if you kind of experiment with stuff like this you can do all kinds of things that you might not expect especially if you've never programmed before i could even do something like array and then you know x plus 1 and this now is going to be size 6. so anyways i'll leave x in there just for example purposes and then after we do this we have two options here we can just declare the array like this we can say okay we're going to initialize what's known as an empty array or an array that just doesn't really have any defined values in it yet that is of a size 5. or what we can do is we can actually pick the elements that we want to go inside of this array so if we decide to not pick then we can just do this and we've now declared an array we've told the compiler hey allocate some room in our computer's memory to store these different elements or we can actually define the elements inside by putting these two scroll squiggly brackets and then defining the different elements that we have so in this case i've created an array of size five and it has the elements one two three four five now if i tried to do something like 6 this would be an error the reason this would be an error is because we cannot change the size of this array which we've already defined to be size 5. by adding this element 6 here that just it doesn't make sense we can't have 6 elements in an array that's only supposed to have five so this is how you define an array we've now created an array now it's also worth noting that when you define an array in this way and you actually pick the elements that are going to be inside of it you do not need to explicitly desi defined sorry the size of the array so right here i've kind of gone a little bit overkill i've said all right the size of the array is five and then i've defined an array that has five elements but c plus plus is actually smart enough to know that if you just leave the square brackets like this and you don't actually put a size inside of here but you do actually declare the elements in your array it's able to just count how many elements are here and automatically create an array of that size so now i can do any number of elements that i want and c plus plus will what's called i believe infer the size it's either imply or for i i always forget which the meaning of those are uh but i believe it will infer the size of this array by looking at the size that you've defined right here so we'll just make this array size 6. now there's one more kind of sneaky notation here that c plus plus has you actually don't need the equal sign you can write it just like this i don't know exactly why c plus plus has this but this is the exact same thing as having the equal sign in so just figured i'd show that to you like these are completely equivalent having the equal sign or not having the equal sign now there's a lot of rules here i apologize we'll get into some more interesting stuff in a second but if you decide to not actually uh define the elements in the array then it is required that you add the size of the array i cannot just do this this is invalid i do need to tell c plus and c plus plus in one way or another what the size of the array is going to be so if i say int r a r r and then of size x and we just do this so it makes it equal to 2 3 4 5 and 6. what i can do now is show you how we can actually access elements in this array first of all though let me actually show you what happens when we just print out arr so c out arr let's go here to the compiler let's compile and let's run and notice we get some random gibberish showing right here so why is this the case well this array right here what this really is is this is a location in our computer's memory in our computer's ram right so whenever you run a program like this like c plus plus program your program is loaded into your computer's memory and when you define an array what happens is you're saying okay i want somewhere in my computer's memory these elements to be stored or i want to allocate space for this array so when you just print out arr like this what it's actually giving you here is the memory location of this array now this doesn't really have to mean anything to you but this is kind of where in your computer's memory this array is actually stored so you can't actually just print out like all of the elements in the array as you might expect at least not in this way so if you want to access specific elements from the array i'm going to show you how we can do that so obviously it makes sense that we have these five values here we want to be able to access each of them i want to do something with the elements in this array so there's something called positions or indices of elements in our array now the indices or indexes start as following we have index 0 which is the first element in our array so if i want to access the very first element in my array what i actually do is i write the name of my array i write my square brackets and then i write the position or the index of the element i would like to access in this case element zero what this says is give me the first element that is in the array i know it seems counterintuitive but computers kind of always start counting at zero so we would get element two here when we use index zero then the second element is going to be index one the third element's gonna be index two fourth three fifth four and then i guess six would be five although we cannot use an index five here because we're trying to access an element that does not exist right if i try to access position five that's trying to get the sixth element in this array this array only has five elements in it and so this is gonna lead to some kind of strange behavior which i will show you later on so we will continue in one second but i need to quickly thank the sponsor of this video and this series which is alco expert algo expert is the best platform to use from preparing for your software engineering coding interviews they have the highest quality coding interview practice questions with over 140 of them on the platform with that said check out algo expert from the link in the description and use the code tech with tim for a discount on the platform anyways let me just show you here when i do array and then at index 0 this is going to access for me the first element which is 2. so it actually gives me this element 2 like that all right so let's uh change this now to just be say position 2 or index 2. now a little quiz for you which element is this going to print out when i run this program give it a guess okay so let's compile let's run and we get four the reason we get four is because we're accessing the second position which really is the third element in the array now a little bit of a trick here if you want to access the last element in the array this is always the size of the array minus one index so if your array has size five the last index in the array is always that size minus one which in this case would be four so that's how you access the different elements that are inside of the array now if you want to change elements in an array you can do this as well so i can access say the very first element and i can change this to be say 10. so now what happens after i add my semicolon here is i say all right array at position 0 or at index 0 i want to change this to be the value 10. so now if i print out array at index 0 you're going to see as it would make sense that this now is going to be well value 10. so that's how you access and i guess change the elements in an array awesome now let me show you what happens when we define an array but we do not give it the unique elements right so if i just define an array and i say this is of size 5 what happens now when i print out the different indices of this array so if i say c out array 0 and then i'm just going to kind of copy this down here this isn't the best way to do this but for now this makes sense let's print out all the different indices and let's do our kind of end l at the end of all these probably should have done that beforehand but it's okay so end l and l okay so what do you think is going to print out just give it a guess you probably will have no idea but let's compile the program and let's see what we get again notice we didn't give the array its elements already we just initialized that we have an array of size five so compile and run and notice we get some completely random values printing out here so this is kind of what happens in c plus plus when you do not define what is being stored in let's just call it a container or in a memory location you're just going to get a bunch of random gibberish you have no idea what's going to be printing out there's no way for me to tell you why exactly we got these values it's just because at this point in time this is what is stored at this location that we're trying to access in our computer's memory you don't really have to understand this the whole point is to realize that if you do not initialize the values of the array you have no idea what is going to be stored in it so just keep that in mind right now even same thing here let's see what happens when i try to access a position in the array that actually doesn't exist so i try to access say position seven obviously our array is only size five so there is no index seven when i try to do this let's see what happens let's compile let's run again we get some random value i can't tell you why we're getting that all i can tell you is that when you try to do this you get something completely unexpected that you cannot predict so hopefully that's somewhat clear that is how you access the elements that is how you change the elements and that is what happens when you kind of create an array but you do not give it the actual values inside of it now of course once we've defined the array like this what i can do is i can say well ar is equal to and then i can set it equal to whatever array that i want that has five elements inside of it so i could say one two oops let's get our commas right two three four five and then if i see out here arr at and let's just go index four let's see what happens so let's compile and oh what is it saying right here uh error assigning to an array from an initializer list okay so it's actually a good error to run into this is something that i didn't see when i was looking at arrays before doing this video so apparently you actually cannot do what i just tried to tell you that you could do what this is saying is that you cannot assign to an array from an initializer list so this is what you call an initializer list don't worry about all the fancy names and it's saying that since we've already declared this array we've said that we have this array i now cannot set the array equal to and then this kind of array right here i cannot do that it's not allowed in c plus plus so if i did this right define the array in this way and then i want to set all of its elements the way that i would have to do it is by looking at each of the specific indexes so if i want to set my array to have say one two three four five as its different elements i would actually have to do the following i would have to say array zero is equal to one and then array one is equal to two and then so on and so forth i'm not gonna do all of them but i'll show you now that we will get element one when i actually kind of declare or set the elements in this way so let's compile let's run and see we do get one now obviously uh this is not this doesn't make too much sense to do it this way right like this is no better than just declaring a bunch of different variables we still have to write all of this code to set all of the elements for our ray now i'm going to show you in the next video kind of some tricks to do this a lot faster right now we just don't have all the tools that we need to really make perfect use of an array but i wanted to show them to you before because then when i go into the next videos you'll see why the things that i'm showing you are like really important if that makes any sense but anyways that's kind of the idea behind an array so there's a few more things i'm going to show you with an array and the first thing that i want to do here is just actually define a new array so let's make this 4 5 4 5 6 7. let's also make an array that's like a string array so let's say string arr2 and then this can be equal to or we can do it in this notation we can say tim and then comma is and then great just to show you that we can have an array like this we also could have an array that has float values so float er 3 equals or we could just do it this way as well 1.1 1.2 maybe negative 0.9 whatever it may be you can define arrays in this way okay so sorry for the abrupt cut i forget what i said before i made the cut here so i'm just going to quickly say yes we can define arrays of all different types you can see examples here that i've written anyways what i want to show you now is how you get the size of an array if you're coming from another programming language you may think this is very trivial very easy to do in a lot of programming languages you can use something called dot size or dot length to actually get the size or the length of the array the way that you do this is you would write the name of the array in this case maybe arr or ar2 or air r3 then you just do dot size or dot length or sometimes there's a function say in python you have this function called len that gives you the length of whatever structure you pass to now in c suppose is not so easy c plus plus makes things a little bit more difficult now to get the size of an array is kind of a two-step process there is this function in c plus plus called size up now notice it's kind of highlighting a fancy color when i write it now what sizeof does is give you the size in bytes of an array now bytes when i say bytes this is like how much room on your computer this array is actually taking up it's not the number of elements in the array it is quite literally the size of this array in memory like how many bytes are being used now the only good thing about this though is that if we know the size of the array in total we know the number of bytes that are being used if we're able to figure out how many bytes are being used for one element in the array we can actually figure out how many elements are in the array so typically when you want to get the size or the length of an array you're asking for the number of elements in the array that's what you want to know so in c plus plus if you want to get this what you can do is you can say something like co and then size of and then you can say arr so this is actually the array right the array object and then you can divide this by the size of one of the elements in the array so if you know the array has at least one element you can say all right well the size of array divided by the size of array and then this is going to be at index 0. so this tells you the size of the very first element and the size of the first element is how much all of the other elements take up as well so all of the other elements will have the exact same amount of space or exact same number of bytes that are being used by them so by simply dividing the size of the entire array by one of the elements this tells you how many elements are in the array now there's a few other ways to determine the size as well but they're a little bit beyond the scope of this tutorial right here i don't want to get into anything too confusing so i'm not going to cover them but let's quickly just run this here and see what we get and we get 4 right so the reason we get 4 is because the size of this one element is something the size of this entire array is something we do the division and since the size of all the elements is the exact same we know we have four elements just some basic math now let's just see out actually the size of just the array so we can kind of see what's happening here and let's see out the size of the array at index or position zero so let's go here let's compile let's run and oops i didn't mean to i didn't want to do them beside each other let's do this again okay and l and and l okay let's try this now clear compile run we get 16 4 and then four so this is perfect this is exactly what we wanted this is telling us that the size of our right is 16 number of bytes that it's using the number of bytes that one element is using is four and so we have four elements in the array now let's just see what happens if we add a very large number as the first element in our array let's see if this still works i want to compile and run and notice we still get the exact same thing so it uses four bytes to represent an integer so hopefully that kind of makes sense i can understand this part might be a little bit confusing especially if you've never programmed before again size and bytes you want to figure out the number of elements you take the size in bytes of the array and then divide it by the size in bytes of any number or any element story of the array that tells you the number of elements that are in it so i think that's all i really wanted to show you for arrays again just keep in mind there's many different ways to define and declare them whenever you create an array you cannot change the size of that array i cannot add another element into this array i would need to create a new one um you can change the value of positions in the array and arrays start indexing at zero so the very first element is zero then one then two then three again the trick is the number of elements in the array minus one is the very last index or position that you can access in the array so hopefully this was informative hopefully you guys learned something from this if you did make sure you may like subscribe to the channel and i will see you in another youtube video you
Original Description
Welcome back to the C++ for beginners tutorial series! This video will cover arrays! An array is a way to store multiple elements in the same variable, and they are extremely important to know how to use when coding. We'll create arrays, see the different types of arrays, distinguish between an array and a list, and more.
💻 AlgoExpert is the coding interview prep platform that I used to ace my Microsoft and Shopify interviews. Check it out and get a discount on the platform using the code "techwithtim" https://algoexpert.io/techwithtim
🔍 Playlist: https://youtube.com/playlist?list=PLzMcBGfZo4-lmGC8VW0iu6qfMHjy7gLQ3
⭐️ Timestamps ⭐️
00:00 | Introduction
00:29 | Array vs. List
01:15 | What is an Array?
03:20 | Creating Arrays
09:03 | Indices and Accessing Array Elements
11:32 | Changing Array Elements
12:04 | Default Array Initialization
13:26 | Array Errors
16:06 | Different Types of Arrays
16:55 | Size of Arrays
◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️
💰 Courses & Merch 💰
💻 The Fundamentals of Programming w/ Python: https://tech-with-tim.teachable.com/p/the-fundamentals-of-programming-with-python
👕 Merchandise: https://teespring.com/stores/tech-with-tim-merch-shop
🔗 Social Medias 🔗
📸 Instagram: https://www.instagram.com/tech_with_tim
📱 Twitter: https://twitter.com/TechWithTimm
⭐ Discord: https://discord.gg/twt
📝 LinkedIn: https://www.linkedin.com/in/tim-ruscica-82631b179/
🌎 Website: https://techwithtim.net
📂 GitHub: https://github.com/techwithtim
🔊 Podcast: https://anchor.fm/tech-with-tim
🎬 My YouTube Gear 🎬
🎥 Main Camera (EOS Canon 90D): https://amzn.to/3cY23y9
🎥 Secondary Camera (Panasonic Lumix G7): https://amzn.to/3fl2iEV
📹 Main Lens (EFS 24mm f/2.8): https://amzn.to/2Yuol5r
🕹 Tripod: https://amzn.to/3hpSprv
🎤 Main Microphone (Rode NT1): https://amzn.to/2HrZxXc
🎤 Secondary Microphone (Synco Wireless Lapel System): https://amzn.to/3e07Swl
🎤 Third Microphone (Rode NTG4+): https://amzn.to/3oi0v8Z
☀️ Lights: https://amzn.to/2ApeiXr
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Tech With Tim · Tech With Tim · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
A* Path Finding Algorithm(Visualization)
Tech With Tim
Python Programming Tutorial #1 - Variables and Data Types
Tech With Tim
Python Programming Tutorial #2 - Basic Operators and Input
Tech With Tim
Python Programming Tutorial #3 - Conditions
Tech With Tim
Python Programming Tutorial #4 - IF/ELIF/ELSE
Tech With Tim
Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Tech With Tim
Python Programming Tutorial #6 - For Loops
Tech With Tim
Python Programming Tutorial #7 - While Loops
Tech With Tim
Python Programming Tutorial #8 - Lists and Tuples
Tech With Tim
Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Tech With Tim
Python Programming Tutorial #10 - String Methods
Tech With Tim
How to Overclock a NVIDIA GPU
Tech With Tim
Python Programming Tutorial #11 - Slice Operator
Tech With Tim
Python Programming Tutorial #12 - Functions
Tech With Tim
Python Programming Tutorial #13 - How to Read a Text File
Tech With Tim
Python Programming Tutorial #14 - Writing to a Text File
Tech With Tim
Python Programming Tutorial #15 - Using .count() and .find()
Tech With Tim
Python Programming Tutorial #16 - Introduction to Modular Programming
Tech With Tim
Python Programming Tutorial #17 - Optional Parameters
Tech With Tim
Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Tech With Tim
Python Programming Tutorial #19 - Global vs Local Variables
Tech With Tim
Python Programming Tutorial #20 - Classes and Objects
Tech With Tim
Cool VBS Script to Prank Your Friends!
Tech With Tim
How to Overclock an AMD GPU
Tech With Tim
Best GPU'S For Mining Ethereum (2018)
Tech With Tim
Recursion and Memoization Tutorial Python
Tech With Tim
Ethereum Mining Rig - Hardware Guide
Tech With Tim
Pygame Tutorial #1 - Basic Movement and Key Presses
Tech With Tim
How to Install Pygame (Windows 8/10)
Tech With Tim
How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
Tech With Tim
How to Mine Ethereum 2018 - WORKING (Super-Easy)
Tech With Tim
Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Tech With Tim
Pygame Tutorial #2 - Jumping and Boundaries
Tech With Tim
Pygame Tutorial #3 - Character Animation & Sprites
Tech With Tim
Pygame Tutorial #4 - Optimization & OOP
Tech With Tim
OBS Studio Tutorial - Best OBS Settings
Tech With Tim
Linear Search Algorithm - Python Example and Code
Tech With Tim
Make Any Mic Sound AMAZING! (WITH OBS)
Tech With Tim
Binary Search Algorithm - Python Example & Code
Tech With Tim
Pygame Tutorial #5 - Projectiles
Tech With Tim
Pygame Game - Mini Golf
Tech With Tim
Pygame Tutorial - Projectile Motion (Part 1)
Tech With Tim
Pygame Tutorial - Projectile Motion (Part 2)
Tech With Tim
Pygame Tutorial #6 - Enemies
Tech With Tim
Pygame Tutorial #7 - Collision and Hit Boxes
Tech With Tim
Pygame Tutorial #8 - Scoring and Health Bars
Tech With Tim
Cloud Mining vs. Hardware Mining - 2018
Tech With Tim
How to Install Pygame on Mac OSX (Fast-Simple)
Tech With Tim
Pygame Tutorial #9 - Sound Effects, Music & More Collision
Tech With Tim
Pygame Tutorial #10 - Finishing Touches & Next Steps
Tech With Tim
How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
Tech With Tim
How to Create a Button in Pygame [CODE IN DESCRIPTION]
Tech With Tim
Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Tech With Tim
Pygame Side-Scroller Tutorial #2 - Random Object Generation
Tech With Tim
Pygame Side-Scroller Tutorial #3 - Collision
Tech With Tim
Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Tech With Tim
How to Create A Message Box in Python - Tkinter
Tech With Tim
Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Tech With Tim
How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
Tech With Tim
Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Tech With Tim
Related Reads
📰
📰
📰
📰
Traversal, Linear Search, Swapping, Shifting & More (Leetcode Code example)
Medium · Data Science
The Rain Knows the Shortest Path
Medium · Programming
Data Structures & Algorithms for Mobile App Developers
Medium · Programming
Data Structures and Algorithms Deep‑Dive — Real-world Applications of Hash Tables (Chapter 3…
Medium · Programming
Chapters (10)
| Introduction
0:29
| Array vs. List
1:15
| What is an Array?
3:20
| Creating Arrays
9:03
| Indices and Accessing Array Elements
11:32
| Changing Array Elements
12:04
| Default Array Initialization
13:26
| Array Errors
16:06
| Different Types of Arrays
16:55
| Size of Arrays
🎓
Tutor Explanation
DeepCamp AI