1 00:00:05,620 --> 00:00:08,760 Welcome to the section 12 challenge solution. 2 00:00:09,520 --> 00:00:12,789 I'm in the section 12 workspace in the ChallengeSolution project. 3 00:00:13,370 --> 00:00:16,849 And before I forget, we're going to write those two functions. 4 00:00:16,869 --> 00:00:22,150 But right here on line 108, don't forget to free up the storage. 5 00:00:22,710 --> 00:00:25,509 Okay, I was going to get into the functions and I knew I was probably 6 00:00:25,509 --> 00:00:26,510 going to forget to mention this. 7 00:00:26,510 --> 00:00:30,520 But remember, that function that we're going to write, this function apply 8 00:00:30,550 --> 00:00:32,549 all allocate storage from the heap. 9 00:00:32,839 --> 00:00:35,020 When we're finished with it, we need to release it. 10 00:00:35,289 --> 00:00:38,199 So we want to say delete square brackets results. 11 00:00:38,760 --> 00:00:39,670 All right, perfect. 12 00:00:39,700 --> 00:00:41,170 Now let's go write those functions. 13 00:00:41,780 --> 00:00:43,839 First thing we'll do is we'll write the print function. 14 00:00:43,840 --> 00:00:45,879 The print function is really easy to understand. 15 00:00:45,880 --> 00:00:46,910 And here it is right here. 16 00:00:48,050 --> 00:00:49,890 Okay, so what does print expect. 17 00:00:49,890 --> 00:00:51,510 And here you can see a call to print. 18 00:00:51,570 --> 00:00:54,870 Let me scroll up just a little bit so you can see it right there. 19 00:00:55,660 --> 00:00:58,630 Here you can see a call to print right here on line 97. 20 00:00:58,920 --> 00:00:59,940 You can see what's happening. 21 00:00:59,940 --> 00:01:04,470 I'm calling print, and I'm passing in an array, right, which is the 22 00:01:04,470 --> 00:01:08,610 address of an integer in this case that just happens to be the first 23 00:01:08,610 --> 00:01:10,160 integer in this array right here. 24 00:01:11,060 --> 00:01:15,480 And I'm passing in array1 size, which is a constant defined to 5. 25 00:01:16,410 --> 00:01:20,910 Now this function expects a constant pointer pointing to a 26 00:01:20,910 --> 00:01:24,000 constant, so I can't change the pointer, and I can't change what 27 00:01:24,000 --> 00:01:25,459 it's pointing to in this case. 28 00:01:25,780 --> 00:01:30,020 So any mistakes I make in here, I want caught because I have no 29 00:01:30,029 --> 00:01:31,390 plans I'm changing anything here. 30 00:01:31,390 --> 00:01:33,830 I'm not modifying the pointer or what it points to, 31 00:01:34,330 --> 00:01:35,880 and I'm passing in the size. 32 00:01:36,300 --> 00:01:37,160 So what am I doing here. 33 00:01:37,160 --> 00:01:41,150 I'm just simply displaying that square bracket, and then I'm just 34 00:01:41,150 --> 00:01:42,710 using a real simple for loop. 35 00:01:42,770 --> 00:01:43,619 There's my I. 36 00:01:43,630 --> 00:01:44,539 That's my loop variable. 37 00:01:44,539 --> 00:01:47,679 I'm starting at zero, and I'm going again less than size. 38 00:01:47,959 --> 00:01:49,490 Size in this case is 5. 39 00:01:49,500 --> 00:01:53,100 So I'm going 0 1 2 3 4 and I stop. 40 00:01:53,380 --> 00:01:55,910 And that's exactly how that array is indexed. 41 00:01:56,780 --> 00:02:00,240 At each iteration, I'm just displaying arr sub-I. 42 00:02:01,049 --> 00:02:07,339 Now right here, I'm using pointer, subscript notation. 43 00:02:09,580 --> 00:02:13,030 I could very easily have used pointer offset notation, right, 44 00:02:13,170 --> 00:02:20,119 which becomes arr plus I and de-reference that guy, exactly the 45 00:02:20,120 --> 00:02:23,590 same they both mean exactly the same, so either one you could use. 46 00:02:24,469 --> 00:02:27,260 Okay, so at this point, I'm looping through there. 47 00:02:27,260 --> 00:02:31,510 When i hit 5, I stop, and I've displayed all those values here. 48 00:02:31,510 --> 00:02:35,760 So this will display 1 2 3 4 5 and a square bracket and then a 49 00:02:35,760 --> 00:02:38,569 new line at the end, and that'll be the output you see from here. 50 00:02:38,940 --> 00:02:40,890 Okay, so hopefully, you got this function. 51 00:02:41,100 --> 00:02:42,780 It's a pretty simple function to write. 52 00:02:43,390 --> 00:02:46,400 Most likely, if you had any issues with this, it was probably 53 00:02:46,400 --> 00:02:51,390 right in here, maybe you did an I less than or equal to size and 54 00:02:51,390 --> 00:02:54,040 you had an off by one error and maybe you printed some garbage. 55 00:02:54,280 --> 00:02:55,460 That's a very common error. 56 00:02:55,679 --> 00:02:57,809 If you use the debugger that probably helped you out. 57 00:02:58,410 --> 00:02:59,329 Okay, so let me clear that. 58 00:03:00,289 --> 00:03:04,829 Now let's go and write the main function here, the apply all function. 59 00:03:05,730 --> 00:03:08,530 Now in this case, this is how I've defined this function. 60 00:03:08,530 --> 00:03:11,589 We know it returns a pointer to an integer, right, because it needs 61 00:03:11,590 --> 00:03:14,669 to return the address of that newly allocated space on the heap. 62 00:03:15,750 --> 00:03:20,980 It expects array1, which is a pointer to an integer and the 63 00:03:20,980 --> 00:03:24,680 size of that array and array2, which is a pointer to the other 64 00:03:24,680 --> 00:03:26,780 array and the size of that array. 65 00:03:27,500 --> 00:03:29,600 So that's what we're passing into this function. 66 00:03:30,219 --> 00:03:33,859 I've declared everything is conts const, all these pointers that way I'm 67 00:03:33,860 --> 00:03:36,709 not messing with pointers, and I'm not messing with what they're pointing to. 68 00:03:37,189 --> 00:03:40,329 I've got no business changing those two arrays that are coming in. 69 00:03:40,340 --> 00:03:42,500 I'm just building a third array, which I'm returning. 70 00:03:43,150 --> 00:03:44,840 So here is the third array. 71 00:03:44,980 --> 00:03:46,129 It's called new array. 72 00:03:46,460 --> 00:03:49,459 Right now, it's just a null pointer and it's a pointer to 73 00:03:49,460 --> 00:03:52,130 an integer, right, an address of that first integer in that 74 00:03:52,130 --> 00:03:53,330 new array that I want to create. 75 00:03:54,049 --> 00:03:58,280 And this is me creating it right here, new int, and this is going to 76 00:03:58,280 --> 00:04:02,750 be 15 in this example because I'm passing in a 5 and a 3 right here. 77 00:04:03,910 --> 00:04:10,540 So I'm allocating 15 integers on the heap. 78 00:04:11,859 --> 00:04:13,890 Right now, that's been allocated. 79 00:04:14,370 --> 00:04:16,440 That all contains garbage right now. 80 00:04:16,920 --> 00:04:19,459 And they're indexed from 0 to 14. 81 00:04:20,610 --> 00:04:25,770 Okay, now I want to loop through those arrays that were passed into me. 82 00:04:26,020 --> 00:04:28,530 So that I can start doing some of these calculations. 83 00:04:28,809 --> 00:04:30,229 So let's do that right now. 84 00:04:30,800 --> 00:04:33,580 First of all, I've got this variable called position, 85 00:04:33,970 --> 00:04:37,849 and position starts at 0, and it's going to go from 0 to 14. 86 00:04:38,200 --> 00:04:41,120 That's what I'm going to use to index into my new array here. 87 00:04:42,469 --> 00:04:44,029 But I've got those other two arrays. 88 00:04:44,070 --> 00:04:50,750 I've got arr1, which was 1 2 3 4 and 5 in this example. 89 00:04:51,140 --> 00:04:56,520 And I've got arr2, which was 10 20 and 30. 90 00:04:57,750 --> 00:05:00,280 Okay, so this is where we're at, and obviously we have the sizes that were 91 00:05:00,280 --> 00:05:02,039 passed in the 5 and the 3 as well. 92 00:05:02,660 --> 00:05:03,539 So what do I want to do? 93 00:05:03,759 --> 00:05:07,450 Well, I want to loop through this array as the outer loop 94 00:05:07,460 --> 00:05:08,950 because I want this to loop slow. 95 00:05:09,250 --> 00:05:11,490 This is the one i want to loop fast because I want to stay 96 00:05:11,490 --> 00:05:13,470 at 10 and then go 1 2 3 4 5. 97 00:05:13,770 --> 00:05:17,010 Then I want to go to the 20 and go 1 2 3 4 5 and so forth, right. 98 00:05:17,349 --> 00:05:22,835 So my I looping variable is going to go from 0 to size 2, which is 99 00:05:22,835 --> 00:05:25,710 0, 1 and 2 because that size 2 is 3. 100 00:05:26,580 --> 00:05:28,480 And I want to increment I by 1. 101 00:05:29,060 --> 00:05:31,650 And then I've got an inner loop, so I've got a nested loop here 102 00:05:32,190 --> 00:05:35,840 where my looping variable is J and it's going to go less than size 1. 103 00:05:36,050 --> 00:05:37,630 So we're dealing with 5 here. 104 00:05:37,880 --> 00:05:39,990 And I'm incrementing that by one at each time. 105 00:05:40,459 --> 00:05:42,800 So look what's happening in the body of the inner loop. 106 00:05:43,410 --> 00:05:45,419 Okay, I'm setting the value. 107 00:05:45,420 --> 00:05:49,020 And again, I'm using pointer subscript notation. 108 00:05:49,930 --> 00:05:53,249 I'm setting the value at new array position, right. 109 00:05:53,389 --> 00:05:56,180 Position right now is 0. 110 00:05:57,670 --> 00:05:58,789 We set it right up here. 111 00:05:59,150 --> 00:06:05,570 So I'm setting the value at that new array position to be arr sub-J, right. 112 00:06:05,570 --> 00:06:11,919 At the beginning, this will be 0 times arr sub-I, which is also 0. 113 00:06:12,410 --> 00:06:14,849 So that's these two guys, right, 10 and 1. 114 00:06:15,139 --> 00:06:19,969 So when we first go through here, the second arr2 is the one 115 00:06:19,970 --> 00:06:21,420 that's looping slower, right. 116 00:06:21,570 --> 00:06:23,494 So this guy's going to go 1 2 3 4 5. 117 00:06:23,494 --> 00:06:33,129 This guy is going to go 10 10 10 10 10, he's not changing, right. 118 00:06:33,259 --> 00:06:36,840 So what we're doing is we're multiplying 10 times 1 and 119 00:06:36,840 --> 00:06:38,659 putting it here, right. 120 00:06:38,720 --> 00:06:40,159 Then we're incrementing position. 121 00:06:40,160 --> 00:06:41,480 So now position is 1. 122 00:06:42,690 --> 00:06:45,670 And then we're multiplying 10 times 2, we got our 20. 123 00:06:45,670 --> 00:06:47,780 And we're going all the way down to 50. 124 00:06:49,359 --> 00:06:53,850 And along the way, we're incrementing that position variable. 125 00:06:53,859 --> 00:06:55,730 This is the position variable right here. 126 00:06:56,890 --> 00:07:00,520 Now we're done with the inner loop, so we go back out to the outer loop. 127 00:07:00,810 --> 00:07:04,100 And now instead of a 10 here, we're going to grab the 20, right. 128 00:07:04,170 --> 00:07:06,000 So this guy now refers to 20. 129 00:07:06,290 --> 00:07:07,419 So we're going to do the same thing. 130 00:07:07,420 --> 00:07:16,120 We're going to go 1 2 3 4 5, and then 20 20 20 20 and 20. 131 00:07:16,120 --> 00:07:21,340 And we're multiplying all of these, storing the product right in here. 132 00:07:21,340 --> 00:07:25,369 So we're going to get 20 40 60 80 100. 133 00:07:26,480 --> 00:07:29,900 And along the way, 5 6 7 8 and 9. 134 00:07:29,909 --> 00:07:32,390 We're incrementing that position variable. 135 00:07:33,390 --> 00:07:36,650 Okay, then obviously on the last iteration of the loop, we're 136 00:07:36,650 --> 00:07:41,560 dealing with 1 2 3 4 and 5, and we're multiplying all of these 137 00:07:41,560 --> 00:07:43,830 guys by that 30 right here. 138 00:07:44,620 --> 00:07:45,860 So what we're doing here. 139 00:07:45,860 --> 00:07:48,609 And then of course, this position is being bumped up each time. 140 00:07:48,850 --> 00:07:51,179 So that's what's pointing into the new array, right. 141 00:07:51,360 --> 00:07:58,469 So now here we've got 30 60 90 120 and I ran out of space but 142 00:07:58,469 --> 00:07:59,959 there's a 150 there at the end. 143 00:08:00,770 --> 00:08:04,770 At that point, that array now has been allocated on the 144 00:08:04,770 --> 00:08:08,130 heap and all those values have been stored into the array. 145 00:08:08,170 --> 00:08:12,570 I might clear this now and then all I'm doing here is returning new array. 146 00:08:13,200 --> 00:08:16,409 So it's returning the address of that first element in the array. 147 00:08:17,620 --> 00:08:24,430 Okay, now I'm back here now in my main, and I just returned 148 00:08:24,870 --> 00:08:26,120 that address right here. 149 00:08:26,440 --> 00:08:30,039 So this pointer results is pointing to that array that I 150 00:08:30,040 --> 00:08:36,219 just erased, that big array of 15 integers from 0 to 14 on the heap. 151 00:08:37,230 --> 00:08:38,429 It's got no name. 152 00:08:40,010 --> 00:08:42,380 The only way I get to it is through this results pointer. 153 00:08:43,940 --> 00:08:45,000 Okay, simple as that. 154 00:08:45,300 --> 00:08:49,319 Then I'm using a constant expression here to basically calculate this 155 00:08:49,350 --> 00:08:52,649 constant result size, which, in this case, is going to be 15. 156 00:08:53,440 --> 00:08:56,350 And I'm just passing results and 15 to the print function 157 00:08:56,350 --> 00:08:59,760 and it's going to print all those values that we calculated. 158 00:08:59,770 --> 00:09:01,110 So let's give this guy a run. 159 00:09:05,610 --> 00:09:06,570 And there's the run. 160 00:09:06,840 --> 00:09:09,100 You could see array1 is 1 2 3 4 5. 161 00:09:09,100 --> 00:09:10,610 Array2 is 10 20 30. 162 00:09:10,630 --> 00:09:15,350 And then the resulting array is 15 elements big, and it's a function of 163 00:09:15,350 --> 00:09:18,960 applying that multiplication of one array to another array and putting 164 00:09:18,960 --> 00:09:20,390 the product in the third array. 165 00:09:21,370 --> 00:09:22,500 Okay, simple as that. 166 00:09:23,080 --> 00:09:25,260 If the debugger might have helped you if you used it. 167 00:09:25,260 --> 00:09:27,650 If not, it's just really a matter of -- the only tricky 168 00:09:27,650 --> 00:09:32,579 part here is this piece of code right here the nested loop, and 169 00:09:32,580 --> 00:09:33,900 this is just one way to do this. 170 00:09:33,900 --> 00:09:36,369 You could have done this a lot of other different ways, but this 171 00:09:36,390 --> 00:09:38,960 is a really simple way to do it because you keep that position, 172 00:09:38,970 --> 00:09:40,130 you just keep bumping it up. 173 00:09:40,410 --> 00:09:44,020 And it's going to go from 0 to 14, and it's as simple as that. 174 00:09:45,080 --> 00:09:47,840 All right, so I hope you guys were able to solve the challenge. 175 00:09:48,139 --> 00:09:51,100 If you did it a different way and you think it's more efficient or 176 00:09:51,139 --> 00:09:52,650 pretty clever, let us all know. 177 00:09:52,779 --> 00:09:53,940 That'd be awesome to learn.