Hey everyone, welcome to Simply Learn's YouTube channel. In this session, we will learn about arrays in data structure. But before we begin, make sure that you have subscribed to our YouTube channel and don't forget to hit that bell icon to never miss an update from Simply Learn.
Now, let's get started with the agenda for today's discussion. So, at first we will learn why exactly we need arrays, followed by that we will understand what is an array, then the types of arrays. Next, we have the declaration of arrays, followed by that we will learn how to initialize an array, then access the elements in an array. After that, we will understand the basic operations that can be performed on arrays and to wind up the session, we have the advantages and disadvantages of arrays.
At the end, we'll have some key takeaways. I hope I made myself clear with the agenda. Now, let's begin with the first topic. That is, why did we actually need an array?
So to understand this, let's go through a simple concept. Let us imagine that we have some data and the data is related to students. Right.
So if we wanted to store the marks of individual students, then we had to declare a variable for each of the student. So you can see. We have student 1, student 2, student 3, 4 and 5 and their individual scores.
So, the score 1 is assigned to student 1 and so on with student 5 being assigned to score 5. So, in the real time, this might be a little clumsy. We have here just 5 students. So, that is okay. But what if we had 500 students or 5000 students, right?
That might be an issue. There we have to create some 500 or 5000 variables and it would be a little tough to handle that, right? Even if we wanted to apply some operations collectively on all the variables, that might be a little tedious.
So what if we had one variable which could store all the five students or 500 students or 5000 students all together and make it really easy for us to apply some operations collectively on all the students, right? This sounds a little bit logical. So, similarly, we do have a data structure which can do that, which can collectively store all the data elements in it as a single variable.
And it will also help us to apply the operations which are logical or mathematical on all the variables and their values together as a whole. So, that is the purpose why we had created arrays. That is the purpose why we needed arrays. So far we have understood what was the necessity for arrays.
Now we will understand what exactly are arrays. So basically an array is a linear data structure that stores the elements in a sequential manner, one after the other. And when you declare an array, the variables will be stored one besides the other, that is adjacent to each other in a sequential order.
Now let us understand how the array is represented. by the compiler. Now, the next part, that is the memory representation of arrays. Now, let us imagine that we have declared an array which stores character type data. And another important point to remember about arrays, when you declare an array, it stores only a single data type, typed elements only.
For example, here we have declared a character type array, right? So, we cannot store integer type data or flow type data in it. We only have to save.
character type array or character type elements in this particular array. So, now let us imagine that we have created an array which is of character data type and these are the elements which are a, r, r, a, y. Now, how does the compiler represent it in the form of memory? So, when you create an array, these are formed that is the index values and the address. So, basically compiler will select a block of memory and it has addresses to that block and it can be anything.
Right here we have 11, 12, 13, 14 and 15. And in real time it might be 1001, 1002, 1003, 1004, 1005. The main motto is assign a sequential block of memory. And this is the array and the elements are a, r, r, a, y. And this particular one which is the last one is the index of the array.
That is the position of the array for user. So, using this index, you can access the elements in the array more on that in the accessing part so let us imagine that we wanted to access the element r so how to access it we might not know the address right in that instance we will use the index this is for our reference so we will tell I want the element from array index number 2 and then we'll have the arr element. So, in the practical form, this is how we use the index.
Now, let's continue with the lower bound and the upper bound. So, the first initial location is called as the lower bound and at the last or the max value of the array is called as the upper bound. Now, let us jump into the next topic where we will understand the different types of arrays. So, these are the different types of arrays.
One dimensional arrays and multi-dimensional arrays. In multi-dimensional arrays, we have two more types that is two dimensional arrays and three dimensional arrays. So, how are arrays different from each other?
Right. So, the first one, we will deal with the first one that is the one dimensional array. Now, how is an array declared as one dimensional? For that, we will use the first one. we need subscript.
So, one-dimensional array requires only one subscript to specify the number of elements in an array. So, how is it actually represented? So, here you can see an example right on my screen. So, here is how a one-dimensional array looks like. So, here we have the data type that is integer data type and marks is the name of the array and this is the size of the array and here The elements will be stored and here it is the index value of the array.
And remember, the index value of array always starts from 0. In the previous example, that was only for basic understanding. So, I just started that with 1. In the practical way, the array index always starts from 0. So, followed by the one-dimensional array, we will deal with the multi-dimensional arrays. In that, we have the following. The first one is the two-dimensional array. and the second one is the three-dimensional array.
In the multi-dimensional arrays, we require more than one subscript. In the previous example, we had one dimensional array, so the subscript is just one, that is 10 columns, right? So, we just had one single row and 10 different columns, but in the multi-dimensional arrays, we need multiple rows and multiple columns, right? So, we need two subscripts for two-dimensional array and more than two subscripts for three-dimensional arrays. So, first we have the two-dimensional arrays.
The 2D array is organized in the form of a matrix, which can be represented as a collection of rows and columns. So, here is the example. As you can see on my screen, we have three columns and three rows and we have two subscripts that define the number of rows and the number of columns and the elements in each and every row.
So, the 1, 2, 3 confines the first row, 4, 5, 6 confines the second row and 7, 8, 9 will be the third row. Now, similarly, we have three dimensional arrays. A 3D array is a collection of 2D arrays which consists of three subscripts, block size, row size and column size. So, here we have an example for that. So, we have three subscripts.
That is the first one is block size, the second one is the row number and the third one is column number. So this happens to be the first block, this happens to be the second block. And this is how a three dimensional array can be represented. Now we have the next stage where we will understand how to declare the arrays.
And the syntax is right on my screen. You need to specify the data type of the array which is integer, float or character, any selection which you make. Followed by that you need to select an array name for your array and inside that you need to declare the array size and sometimes you can also eliminate the array size if you wanted to you know have a dynamic array and here is an example for that you have integer data type name of the array is example and the array size is 6. so the compiler will allocate continuous memory block of 6 integer type blocks So, these are the few more methods to initialize the array. So here you can see integer data type, array name is a, element size is 5 and we have 5 elements. The method 2 is, remember I have said you can eliminate the size of the array, right.
So here you can use this way as well. Integer data type, array name a and no size here and you can directly declare the elements. And the method 3rd.
So here you have the name of the array, size of the array and your allocating the elements to the individual memory locations. This is also another method to initialize array and method 4 is using a for loop. You can just initialize a for loop and the i value will be stored in the array locations starting from zero.
Now, let us understand how to access the elements in an array. So, to access the elements in an array, you need to specify the name of the array and memory block where you have that element that you want to access. So, here we have an example which is integer data type. Name of the array is x, y, z and the element we wanted to access is 5. So, here we have an example that is integer type.
array. Array name is xyz and it has the size 5. And now we wanted to access the first element in an array that can be done using the first index that is xyz of 0 index that is the first element and accessing the second element and so on. xyz of 1 will be the second element, xyz of 2, 3, 4 will be the third and fourth and fifth elements. Now, let us understand some basic operations that can be performed on the array elements. So, the following are the operations that can be performed on the array elements.
That is the traversal, insertion, deletion, searching and sorting. Now, let us get into practical mode and try to execute some examples based on traversal of the array, insertion of an element into the array, deletion of an element from an array, searching for an element in an array and sorting the complete array. array.
So at first we'll carry out the traversal operation. So here we have an array that is a of 5 and the elements are 1, 2, 3, 4 and 5. Now we are going to use a for loop to traverse through this array and print all the elements sequentially. Now let us execute this code and see the output. So as you can see the code got successfully executed and the elements that is 1, 2, 3, 4 and 5 from the array a are printed sequentially. Now, moving ahead, we'll try to execute some examples based on insertion of element into the array.
So, on my screen, you can see an example for inserting an element into the array at the starting position of the array. Now, let us try to execute this code and see the output. Don't worry about the codes.
These code documents will be attached in the description box below and you will be able to have an access to it and run these codes in your personal laptop and get a better learning experience. Now you can see the code got successfully executed and now it is asking for the size of the array. Let's declare it as 5 and now let's enter the elements into the array. Now it is asking for the element to be added at the beginning of the array.
Now we have 2, 3, 4, 5 and 6 existing in the array. Let's add the first element 1 and there you go. The resultant array is 1, 2, 3, 4, 5 and 6. The element which we added at the ending, which is 1, is supposed to be in the first or the beginning part of the array, which is here at the first index location as planned.
Now, let's get started with another example on the insertion. Now, let's try to insert an element at any given location as per the user's choice. So, as you can see, we have a program on my screen and running this program must help us to insert an element into any given location of the array.
Now let's try to quickly run this program and see the output. So there you go. Enter the elements into the array.
Let's enter 1, 3. Let's try to run this program and see the output. So there you go. The program got successfully executed and now the output is asking for us to tell it the number of elements in an array. Now let's say 4 elements.
So now let's enter the four elements 1, 3, 4, 5 and 6. Okay, so the four elements which is 1, 3, 4, 5 are been inserted. Now it is asking for the location where you want to insert the element. Now let us insert it in the second location.
Now let us set the value as 2 and there you go. The resultant array is 1, 2, 3, 4 and 5 and the element which is 2, was supposed to be added in the second location and it has been successfully added. Now, let's try to execute another example based on insertion, where you will try to insert the element in the last position.
Now, this program on my screen will help us to add an element at the end of the array. Let's quickly run this program. Now, it is asking for us to enter the five elements of the array. Let's enter that.
Now, it is asking to enter the element to be inserted. Now let us enter the element and this element should be added at the end location as discussed. The resultant array is 1, 3, 5, 7, 9 and 11. Now let's continue with the deletion operation.
Now the program on my screen is an example for deleting an element from the beginning position of an array. The program got successfully executed and it's asking for us to enter the size of the array. Let's enter the size as 5 and now let's enter the elements of the array that is 1, 2, 3, 4 and 5. Now the element from the first location that is 1 has been deleted and the resultant array is 2, 3, 4 and 5. Now, the code on my screen will help you to eliminate the element from the ending location of an array. Now, let's try to quickly run this program and see the output.
So you can see that the program is successfully run and it is asking for us to enter the size of the array. Let's enter 4. Now, let's enter the elements of the array. That is 1, 2, 3 and 4. Now, after eliminating the last element from the array, that is 4, we have the resultant array that is 1. 2 and 3. Now a little homework for you guys.
Let's try to execute an example for deleting an element from any location of an array. This will be a homework. Now don't worry, try on your own and if you don't get that you can refer to the comment sections below where you have the code document attached in the description box and you can refer to it. Now let's continue with the next operation where we will search for a given element in the array. So, you can see that on my screen we have an example which will help us to sort the elements in an array.
Let's quickly run this. So, there you go. The program got successfully executed and now it's asking for the array size. Let's enter 5 and now let's enter the elements in a random order.
Now, the resultant array should be sorted. So there you go. The array after being sought is, it's 2, 3, 5, 14 and 21. So there you go, the array got sorted. So now we have finished our practical session. Let's have a quick overview again.
So traversal in an array is a process of visiting each element once. Traversal can be done by various means, that is by counting the array elements, printing the values sorted in the arrays and some of all elements present in the array and many more. So, this is how we traverse our array from the first location to the last location. Again, the insertion operation. Insertion in an array is the process of including one or more elements in an array and it can be done at the beginning, at the end and at any given index of an array which we executed for all the three types of examples.
Now, next we have the deletion operation. So, deletion operation or deletion of an element is a process of removing a desired element and reorganizing the array. And it can be done at the beginning, at the end and at any given index.
So, we have executed the example for at the beginning and at the end. And the homework was at the given index. Don't worry if you don't figure it out, you can always refer to the example document which is mentioned in the description box below. Now, the search operation.
Searching is a process of finding a given value in the list of values and it decides whether the search key is present in the array or not and we have executed an example on that. And again, the sort operation, where we will sort the given array. If the given array is not in the default order or in a given order, that is ascending or descending, then we can sort the order of the array by the user's choice, given it is ascending order or descending order. Now, in the next section of the tutorial, we will discuss the advantages of using arrays.
So, the first one is, array stores multiple elements of same data type with same name. Next one, elements in an array can be accessed randomly using just the index number. Array memory is predefined so there is no extra memory loss. Array avoids memory overflow. And finally, 2D arrays can represent the tabular form of data in a very efficient way.
Now let us also have a look on some of the disadvantages of using arrays. The first one, the number of elements in an array should be always predefined. So there is no chance that you can add some extra element in the last moment. Array is static in nature. So, its size cannot be varied after declaration.
What if, consider that you wanted to store 10 student data in one array and you end up having just 7 students. So, the remaining 3 is a waste, right? So, you are wasting memory there. That's the problem.
And insertion and deletion operations in an array is quite difficult as the array stores elements in a continuous form. And finally, allocating excess memory than required may lead to memory wastage. Now we have the final key takeaways of the arrays. So we discussed why do we need arrays, then we discussed what are arrays, followed by that we discussed types of arrays. Next we had declaration of arrays, accessing elements from an array, initializing the arrays and basic operations on array that is the practical demo part and finally we have discussed the advantages and disadvantages of using an array.
Now, with that we have come to an end of this tutorial and if you have any queries regarding the topics covered in this tutorial or if you need the quotes that are executed in this particular tutorial, then please feel free to ask them down in the comment section below and our team of experts will be happy to resolve all your queries. Until next time, thank you, stay safe and keep learning. Hi there, if you like this video, subscribe to the Simply Learn YouTube channel and click here to watch similar videos.
To nerd up and get certified, click here.