All right. Let me help you with the first programming challenge. So I'm opening up our MyProgrammingLab, and then the plus sign next to Week 1, and then the plus sign next to Chapter 9, and then Programming Challenges. And let's go ahead and do the first one.
All right. So the first one is reverse array. So they want us to write an array. And let's see. The array should-- the function should receive the array, and then it should take the array size as an argument as well. So the array itself, plus the array size.
The function should then create a copy of the array. However, the elements should be reversed in the copy. And the function should then return a pointer to the new array. And then once you do that, once we have the function done, then we want to demonstrate the function in the main. And it needs to read in an integer that's not more than 50.
And then based on that, it should read that many items from the data file-- So a file called data. So a file named data-- into the array in the program. And then passes the array to your reverse array function and prints the values of your reversed array. The standard input, one value per line. And then you can assume that the file data has at least n number of values. All right?
So I think it's going to be easier if we just pop it into Visual Studio. So I want to boot open Visual Studio real quick. All right. Then I'm going to pop back on over and I'm going to grab the instructions. And then that way, I don't have to keep popping back and forth. All right.
So then I'm going to /*. And notice how it puts the */ on the other side automatically for me? But I need to move it down here. OK. So everything within the /* and the */ gets ignored, because that is a block comment. All right?
So then I'm going to drop this here. And then I want to drop this a couple lines like this. All right.
So they want us to write the function. So the function needs to go above the main. Otherwise you have to use prototypes. And prototypes are a good way to go. There's nothing wrong with prototypes. They're fantastic. I think I'm going to write this without prototypes.
All right. So let's see. Write a function that accepts an int array. And it's going to reverse the array. So I'm going to call it reverse. And then the function should return a pointer to the new array.
So now my question is, what is the data type? And let's see if it accepts an int array. OK. So what it's going to return back is-- and then ///, three slashes is documentation comment. So this is going to reverse content of array. And three slashes, you tell what the method is going to do. And again, this is called a documentation comment. And then I'm going to say it's going to return back an int pointer.
So reverse and then it needs to receive an array. So int array bracket bracket. And array is actually a bad name, because did you notice how it went blue? So array is actually a keyword, like int is a keyword. That type of thing.
So let me write array real quick and-- see how it goes blue? So I don't think I should use array. So let me go ahead and call it numbers, and then array. All right? So you have put the bracket bracket to let C++ know that you're receiving an array.
And then I'm going to say int size. And then curly brace, curly brace. Whoops. Visual Studio 2013 automatically does that curly brace for you, which is kind of cool. Visual Studio 2015, I should say.
So I'm going to pop over here. And it says it accepts an array and a size. So those two things right there. And it creates a copy of the array. So I'm going to go ahead and say int.
Now, the array. I cannot create a standard array on the stack, because the array size is going to depend upon what is given to us-- size. So if you create a standard array on the stack, you have to give it a constant. You have to give it a hard number for the size so that C++ knows how many bytes on the stack to set aside.
But if you don't know, then you have to create a dynamic array. And a dynamic array can be anything you want, because this can be over there on the heap. And the heap is the bottom of your memory. And it's not controlled at all by C++.
We control as software developers. So the heap, we can do whatever we want to it, and C++ doesn't care. It's not even going to look.
So what it's going to do-- what we're going to do-- is create a pointer to the reversed array. And let's see. I'm going to call it flipped. I could call it reversed, like that. I cannot call it reverse because if I call it reverse, then C++ is going to have a hard time.
Because when you say reverse, do you mean the method or do you mean the array? So I could call it reversed, and that makes it completely different. However, I'm going to call it flipped just so that there's no confusion at all. All right?
And then I'm going to create a new array of integers that is based on the size. Like that. All right. So the size that you're giving me, that's how big the array is going to be. And it's on the heap, so we can do this.
We can use a variable in here, because we're creating it over there on the heap. And then the new keyword is going to give us a back a pointer to the array. So that pointer we're going to put into flipped. And then that's going to be an integer pointer. All right?
So now the array should create a copy of the array. All right? So this is going to be the exact same size. However, the values should be reversed in the copy. All right? So how do I reverse it?
So here we're going to reverse the values in the copy. So what I'm going to do is I'm going to create a for loop. I love how you can just type for and then hit your Tab. And then Visual Studio creates a for loop for you.
So the data type. I'm going to use int. And then over here-- and in reality, I should be using a short. Because a short is only two bytes instead of four bytes. I'm using the int just to keep everything as simple as possible. I might change that with my demos for next week.
So what is the size? The size was given to us right up here. See that? OK.
So now in the flipped array at position i-- well, let's think about the first position, position 0. That's my very first position, right? At that first position, what do I want to put? I want to put the numbers that I received-- the numbers array, up here, that I received-- but I want to put the last item. The last item. So where is the last item?
The last item-- if it's 20 positions-- so if I have an array of 20, the last item is going to be in slot 19. Because the first item is going to be over in slot 0, right? Because we're zero-based. So if it's 20 size, then I want 19-- like this-- to get the very last item. OK?
[End of Audio]