1
00:00:00,750 --> 00:00:06,780
OK, so let's continue on with our discussions about functions and how they work.

2
00:00:07,410 --> 00:00:11,460
This is part two of us talking about functions.

3
00:00:13,020 --> 00:00:20,010
So first off, we are going to do a practice problem, and this might take a while.

4
00:00:20,130 --> 00:00:22,320
You can take as long as you'd like.

5
00:00:23,100 --> 00:00:24,750
We're stepping things up a bit.

6
00:00:24,810 --> 00:00:33,900
So things in the course are about to get a little bit harder on a little bit harder to understand what's

7
00:00:33,900 --> 00:00:39,270
going to push you to try and challenge you to solve more difficult problems.

8
00:00:39,540 --> 00:00:45,300
And this one might be significantly more challenging than the previous problems that you've encountered.

9
00:00:46,380 --> 00:00:49,260
So I will go ahead and read the description.

10
00:00:50,370 --> 00:00:51,970
I might give a few hints.

11
00:00:52,260 --> 00:00:53,270
And then, of course, I will.

12
00:00:53,280 --> 00:00:57,450
As always, I will go over a solution.

13
00:00:57,720 --> 00:01:02,850
There are multiple solutions, but I will go over my solution for the problem.

14
00:01:03,540 --> 00:01:12,210
So without further ado, make a program that reads the three words which are going to be strings.

15
00:01:12,240 --> 00:01:13,170
Each word is a string.

16
00:01:14,100 --> 00:01:15,900
So three words from user input.

17
00:01:16,800 --> 00:01:24,600
The program should then find the sum of the decimal values of each character in the string and in talking

18
00:01:24,600 --> 00:01:27,750
about each string for each one of the strings.

19
00:01:28,410 --> 00:01:29,460
So that's what this means.

20
00:01:30,960 --> 00:01:35,700
Basically, you're going to sum up all of the decimal values when I say decimal values, think back

21
00:01:35,700 --> 00:01:38,640
to that SCIRI, the ASCII table.

22
00:01:39,300 --> 00:01:46,500
So remember there are numeric integer values for each character, so there's multiple characters that

23
00:01:46,500 --> 00:01:47,550
make up a string, right?

24
00:01:48,210 --> 00:01:52,410
Each one of those characters has an ASCII decimal value.

25
00:01:53,960 --> 00:01:59,090
That can be gotten by indexing a string in a particular position to get the character, so remember

26
00:01:59,090 --> 00:02:04,520
back to those lectures, if you need to head back to those lectures, go ahead and go to those lectures

27
00:02:04,520 --> 00:02:08,750
when I tell you to pause the video so you can refresh yourself on these concepts.

28
00:02:09,900 --> 00:02:12,540
But we are referring to the ASCII table and talk about this.

29
00:02:12,840 --> 00:02:18,270
You're summing up all of those decimal value, so each no value that goes with each character, you

30
00:02:18,270 --> 00:02:23,190
will add those together for the whole string and that will give you a sum for that particular string,

31
00:02:23,580 --> 00:02:26,310
except you must do this for each one of the strings.

32
00:02:27,000 --> 00:02:32,460
And then what you should do after computing the sum for each one of the three strings, you should print

33
00:02:32,460 --> 00:02:37,440
out the string that has the biggest sum, as well as the sum itself.

34
00:02:38,170 --> 00:02:38,420
All right.

35
00:02:38,490 --> 00:02:41,580
So the sum that goes along with the string and the string that has the biggest.

36
00:02:41,730 --> 00:02:44,460
So here's an example I run this program.

37
00:02:44,460 --> 00:02:45,690
I call it func p.

38
00:02:46,890 --> 00:02:49,110
So it asks for three words, right?

39
00:02:49,110 --> 00:02:50,640
And says entire word and press enter.

40
00:02:50,910 --> 00:02:54,150
And so I enter ball and then same thing as a cat.

41
00:02:54,150 --> 00:02:55,710
And then I say zebra.

