C++ Reversing an array

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's up guys this is Alex lui introduction C++ what I'm going to show you today is how to reverse an array and this is a second lesson that builds upon our lesson previously on how to swap elements in an array so if you haven't seen that one already please take a look at that lesson first because that will teach you how to swap elements within an array because that's what we're gonna use to reverse the values in an array so suppose that you have the following array where you have five elements going from zero to four and you have the value is 10 20 30 40 50 and you won the resulting array to actually say 50 40 30 20 10 so that 50 starts at the zeroth element 40 at the first 30 and then 20 and 10 so last time that we looked at this array I had you guys swap the first and last elements with each other we're gonna build upon that problem except that now we're gonna do the same thing and swap not only the first and last but then we're gonna move the swap by 1 and decrement the swap on the end by 1 so what we want to do is we actually want to swap the first and last element as you see here and then we want to continue swapping by moving the first one up the last one down so that we can swap 20 and 40 so then you would have 50 40 30 20 and 10 and how do we do this well first we would have a first pointer that will point to the first element and then we'd have a last pointer that points to the last element and what we're gonna do is we're gonna increment this by 1 every time we're done swapping and we're going to decrement this by 1 when we're done swapping so if you look at the code a little bit of code snippet here to swap elements is very easy we were able to do this using our variables with a variable temp correct so if we look at that we declare a temp variable we store 10 in temp and now we can write over whatever is an element 0 with element 4 so that we can say element of 0 is equal to whatever is in 4 and then we copy back temp into element 4 so that's gonna look like that look like that so now we've swapped the first and last and we want to continue doing this for the next element right up until we reach the middle right because we don't want to keep swapping if we keep going and first passes the middle then we're gonna all our swaps are gonna just ruin the whole process so what you want to do is you want to go in loop until these two either meet each other or your first meets up to the last so right now what we're gonna do is we're gonna increment first by 1 and we're gonna decrement last by 1 so that we move our arrows over 1 and this one under 1 and we'll do the same thing that we did here we'll just swap these together okay so we swap again but this time we're actually gonna store our temp variable as 20 okay because we don't longer want to do we don't longer want to hold on to whatever was in 0 these two on the end are done so as you saw here we've incremented first we decrement that last by 1 and now we want to now store whatever is in element 1 in temp and then once we have that in temp then we go along the same routine that we did before by swapping we put 20 over an element 3 or index 3 and [Music] we can copy over the element index 3 into index 1 okay because we got it first we save temp so we have a copy of whatever is in one we're gonna write over what is an element once such as like that right we do that right and then we write over whatever is an element 3 with 20 so if you look at that we made a copy of index 1 in temp then after we made a copy of index 1 we put 40 into index 1 okay and then now we we copy whatever was in temp into index 3 which is what you see on the left hand side with the temp variable okay then we got to move our first and last pointer over but now the point where they meet each other is when we're gonna stop okay this is where we're gonna stop so their condition if we're gonna do this in C++ is that we never want to get to the point where first and last meet because if we get to that point there's no sense in swapping anymore everything else has been swapped so what you want to do is you want to say while your first is less than last in terms of indexes we want to swap otherwise we just break out of the loop and this would be your array in reverse 50 40 30 20 10 okay as we see in here okay so let's take a look and the code okay so we take a look at the code we are going to ask the person to enter a couple numbers our array is gonna be of size 5 from 0 to 4 okay from 0 to 4 and then I'm gonna print out the array before the reversal I'm gonna have a pause there so you can take a look and then here is a meet of the whole thing okay here is a meat of the whole thing so what I want to do is I want to assign first to the zero if index and I want to assign last to the last index which is gonna be size minus one always do minus one because we only go we start at zero okay so while first is less than last meaning that while our first value of index is less than the last index so if we take a look okay if we take a look here we want to always loop while first is less than last so if this is less than last in which in this case it was even if we start at the beginning okay well so is zero less than four yes so we want to swap okay when we get to the next iteration it's two less than there's one less than three yes we want to swap and then it's two less than two no so we're not gonna swap anything okay so this is the condition and we just do something repetitive so that while first is less than last our first index is gonna be our temp then we're gonna overwrite whatever is in the first index which initially is 0 with last and then we overwrite last with temp and this is where we're increasing and moving our arrows first and decreasing moving our arrows last so if we take a look up here see how the arrows are moving so first starts at zero then we could go to one and finally we stop right because eventually it's gonna get to a point where first is not gonna be less than last so we break out so let's take a look let's run this real quick so if I enter 10 20 30 40 and 50 okay you see my reversal my array B for reversal is gonna be 10 20 30 40 50 okay I just press you to continue and then we reverse the array and do 50 40 30 which is what we're doing here now this will work in any situation so in terms of the size of the array so let's say my array size was we'll do a big array we'll do 10 okay let's see it let's say it was 10 the array size was 10 and it should reverse it because we are going to go up to the size which is 10 right but you really get to 9 because you 0 to 9 and here we are again we start at 0 and we're gonna end at 9 and we're just gonna move our arrows 1 going down 1 going up okay so let's take a look so if we take a look here I've prompted myself to enter our 10 numbers and I'm gonna go from 10 to 100 increasing 1 by 10 now mind you I didn't make any changes except change the size to 10 I changed it from 5 to 10 I didn't make any code changes I haven't made any code changes okay and this is the way it should be for most programs you should you should be able to just perhaps even edit one line or with one or two lines and then everything should work if you want to increase your array size so if we take a look at this this is our original array 10 20 30 up to a hundred okay and now if we do a reversal it reverses so 100 90 80 70 ok so there's gonna be a point where the arrows are gonna move and they're gonna eventually gonna meet up and that all this while loop condition is gonna take care of that and again we're just doing this repetitively while swapping okay we're gonna do this repetitively while swapping okay so a couple of things that we did this is really the meat of the whole program okay so if you're taking notes this is the meet of the whole program because this is that what the piece of code that reverses everything let me actually do this this reverses everything everything I've done before is just reading values in and printing amount and everything after is gonna print out the array this is the meat of the whole program this is what moves the first and last pointer indexes and these first three lines here 29 to 31 are the ones that we do when we swap that's the swapping okay once we learned how to swap then we the only thing we have to figure out is how do we iterate through the array so we kind of have to do two loop to iterate errs one forward one backward okay I hope you learned a bit more about arrays next couple of video lectures will be about sorting and one thing you should remember is please understand swapping and this lesson first before you go into the sorting video because now we really get into heavy duty swapping and as well as inner loops okay if you have any questions you could always email me at part-time magic at gmail.com or you can visit me at WWE junk comm thanks and happy programming
Info
Channel: Avexta Education
Views: 6,113
Rating: 4.9120879 out of 5
Keywords: C++ array reversal, C++ reverse array, C++ array iteration reverse
Id: WI54wvu0_4E
Channel Id: undefined
Length: 12min 15sec (735 seconds)
Published: Mon Dec 04 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.