1 00:00:05,550 --> 00:00:08,189 Welcome back to the continuation of the solution. 2 00:00:08,840 --> 00:00:10,440 We stopped at mean right here. 3 00:00:10,440 --> 00:00:14,869 And the last 2 parts that we've got to do are display the smallest number 4 00:00:14,920 --> 00:00:16,440 and display the largest number. 5 00:00:16,859 --> 00:00:19,330 And then of course we have to handle quit which is really easy to do. 6 00:00:19,820 --> 00:00:23,100 All right, so let me scroll back down here and clean up a 7 00:00:23,100 --> 00:00:24,520 little bit of this white space. 8 00:00:26,700 --> 00:00:28,790 All right, so this is where I right here. 9 00:00:29,059 --> 00:00:33,120 So now the user hasn't typed in a p or an a or an m, maybe they've 10 00:00:33,130 --> 00:00:35,470 typed in an s, which is the smallest. 11 00:00:35,470 --> 00:00:37,880 So we'll do another else if ladder here. 12 00:00:38,060 --> 00:00:45,560 We'll say else if the selection is equal to a capital s or 13 00:00:46,300 --> 00:00:48,470 selection is a lowercase s. 14 00:00:49,670 --> 00:00:53,450 In this case, we need to calculate the smallest number in the List. 15 00:00:53,950 --> 00:00:55,550 There's a lot of ways to do this. 16 00:00:55,640 --> 00:00:59,490 But again let's deal with the case where the list is empty, right. 17 00:00:59,720 --> 00:01:03,500 So we'll say if numbers just like we have before. 18 00:01:04,138 --> 00:01:13,020 If numbers.size is 0, then we've got an empty list. 19 00:01:13,120 --> 00:01:20,380 We could just say cout and we could say unable to determine the smallest, 20 00:01:21,550 --> 00:01:27,580 list is empty or something like that, any kind of message that makes sense. 21 00:01:28,170 --> 00:01:29,470 And we'll put an endline here. 22 00:01:31,190 --> 00:01:32,550 Now we're in the l section. 23 00:01:33,310 --> 00:01:37,360 At this point, obviously, there is some data in that vector so 24 00:01:37,360 --> 00:01:41,500 what we can do is, we can again iterate through the entire vector 25 00:01:41,500 --> 00:01:42,970 and look for the smallest number. 26 00:01:43,150 --> 00:01:43,990 Well, how do we do that. 27 00:01:44,219 --> 00:01:46,880 It's pretty simple just pick the first number and assume it's the 28 00:01:46,880 --> 00:01:50,700 smallest and then iterate through the vector and if you see anything 29 00:01:50,700 --> 00:01:54,480 smaller just replace that 1 with the smallest 1 yeah so it's again 30 00:01:54,480 --> 00:01:55,280 it's pretty straightforward. 31 00:01:55,290 --> 00:01:55,970 So let's do that. 32 00:01:56,230 --> 00:01:59,080 Let's say we've got a smallest, we'll call it smallest. 33 00:02:00,300 --> 00:02:05,770 And what we'll do is we'll initialize that guy to numbers at 0. 34 00:02:06,700 --> 00:02:08,800 All right, so let's think about what's going on here. 35 00:02:09,120 --> 00:02:11,790 What we're doing here is we're saying that the smallest integer 36 00:02:12,270 --> 00:02:16,130 is the first integer in the vector right now, reasonable assumption. 37 00:02:16,490 --> 00:02:18,970 We know that there's at least 1 integer in there. 38 00:02:19,320 --> 00:02:20,060 So we'll do that. 39 00:02:20,509 --> 00:02:23,650 Then what we'll say is we'll do again arrange base for loop. 40 00:02:23,699 --> 00:02:29,090 We'll say auto, we'll call it num and the collection is numbers. 41 00:02:30,010 --> 00:02:34,450 So we'll loop through there and what we'll say is if the number 42 00:02:34,450 --> 00:02:39,690 we just read is less than the smallest that we've already seen 43 00:02:40,260 --> 00:02:44,040 then what do we do then we make the smallest the number we just read. 44 00:02:45,400 --> 00:02:45,870 That's it. 45 00:02:45,910 --> 00:02:46,850 Really straightforward. 46 00:02:47,190 --> 00:02:49,970 Now we don't do an output statement but look where we're going to do it 47 00:02:49,970 --> 00:02:53,740 we're not going to do it here, we're going to do it here because at this 48 00:02:53,740 --> 00:02:54,999 point, we're out of that for loop. 49 00:02:55,309 --> 00:03:02,429 So we're going to say cout the smallest number is a 50 00:03:02,560 --> 00:03:09,520 colon and its smallest, and we'll provide an endline. 51 00:03:10,929 --> 00:03:11,470 That's it. 52 00:03:11,620 --> 00:03:16,870 Let's test this out, and we could put data in here up top, 53 00:03:17,370 --> 00:03:20,530 it's right up here just put a bunch of data and we know what the smallest is. 54 00:03:20,730 --> 00:03:22,530 But I really want to test it all as we go. 55 00:03:22,530 --> 00:03:23,790 So I'm going to run it now. 56 00:03:24,670 --> 00:03:31,740 And let's add 10, and let's add 20. 57 00:03:32,630 --> 00:03:33,789 And we'll select s. 58 00:03:33,929 --> 00:03:34,809 We expect 10. 59 00:03:35,699 --> 00:03:37,339 That's exactly what we got back. 60 00:03:37,830 --> 00:03:39,350 Let's add another number. 61 00:03:40,230 --> 00:03:41,620 Let's add negative 20. 62 00:03:43,170 --> 00:03:46,420 Let's print the numbers, and we've got 10, 20 negative 20. 63 00:03:46,870 --> 00:03:49,679 If we want the smallest at this point I'll press s again, 64 00:03:49,950 --> 00:03:51,199 we should get negative 20. 65 00:03:51,969 --> 00:03:53,499 And there it is, negative 20. 66 00:03:53,719 --> 00:03:55,760 So a whole bunch of other test cases you could do, 67 00:03:55,810 --> 00:03:57,140 but this works just fine. 68 00:03:57,620 --> 00:03:58,959 I'll press q to quit. 69 00:03:59,700 --> 00:04:02,820 And next is the largest, right. 70 00:04:03,200 --> 00:04:06,719 The logic is exactly the same as this except it's going the other way. 71 00:04:06,960 --> 00:04:09,530 So in this case let me clean up that white space. 72 00:04:09,559 --> 00:04:15,959 Right here, we're going to say else if the selection is equal 73 00:04:15,960 --> 00:04:22,869 to a capital l or the selection is equal to a lowercase l. 74 00:04:23,990 --> 00:04:27,059 Now the user wants to find the largest number in there. 75 00:04:27,059 --> 00:04:28,600 I'm going to use another block statement. 76 00:04:29,039 --> 00:04:32,970 And the same logic I'm going to say in largest and I'm going to 77 00:04:32,970 --> 00:04:38,099 assume that largest is the number at the zeroth location, right. 78 00:04:38,120 --> 00:04:39,669 The first number in the vector. 79 00:04:40,870 --> 00:04:42,789 Oops, let me back up here. 80 00:04:42,790 --> 00:04:45,750 I forgot to check to make sure that there's numbers in there, right. 81 00:04:46,340 --> 00:04:49,310 So if numbers.size, I got a little bit ahead of myself there. 82 00:04:49,690 --> 00:04:53,730 If numbers.size is equal to zero, then in this case, there 83 00:04:53,730 --> 00:04:55,250 is nothing to check, right. 84 00:04:55,530 --> 00:04:57,440 So we'll just do the same thing we did out here. 85 00:04:57,440 --> 00:05:05,949 We'll say cout unable to determine largest, list is empty. 86 00:05:05,959 --> 00:05:10,900 Here's the else part. 87 00:05:12,900 --> 00:05:14,600 That's the part where I got ahead of myself. 88 00:05:15,110 --> 00:05:18,159 Let's assume that the largest is that first number. 89 00:05:18,160 --> 00:05:21,260 I know there is a first number, right because the size was not 0. 90 00:05:22,150 --> 00:05:25,680 So at this point, we're going to loop through the collection again. 91 00:05:25,980 --> 00:05:29,690 So I'm going to say auto num, just like we did before. 92 00:05:30,870 --> 00:05:35,860 And we're going to check if the number we just read is greater 93 00:05:35,860 --> 00:05:40,450 than the largest number we've seen, then what we're going to do is 94 00:05:40,450 --> 00:05:46,990 we're going to say that the largest number is now known, simple as that. 95 00:05:47,770 --> 00:05:53,360 And our output statement now says the largest number 96 00:05:53,580 --> 00:05:57,809 is, and we'll say largest. 97 00:06:00,990 --> 00:06:03,039 Okay, so that should take care of the largest case. 98 00:06:03,050 --> 00:06:04,270 So let's test this out. 99 00:06:06,450 --> 00:06:07,580 So let's add some numbers. 100 00:06:07,580 --> 00:06:09,560 We'll add negative 10. 101 00:06:10,320 --> 00:06:12,929 We'll add a 1000. 102 00:06:13,889 --> 00:06:17,650 We'll add 200, and we'll add 300. 103 00:06:17,650 --> 00:06:21,049 So let's display our list. 104 00:06:21,600 --> 00:06:24,200 So you've got negative 10, 1000 200 , 300. 105 00:06:24,560 --> 00:06:28,380 We expect the largest to be a 1000, so I'll press l, and 106 00:06:28,429 --> 00:06:29,840 the largest number is a 1000. 107 00:06:29,840 --> 00:06:31,210 Let's check the smallest again. 108 00:06:31,259 --> 00:06:34,619 S, the smallest is negative 10. 109 00:06:34,629 --> 00:06:35,059 Cool. 110 00:06:35,100 --> 00:06:36,110 So we're almost done, right. 111 00:06:36,139 --> 00:06:37,870 All we need to handle is the q case. 112 00:06:39,520 --> 00:06:40,280 I'll quit here. 113 00:06:40,280 --> 00:06:43,969 And the q case is real simple because all we want to do is just say goodbye. 114 00:06:44,280 --> 00:06:45,349 So let's do that here. 115 00:06:45,360 --> 00:06:54,580 We'll say else if the selection in this case, right, just like before is 116 00:06:55,209 --> 00:07:03,489 an uppercase q or if the selection is equal to a lowercase q, in this case 117 00:07:03,490 --> 00:07:07,629 what we want is to simply say cout. 118 00:07:07,669 --> 00:07:09,210 And let's do that in a block as well. 119 00:07:09,559 --> 00:07:11,740 You really want to do these in blocks because you never know 120 00:07:11,740 --> 00:07:14,000 when you're going to come back to add code, and it's already 121 00:07:14,010 --> 00:07:15,460 set up to add code this way. 122 00:07:15,760 --> 00:07:21,479 So we'll say see out goodbye, endline. 123 00:07:24,849 --> 00:07:25,309 Perfect. 124 00:07:25,410 --> 00:07:28,150 And while we're here we may as well handle that other case right. 125 00:07:28,510 --> 00:07:30,339 That's the catch catch-all else, right here. 126 00:07:31,510 --> 00:07:33,900 That's going to be the end of the else if else ladder. 127 00:07:34,020 --> 00:07:35,330 What happens here. 128 00:07:35,330 --> 00:07:38,719 Well, they typed something in that I have no idea what to do with. 129 00:07:38,720 --> 00:07:40,540 So at this point what do we say, 130 00:07:40,540 --> 00:07:43,840 cout unknown selection. 131 00:07:45,440 --> 00:07:46,740 Please try again. 132 00:07:49,340 --> 00:07:51,040 That's as simple as that. 133 00:07:52,040 --> 00:07:57,440 Okay, seems like a lot of code and it sort of is. 134 00:07:57,440 --> 00:08:01,110 When we talk about functions very soon, we're going to make this 135 00:08:01,110 --> 00:08:04,510 so much easier because instead of all of this code in here, 136 00:08:04,510 --> 00:08:08,410 we're just going to have one line that calls a function, and one line that 137 00:08:08,410 --> 00:08:09,910 calls a function here and so forth. 138 00:08:09,910 --> 00:08:13,310 So we'll get to there soon. But let's finish this up now. 139 00:08:14,510 --> 00:08:16,110 Building and running. 140 00:08:16,120 --> 00:08:19,230 Let's press q, we expect a goodbye message now. 141 00:08:19,230 --> 00:08:21,430 There it is, goodbye. And we quit. 142 00:08:21,430 --> 00:08:25,730 And let's type in something that we don't know what to do with an o, 143 00:08:26,230 --> 00:08:28,270 unknown selection, please try again. 144 00:08:28,270 --> 00:08:31,470 A 1, unknown selection, please try again. 145 00:08:31,870 --> 00:08:33,400 And that's it. 146 00:08:34,000 --> 00:08:35,169 That's the solution. 147 00:08:35,650 --> 00:08:40,839 Of course, you could have done this with a switch statement here in which 148 00:08:40,840 --> 00:08:45,499 case it might have looked something like switch off of selection. 149 00:08:48,369 --> 00:08:57,995 And then here you would have case no Q case lowercase q you know 150 00:08:59,340 --> 00:09:02,280 cout goodbye, and then break here. 151 00:09:02,980 --> 00:09:05,020 And then you'd have all your other cases, just 152 00:09:05,020 --> 00:09:06,090 like you saw in the videos. 153 00:09:06,520 --> 00:09:09,459 But in this case, I think the switch statement is is okay 154 00:09:09,460 --> 00:09:10,809 if we're using functions. 155 00:09:10,809 --> 00:09:12,120 We could just call functions here. 156 00:09:12,370 --> 00:09:14,929 But otherwise, the switch statement 10ds to get even longer. 157 00:09:15,190 --> 00:09:17,660 And I don't know how many lines of code we wrote for this example. 158 00:09:17,660 --> 00:09:22,479 Let's see, we started on about line 65 because the rest was just 159 00:09:22,480 --> 00:09:23,760 the you know the stuff at the top. 160 00:09:24,060 --> 00:09:28,190 So we went from about 65 down to about 140. 161 00:09:28,490 --> 00:09:31,529 So I don't know about 70 lines of code or so. 162 00:09:32,049 --> 00:09:34,339 It would be obviously much less when we do functions. 163 00:09:34,340 --> 00:09:36,740 And with the switch statement, I think it would be even longer the 164 00:09:36,740 --> 00:09:39,819 amount of code we'd written just because of the syntax of the switch. 165 00:09:39,830 --> 00:09:42,560 But the if else if ladder works great here. 166 00:09:42,940 --> 00:09:48,029 Obviously, if i decide to add another option here, it's just 167 00:09:48,030 --> 00:09:49,260 a matter of coming in here. 168 00:09:49,860 --> 00:09:52,490 And you could do it anywhere really because it's all sort 169 00:09:52,490 --> 00:09:53,729 of mutually exclusive, right. 170 00:09:54,090 --> 00:09:55,400 And you can just do it right here. 171 00:09:55,870 --> 00:09:59,150 Else if at it between this one and this one and there you've got your 172 00:09:59,150 --> 00:10:01,080 other option just as easy as that. 173 00:10:01,720 --> 00:10:04,300 Okay, so I hope you enjoyed this challenge. 174 00:10:04,500 --> 00:10:08,110 This was a challenging challenge because it's using everything 175 00:10:08,110 --> 00:10:09,330 that we've learned so far. 176 00:10:09,690 --> 00:10:11,670 But there's a lot of logic going on here, right. 177 00:10:11,830 --> 00:10:14,480 It's a simple program but there's a lot of logic going on here. 178 00:10:14,480 --> 00:10:16,220 You've got you know a do while loop. 179 00:10:16,220 --> 00:10:20,839 You've got for loops looping through the iteration, and thank goodness that 180 00:10:20,840 --> 00:10:26,589 c++11 gave us this range-based for loop because that makes it so simple. 181 00:10:26,589 --> 00:10:30,100 We don't have to do things like for int I equals 0, i less than 182 00:10:30,490 --> 00:10:33,830 you know the size of the vector and then increment I along the way. 183 00:10:33,830 --> 00:10:35,810 We don't have to deal with any of that, so it makes it 184 00:10:35,810 --> 00:10:36,910 really really straightforward. 185 00:10:37,570 --> 00:10:39,780 Okay, so I hope you enjoyed this section.