 Hello everyone. I'm Sanjay Gupta. I welcome you on Sanjay Gupta Tech School. So today in this C and C++ coding bootcamp, we are going to discuss about 1D array. So this is day 14 and I hope till now you have gone through all the videos. Those were related to different topics which are available in C programming, right? So if you have any question in previous session, do utilize the telegram group whose link is available in the description of this video and session tracker link is also available in the description of this video. And if you are for the first time on this YouTube channel and if you want to know about your instructor. So with this slide, you will be able to know like who I am and what I'm doing, what I have done. So you will get to know everything about me with this slide. So consistency is the key. So please do regular practice and follow all the session. And I'm sure if you're a beginner, no matter whatever is your background, technical, non-technical, if you want to learn your first programming language, so I think C programming is the best way to understand or start the programming, right? So do regular practice and session tracker link is available in the description of this video. So here you will see lots of sessions I have already delivered and their links are available, right? And some more additional video links I added so you can just go through those. And if you have any kind of question, so with this telegram group, like you can just scan the QR code and you can become the part of that telegram group. So follow Sanjay Gupta Tech School on YouTube, LinkedIn, Instagram or Telegram and all the important links are available in the video's description. Okay, so with this, let's start arrays and C programming, right? So till now we have gone through how we can write conditional statements, how we can write iterative statement with the help of loop. Now array is very, very different in programming and everybody needs to understand the uses of array because these are used in other programming languages as well. So in some programming languages, they are known as collections. In some programming languages, they are known as arrays, right? So basically, we need to understand what array is. So basically, if we talk about the definition or some important points related to array. So on the screen, you can see there are four points listed, which tells about what is actually an array. So the requirement is whenever you have lots of data available with you, and you want to store them together. So what you can do, you can create an array. So basically an array is a collection of more than one elements. So till now, if we talk about last 13 sessions, so what we have done, we have created variables where we can store single, single values, right? Now, if your requirement is in a single variable, you want to store more than one values. So what you will do, you have to create an array, right? So that is the basic need of creation of array. So array basically stores homogeneous, which means similar type of elements. So because of data type, like if you are creating an array, so for all the data, data type will be same. So that's why you can store similar type of elements in an array. And all elements of array store under a single variable. So you need to just create a variable. It's data type, and then you need to define like how many elements you will be storing. And all elements of array stores continuously in memory. So one after another, your elements will be available in the area. So what is the need of an array? So if we store more than one values in a variable, then it only stores the last value. That is one variable can store one value at a particular time. So this slide is very much important for you. If you are a beginner, right? So you need to understand the need of an array. So sometimes people ask you why we are creating array if we have variables available, right? So if we can store values into variables, so what is the need of array? So with this slide, you will be able to understand the need of array. So if we store more than one values in a variable, right? In one variable, if you are storing more than one values, so it will, it will always store the last value because the prior values will be overridden. So one variable can store one value at a particular time. So I hope everybody will agree with this statement, right? Now, if you have more than one values and you want to store them, so what you will do? So we have two solutions. One is we can store more than one values into more than one variables at the same time. So if you want to store five values, so you can create five variables. If you have 10 values, you can create 10 variables. Now, again, the problem is if you have 10 variables and you are storing 10 values into them, so those 10 variables will be having different names, right? So what you can do? You can create an array, right? Third point says, but in case of array, we can store more than one values in single variable at the same time, right? So through array, variable name will be same, like a variable name you have to create one. And in that one variable, you can store many number of values, right? So now we need to understand how we can create array. So that is very much important, right? So I'm going to show you how we can create arrays in C programming and how we can store multiple values into them. So I will be explaining each and every concept and we'll be implementing a few examples in front of you. So if you see types of arrays, so there are three types of arrays available. We have one D array, which is collection of more than one elements. One D array means there will be only one row available. Then we have 2D array. In 2D array, we will be having collection of rows and columns, right? So if you have more than one one D arrays of same data type, and if you want to club them together, so in that case, you will be able to create a 2D array, right? So 2D array is basically collection of more than one one D arrays, or you can say if you want to have rows and columns in place and that rows and columns will be identified with us with a single variable. In that case, you can use a 2D array notation and the last one is 3D array. So 3D array is a collection of more than one 2D arrays, right? So if you want to group 2D arrays, so in that case, you will be able to create a 3D array, right? So in today's session, we'll be focusing on one D array, like how we can create one D array, how we can use that one D array to store multiple values, okay? So first of all, we need to understand the declaration. So you just need to define the data type, then you will be defining the name of array, and then in square brackets, you will be defining the size of the array, right? So data type can be anything. So if you see the examples, we have three examples int, a10, float, b10, and care, c10, right? So three examples are available in front of you, and you can see these three examples are having three different data types, okay? And if we talk about int a of 10, so a is a variable which is having 10 blocks, and if I say like this integer type variable a is an array which can store 10 integer elements into it, and all those integer values will be identified with a, then we have float b of 10, so this is another array which will be having 10 elements of float type, then we have care, c of 10, so c is another name, which is of type character, and it can store 10 character values, right? So you can declare any type of array, and in the comments, you can see we have total size, like for which array, what number of bytes will be allocated in the memory, so it depends on the size, like how many elements you want to store, and according to that, your actual memory will be occupied, so this way we will be declaring arrays and c programming, now if I move forward, so here you can see the memory allocation, so basically, we have an example of int a of 10, so let's say we have 10 elements, we want to store them together under a single variable, so here variable name is a, data type is int, so this a variable can store 10 elements, so 10 blocks are available here, and one important terminology is available, that is known as index, right? So this index is basically starting from 0, and it is ending till 9, so starting from 0, that's why it is ending at 9, because size is 10, now you might be thinking why it is starting from 0, so whenever you are using array, or maybe if you talk about any other programming language like arrays are available in c, c++, java, and if you talk about apix, so in apix, this array is basically known as list, that is available in java as well, so basically in these cases when you have group of values, so always first location will be identified with 0, okay? So basically index is used to access location of an array uniquely, now you might be thinking why these many indexes are available, because this whole memory location is identified with name a, now if 10 blocks are having same name, that is a, how you can tell to the compiler which location you want to access, so that we identify with the help of index, so at the bottom you can see examples a9 equals to 50, so what does it mean? Assign 50 at 9th index of the array, so a9 means last element, last index, last index will be having element as 50 assigned, then we have x equals to a of 3, so what is happening here? a of 3 means 3 is an index, so whatever value is available at a3 index, so that value will be assigned to x variable, so it means this position, 4th position whose index is 3, so that value will be assigned into x variable, so this way basically array occupies memory, and it is very important for you if you are a beginner, so you need to understand like how memory is allocated, so I hope with this example you are able to understand, now moving further we need to know how we can initialize 1D array, so 1D array we can initialize with these examples you can see, so whenever you want to initialize 1D array, so basically you just need to put the values in curly basis, so first is int a of 5 equals to 1, 2, 3, 4, 5, so 5 is the size and you are assigning 5 values, so that is okay, if you don't have size and you assign some values that is also okay, because the total number of values will decide like how many elements that array can store, third is not okay because size is not available and number of elements is also not available, so that will give you an error message, then we have int a of 2 equals to 1, 2, 3, 4, 5, so size is 2 and we are assigning more than 2 values, so this is also not okay, so now what we have, we have fifth option, fifth option says int a of 5 equals to 1, 2, so size is 5 and we are initializing less than 5 values, so we are initializing only 2 values that is 1 and 2, so this is also okay, so this you need to know like whenever you are initializing any array, so this way you are able to do that, so I hope till now you are able to understand whatever I explained, so now moving forward, so this is another thing which is very much important, so what you need to do, you just need to know how we can read values into array and how we can display the values which are available inside array, so here you can see whenever you are working with array, so in array we have lots of values right and those values you can process with the help of loop, so in arrays you will understand the real application of loop, so here for loop which is starting from zero i equals to zero, condition is i less than 10 and with loop we have scanf statement which is having percent either ampersand of i, so this scanf will repeat 10 times, so loop is starting from zero and it will end till 9, so zero to nine those are 10 elements, so 10 rotations will be done and one by one each element will be stored into a array, so name of array is same that's why we are able to apply a loop and here you can see we have i, so i is basically zero, so first time it will be a zero, so first element will go to a zero, then i plus plus will happen, so next element will go to a of one, then next element will go to a of two, so this way array name is same that's why you are able to repeat that statement with the help of loop and if you want to print values of array, so again same loop you can write but instead of scanf you need to write printf and if you are using scanf so you need to use address operator so that you can define on which location you want to store the value and in case of printf that ampersand is not required compiler is capable enough to fetch the value from that particular index without using ampersand sign, right? So this way I hope you understood the basic concepts of 1D array, how we can create 1D array, how we can allocate memory to 1D array, how we can initialize it, how we can read values into 1D array and how we can display the values which are stored into array, so these are some basic concepts which are related to 1D array and everybody needs to know them. Now basis on that I'm going to implement a couple of examples in front of you so that you can understand how actually we can write a program in C that is implementing array. So the program says calculate some of 10 numbers using array. So what we are going to do we will be reading 10 numbers from user and then we'll be calculating some of those 10 numbers with the help of array, right? So now moving to the compiler so you can open any online compiler where you can write the code. So I'm also using this online compiler and this unnecessary code I'm just removing. So here I'm going to declare one 1D array and I variable for loop and some variable so that we can store the calculated some into this variable, right? Now what I need to do? First of all, I need to read 10 values from user. So I'm going to write a printf statement which will say enter 10 elements. That's it. So we don't want to show to the user and user that like we are using array or any other data set. So to user you just need to show enter 10 elements, right? Now you can implement a follow which will start from zero. Then it will go to 10 I less than 10 then I plus plus now inside this for loop what you need to do you will be writing scanf then m percent and a of I okay. So this way you you will be writing the loop where you can receive elements from the user. So this time we created array of size 10. So that's why loop is repeating 10 times. If you modify it to 20 so what you can do wherever you have written 10 so you just need to replace it to 20. So loop will repeat 20 times and 20 elements you will be able to store, right? Now if you want to calculate some so we have two ways one is in the same loop where we where we are reading the values from the user we can apply calculation, right? And what you can do after completion of this loop you can create one more loop and in that loop also you can do some calculation where you will be able to calculate some of the values, right? So here what I'm going to do I'm going to write some equals to some plus a of I so whatever value will be available into a of I that value will be added into some initially some is 0 so 0 plus and that value so both will be added and the result will be stored into some so this this process will happen 20 times because right now size is 20 and loop will repeat 20 times but again I am just reducing it to 5. I'm just reducing it to 5 for demo purpose but in your case whenever you are going to implement this in your system so you can have any number of size right whatever number of values you want to process you can now up sorry after completion of this loop I can have a print of statement there I'm writing some equals to percent D and the variable some so this is the loop line number eight nine ten eleven these four lines are basically loop which will repeat and here after completion of loop I'm just printing some of the values okay so I have one comment like Amit is saying hi so hi Amit I hope you are enjoying the session and you are able to understand whatever I explained so far and I hope you are able to understand the program which I am demonstrating right so if you have any question you can ask so basically this code is calculating some of ten set some of five numbers and that some will be stored into some variable and after completion of this loop that some will be displayed now what I'm going to do I'm going to run this code so here I'm entering to five times here I'm writing here I'm entering to five times so you can see the result is 10 you can see the result is 10 so five times loop is repeating and all the elements are added and final result is basically 10 right so processes working fine it is showing all the correct results and if you want to improve it or you want to enter more numbers so you can just modify the size of area accordingly you can modify the termination of loop and the whole process will remain same so this is the benefit of area like if you want to quickly modify you want to process ten numbers so easily what you can do you can change five into ten right so loop will repeat ten times and ten numbers will be added automatically and after completion of this loop this printer will be displaying some on the screen okay so this was first example that I demonstrated in front of you now I have one more example so this I'm going to implement as well so we need to count the number of even and odd values in area so we need to count number of even and odd values in area so basically what will happen you will be receiving lots of numbers to gather into an area and then you need to identify from that area how many numbers are even and how many numbers are odd okay so now I'm going to implement solution for this so here I'm going to modify this code so even is zero or is also zero so in previous example what we did we just calculated some now in this example we need to count how many are even values and how many are odd so what I'm going to do here with this code snippet line number seven to ten I just displayed a message enter ten elements then a loop which will repeat ten times and ten numbers will be entered now here what will happen I'm going to implement loop one more time so I'm going to show you one more aspect like it is not necessary to write logic whenever you are reading the values it is also possible to implement one more loop where we can write the business logic so here what we need to do we need to write if a of I modulus to double equals to zero right so it will be incrementing even plus plus because if number is divisible by two completely if a of I modulus to double equals to zero so if number is completely divisible by two and result is equals to zero so we can say even the number is even so I'm just incrementing even plus plus and here else I can write odd plus plus if number is completely divisible by two so it means it is even if it is not divisible then automatically that number will be odd so with this loop code will be able to identify how many even numbers and how many odd numbers are available in the area right so what we can do here I can write printf even equals to person D then backslash and odd equals to person D then even comma odd so this way with this single printf statement both even and odd count will be displayed and in different value different rows or you can say in different lines because I have used backslash and here right so this way you can implement array programming in C so I hope if you are a beginner so for you it is very fruitful and you are able to understand how I implemented the logic one more thing what you can do this logic which I implemented inside this loop you can you can cut it and you can paste it here it is also possible and in that case no need to write loop one more time so this is another way to do this so I'm just going back backward so that we have two different loops so I will be testing it first then I will be pasting the code again in the first loop and will remove the second loop and again I will test so we have 10 size or 10 numbers I need to enter and then we'll see the result so I'm going to run this code so I'm entering one two three four five six seven eight nine and ten so I entered five even and five odd you can see the result is showing that it means code is working fine now what I'm going to do I'm cutting it from here removing this loop and pasting the code here right so what is happening here what is happening here loop is repeating ten times and receiving one value at a time and immediately we are checking whether that number is even or odd and accordingly even and odd counting is happening right so this way result will be processed so now again I'm going to run this code and entering 10 different values so you can see the result is same even equals to five and odd equals to five right so this way in both the case code is working fine so I hope you are able to understand the logic with this way so these two problems I demonstrated in front of you today so do practice if you're a beginner so for you I think it might take 15 to 20 minutes if you want to write the code so initially what you can do first code you can write by seeing the solution in front of you and second program you can start doing yourself so that you can have enough confidence right so this is it for today like I just started with basic introduction of one year and then implemented couple of examples tomorrow also I will be having the session on one year so I will implement more programs based on one year so that you can understand how those array concepts are used in C programming okay so please write a review or feedback about this boot camp where all the sessions are available back to back with proper exercises and I hope you are learning the concept as well as you are learning how to implement solution for complex programs right so this is it for today's session see you tomorrow with some more insights related to one day array thank you so much for joining today's session see you tomorrow same time keep practice keep practice practicing and be consistent thank you so much