C++ Programming Tutorial 46 - Working with Arrays

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone welcome back this video we're going to begin our discussion on arrays and how to work with them now make sure you check out the previous video if you were just jumping in here that's going to give you the foundational layer for collections in general that's where we talk about arrays vectors and templatized arrays so highly recommended you check that video out but if you don't want to that's fine let's just jump right in but first check out our sponsor rad studio is the IDE of choice for C++ development quickly build native mobile and desktop applications from a single C++ code base and deploy to Windows Mac iOS and Android with rad studio user interface design has been made easy with hundreds of pre-built components for cross-platform development you can easily integrate with popular source control management systems databases api's and you can make your life easier with numerous third-party extensions let rad studio do the heavy lifting when it comes to C++ development give it a go with a free trial by following the link in the description so what does an array look like well this is a brand new file so you can just start with me typing this out first thing we need to make a main function that's going to look like that and you can put the return 0 if you want but it will implicitly return 0 if you don't put that now in general I just include iostream because I tend to use that in every single example so we're going to create a collection specifically an array and by the way the term collection is a more general term to include arrays vectors and basically anything that can store multiple pieces of data in one variable so if I say collection I could mean any of those things but in this situation we're specifically talking about the boring standard C arrays so let's create one it's going to look a little bit something like this and it's usually a plural name so it's clear that it's a collection and then in here you can put the number of elements you want to be able to contain we'll go with 20 now a cool little trick if you know the data ahead of time you don't have to put this value here you can leave that blank and you can assign it the values so for example we can put a 10 at 13 fifty-four and whatever we want the data to be so this is a great way to basically fill the array but the thing is it'll only fill it to fit the amount of data we have right here so in this case it's going to make the size seven so it'd be the same thing as making the size seven there so this is a great option if you need to basically put all of the data in ahead of time and you know that at compile time maybe there's a select couple of options for some particular thing but usually you don't know all of the data ahead of time so instead you need to basically type in that data there and my battery is low so I should probably get a plug in max always tend to annoy me when it comes to battery because they give you about 30 seconds to get a plug in before it shuts down or at least that's my experience so here's what we're going to do we're going to work with this guesses array as is and then we'll go through an example where we don't pre fill it with data so when it comes to the variable guesses the square brackets are attached to it when we create it but when we reference that array you don't use the square brackets so for example if you wanted to pass this entire array to a function you would just use the keyword guesses so if somebody asks you what guesses is the answer is an integer array but oftentimes you are going to see that integer array with square brackets and some number and this is basically a way to grab an element by the index so for example if we put the value 3 that's going to grab the element with the index 3 this is 0 this is 1 this is 2 and this is 3 so it's going to give us the value 42 and this is an integer so if somebody asked you what guesses index 3 is it would be an integer you may also hear this red as guesses of 3 so basically the square brackets in a number can be read of some number but you can basically call it whatever you want just be clear that you're grabbing the element inside of guesses and because this is an integer we can use it anywhere an integer is expected including inside of an output so we can try it here and we run this thing we can see we get the value 42 so it seems that we did it just like we were expecting we can also modify the element using that index so for example I could go down here and say guesses of three is now going to be equal to three hundred outputting it again you can see that that value will now be updated if first spits out forty-two and then it spits out three hundred so next thing I wanted to talk about is how to work with the array when we don't have that data already there so for example let's say we have guesses and it has the size twenty well now we need to manually put that data in I'm going to talk about filling an array from user input inside of a loop and a couple of videos but for now let's just do a single element you can do that by saying guesses of zero and assigning is some value or you can get it from standard input so for example we can say standard C in sorry and store that inside of guesses zero so now when we output we could output guesses of zero and see what the value is when we run this we put in a value 413 and you can see it outputs 413 so that is how you can assign a value if you want to assign each one manually in code or you can do it from console input using this technique technique technique technique right here now one downside with these stupid darn arrays is that you might have a size bigger than the total number of elements inside of the array so you might have the size twenty but you might only fill five of those spots so sometimes you're required to keep track of how many elements are in the array even though the size is right there so you might do that with another variable usually you wouldn't call it size you would call it something like number of elements and then that could contain five the reason you wouldn't call it size because usually size indicates this value right here it's the total possible size of the array this is going to come up when we're working with loops so now I want to talk about how we can iterate through an array and print out each element so I'm going to assign this a bunch of elements here let's just do it the the nice and easy way like that and now we're going to use a for-loop typically you'll use a for-loop for iterating through collections we'll say int I equals 0 I is less than hmm something let's just call it size and then I plus plus now if you're coming from other languages you might be used to seeing something like guesses dot size or guesses dot length but that doesn't quite work when it comes to C style arrays instead we have to calculate that size so what we're going to do is we're going to create a variable call in size and how are we going to get this value well we actually use the sizeof operator and we pass in guesses that's going to give us the total size which isn't quite what we want but I just want to show you guys we're gonna output that so we got the size and we're outputting it here when we run this we get the value 20 but there's 5 elements so each element is taking up four of these numbers so what are these numbers these numbers are actually bytes so the total size of guesses is 20 bytes in order to get the number of elements we actually have to divide this by the size of one element so we could say / sizeof you could say guesses of 0 if you wanted sorry guess is plural and now we get the output 5 so that's how we can calculate the size there's 5 elements 1 2 3 4 5 now inside of our for loop it's going to correctly reference that size and we should be able to output each element like so when we run this thing you can see we get each element starting with 12 the 5 is not part of that that's the size so that's coming from this line and then it starts on 12 43:23 843 and 23 cool another thing you got to keep in mind is that if the size of the array is larger than the number of elements it's going to mess things up just a little bit so to see this I'm actually going to replace this end line with a tab just to make it a little cleaner keep it all in one line when we run this thing you can see we get 12 43:23 blah blah blah and then we get a bunch of these zeros so that is basically just the default integer when the size is larger and in this case we have a size of 10 so it prints out 10 numbers often times these zeros are going to be useless and you're not going to want to use them so we need a different way to keep track of where to stop size is not going to work in this case so you might have a different variable such as int num it's and that's going to be equal to 5 and then for our for loop instead of comparing the size we can compare to num elements and now when we run it we only get the first five elements even though the size tells us there's ten elements another way you might see is to put a negative one if for example all the numbers are positive and a negative one would be unacceptable then you might use a negative one is basically a flag to say work done and you can case on that and see if the value is negative one not my favorite method I'd prefer to keep track of the elements separately but that is very similar to the way C style strings work with using the null terminating character as basically a stopping point so it's totally not unheard of you might see that in your coding experience so that my friends is my introduction to collections specifically arrays and how to use them hopefully that gave you a really clear picture of how they work and their limitations one more thing I wanted to throw out here is that this size thing although it works it also has its limitations because that's not going to work inside of a function call what do I mean exactly well that's actually the topic of the next video we're going to be talking about how to use arrays inside of functions so be sure to check that out and if you've enjoyed this content do me a favor and click that subscribe button it really helps out the channel and you know it's basically my value indicator so yeah go click alright thanks guys see you in the next one [Music]
Info
Channel: Caleb Curry
Views: 68,835
Rating: undefined out of 5
Keywords: c++, programming, tutorial, working, with, arrays, code, coding, beginner, intermediate, caleb, curry, calebthevideomaker2, c (programming language), array data structure
Id: HpBel7_2k0k
Channel Id: undefined
Length: 11min 2sec (662 seconds)
Published: Mon Mar 18 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.