Now, however, I don't know what the size is because the user is giving me the size. So, I'm just going to use "size minus 1." You see that?
So, for the first position of my flipped array, I want to use the last position of my old array. See how it gets the last number and puts it in the first position of my new one? OK. But, I want to do this for every single one.
So, instead of using 0, I'm going to use I. On the very first run through, I is 0, so that is going to work.
But what about the second one, when I is 1? When I is 1, then it's still going to take that very, very last spot. But, wait a minute, I don't want the very, very last spot. I want the next to last.
So, what if I come over here, at the end, and I say [? "also, ?] minus [? I?" ?] So, I want to minus 1. OK, because we're zero based. But then I also want to minus I.
So, let's say the size is 20. 20 minus 1 is 19. OK, and then 19 minus 1 is 18. So, when I is incremented up to 1, this will be position 1. And numbers at position 18 is going to be dropped into it.
And then when I is 2-- so I is 2-- then it's going to be 20 minus 1 is 19, minus 2 is going to be position 17. Then it's going to be position 16, then 15, then 14.
You see how it's going down like that? So I'm starting at the end, and I'm going down, all the way down. So that's how we flip the numbers, right there. OK, so that's the logic of flipping it.
And then, once I have it flipped, I need to return a pointer to the array. And, well, guess what? Flipped is a pointer. You see that? It's a pointer to the array. That should do it.
So, this part right here is done. And then "demonstrate the function by using [? in the ?] main." All right, so I'm going to come over here to the main. Let's see. I'm going to go ahead and get rid of this part because this part is done.
So, now, here in the main, you want to-- let's see-- "read an integer N that is not more than 50 from the standard input." So, I'm going to say "C out." And I'm going to say "enter." So, "how many numbers to read?" And then [? "0-50." ?]
And then I'm going to say "int N," and then I'm going to [INAUDIBLE] it to 0. Then I'm going to read, "int to N" like that. So, that's getting input from the user, right?
So, now I'm going to say-- let's see-- "the program then passes the array to the reverse array." So, we need to create the array. So, I'm going to say "int pointer." My array is going to become a new int array with N positions. Like that.
So, I'm creating the array based on the size that they tell me. And then, I need to read that many integers from a file. And the file is called data, and it already exists.
So, here I'm going to get the size and create the dynamic array. And then here, I need to read from the file. Now, reading from the file, the first you've got to do is you have to come up here to the very, very top, and you have to bring in fstream. So, we need to include fstream.
And this is required to read or write to the file. And you'll notice that fstream and [? iostream ?] look the same. You see that? One is for the console from your Input/Output and the other is from the file. So, they work very, very much the same.
So, I'm going to come over here and I'm going to read from the file, and how do I do that? I need to do my input file stream. And I'm going to call it [? ifile. ?]
You can actually call it anything you want. You could call it [? I ?] [? F. ?] That looks terrible though. It doesn't describe what's inside of it.
So, I can say "N from the file." You can call it whatever you want. It just has to be an ifstream. So I'm going to call it [? ifile, ?] and then I'm going to give it the data name.
The data name is data. And they did not give an extension. They just said data, that's it. So, I'm just going to use just plain old data.
And that right there, opens the pipe from the file. And it's actually a file stream to be technically correct. It is a file stream. OK? And that creates that file stream.
Then I'm going to do a for loop. And I'm going to say "int I." And then "how many items do I need to read?" I'm going to hit N, like that. All right. So, N as right here. That's what the user told us.
So then, if I'm reading from the console, I'm going to say [? C in ?] goes into-- and then I need to read into my array-- so that's going to be "my array at position I." Like that. OK. But instead of reading from the console, I want to read from the file.
They're both streams. So, all I have to do is change this to [? ifile ?] because that's the stream that I created that is from the file. So, reading from the console and reading from the file is very, very much the same thing. You're using the same syntax and everything. It makes it very easy to do this.
So, I'm going to read that many items from the file into my array into each position. OK? Now, once I finish reading, I need to close the pipe. So, after I read, I need to close the pipe like that. All right, so now I just read it into the file.
All right, now, here on my local computer I don't have the data file, so I'm going to do one thing. And I need to remember to pull this off. And, let's see. And that is, I'm going to write the file. So, I'm going to write the file.
And then I need to remember to delete this code because it's saying that the file has already been written. But I'm going to just put a bunch of [? astrices ?] to make sure that I remember to delete this.
So, now, to write to the file is going to be an output file stream. And I'm going to call that [? ofile. ?] And, again, I'm going to use the same name that they wanted to use. And then I'm going to say [? 4. ?] And then I'm going to say int I is 0.
And then, I'm going to pop in 50 items. Because they're saying the data could have as much as 50 items, right? And then to do to the output, I'll go C out and then I'll put I. Something like that, right? And then I'll put [? end L ?].
So, this will drop 50 into my file, right? So, instead of going to the C out though, watch this. All I do, is just go to the pipe-- to my output pipe. That's it. So, you can see that they work really the same. Then I need to remember to close my pipe just by going to close.
And you have to remember to open your pipe before the loop. And you have to remember to close your pipe after the loop. If you close or open inside of the loop, it's going to crash because on the next loop around, it's going to be reopened or it's going to be already closed. That's going to cause issues. So, just make sure that you open the pipe before the loop, and you close the pipe after the loop.
[End of Audio]