Well, hello and welcome to the first part of my C++ Data Structures and Algorithms series inside of
my C++ course.
In this part, we are going to just make sure we completely understand arrays and then I'm going to
move into understanding data structures as a whole.
And this part of my C++ course is going to cover all of the most common algorithms and data structures
you should be aware of.
And I have a lot to do.
So let's get into it.
OK, so here are the headers that we are going to need here.
And basically an algorithm.
It's just a series of steps that we are going to take to perform a calculation or solve some type of
a problem in a data structure is going to define how data is arranged in memory and how you can interact
with it.
And data structures normally allow for input, deleting and searching, and we will cover that in this
video.
And in C++, arrays can be declared either dynamically or statically.
And I'm going to make sure you're 100 percent, you know, arrays and especially multidimensional res.
So now inside of Main here, what I'm going to do is to find some static arrays.
Now a static array is going to have a fixed size.
And this is an example of what we call a non initialized array.
And here is another static fixed size array.
Let's just call it array two and let's say it has five values.
This is an example of where we would have and initialized array.
So let's just go.
There is not initialized and there is an initialized.
There we go.
Now, on the other hand, you're going to be able to alter dynamic arrays size.
At runtime, so let's go and create those so we have what do we have here?
We have static arrays, so let's go and make sure we're aware, not static, and then we have dynamic.
So how do we define this?
Well, we use a pointer.
So I'm going to say pointer is equal to new and and we will define it as five size.
And I'm going to say four and I is equal to zero while I is less than five.
I am going to go and initialize this guy.
So how do we initialize it?
Well, a pointer like this and I and I'm going to say that I just want it to be whatever the index value
is.
And there we go.
Now, it's very important whenever we go and define a dynamic array like this, that we d allocate memory
allocated for it.
So to do that, to delete like this and there we are now, we have de allocated that memory and it's
been freed.
Now I can come in again and create a new one.
And this time I'm going to say that I want 10 spaces available for us to use and I'm going to come in
here and just copy this part right here.
No reason for me to type that all out again.
So there we go.
And let's say that we want to just go and place values in six of those.
No problem, we can do so.
And then afterwards, let's say that we want to cycle through and we want to output those six values.
So, again, we're going to say six here and then we'll just change what we're doing here.
So I'm going to.
Well, let's get rid of this part right here.
And I'm going to say see out like this, a pointer exactly like that.
And then and l like that.
And then what do we do after we're done with an array?
We allocate the memory.
So make sure that you do that, that we are.
And then we can come in here and we can run this.
You can see one one one one one one one.
Why is that?
Because I didn't put I inside of there, so let's throw it inside of there and let's run it again.
And now you can see we got zero one, two, three, four or five, precisely what we were looking for.
Now, there's a lot of confusion whenever it comes to multi dimensional arrays.
All right, so we are going to cover those in massive detail here.
So what I want to do here is I want to create a static, multidimensional ray that has three rows and
four columns, so let's just call this an array.
And what did I say?
I want three rows.
You define how many rows first and then you define how many columns.
And I'm going to say it's four.
There we go.
So we have three rows, four columns.
And if you add another one after that, what I like to think of is it's like a spreadsheet.
So we have three rows that is going downwards.
You create a new row and then you have columns after that, four columns.
So it's going to be one, two, three down.
Maybe I can do something here.
So you're going to have one, two, three down.
So there you go.
That is what the three is making.
And then the four part is saying that there is going to be four items inside of each row or four columns,
however, you would like to think of it that way.
So there you go.
That's what the three and four do.
And then I like to think of it as like a spreadsheet and like there's an additional sheet full of information
and that's what the two is.
So the two part is saying that there's actually two of them.
All right.
Two blocks.
There are three rows and four columns in size.
Let's go and mess around with it so that it makes more sense.
So what I'd like to do now is initialize my multidimensional array.
So I'm going to show you how to do that.
So I'm going to say, let's just go and create a new one.
So I'm going to say an array to and this is going to be three rows and two columns.
Repeat it after me.
You know, stick a little bit better then.
And then what we're going to do is define the values that go inside.
I'm just going to keep it nice and simple.
One and two and three and four and five and six.
Five and six.
Exactly like that.
And then don't forget your semicolon here at the end.
Now, what I'm going to do is go see out and of course, I'm going to be able to access this information
using indexes.
So zero and one.
That's what I'm looking for.
So I'm looking for this one.
This is the zero.
And then this would be zero and this will be one.
All right.
So the zero part here targets this.
The one part here then targets this, the next item inside of that array.
So we should be expecting two to come out here whenever we run this.
And let's see if that's what we get.
And you can see, indeed, that's what we got, we got an extra two right there.
All right.
So that's how we're accessing those values.
But what if you want to be able to access all the values?
Well, you're going to say for int like this, J is equal to zero.
And I'm going to say while J is less than three.
So I'm working with the rows here first and J we have to increment J obviously.
And then let's just go and copy this so we don't have to type it out all over again.
So there we go, paste that in there and I'm going to indent this and I'm going to change this to K
so all the code is heavily commented and available in the resources directory.
So whenever we're cycling through multidimensional race, we are normally going to cycle through the
rows and then the columns in each once will get row and then we'll go column information, you know,
move left to right exactly like that.
OK, so we have that all set up and then we just simply come in and go Amaury two and J and K like this,
and then we can go and print all of that information out.
Let's just do it all on one line here just to be different.
And if we run it, you're going to see that we print it out one, two, three, four, five and six
exactly like we would expect.
OK, so what's up next?
Let's create a 3D multidimensional, right.
So how do you do that?
Well, this time I'm going to say that I want this to be string's because I want this to demonstrate
what it's doing.
So we have rows.
Then why do we have we have columns and then what do we have?
We want two of those.
So have those set up.
And then let's also come in and initialize them.
Then this causes a little bit of confusion sometimes.
Let's go throw the semicolon here first.
All right.
So I think it causes confusion based off of structure, how we use these curly brackets.
I think it's much more understandable to throw a curly bracket right there and then define everything
inside of it.
So what are we going to do inside of here?
We have another curly bracket and this is going to represent the upper left hand corner.
And this is going to represent remember I said spreadsheet.
This is going to represent the second part of our spreadsheet.
So we have that set up.
And this is first spreadsheet.
Second spreadsheet.
That's what the third value is.
And throw that like that, that like that.
And then as we go down in our first, we are going to have a deal with our zero row items here.
So I'm going to come in and I'm just going to change this to a one and this to a two.
You can see the columns go down.
They continue working like we expect.
And two, exactly like this.
Well, then this ends.
And I want a new grouping here.
So what am I going to do?
I'm just going to copy this.
So I'm going to copy that and paste that in, because as you're going to see, this is the major part
that's going to change.
These are going to represent the one rose.
So I can just change all of these to one exactly like this and like this and like that.
And then the columns stay the same.
And then the second page are three dimensional part also stays the same.
And there we are.
We just created a three dimensional array.
Now, if we want to go and print this out, it's going to be slightly more complicated.
Not much.
And we can just what are we going to do?
We're going to do rows and columns and then are multiple sheets.
So through this inside of here, let's just change this to I and I and I.
All right.
And we're going to change this to two.
We have two rows jump down here and it didn't automatically put my curly bracket in there.
So let's go like this.
And then what are we going to do after that?
And look at that, then do it again.
Let's close out there and we're going to be dealing with three pieces of information here.
So what's that mean?
It means we need three four loops I.
J and there are three columns as we see.
And then with this, I'm going to change this to K, so change all those two K and this 2k and then
we only have two individual parts here.
So let's throw this inside here.
Change that.
The two.
Very simple.
So it's going to do each of those rows, print out the columns and then for the columns it's going to
print out the second part.
So I'm going to use print F here this time and just make sure that you have all of the header files
are exactly the same just because it's going to be very easy to print.
This so print f like this, and then I'm going to say that I want a integer inside of here like this,
and then I'm going to say equal to and I will get my string value and I'm just going to throw a new
line inside of there.
Again, just to keep this simple, I want I to go into this first position here and I want J and then
I want K and then what do I want?
Well, I want to go and get each of the individual array pieces.
So this is going to be I and J and also K.
Now, if I want to be able to print this out, I have to change this into what's called a C string and
then it will represent it as a string and print it out.
And if I run this, you're going to say that we got exactly what we were looking for and everything
is placed exactly as we would expect.
OK.
So there you go.
Rundown of arrays as well as multidimensional res.
Now, what I want to do is ease you into the understanding of data structures and algorithms.
And then the next part of the data, we're going to get a lot more complicated.
So I'm actually going to create a class that I call array, and I have to think about what I want to
have inside of it.
Well, I'm going to have some private parts inside of here and I'm going to have a array inside of it.
And there we are.
So it's going to have 50 spaces.
And I'm going to say that it's going to have an array size.
And then whenever it starts off, I'm going to say it has a default array size of 10.
And what am I going to have public inside of my array class?
Well, I'm going to have a whole bunch of awesome little functions.
So let's say I want to automatically populate my array.
Well, I'm going to say generate random array is going to be OK.
Name for a function that generates random array values.
What else am I going to want to do?
Let's say I want to be able to display my array information and data in a way that looks kind of nice.
What else?
Well, let's say that I also want to be able to find out what value is at a specific index.
Well, and do that as well.
It's going to return an integer.
I am going to also want to find out if something is in an array and a specific value.
So I'll say, is this value in the array?
I could do like search or whatever, but I'm just going to do that.
All right.
Let's say we also want to be able to delete values.
These are things that you should be able to do with all data structures.
Well, maybe not display, but all the rest.
What else?
Well, we might also want to be able to come in and append a value at the end of our array.
So there you go.
Those are some good functions that a data structure very well should have.
Don't forget your semicolon.
All right.
So after we define those, how are we going to come in here and get our random values?
Well, I'm going to say that I want to generate random array.
There it is.
And what is it going to do?
Well, it is just going to go and get a for loop and int I is equal to zero.
And while I is less than the array size, I want to continue incrementing this, of course, and then
I need a random value generator.
Do you guys remember what you do when you need a random value generator?
Well, you need a seed, so I am going to put this in Maine wants to get random values.
If I would have stuck this up inside of the function, guess what?
Your values wouldn't be random anymore.
You can go and find that out by putting it in the function instead.
So I'm going to go like this.
There we are.
And another thing you're going to be dealing with Boleyn's and I like seeing true or false instead of
ones and zeroes.
So do you remember how to do that for Alpha like that?
So there you go.
That's just a couple little silly things I decided to do.
OK, so here is our generate random array.
And now what we can do is just say the array like this and I is going to be equal to and loops, Rande.
And let's say that I would like it to generate so that it looks nice values between 10 and 50.
So I'm going to say 50 plus 10.
Exactly like that.
And there you go.
That's all I need to do.
Now, I generated some random array values.
What else I like to do?
Well, I said that I'd like to be able to display this information in a nice way.
So I'm going to say void and array again and display like this.
And let's just say I want to go and print out a bunch of dashes, so let's throw in 10 dashes.
Looks pretty good.
And after I do this, I then want to go and cycle through and output the indexes as well as the values
at the index.
So which is equal to zero like this.
Well, I is less than array size and increment my value for I.
Now what would I like to do inside of here.
Well I'm going to say setout and I'm going to go the array.
Well actually you know what, I'm going to put a bracket inside here first.
So I like this.
Put a space and then I'm going to get my index for the array item, throw that inside of there and then
another space and then like this and like that.
I'm not going to put endow on there because then I want to put the value inside of there.
So I'll do this and then go the array and get the individual values at that index like this.
And there we are and we'll throw that there and then do an end.
All right, then what do I want to do?
I want to do the same thing with setout.
So I'm going to have these all be divided up.
It's not going to be the fanciest looking interface you've ever seen, but it gets the job done.
OK, what else did I say?
So I'm generating random values I want to display and I want to find out what is at a specific index.
How do we do this?
Well, let's go on here and an array.
And I'm going to say at and it's going to be a specific index.
What would I like to do?
Well, first off, I want to make sure that the index value is less than my array size.
And if it is, then I'll just return it.
So I'm going to say if index is less than array size, well, then no problem.
We will return whatever is at that index.
No problem.
There we go.
And we just created it.
All right.
Otherwise, what I want to do is I want to basically create a message that it doesn't exist so long
as I see out and I'll say that index doesn't exist.
And then let's say that we want to return like negative one or something.
Just return something.
OK, so there we go.
That is how we can get an index value or a value add a specific index.
What else that I want to do here.
So I also wanted to check if there was a value in the array and it's going to spit out true or false.
So come in here and let's go like that and then like this.
So how are we going to check this?
Well, I'm going to say that I want to create a boolean inside of here.
I'm going to call it in array like that, and I'm going to set it as false by default.
Then I'm going to cycle through all of my array values.
And I is less than array size.
And that we are.
And we go, oops.
Like this, there we go.
All right, so I'm going to say, if thou is equal to the array and checking all those different guys,
and then if that is true, I'm going to mark in array to be equal to true.
Very simple.
And I've searched through everything here and it's given me an error message because I forgot to do
this.
There we are now.
They all go away.
OK, so it starts off as false.
It's only going to be set to true if the value is found and then at the end it's going to return the
true or false value.
All right.
What's next?
I thought Ziggler little lines came up there as an error.
What are we going to do now?
Well, we want them to be able to delete values.
So also come in here and allow for that.
So like this and array and there we go.
So what do I want to do here?
Well, I want to make sure the index is less than my array size and that the index that they are asking
for is either going to be greater than or equal to zero.
So I'm going to say if and index less than array, size and index is greater than or equal to zero.
So if those are true, well, then what I want to do is I want to go to my index value that that they
want me to get rid of.
And what I'm going to do is just get the value that comes after it in the array and move it up.
And then I'm going to go to the value that I just moved up and I'm going to get the value after it and
move it up.
And that is going to make these array items disappear.
All right.
So if you think of it like this, let's say I had one and two and three and the and let's do four.
And I say that I want to get rid of this value.
Well, what I do is I take this value and move it up into this index.
So this essentially becomes three.
Then I jump to this one and I say I want this value to be where that is.
So that becomes four.
And then why I'm going to do is just decrement the size of our array and this is all we're going to
have left.
All right.
Just moving stuff up inside of the array.
Pretty simple stuff.
So to do so, I am going to create a for loop.
So I'm going to say four again and is equal to.
But this time we're going to start at the index because we're not interested in anything that comes
before that.
We are only interested in what comes after the index.
So we're going to start there.
I must say I less than and again, because this is a zero index array, we can take one off of our array
size I that we are like this and then we just say the array at our index is going to now be equal to
whatever the array item after it is.
So we'll just say or I plus one.
There we are.
And remember, don't forget the final part.
If or after we do all this we have to decrement whatever array size is.
All right.
So there we deleted, we did all kinds of stuff.
And on the last say I'd like to do is to be able to append values at the end of our array objects.
So again, array like this and jump in here like this.
And how do we do that?
Well, first off, we want to check if we have any more room in our area.
And we said there are array size or maximum size is going to be 50 because that's what this array here
is, say 50.
So I'm going to say Iev array size is less than 50.
Well, in that situation, we can append a value at the end.
So I'm going to go the array like this and array size because it's zero index again.
And Val, this is where we want to store our value.
And don't forget, we increased our number of items in our array, so increase our array size.
And in the situation in which we don't have any more room, we're just going to say see out like this
and we'll say error like that.
No more room in array just to be nice and simple and new.
All right, so we have all these set now let's test this, so I'm going to come down inside of here.
You know what?
I can easily just get rid of all this stuff because we don't need it anymore.
Like I said, it's in the resources directory.
So there we are.
And let's start messing around with our array class and see how it works for us.
So what I want to do is I want to create an array object with random values and then display them so
I can just say array my array.
You are I can now say my array and generate random array like this just to throw some values inside
of it.
And then I can say my array and display and there that is.
Ah, there you go.
And if we run it you're going to see a generated aura and through some random values inside of it.
All right.
So that's pretty useful.
So let's come in and maybe want to do another one so we can of course, do that.
We're just going to have to change this to array, to retool, to to do that, run it.
And now we have two arrays that we're working with.
OK, let's continue testing everything else we've done.
Let's say we want to get the value that is at index three.
Well, let's just get rid of this because it's kind of getting in a way.
I just wanted to do something there.
All right.
So what I want to do here, well, I'm going to get the VALUATE Index three so very easily just do this
my array, and that is the name of it.
And let's say we want to add index three and there we are running again.
Index three is twenty three.
The value it returns is whoops, you have to trust me here 23 say there it is and there we go.
All right.
What else we would like to do.
Well, I'd also like to check if a value is in the array.
Well, you know what?
I'm actually going to append a value first.
So I'm going to say my array dot append and I'm going to put 99 inside of there a value that I know
is not inside of there.
And then after I do that, I am then going to check if it is inside of the array or not.
So let's just go and copy this so I know 999 is inside of there because I just stuck it inside of there.
So I'm going to say that I want, let's say, 99 in array.
So there that is and like this, like that.
Loops like that.
And then my array and this one is going to be in array and we're looking specifically for 99.
And because we change this, it's going to come back as true or false.
So there we are and we were on it.
And you see 99 in array comes back as true.
And that's pretty cool.
And this is before we put the 99 inside of there.
OK, so we'll go and display it again here.
Just give us a second.
All right.
What else would I like to do?
Let's say I would like to come in and delete an item song like this delete index.
And I'm going to say let's say I want to get rid of index two.
I can do that.
And I think that's all we're going to do.
So let's say they got tested everything out here so we can go call display again and run it.
And there you go.
You can now see the 99 that is down inside of there.
You can also see that I went and got the authority out of there.
And if we look at these different guys I deleted from index to.
Right.
So there's the twenty eight and right there and there's so this should be forty seven and thirty.
If we delete the twenty eight and you can see forty seven and thirty are right there, the twenty eight
has been deleted.
All right.
So there you go guys.
A rough sort of run right into arrays and messing around with a simple data structure.
And this data structure is going to get a lot more complicated in the next video as I further explore
data structures and algorithms.
So, like always, please leave your questions and comments down below otherwise the next time.