All language subtitles for 4. Arrays – Element Accessing

af Afrikaans
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bn Bengali
bs Bosnian
bg Bulgarian
ca Catalan
ceb Cebuano
ny Chichewa
zh-CN Chinese (Simplified)
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
tl Filipino
fi Finnish
fr French
fy Frisian
gl Galician
ka Georgian
de German
el Greek
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian Download
is Icelandic
ig Igbo
id Indonesian
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
km Khmer
ko Korean
ku Kurdish (Kurmanji)
ky Kyrgyz
lo Lao
la Latin
lv Latvian
lt Lithuanian
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mn Mongolian
my Myanmar (Burmese)
ne Nepali
no Norwegian
ps Pashto
fa Persian
pl Polish
pt Portuguese
pa Punjabi
ro Romanian
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
st Sesotho
sn Shona
sd Sindhi
si Sinhala
sk Slovak
sl Slovenian
so Somali
es Spanish
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
te Telugu
th Thai
tr Turkish
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu
or Odia (Oriya)
rw Kinyarwanda
tk Turkmen
tt Tatar
ug Uyghur
Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated: 1 00:00:00,360 --> 00:00:01,310 All right, guys. 2 00:00:01,380 --> 00:00:06,870 So know that you know how to declare an array and also you know how to initialize an array. 3 00:00:07,290 --> 00:00:12,480 That's time that we learn how we can access each of its elements. 4 00:00:12,600 --> 00:00:18,270 So, first of all, we need to understand that an array is just a collection of fail amounts. 5 00:00:18,750 --> 00:00:23,130 We also said that these elements are organized in some manner. 6 00:00:23,850 --> 00:00:25,200 How are they organized? 7 00:00:25,320 --> 00:00:28,320 Simply in some consecutive manner? 8 00:00:28,390 --> 00:00:31,020 Simply one after the other. 9 00:00:31,620 --> 00:00:40,020 And we can refer and access any of these elements by using their, let's say, location within this 10 00:00:40,080 --> 00:00:41,010 given array. 11 00:00:41,100 --> 00:00:48,240 So we can say that every element, every element in an array has its location. 12 00:00:48,600 --> 00:00:51,870 And this location is called an index. 13 00:00:51,990 --> 00:00:58,740 And in C land, which we say that the first element is located at index zero. 14 00:00:58,770 --> 00:00:59,130 OK. 15 00:00:59,310 --> 00:01:00,530 So it index zero. 16 00:01:00,540 --> 00:01:02,460 We have our first element. 17 00:01:02,580 --> 00:01:09,300 And the second element will be, of course, located under index number one and so on and so forth. 18 00:01:09,390 --> 00:01:10,440 You've got the idea. 19 00:01:10,500 --> 00:01:10,910 OK. 20 00:01:11,400 --> 00:01:17,820 So if you have an array with five elements total, then the last index will be. 21 00:01:18,090 --> 00:01:19,620 Think about it for a second. 22 00:01:19,690 --> 00:01:20,130 Okay. 23 00:01:20,150 --> 00:01:23,040 WOR just take a look at the screen right now. 24 00:01:23,370 --> 00:01:26,460 And you are right, the index of the last element. 25 00:01:27,060 --> 00:01:30,780 The fifth element in this array will be four. 26 00:01:30,840 --> 00:01:35,040 OK, so the first element will be at index zero. 27 00:01:35,250 --> 00:01:40,260 And the last element will be at index size minus one. 28 00:01:40,320 --> 00:01:46,530 The size of the array which in this example is five minus one, which is a total of four. 29 00:01:46,860 --> 00:01:49,050 So that's how you find of that. 30 00:01:49,410 --> 00:01:50,710 That's how you treat Aurora. 31 00:01:50,850 --> 00:01:58,560 That's shown some guidelines that the first element is at index zero and the last element is an index 32 00:01:58,770 --> 00:02:00,450 size minus one. 33 00:02:00,540 --> 00:02:07,230 Please note that indexes should always been to jurors and also make sure that you do not exceed the 34 00:02:07,230 --> 00:02:08,460 size of the array. 35 00:02:08,910 --> 00:02:11,910 When you try to access one of its elements. 36 00:02:11,970 --> 00:02:18,600 For example, if you have an array with five elements, then your last element in the array will be 37 00:02:18,600 --> 00:02:21,930 at index four and not at index five. 38 00:02:22,020 --> 00:02:27,480 So make sure you're not trying to access different addresses that do not belong to you. 39 00:02:27,930 --> 00:02:34,280 Otherwise, if you don't pay attention to it, you may end up with an access violation error. 40 00:02:34,440 --> 00:02:40,620 And that's simply because your program is not intended for working with the address you are trying to 41 00:02:40,620 --> 00:02:41,050 access. 42 00:02:41,100 --> 00:02:41,370 OK. 43 00:02:41,430 --> 00:02:51,030 These value or data that is stored on their Index five does not belong to your program and that's why 44 00:02:51,030 --> 00:02:54,530 you are going to get an access violation error. 45 00:02:54,660 --> 00:02:56,280 Just like in real life. 46 00:02:56,310 --> 00:02:59,400 Don't try to access what's not yours. 47 00:02:59,520 --> 00:03:06,630 And because the compiler is not always going to warn you about your attempts to access things that do 48 00:03:06,630 --> 00:03:11,490 not belong to you, these responsibilities on you, the developer. 49 00:03:11,580 --> 00:03:17,190 So now we know that indexes specify the location of elements within the array. 50 00:03:17,670 --> 00:03:21,840 So let's create a simple Interrail it's created and initialize it. 51 00:03:21,900 --> 00:03:24,330 OK, so array initialization. 52 00:03:24,390 --> 00:03:25,800 So how it looks like. 53 00:03:25,950 --> 00:03:32,640 So first of all, you specify Ent., which simply tells what will be the type of all of the elements 54 00:03:32,640 --> 00:03:33,750 inside of this array. 55 00:03:34,140 --> 00:03:36,230 Then you use of the array name. 56 00:03:36,330 --> 00:03:39,660 Square brackets to specify that it's an array. 57 00:03:40,140 --> 00:03:45,510 And in the curly brackets you specify all the values that this array will have. 58 00:03:45,540 --> 00:03:45,750 OK. 59 00:03:45,810 --> 00:03:48,030 We didn't even specify the size of the array. 60 00:03:48,300 --> 00:03:53,640 But it knows by itself well how to fill up this array. 61 00:03:53,670 --> 00:03:58,290 So on the left side, you can see this syntax in our C programming language. 62 00:03:58,500 --> 00:04:03,420 And on the right right side, you can see basically what happens behind the scenes. 63 00:04:03,450 --> 00:04:07,290 OK, how of the array is stored in the memory? 64 00:04:07,380 --> 00:04:15,000 And now if we want to talk about how you can access one of of the elements of the array or any of the 65 00:04:15,000 --> 00:04:20,820 elements in this array, you can simply do these by specifying the index of the element that you want 66 00:04:20,820 --> 00:04:21,600 to access. 67 00:04:21,900 --> 00:04:23,850 In square brackets. 68 00:04:23,900 --> 00:04:24,190 OK. 69 00:04:24,510 --> 00:04:26,550 So you specify the array name. 70 00:04:26,760 --> 00:04:28,740 First of all, you specify the array name. 71 00:04:29,100 --> 00:04:35,400 And then you open square brackets and feeling the index of your desired element. 72 00:04:35,760 --> 00:04:38,130 And then, of course, you close the square rockets. 73 00:04:38,490 --> 00:04:43,550 So, for example, we can see here that we use the test the way we've initialized previously. 74 00:04:43,620 --> 00:04:45,480 This is the name of the array. 75 00:04:46,020 --> 00:04:53,130 And in the square brackets, we use the number two of the index two, which simply says go to test the 76 00:04:53,130 --> 00:04:55,210 ray to location. 77 00:04:56,070 --> 00:04:58,560 Number two or two index still. 78 00:04:58,890 --> 00:04:59,880 And take the. 79 00:04:59,960 --> 00:05:02,030 Value that is stored there. 80 00:05:02,120 --> 00:05:04,670 Which in this case is just three. 81 00:05:04,760 --> 00:05:12,620 So now once you know how to access you any value within a given array, you can access, change it, 82 00:05:13,160 --> 00:05:14,840 modify however you like. 83 00:05:14,870 --> 00:05:19,970 You can read from the user using scanner function and so on. 84 00:05:20,090 --> 00:05:24,230 So how would you change the value of one of the elements? 85 00:05:24,260 --> 00:05:28,000 Basically what you have to do is the first of all, access. 86 00:05:28,070 --> 00:05:32,830 First of all, you access the element by using the array name and its index. 87 00:05:32,870 --> 00:05:33,010 OK. 88 00:05:33,140 --> 00:05:39,320 So let's say we want to access the last element and to change it to be nine. 89 00:05:39,440 --> 00:05:41,480 So that's how you access it. 90 00:05:41,810 --> 00:05:42,800 The last element. 91 00:05:43,250 --> 00:05:47,570 And now you can use the assignment operation to assign a new value to it. 92 00:05:47,600 --> 00:05:51,470 So, for example, we'll just use equals two nine. 93 00:05:51,560 --> 00:05:53,120 And what it will do. 94 00:05:53,420 --> 00:05:57,890 It will modify from 10 to just nine. 95 00:05:57,950 --> 00:06:00,570 OK, so that's how you access and modify. 96 00:06:01,090 --> 00:06:05,240 OK, that's an example of how we modify a value within a given array. 97 00:06:05,990 --> 00:06:12,390 And also we can do pretty much the same if we want to print out some value of some element within visa 98 00:06:12,390 --> 00:06:12,730 rate. 99 00:06:12,830 --> 00:06:18,410 So, for example, we can print out the value of the first element of the array. 100 00:06:18,530 --> 00:06:21,980 And we just access it just like we've done it previously. 101 00:06:21,980 --> 00:06:26,510 So we specify the array name and the index we want to access. 102 00:06:26,810 --> 00:06:29,150 And then we simply access its value. 103 00:06:29,180 --> 00:06:36,320 So in this case, what will be printed is just to write, because two is the value off of the first 104 00:06:36,320 --> 00:06:37,520 element in this array. 105 00:06:37,640 --> 00:06:38,090 OK. 106 00:06:38,140 --> 00:06:44,040 So now if we want to print out idea of the second value on this array. 107 00:06:44,060 --> 00:06:53,210 The second element, we will use dest array and index number one, which will print simply five to the 108 00:06:53,210 --> 00:06:53,570 screen. 109 00:06:53,690 --> 00:06:55,010 Good, good, good guys. 110 00:06:55,070 --> 00:07:03,800 And now you know how to access, modify and print the values of elements within a given array. 111 00:07:03,920 --> 00:07:10,430 And now it's time to write a simple program that will create an array of, let's say, five grades, 112 00:07:10,820 --> 00:07:18,260 get the grades as input from the user and find out what is the lowest grade in this given array. 113 00:07:18,410 --> 00:07:21,990 Let's just go over the code here line by line. 114 00:07:22,040 --> 00:07:26,120 So first of all, we include the standard input output library. 115 00:07:26,720 --> 00:07:28,940 Then we start our main function. 116 00:07:29,150 --> 00:07:36,590 And now what we're doing, if we is we declare are a an array of in the jurors. 117 00:07:36,590 --> 00:07:40,640 And the name of this array is a grades and it's of size five. 118 00:07:41,920 --> 00:07:50,020 And what we are also going to do is to create an AI variable that will help us using the duration of 119 00:07:50,260 --> 00:07:55,960 the loop to read the valleys, the grades from the user at every time. 120 00:07:56,100 --> 00:08:02,200 And also what we are going to do is to create another additional variable called lowest grade. 121 00:08:02,560 --> 00:08:08,110 And it will store the lowest grade so far while we run our loop. 122 00:08:08,200 --> 00:08:12,910 So let's create our for a loop because we know it should be done only five times. 123 00:08:12,970 --> 00:08:18,520 So for I equals to zero as long as I is less than five. 124 00:08:18,550 --> 00:08:22,180 Then on every iteration, we are going to increment I by one. 125 00:08:22,600 --> 00:08:28,320 And in the loop, buddy, we are going to specify these following message after grade number. 126 00:08:28,540 --> 00:08:32,710 And instead of these percentages, we we are just going to specify plus one. 127 00:08:32,740 --> 00:08:39,040 So the first iteration will have enter grade number one and their array number two and so on and so 128 00:08:39,040 --> 00:08:44,560 forth and on every duration we are going to read the value from the user. 129 00:08:44,680 --> 00:08:53,110 The grade he inserts and storeyed inside are its corresponding position, its course corresponding location 130 00:08:53,350 --> 00:08:56,010 inside of these grades array. 131 00:08:56,050 --> 00:09:01,780 So we as the ampersand to specify the address where grades aren't the index. 132 00:09:01,840 --> 00:09:08,830 I reside resides and we are going to put inside this location. 133 00:09:08,860 --> 00:09:13,840 This element, the value that we've read from the user, from the console application. 134 00:09:13,930 --> 00:09:22,240 So once we are done with our for a loop, we are basically going to add to initialize kind of far set 135 00:09:22,240 --> 00:09:26,830 some value, some default value, which is a must step to the lowest grade. 136 00:09:27,100 --> 00:09:32,290 So we will assume that the lowest the grade is our first grade that a user inserted. 137 00:09:32,410 --> 00:09:36,970 And then what we are going to do is to create another or for a loop. 138 00:09:37,020 --> 00:09:37,390 Okay. 139 00:09:37,720 --> 00:09:40,240 That runs from I equals to one. 140 00:09:40,270 --> 00:09:42,040 As long as is less than five. 141 00:09:42,430 --> 00:09:49,330 And we are going to see if there is any lower grade inside of these array of grades. 142 00:09:49,810 --> 00:09:57,700 And if that's the case, if we find that some element in this array is lower than the lowest grade so 143 00:09:57,700 --> 00:10:01,480 far, we are just going to update the lowest. 144 00:10:01,530 --> 00:10:08,380 Great to be equal to these particular grade because we are looking for the lowest grade inside of this 145 00:10:08,530 --> 00:10:09,400 array of grades. 146 00:10:09,520 --> 00:10:15,430 And once we are done with the second loop, we are going simply to print the lowest grade to the screen. 147 00:10:15,460 --> 00:10:21,820 So we know that at this point, at this print off-line, we are over. 148 00:10:22,060 --> 00:10:29,800 These two are for loops and we know that lowest grade that these variable will hold the lowest grade 149 00:10:30,040 --> 00:10:32,130 in these grades array. 150 00:10:32,230 --> 00:10:36,020 So if we take a look at how some some example. 151 00:10:36,050 --> 00:10:36,340 OK. 152 00:10:36,670 --> 00:10:38,350 So enter grade number one. 153 00:10:38,590 --> 00:10:40,070 Let's say seventy eight. 154 00:10:40,090 --> 00:10:41,370 Grade number and grade. 155 00:10:41,500 --> 00:10:44,760 Number two, 60, 80. 156 00:10:44,770 --> 00:10:47,380 And let's say 90 and a hundred. 157 00:10:47,440 --> 00:10:47,680 OK. 158 00:10:48,070 --> 00:10:53,230 So after inserting all these grades, we will have an array of five elements. 159 00:10:53,350 --> 00:10:56,410 And the first element will be equal to 70. 160 00:10:56,440 --> 00:11:00,320 The second one to 60, 80, 90 and a hundredth. 161 00:11:00,670 --> 00:11:03,310 This will be our array of grades. 162 00:11:03,550 --> 00:11:10,360 Now, the second iteration is going to run and to find out what is the lowest grade inside of this array 163 00:11:10,630 --> 00:11:17,530 and a corresponding message with your lowest grade is 60 will be printed to the screen. 164 00:11:17,800 --> 00:11:24,040 And we can see that it's exactly what we expected our program to find out, because visually we can 165 00:11:24,040 --> 00:11:24,460 see. 166 00:11:24,580 --> 00:11:30,000 Right, because it's a simple program that 60 is the lowest grade. 167 00:11:30,130 --> 00:11:35,710 Now, once you've got all your grades and you've found out what is the lowest grade, you may also want 168 00:11:35,710 --> 00:11:41,030 to print out all the grades to the screen just to take another look at them, for example. 169 00:11:41,060 --> 00:11:41,300 OK. 170 00:11:41,680 --> 00:11:42,980 So how will you do it? 171 00:11:43,000 --> 00:11:44,710 How will you accomplish these tasks? 172 00:11:44,830 --> 00:11:49,580 Can you simply use just one print off-line to print the whole array? 173 00:11:49,600 --> 00:11:50,590 Something like this. 174 00:11:50,590 --> 00:11:57,970 Print f your grades are percentage the and specified grades and we know that grades is in an array. 175 00:11:58,090 --> 00:12:04,300 Come on, you can, you can go and try this one on your own to run this command and see what happens. 176 00:12:04,750 --> 00:12:09,850 And basically what you're probably going to see is that not all the grades were printed out. 177 00:12:10,420 --> 00:12:18,160 We have a total of five of these elements, but only one strange value was printed maybe on your screen. 178 00:12:18,160 --> 00:12:25,690 It's some other value, but it's still very strange why some strange value was printed out. 179 00:12:26,110 --> 00:12:30,970 If we have some grades and it's five elements. 180 00:12:31,030 --> 00:12:32,140 So why is that? 181 00:12:32,230 --> 00:12:38,180 So if we take a look at the following array, let's say that these are the grades the user specified. 182 00:12:38,680 --> 00:12:40,960 We can clearly see that this number. 183 00:12:41,480 --> 00:12:45,310 On the screen is actually not even a part of our ouray, okay? 184 00:12:45,710 --> 00:12:46,950 It's not a value. 185 00:12:46,950 --> 00:12:48,830 It does not represent any value. 186 00:12:49,220 --> 00:12:54,320 So what is this number you see when we declare an array or anything else? 187 00:12:54,620 --> 00:13:01,580 It will reside somewhere in the computer's memory and it should have some unique address where, for 188 00:13:01,580 --> 00:13:03,780 example, it can be stored. 189 00:13:03,810 --> 00:13:03,930 OK. 190 00:13:04,010 --> 00:13:05,090 Some unique place. 191 00:13:05,210 --> 00:13:12,280 So this way, the way we've just seen that we can print our grades, how we've done it are simple. 192 00:13:12,300 --> 00:13:17,450 We simply printed out the address of the grades array. 193 00:13:18,050 --> 00:13:24,050 And although it's nice that we can print the actual addressing memory of a given variable of a given 194 00:13:24,050 --> 00:13:24,470 array. 195 00:13:24,860 --> 00:13:26,390 That's not what we wanted. 196 00:13:26,960 --> 00:13:31,100 That's another way to print all the arrays, elements or values. 197 00:13:31,610 --> 00:13:33,230 So how should we do it? 198 00:13:33,320 --> 00:13:36,430 So there is no magic in C programming language. 199 00:13:36,470 --> 00:13:41,810 If you want to print all the elements of the array, you must go over each and every one of them. 200 00:13:42,230 --> 00:13:44,290 And this can be done using loops. 201 00:13:44,330 --> 00:13:44,810 All right. 202 00:13:45,200 --> 00:13:52,010 So first of all, let's use a for loop to iterate over all the elements of this array from index zero 203 00:13:52,040 --> 00:13:55,130 up to index for five is not included here. 204 00:13:55,140 --> 00:13:55,490 Right. 205 00:13:55,580 --> 00:14:02,510 And here we know that I will represent the index of each of the elements in these array as well as it 206 00:14:02,510 --> 00:14:04,970 will also allow us to access any of them. 207 00:14:05,540 --> 00:14:11,380 So if we want to access and print the values, we can use that print off-line. 208 00:14:11,450 --> 00:14:14,540 OK, so print F grades at the index. 209 00:14:14,600 --> 00:14:19,280 I equals to the value of this particular index. 210 00:14:19,760 --> 00:14:25,920 So Van, on the first thing duration we are going to print grades at the index of zero equals to 70. 211 00:14:26,300 --> 00:14:31,250 Then on the second iteration equals 60 to 100. 212 00:14:31,270 --> 00:14:32,660 And then to 80. 213 00:14:32,750 --> 00:14:34,160 And so on. 214 00:14:34,160 --> 00:14:37,070 Until the last element which is 90. 215 00:14:37,130 --> 00:14:37,380 OK. 216 00:14:37,450 --> 00:14:41,930 So that's how you print all the elements of your given array. 217 00:14:42,290 --> 00:14:49,820 And it doesn't matter if this array is just an integer array or characters array or floating point array. 218 00:14:50,390 --> 00:14:56,540 The structure of printing all the elements of this array will be pretty much the same. 219 00:14:56,690 --> 00:15:02,570 And also, if you would like to print the elements of the array starting from the last element up to 220 00:15:02,570 --> 00:15:06,290 the first and not vice versa, what would you do? 221 00:15:06,710 --> 00:15:16,130 So this this approach of just starting from the last index and then on discriminating one are from from 222 00:15:16,130 --> 00:15:20,180 the index going to the left by the index is very straightforward. 223 00:15:20,780 --> 00:15:27,830 In this case, we simply going to print all the elements starting from the last index to the first one. 224 00:15:28,250 --> 00:15:32,240 And this index in our example is simply four. 225 00:15:32,450 --> 00:15:34,790 So I hope that's clear to you guys. 226 00:15:34,940 --> 00:15:40,760 And now for the end, let's talk about how all the elements of the array are arranged. 227 00:15:41,300 --> 00:15:45,140 Basically, array is a consecutive sequence of elements. 228 00:15:45,590 --> 00:15:52,100 The name of the array in our example is grades, and it actually represents the address of the first 229 00:15:52,100 --> 00:15:53,870 element of that array. 230 00:15:53,960 --> 00:16:00,620 Right after the first element of this array lies the second one in in sort of a consecutive manner. 231 00:16:01,100 --> 00:16:07,820 So the addresses in memory of the elements of the array are simply organized one after another. 232 00:16:08,330 --> 00:16:11,500 You can see that the address jumps by four. 233 00:16:11,510 --> 00:16:11,860 Right. 234 00:16:12,380 --> 00:16:17,330 And the reason for that is because we have an array of grades, an array of integers. 235 00:16:17,690 --> 00:16:22,100 So we know that each element in this array is off an integer type. 236 00:16:22,190 --> 00:16:25,460 And we know that an integer is just four bytes. 237 00:16:25,580 --> 00:16:29,270 So do store an integer in terms of memory usage. 238 00:16:29,540 --> 00:16:30,950 You need four bytes. 239 00:16:31,340 --> 00:16:36,110 That's why the address of the second element is greater by four than the first. 240 00:16:36,500 --> 00:16:40,400 That's the difference between every two AGC and two elements. 241 00:16:40,520 --> 00:16:48,710 So I hope this video makes it clear to how you can initialize, declare you as read, modify axis and 242 00:16:48,710 --> 00:16:49,190 so on. 243 00:16:49,580 --> 00:16:54,680 Basically how you can do most of the operations using arrays. 244 00:16:55,130 --> 00:16:57,580 So this is it for these video guys. 245 00:16:57,710 --> 00:17:06,860 I think we've covered it up a lot in a very short time and probably we are very close to practicing. 246 00:17:07,460 --> 00:17:09,050 So have a good day. 247 00:17:09,500 --> 00:17:13,190 Keep on moving forward and I'll see you in the next video. 23332

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.