42
00:02:55,710 --> 00:03:01,620
And it says the string with the largest, some string with largest thumb is the zebra with a sum of

43
00:03:01,620 --> 00:03:02,490
five hundred thirty two.

44
00:03:02,520 --> 00:03:09,210
So this 532 comes from adding up the decimal values of each one of the characters.

45
00:03:09,240 --> 00:03:17,610
So when you have a character, you can actually add it to an integer and like, let's say you have an

46
00:03:17,610 --> 00:03:19,710
integer, the zero, if you add.

47
00:03:21,110 --> 00:03:26,390
A character to it, that integer will now hold the decimal value of that character.

48
00:03:26,690 --> 00:03:34,400
OK, so go ahead and pause the video and see if you can solve this problem.

49
00:03:35,090 --> 00:03:43,880
It is, like I said, quite quite a difficult problem, except you have all the tools that you need

50
00:03:44,270 --> 00:03:45,770
to solve the problem.

51
00:03:47,800 --> 00:03:53,140
So with that, I will give you a few hints of things that you might or might not want to use.

52
00:03:54,380 --> 00:04:02,540
But something that you should definitely use is a function because we're trying to practice practice

53
00:04:02,540 --> 00:04:03,410
functions, OK?

54
00:04:04,460 --> 00:04:09,290
So you should be using functions at least tests have one function.

55
00:04:09,290 --> 00:04:14,180
You don't have to have multiple functions because we haven't talked about using multiple functions yet.

56
00:04:14,180 --> 00:04:15,520
It's really the same thing.

57
00:04:15,530 --> 00:04:20,960
You could just do the same thing we've learned multiple times, so it's not any harder to make another

58
00:04:20,960 --> 00:04:21,620
function.

59
00:04:21,860 --> 00:04:24,240
But I'm not expecting you to do that.

60
00:04:24,260 --> 00:04:25,910
You can just make one function.

61
00:04:27,740 --> 00:04:35,870
You also are going to probably want to use a loop or even potentially multiple loops.

62
00:04:38,140 --> 00:04:44,620
You may or may not want to use an array you don't have to, but you could.

63
00:04:46,590 --> 00:04:54,540
You are probably also going to want to use a conditional statement or multiple conditional statements.

64
00:04:56,210 --> 00:05:02,940
Multiple are not needed, you can use one, but it can also be done by using multiple conditional statements

65
00:05:02,940 --> 00:05:10,700
so you can just use an air for you can use an IS if LCF and else multiple combinations of those things.

66
00:05:12,110 --> 00:05:13,970
So there's a few ways to do it, but I do.

67
00:05:14,260 --> 00:05:17,600
I recommend that you think about using a for loop.

68
00:05:18,950 --> 00:05:26,090
Definitely using a function because we're trying to practice functions and probably using a conditional

69
00:05:26,090 --> 00:05:27,380
statement as well.

70
00:05:28,580 --> 00:05:34,810
Also keeping track of a total kind of variable for the sum, right?

71
00:05:34,820 --> 00:05:40,430
Remember those accumulator variables that we were talking about where you add to them and you can keep

72
00:05:40,430 --> 00:05:41,480
track of a sum?

73
00:05:42,950 --> 00:05:47,850
Also, think about updating a variable variable, right?

74
00:05:47,870 --> 00:05:48,410
So.

75
00:05:50,120 --> 00:05:56,360
You might want to make a variable that can be overwritten with a new value.

76
00:05:57,630 --> 00:06:04,110
OK, so those are my hints, so if if you weren't able to figure it out without the hints, go ahead

77
00:06:04,110 --> 00:06:05,370
and try and figure that out now.

78
00:06:07,270 --> 00:06:09,290
And then I will go ahead and go over.

79
00:06:09,310 --> 00:06:11,950
One of many potential solutions.

80
00:06:13,980 --> 00:06:18,450
So let's head over to the editor to look at a solution.

81
00:06:19,290 --> 00:06:21,330
So I'm going to head into Visual Studio here.

