MyProgrammingLab Programming Challenge 70027 Transcript

Print

All right, let me help you with the second programming challenge, the 70027. 

All right, so this is array expander. "Write a function that accepts an int array and the array size. The function should create a new array that is twice the size of the argument array." All right, "The function should copy the contents of the argument array to the new array and initialize the unused elements with a zero. The function should return a pointer to the new array, and then demonstrate it by creating a main that asks for an input from the standard input, and reads data from the file data, and then the program then passes the array to the expander." And then the expander, then it should print the values, one value per line, and that's for the entire array. Okay, perfect. 

All right, "there are no prompts for the integer," all right, so got it. And, "If it's less than zero," that's perfect. All right, let's go ahead and go back over here. And this is the last one that we did, and I think I'm going to go and just reuse the code because it's very, very similar. So the fstream is for reading and writing files. And then this right here is going to be expand the array to twice the size. I can do that. All right, so this right here is going to be called "expand". And then we're going to receive the array and the size of the array, and then I could call it "expanded" like that. And "expanded" is different from the word "expand", so this will be golden. But I think I'm going to call it "bigger". 

Let's see, so if I come over here and then I say "bigger" is going to be a new, and we don't want it to be the same size. We want to be literally twice as big. So I'm going to say "times two", like that. All right, and then right here it's going to be copy the values to the new bigger array. I can do that. 

So for size, up until size then we're going to want to copy those values. So I'm going to say, bigger is going to be whatever it is in size. So we're going to go one to one. So the zero position here is going to go into the zero position here. 

But wait a minute. In addition to that, they also want the extra elements, the unused elements, to be initialized with a zero. Okay, so that's going to take a little more thought. So what we're going to do is, we're going say, "If I is less than size, then we're going to copy the element over here to the bigger array. Otherwise, the bigger array at position I, is going to be set to zero." Just plain old zero. And then here, instead of using size, we're going to use size times two. So that's going to get double the size, right, because that's the new size of, that's the size of our new array. 

So we want to go through every single position. If it's size, then we're going to grab it from the old array that's coming in, and then put into the new array. Otherwise, once we've passed that number, then we just want to set the values to zero. They were going to return back the bigger array like that. 

All right, let's think about the logic here. We get the array and its size. We create a new array dynamically that's literally twice as big. Okay, then we're going to go through every single spot twice as big. For the smaller numbers, smaller positions, we're going to take the old values and put them into the bigger array. And then for the additional slots, additional spots in the array, we're just going to assign zero to it, and then we're going to return the pointer to that array right here. That looks pretty good to me. 

All right, so let me get rid of this. And I think I want to keep this rate here a bit, and then I need to remember to delete this out so it doesn't mess up my answer. I want to keep this because I want to write data to it, and then I want to make sure everything looks good. 

All right, so, "Get the size from the user." Again we're not prompting because this right here says, "There are no prompts for the integer." All right, so that's fine. Then we're just going get the number, and then if it's out of bounds we're going to terminate silently and return a -1 for error. And there we're going to create the dynamic array that's that big, however big they tell us. We're going to open up a pipe to the file, and then we're going to read from the file into the array at that position, and for however many they give us. Then we're going to close a pipe. 

Then here we want to call the method. So this right here is going to be expanded. So let's call it "larger". I don't want to use bigger. I could use bigger because it's a different scope, but I'm going to call it larger, just so there's no confusion. And then this right here is going to be expand. So that's going to expand the array. I'm going to send the current array and the current size. Now, I've still got blood. So it is expand. Okay, the blood went away. It just needed a second for it to go away. 

So now once it's expanded then I want to show every single position. Now with this right here, I don't want to just go to n, I want to go to n times 2. Because the expanded array is literally twice as big as the original array. So that's why I need to say n times 2. 

Now I want to show every single spot. So I'm going to say "larger", like that. And then I need to get rid of the memory leak. I want to prevent the memory leak so I need to get rid of the array from the heap. Because remember, if I create the array on the heap, I personally have to get rid of it, C++ won't. So I need to make sure I get rid of it. So now here I'm going to go ahead and say the larger array, I need to get rid of that off the heap as well. All right, that looks pretty good. That looks good to me. 

All right, so let's think about this. So, I'm writing data just to see that it works, right. So that's going to be locally, and I need to get rid of this because that's already done for us on MyProgrammingLab. Then I'm getting the size from the user, and then I'm creating the dynamic array based on that size. So everywhere that you use the new keyword, you see I use the new keyword here, and I use the new keyword down here, and everywhere you use the new keyword you have to make sure that you have a delete statement for that new keyword. And in this case, I'm creating a new array so I have to not only use a delete key word but I have to use the bracket, bracket to delete the array from the heap. 

All right, that looks good to me. I'm going to go ahead and run it and see what happens. So it sits there because I don't have a prompt, but they told me not to do a prompt on my programming lab so I'm going to go ahead and just say 10. Okay? And then you can see that it starts off at 0, and it goes all the way up to 9, so that's 10 slots that I read from the file. And then I've got 10 zeroes for the extra spots. That looks good. 

All right, I'm going to do Control-A to copy. I'm going to do a Control-C. Control-A selects all, Control-C copies, I'm going to flip over here, and then I'm going to do a Control-V to paste. And then I did this to make sure I don't forget to delete this. So I'm going to go ahead and delete this out because the data file already exists. And then my system pause, they don't want the pause on it, so I'm going to go ahead and get rid of that as well. 

Now if I leave the pause, watch what happens. All right, I'm going to go ahead and submit. And you can see compiler error, and it's saying we have a problem here on system pause. You see that? So it doesn't want that system pause. But no big deal, it's easy to get rid of. All right, so I'm going to pop it. 

Let's see if I have any other errors. Nope, it is correct. So you can see that we are creating an array that's twice the size, and then I'm taking the items from the older array and putting into the new array for however the original size was. And then the extra spots, I'm assigning zero to the extra spots. And that is the programming challenge.

[End of Audio]