Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,330 --> 00:00:06,390
All right, so now it's time to see the solution and what do we want to do is to write a function,
2
00:00:06,540 --> 00:00:12,960
write a function that simply gets an array, that gets an array in some way that we are going to see
3
00:00:12,960 --> 00:00:13,550
right now.
4
00:00:13,560 --> 00:00:15,430
And also the size of the array.
5
00:00:15,450 --> 00:00:24,240
OK, the size of the array and all these function has to do is to find the sum of all the elements in
6
00:00:24,240 --> 00:00:25,600
this given array.
7
00:00:25,650 --> 00:00:32,530
So we can say that finds the sum of all elements in the array.
8
00:00:32,820 --> 00:00:40,680
So that basically what our function should do and let's say also that the function we said previously,
9
00:00:40,680 --> 00:00:45,920
it should it should function, should return the sum.
10
00:00:45,930 --> 00:00:49,990
OK, so it finds it and then it returns the sum.
11
00:00:50,100 --> 00:00:53,360
So the instructions are pretty clear to us.
12
00:00:53,370 --> 00:00:58,000
And now what we are going to do is to write the signature of our function.
13
00:00:58,260 --> 00:01:03,900
So first of all, what we are going to do is we know that let's suppose that we want to work with the
14
00:01:03,900 --> 00:01:05,040
array of integers.
15
00:01:05,160 --> 00:01:12,390
So the sum of an array of integers is also going to be an integer.
16
00:01:12,400 --> 00:01:15,840
So that's why the type of the function is going to be int.
17
00:01:16,050 --> 00:01:18,980
And now we are going to write the function name.
18
00:01:18,990 --> 00:01:27,100
Let's call it some of array and this function should get two arguments, OK?
19
00:01:27,120 --> 00:01:34,230
The first one should specify how to work with the array and the second one is the size of the array.
20
00:01:34,830 --> 00:01:41,490
So let's say that in the main in the main function, we created some, let's call it test array.
21
00:01:41,490 --> 00:01:52,880
So into array or a rather, let's say of size three and IT values are one, three and 10 for this example.
22
00:01:52,890 --> 00:02:02,670
OK, and we know that these arrays created inside the main block and the array is has three elements,
23
00:02:03,180 --> 00:02:11,880
one three a.m. and ARRL simply represents the name of the array as well as it also specifies where is
24
00:02:11,970 --> 00:02:15,030
the first address of the first element of this array?
25
00:02:15,060 --> 00:02:15,450
Right.
26
00:02:15,450 --> 00:02:20,490
So we know there is a sequence of three elements starting from some address.
27
00:02:20,500 --> 00:02:23,880
For example, let's say the address is three thousand in.
28
00:02:23,880 --> 00:02:30,960
What we want to do is to call these functions some of Array and Paysite, these initial address where
29
00:02:31,110 --> 00:02:32,550
the array resides.
30
00:02:32,880 --> 00:02:40,170
So if we want to send to pass and address, we know that here on the function side, there should be
31
00:02:40,170 --> 00:02:42,990
some variable getting this address.
32
00:02:43,320 --> 00:02:52,470
And what variable basically you may think of using to store an address, it's probably going to be of
33
00:02:52,470 --> 00:02:53,730
a pointer type, right?
34
00:02:53,880 --> 00:02:55,490
It's a pointer to end.
35
00:02:55,620 --> 00:03:01,620
So we are going to send the address of the first of the first R element in this disarray.
36
00:03:01,920 --> 00:03:07,410
And this address is going to be received here by a parameter in our function.
37
00:03:07,420 --> 00:03:17,190
So we are going to call it and be a R r OK, so in Star Purulia, it will hold the address of the array
38
00:03:17,430 --> 00:03:24,210
and also we are going to fast here the size because we are working in some remote function and we should
39
00:03:24,210 --> 00:03:28,150
know the size of the array and how we created our signature.
40
00:03:28,170 --> 00:03:32,460
And now let's just write the implementation of these functions.
41
00:03:32,480 --> 00:03:33,750
So there you go.
42
00:03:33,870 --> 00:03:37,860
And here we are going to write everything regarding these function.
43
00:03:38,190 --> 00:03:41,520
So it receives the address, it receives the size.
44
00:03:41,640 --> 00:03:45,480
And now what we want to do is defined the sum of all the elements.
45
00:03:45,490 --> 00:03:49,310
So in this case, one, three and ten, it's a total of 14.
46
00:03:49,890 --> 00:03:56,520
So, for example, what we are going to need is to create additional variable int I, and it will simply
47
00:03:56,520 --> 00:04:00,350
allow us to iterate over all the elements in this area.
48
00:04:00,510 --> 00:04:06,870
And also we need to create additional variable called sum that will sum up everything in this array.
49
00:04:07,050 --> 00:04:09,630
So enti in some equals to zero.
50
00:04:09,630 --> 00:04:12,270
And now we are going to write our for a loop.
51
00:04:12,270 --> 00:04:14,190
So four equal to zero.
52
00:04:14,190 --> 00:04:22,620
Right, because we start from index zero up to I as long as I is less than size I plus plus which is
53
00:04:22,620 --> 00:04:28,840
what we are, that is what we are going to do on every iteration to increment I by one.
54
00:04:28,950 --> 00:04:35,550
So inside of the for loop it's going to be very simple and we actually are going to use just one command.
55
00:04:35,700 --> 00:04:42,120
And this command is going to be to add the previous sum, to added the value of the current value that
56
00:04:42,120 --> 00:04:43,460
we are looking at.
57
00:04:43,770 --> 00:04:47,790
So at every duration we are going to look at specific index.
58
00:04:47,790 --> 00:04:54,930
For example, when I was to zero, we are going to inspect this element when I equals to one this element
59
00:04:54,930 --> 00:04:57,770
and when I equals the two of these elements and so on.
60
00:04:58,200 --> 00:04:59,930
So we are going to submit.
61
00:05:00,260 --> 00:05:09,770
In our some variable, so some equals to our previous some the sum that we have so far, plus the error
62
00:05:09,800 --> 00:05:11,550
and of the eye index.
63
00:05:11,570 --> 00:05:13,360
OK, so very simple.
64
00:05:13,370 --> 00:05:22,010
We could just use this like some equal to PRR an index saying, OK, just in some sort of abbreviation.
65
00:05:22,340 --> 00:05:26,540
I am writing it like this so it will be more clear to you guys.
66
00:05:26,570 --> 00:05:34,730
And basically, once you are done with your for a loop, you are going to have all the sum, the sum
67
00:05:34,730 --> 00:05:37,070
of all the elements in your array.
68
00:05:37,340 --> 00:05:43,760
And what you are going to do is to return to a return just the sum.
69
00:05:43,760 --> 00:05:47,030
Right, because we return the sum of all the elements.
70
00:05:47,360 --> 00:05:48,830
So pretty simple, guys.
71
00:05:49,010 --> 00:05:53,840
You just use here the pointer, pointer, pointer to end.
72
00:05:53,990 --> 00:05:55,640
That is called the error.
73
00:05:55,820 --> 00:06:00,550
It holds the address of the first element inside of the original array.
74
00:06:00,800 --> 00:06:03,560
The actual array that you've created in your main function.
75
00:06:03,680 --> 00:06:06,690
And then you simply iterate over all of its elements.
76
00:06:06,710 --> 00:06:13,940
So basically PRR just points to this area, points to this one, and then when the eye index increments,
77
00:06:13,940 --> 00:06:16,610
you point to this one and then to that one and so on.
78
00:06:16,730 --> 00:06:22,250
So also if we want to make sure that everything works, we can create additional variable inside of
79
00:06:22,250 --> 00:06:23,030
the main function.
80
00:06:23,060 --> 00:06:24,500
Let's call it result.
81
00:06:25,030 --> 00:06:27,860
Result some, let's call it just resolved.
82
00:06:27,860 --> 00:06:30,670
And each should hold the value.
83
00:06:30,950 --> 00:06:33,540
The sum of it is return from these function.
84
00:06:33,800 --> 00:06:38,750
So what we are going to do now is the right result equals OK equals.
85
00:06:39,200 --> 00:06:42,450
And here we are going to specify the function name.
86
00:06:42,710 --> 00:06:42,990
OK.
87
00:06:43,010 --> 00:06:45,530
The function name is some of the way in here.
88
00:06:45,530 --> 00:06:48,560
We are going to pass it to our arguments.
89
00:06:48,590 --> 00:06:55,120
The first argument is the address of the first element, which can be denoted by here by HRR.
90
00:06:55,190 --> 00:07:01,770
And also we can send here the signs which in our example is just three.
91
00:07:01,820 --> 00:07:02,990
So I hope that's clear.
92
00:07:03,230 --> 00:07:11,060
And just to make sure that everything works, let's say a half of the resolve some equals to percentage
93
00:07:11,090 --> 00:07:15,620
D, you know, just to see that it gives us 14 in our example.
94
00:07:15,890 --> 00:07:22,790
And here we are going to specify result and basically there you go.
95
00:07:23,030 --> 00:07:24,720
Let's just build it and run it.
96
00:07:24,770 --> 00:07:28,490
OK, so build in Iran and we have a problem here.
97
00:07:28,540 --> 00:07:30,110
Oops, that's my bad.
98
00:07:30,390 --> 00:07:38,110
I took notes from Python and by mistake I use comments from Python and not from C..
99
00:07:38,210 --> 00:07:43,730
So now we are ready to build and run it and hopefully everything will go smoothly and you can see the
100
00:07:43,730 --> 00:07:46,220
results equals to 14.
101
00:07:46,790 --> 00:07:47,650
Awesome guys.
102
00:07:47,660 --> 00:07:48,530
Great job.
103
00:07:48,560 --> 00:07:56,030
I hope you see how you can use pointers here to pass erase from the main function to some other or other
104
00:07:56,780 --> 00:08:04,010
totally separated function that will make some operations on this array and return some result based
105
00:08:04,010 --> 00:08:04,680
on your needs.
106
00:08:05,030 --> 00:08:06,810
So thank you guys for watching.
107
00:08:06,830 --> 00:08:08,120
It was a good video.
108
00:08:08,120 --> 00:08:10,280
I hope you find this information useful.
109
00:08:10,280 --> 00:08:11,810
Practice on your own.
110
00:08:11,810 --> 00:08:15,170
Keep moving forward and I'll see you in the next video.
10813
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.