82
00:06:22,530 --> 00:06:24,060
So let's check this out.

83
00:06:24,450 --> 00:06:31,200
I, of course, still have this wrist injury on, so I typed it out already.

84
00:06:31,770 --> 00:06:32,970
I won't keep doing this.

85
00:06:34,200 --> 00:06:40,640
I do have some videos already recorded where I do type stuff out along with me explaining it.

86
00:06:40,650 --> 00:06:42,180
I believe that's a better way to do it.

87
00:06:42,180 --> 00:06:48,930
But right now I just type too slow, so I am going to just go over the code that I've already written.

88
00:06:50,860 --> 00:06:52,630
So I went ahead and made a function here.

89
00:06:52,810 --> 00:06:54,040
I called it get some.

90
00:06:55,090 --> 00:07:01,180
Its purpose is just to get the sum of whatever word is passed to it.

91
00:07:02,050 --> 00:07:03,820
I'm gonna go ahead and put a comment here for that.

92
00:07:04,270 --> 00:07:21,370
So I'm just going to say gets some of the perimeter word and I'm going to say some of the character

93
00:07:23,980 --> 00:07:25,480
tests or values.

94
00:07:27,960 --> 00:07:30,690
Someone put a comment there to show what it does.

95
00:07:32,440 --> 00:07:36,070
So let's leave this function alone for a second, you might have already kind of understood what was

96
00:07:36,070 --> 00:07:39,550
going on here, but I'm going to start down with main because that's where we're kind of starting.

97
00:07:41,260 --> 00:07:50,830
So what I did first was I declared a variable string for a word, and this is representing each one

98
00:07:50,830 --> 00:07:55,120
of the words I'm going to read into, except I'm going my plan was to overwrite the word.

99
00:07:56,250 --> 00:08:01,150
Over and over, just use one variable, she'll see how I do that down here, and this is the second.

100
00:08:01,500 --> 00:08:06,300
I also make a variable called larger string because we care about printing out the largest string,

101
00:08:06,300 --> 00:08:06,600
right?

102
00:08:07,980 --> 00:08:09,870
I made a variable called sum.

103
00:08:10,230 --> 00:08:17,070
That way, I can record the sum coming from the function that I want to do the sum calculation for a

104
00:08:17,070 --> 00:08:17,670
given word.

105
00:08:19,170 --> 00:08:26,520
And then I make a largest variable, and I started out with the value of zero.

106
00:08:26,610 --> 00:08:29,910
So I declare this variable in initialize it with the value of zero.

107
00:08:29,920 --> 00:08:36,240
I do that because I want to be zero is basically the smallest value, and my goal with this variable

108
00:08:36,240 --> 00:08:43,320
is to keep track of the largest thing as we encounter new words and new sums.

109
00:08:43,620 --> 00:08:51,090
This variables purpose is to be able to compare the new sum that we have to see if it's larger than

110
00:08:51,090 --> 00:08:51,780
the previous.

111
00:08:52,020 --> 00:08:53,790
If so, it will update it right?

112
00:08:53,940 --> 00:08:56,250
So keeping track of a maximum value?

113
00:08:56,430 --> 00:08:57,480
That's what this is for.

114
00:08:59,520 --> 00:09:08,760
I chose to do a for loop to be able to cycle through the entering of each word so it would see in over

115
00:09:08,760 --> 00:09:14,850
and over again for exactly three times because the problem statement says that the user should enter

116
00:09:15,090 --> 00:09:16,080
three words.

117
00:09:16,120 --> 00:09:24,150
That's why I started zero and go for I less than three, so I will be zero, then one, then two, and

118
00:09:24,150 --> 00:09:27,150
then it will stop once it is three, so it won't run for the third time.

119
00:09:29,640 --> 00:09:37,830
So it will have three iterations, so this year out just says enter word and press enter, then what

120
00:09:37,830 --> 00:09:41,340
I do is I read in a string.

