1 00:00:05,660 --> 00:00:09,360 In this video, we'll learn about arrays and their characteristics. 2 00:00:10,060 --> 00:00:12,660 We'll also see the typical use case for an array. 3 00:00:13,560 --> 00:00:16,960 An array is a compound data type or a structured data type. 4 00:00:17,460 --> 00:00:21,460 This means that it's a data type that's made up of other data types. 5 00:00:21,460 --> 00:00:25,260 For example, a collection of test scores where each test score 6 00:00:25,260 --> 00:00:29,360 is an integer is considered a compound data type made up of integers. 7 00:00:30,260 --> 00:00:32,759 We say that this is an array of integers. 8 00:00:33,160 --> 00:00:37,260 It's important to understand that all elements of an array must be of the same type. 9 00:00:37,560 --> 00:00:40,160 And each element can be accessed directly. 10 00:00:41,160 --> 00:00:43,760 We'll see some other array characteristics shortly, 11 00:00:43,760 --> 00:00:46,760 but first, let's see why we use arrays and why they're so useful. 12 00:00:48,060 --> 00:00:50,860 Suppose we need to model my test scores in a class. 13 00:00:50,860 --> 00:00:55,360 That's pretty easy. We've already seen how to use variables and this would be a perfect use of variables. 14 00:00:55,860 --> 00:00:58,360 So suppose I have three test scores that I need to track, 15 00:00:58,860 --> 00:01:01,160 and they're all graded as integers out of a 100. 16 00:01:01,860 --> 00:01:06,120 I can simply declare three variables as integers, give them meaningful names, 17 00:01:06,120 --> 00:01:09,120 initialize them to 0 or to whatever values I wish. 18 00:01:09,720 --> 00:01:12,710 But what if I need to model more than just a few test scores. 19 00:01:13,590 --> 00:01:17,590 Suppose I'm a school administrator, and I need to model the test scores for 20 00:01:17,590 --> 00:01:18,950 100 students, 21 00:01:18,950 --> 00:01:20,950 so that I could do some statistics on them. 22 00:01:21,450 --> 00:01:25,650 Modeling each one of them as its own variable with its own name 23 00:01:25,650 --> 00:01:27,250 can quickly get out of control. 24 00:01:28,050 --> 00:01:31,050 Now I have to keep track of 100 variable names. 25 00:01:31,600 --> 00:01:34,200 You could still do it but it becomes tedious and error prone. 26 00:01:35,100 --> 00:01:39,090 What if I had 10,000 test scores to model? You get the idea. 27 00:01:39,090 --> 00:01:41,390 This is a typical use case for an array 28 00:01:41,590 --> 00:01:43,590 and that's when they're really, really useful. 29 00:01:44,590 --> 00:01:46,690 Arrays allow you to tell the compiler 30 00:01:46,690 --> 00:01:49,890 to give you a collection of as many elements as you want 31 00:01:49,890 --> 00:01:52,190 and give the entire collection a single name. 32 00:01:52,490 --> 00:01:55,490 So you no longer have to name each individual element. 33 00:01:55,490 --> 00:01:58,490 Let's go over some of the characteristics or properties of arrays. 34 00:01:59,090 --> 00:02:03,890 Arrays are fixed in size. Once you create an array of a specific size, 35 00:02:03,890 --> 00:02:05,790 the size cannot change. 36 00:02:05,790 --> 00:02:10,389 So if you create an array of 100 test scores and then you need space for more than that, 37 00:02:10,590 --> 00:02:15,580 you'd have to go back to your source code, change the max size and recompile. 38 00:02:16,580 --> 00:02:20,880 The size of the array is fixed. Arrays will not grow or shrink on their own while 39 00:02:20,880 --> 00:02:21,880 the program is running. 40 00:02:22,580 --> 00:02:26,480 As mentioned earlier, all the elements of an array must be of the same type. 41 00:02:26,480 --> 00:02:30,680 So we can have arrays of integers, arrays of doubles, arrays of persons, 42 00:02:30,680 --> 00:02:32,780 arrays of accounts and so forth. 43 00:02:32,780 --> 00:02:35,080 Arrays are stored contiguously in memory. 44 00:02:35,580 --> 00:02:39,080 So if you tell the compiler that you need an array of 100 integers, 45 00:02:39,080 --> 00:02:43,440 the compiler will generate code that allocates exactly 100 integers 46 00:02:43,440 --> 00:02:44,800 that are contiguous in memory. 47 00:02:45,700 --> 00:02:48,700 That means that the memory will be allocated as one chunk. 48 00:02:49,100 --> 00:02:51,300 Array elements are individually accessible 49 00:02:51,300 --> 00:02:53,800 so I can access any test score that I wish. 50 00:02:54,500 --> 00:02:58,160 You can do that using the position or index of that array element. 51 00:02:58,160 --> 00:03:01,260 So I can tell the compiler to give me the second element in the array 52 00:03:01,260 --> 00:03:03,260 or the 1500th element in the array. 53 00:03:04,060 --> 00:03:06,960 Be aware that array indexes start at 0. 54 00:03:07,510 --> 00:03:12,500 This means that the first element in an array is at position 0, not at position 1. 55 00:03:13,100 --> 00:03:16,400 And the last element is at position size -1. 56 00:03:17,280 --> 00:03:19,480 You can see an example in the figure on the right. 57 00:03:19,880 --> 00:03:22,080 Here we have an array of 10 test scores. 58 00:03:22,080 --> 00:03:26,580 The array is named test scores and it's indexed from 0 to 9. 59 00:03:27,080 --> 00:03:31,070 Many, many programming languages have compound data types like arrays, 60 00:03:31,070 --> 00:03:34,170 and the majority of them start their array indexes at 0. 61 00:03:34,670 --> 00:03:38,900 I know that most of us start counting at 1. With arrays, you must start at 0. 62 00:03:39,500 --> 00:03:43,500 This is a common cause of errors and programs, especially with beginning programmers. 63 00:03:44,500 --> 00:03:47,700 It's also important to understand that c++ 64 00:03:47,700 --> 00:03:49,300 arrays are not bounds checked. 65 00:03:50,000 --> 00:03:54,200 That means that there's no check to make sure that when you access an individual array element 66 00:03:54,200 --> 00:03:56,700 that it's between 0 and size -1. 67 00:03:57,500 --> 00:04:02,000 It's the programmer's responsibility to be sure that you don't go over bounds. 68 00:04:02,500 --> 00:04:06,300 If you go overbound, your program may have strange behavior and could even crash. 69 00:04:07,400 --> 00:04:11,000 Finally, in a few sections, we'll talk about iteration or looping. 70 00:04:11,500 --> 00:04:15,400 As you can imagine, looping is used all the time with array since we often need to loop 71 00:04:15,400 --> 00:04:17,700 through the arrays and process every element. 72 00:04:18,060 --> 00:04:21,060 Just like variables, always initialize the arrays 73 00:04:21,060 --> 00:04:23,060 or they'll contain unknown values. 74 00:04:23,260 --> 00:04:26,360 Arrays are extremely efficient in c++ since they are 75 00:04:26,360 --> 00:04:28,360 bare bones raw arrays. 76 00:04:28,360 --> 00:04:31,960 Later in this section, we'll talk about c++ vectors, 77 00:04:31,960 --> 00:04:36,160 which are super powerful dynamic and safer to use in arrays. 78 00:04:36,660 --> 00:04:39,160 In the next video, we'll see how to declare arrays.