121
00:09:42,890 --> 00:09:48,770
Basically, it's doing C in into this string variable I created, so it'll take everything that it sees

122
00:09:48,770 --> 00:09:51,380
up until whitespace the first whitespace character.

123
00:09:54,050 --> 00:10:00,680
And then what happens is I call to get some and pass it the string variable.

124
00:10:01,340 --> 00:10:02,600
So let's head up there.

125
00:10:03,140 --> 00:10:06,710
Let's not pay attention to this quite yet, although you probably know what's going on here.

126
00:10:07,580 --> 00:10:08,690
This will get cold.

127
00:10:09,080 --> 00:10:13,130
So I pass the word that just got injured to the user to this function.

128
00:10:13,130 --> 00:10:14,360
So let's head up here.

129
00:10:15,260 --> 00:10:16,490
Here's the variable.

130
00:10:17,600 --> 00:10:20,390
So what I do here is I make a new variable.

131
00:10:20,810 --> 00:10:25,400
Call it some assert equal to zero to start out with as an initialization.

132
00:10:26,860 --> 00:10:33,820
I loop for the length of the word because it's a string, right, so I do string the the word variable

133
00:10:34,060 --> 00:10:39,340
length for the length function, and I say, let's go as long as it's less than length, because remember,

134
00:10:39,340 --> 00:10:43,720
the last character is the word length minus one.

135
00:10:43,750 --> 00:10:47,920
That's why I say, let's go as long as it's less than the word length.

136
00:10:48,760 --> 00:10:54,130
I started zero so we can still capture position zero and I do an A-plus plus, so we're incrementing.

137
00:10:55,460 --> 00:10:58,280
Then what I do is I use this plus equal operator.

138
00:10:58,610 --> 00:11:05,740
So remember, this could also be some equal sum plus word index I use.

139
00:11:06,290 --> 00:11:11,860
The word index I hear is that we're trying to grab each character.

140
00:11:12,800 --> 00:11:15,410
Of the word that was passed to the function.

141
00:11:16,820 --> 00:11:23,450
And add it to are some variable, it's kind of like another total, we keep accumulating things to it.

142
00:11:23,720 --> 00:11:26,570
So it's like, this is that accumulator variable I'm talking about.

143
00:11:26,570 --> 00:11:31,040
It starts off as zero, let's say, for example, we have the word car.

144
00:11:32,420 --> 00:11:40,520
I'm going to find the well, I'm not going to find C++ will find the ASCII decimal value of C and add

145
00:11:40,520 --> 00:11:41,750
it to this integer.

146
00:11:41,750 --> 00:11:48,800
It sees that this is an integer and it's going to add the integer representation of the character C

147
00:11:49,460 --> 00:11:50,330
to some.

148
00:11:50,330 --> 00:11:56,060
So it will be zero plus whatever the value is of C, we will go back for a second iteration.

149
00:11:56,060 --> 00:12:02,060
So now I, as one one, is still less than the length of the car cars three, right?

150
00:12:02,060 --> 00:12:02,690
That's the length.

151
00:12:03,380 --> 00:12:07,610
So then we grab position one of car, which is a right.

152
00:12:08,150 --> 00:12:09,710
I'm using this car example again.

153
00:12:09,710 --> 00:12:10,850
Here I can write it out for you.

154
00:12:10,850 --> 00:12:11,300
Like this?

155
00:12:12,080 --> 00:12:13,100
Now we're on the A.

156
00:12:13,460 --> 00:12:16,760
So we get the value of a and we add it to some.

157
00:12:18,170 --> 00:12:23,630
Same thing we go back again now as to it still is less than the length of the word because of the length

158
00:12:23,630 --> 00:12:24,800
of the word is three.

159
00:12:25,610 --> 00:12:30,780
Now we add the decimal value of our to our sum, we go back up.

160
00:12:30,800 --> 00:12:33,530
Now I is three is the same as the length.

161
00:12:33,530 --> 00:12:35,060
We don't do anything else.

162
00:12:35,300 --> 00:12:36,260
We have ourselves.

163
00:12:36,260 --> 00:12:37,550
We return the sum, right?

164
00:12:38,900 --> 00:12:42,890
So let's head back down now that we have our sum, so we come back to here.

165
00:12:43,520 --> 00:12:45,860
The value is here.

166
00:12:47,060 --> 00:12:54,200
We then store the value in this other some variable that is local to the scope of the main function.

167
00:12:55,390 --> 00:13:01,410
This is not the same as this in memory.

168
00:13:02,260 --> 00:13:11,800
This sum right here is local to the scope of this function, so only this function gets knows about

169
00:13:11,980 --> 00:13:13,120
this variable.

170
00:13:14,320 --> 00:13:19,180
Maine is the only function out of two of these that knows about this variable.

171
00:13:19,180 --> 00:13:24,820
So despite the fact that they have the same name, C++ doesn't see it that way.

172
00:13:25,240 --> 00:13:32,980
It sees them as totally separate variables, except now they will have the same value, right?

173
00:13:33,280 --> 00:13:36,880
This sum was calculated here.

174
00:13:37,060 --> 00:13:40,530
It was passed back down to where we called the function.

175
00:13:40,540 --> 00:13:46,540
So it's right here and then we took that value and stored it in this other some variable.

176
00:13:49,060 --> 00:13:52,120
Then what we do is we have an if statement.

177
00:13:53,180 --> 00:13:59,630
The statement asks the question is some greater than largest?

178
00:14:00,320 --> 00:14:01,820
So it's a condition, right?

179
00:14:02,180 --> 00:14:06,770
If this is true, if some is greater than largest, then we will run the code in here.

180
00:14:07,790 --> 00:14:09,140
Well, what is the largest?

181
00:14:09,140 --> 00:14:10,550
Largest is zero.

182
00:14:10,670 --> 00:14:12,710
So far, we haven't changed it.

183
00:14:12,920 --> 00:14:16,040
This is our first iteration through this for a loop.

184
00:14:17,070 --> 00:14:19,020
I is zero.

185
00:14:19,890 --> 00:14:24,780
We have read in this one first word, we've only encountered one word so far.

186
00:14:25,890 --> 00:14:30,420
And we're asking if the some of that word is greater than zero.

187
00:14:31,320 --> 00:14:36,150
Well, if we entered anything for the word, it's going to be greater than zero, right?

188
00:14:36,150 --> 00:14:39,940
Because every character has a value greater than zero.

189
00:14:39,960 --> 00:14:43,150
Every character in the alphabet has a value greater than zero.

190
00:14:43,170 --> 00:14:44,970
You can look at the ASCII table for that.

191
00:14:47,200 --> 00:14:53,230
So, yes, some will most likely be greater than largest if we entered car.

192
00:14:53,260 --> 00:14:55,570
It definitely will be greater than largest.

193
00:14:56,860 --> 00:14:59,390
So this condition will be true then.

194
00:14:59,410 --> 00:15:00,540
So what do we do?

195
00:15:00,550 --> 00:15:02,750
We say largest is no longer zero.

196
00:15:02,770 --> 00:15:05,200
It's now we'll hold the value of some.

197
00:15:06,120 --> 00:15:10,230
Why are we doing this, because we're keeping track of the largest thing?

198
00:15:11,210 --> 00:15:18,410
Each time we find something bigger, we overwrite our largest variable with that biggest thing that

199
00:15:18,410 --> 00:15:19,400
we just found.

200
00:15:21,360 --> 00:15:27,510
So that's what we do largest is now going to hold the some of the characters of car.

201
00:15:29,210 --> 00:15:38,600
We also know that since we found the largest sum so far, that means that the current word that we just

202
00:15:38,600 --> 00:15:42,500
looked at is the biggest word so far.

203
00:15:43,340 --> 00:15:48,890
So what we do is we update our string, our largest string variable.

204
00:15:48,920 --> 00:15:54,770
This variable keeps track of the largest words so far by largest word.

205
00:15:55,040 --> 00:16:01,670
We don't mean necessarily length, although a longer word normally leads to a bigger sum.

206
00:16:02,420 --> 00:16:08,870
But what we're really saying is the sum of the characters, the number right, the number was larger.

207
00:16:08,870 --> 00:16:15,260
So therefore we should now update the largest string to have this word.

208
00:16:15,260 --> 00:16:20,810
And this word is the actual word that we've sent just recently, right in this iteration of the Loop,

209
00:16:21,590 --> 00:16:25,250
that was the word that now has the largest sum if we entered here.

210
00:16:25,550 --> 00:16:27,110
We record the largest word.

211
00:16:27,110 --> 00:16:29,600
We record that words sum.

212
00:16:30,380 --> 00:16:31,850
These are the current largest things.

213
00:16:33,220 --> 00:16:42,670
We do this for each one of the words, if we find that the next word after car has a bigger sum, then

214
00:16:42,670 --> 00:16:45,960
this will be true once again and we will update it.

215
00:16:45,970 --> 00:16:47,380
We will overwrite.

216
00:16:48,760 --> 00:16:52,150
These some four car with the sum for this new word.

217
00:16:52,330 --> 00:16:53,920
We will also overwrite.

218
00:16:55,030 --> 00:17:02,590
The largest string, which previously contained car now is going to contain this latest word that we

219
00:17:02,590 --> 00:17:03,070
looked at.

220
00:17:04,180 --> 00:17:05,540
So hopefully that makes sense.

221
00:17:05,560 --> 00:17:07,720
I'm trying to explain it as clearly as possible.

222
00:17:08,900 --> 00:17:11,810
I tried to do this in a somewhat efficient manner.

223
00:17:12,980 --> 00:17:15,150
You may have to have a totally different approach to this.

224
00:17:15,170 --> 00:17:17,950
It could have involved a lot more conditional statements.

225
00:17:17,960 --> 00:17:20,240
It may have not involved a loop right here.

226
00:17:20,480 --> 00:17:22,880
It may have only had a loop right here.

227
00:17:23,240 --> 00:17:31,130
It may have somehow had no loops and it was just like totally hardcoded saying that's fine as long as

228
00:17:31,130 --> 00:17:31,880
you solved it.

229
00:17:32,480 --> 00:17:33,140
That's great.

230
00:17:33,710 --> 00:17:36,250
If you didn't solve it, don't worry.

231
00:17:36,260 --> 00:17:39,260
Just like I say every time, this was actually a really hard one.

232
00:17:39,270 --> 00:17:41,190
So this is like an extra hard one this time.

233
00:17:41,210 --> 00:17:46,160
So if you surprised it, if you saw that, I'm actually really surprised and I give you ultra props

234
00:17:46,160 --> 00:17:48,880
for solving, this is how I did it.

235
00:17:48,890 --> 00:17:52,580
You do not have to do it like this, but I wanted to show you this way of doing it.

236
00:17:53,580 --> 00:18:00,210
Of course, once we're done going through all three words by using this for loop, we now have them

237
00:18:00,210 --> 00:18:03,870
stored and we have the answers stored in this variable and this variable.

238
00:18:03,870 --> 00:18:09,120
So therefore we have a see out statement that prints both of these variables because they both have

239
00:18:09,120 --> 00:18:09,660
the answers.

240
00:18:11,720 --> 00:18:15,320
OK, so hopefully that was enough of an explanation for you.

241
00:18:17,270 --> 00:18:20,330
So like I said, if you did it a different way, that's totally fine.

242
00:18:20,600 --> 00:18:22,200
Just wanted to show you this way.

243
00:18:22,220 --> 00:18:25,280
So you had a solution in case you didn't figure it out.

244
00:18:25,580 --> 00:18:30,890
And also to show you a different way of doing it if you did it one of those other ways.

245
00:18:32,300 --> 00:18:34,880
So let's head back to the presentation.
