• 2 days ago
Transcript
00:00Hello everyone. This is Sandeep. In my last video, I have explained about array. What
00:08is array? Why we use array? I just quickly recall the things that array is a variable
00:16which can store similar data type elements, more than one similar data type elements.
00:23Suppose if I have to store in a variable a salary of five employees, so the declaration
00:35will be like this. First of all the data type and the name of the variable and the size.
00:44Once I will declare it, the five memory locations are going to allocate and in array, the counting
00:53always starts from zero. So this will be the zeroth position, this will be the first, this
00:58will be the second, this will be the third and this will be the fourth. So in all these
01:04locations we will be able to store the salary of employee, salary of employees.
01:15How to store and how to display it on screen? The things will be more clear when I create
01:22one of the program. I'm going to create one of the easy program and that program is to
01:29input salary of three employees from the keyboard and to display it on screen. First of all
01:37what I have to do, I have to declare the array variable. I will take data type, then name
01:43of the variable and the size of it. Once I will declare it, the three memory space at
01:49the run time going to allocate inside the primary memory inside the RAM. That will be
01:56what the zeroth location, this will be what first location and this will be what second.
02:03So three locations are going to create. So I'll make the program in code blocks to read
02:12the salary of the employees and to store it on the and to store inside the variables
02:19and then to display it on the screen. Let us see how to do it. So the first thing what
02:25I will do, I will include the standard input output header file. Let me save it also. So
02:32I will go to file, I will click on save. Let us give a name to it what PRG array and save
02:39it. Now I will use what int main and inside it the first thing which I will do, I will
02:48declare the array variable with element size what 3. So with this how many values are going
03:02to store? 3. Inside which variable? Cell variable which is array variable. Having the similar
03:09data type integer, for that we have used what int. I have taken one more variable i. Now
03:17I just give a message here inside the printf that is to input salary of three employees.
03:31And I will not use scanf directly as I have to apply the loop as to reach to the position
03:39of an array. So I will use what i equals to 0, i less than equal to 2 or I can also write
03:50in this way less than 3, it's same thing. So 0, 1, 2 it's going to carry on the loop
04:00for three times, 0, 1, 2 and then it's going to terminate. So we all know how the loop
04:07works, we have covered this. Now inside the loop I am going to use scanf. We will use
04:17what percent d, comma what cell ampersand or you can say the and sign cell and inside
04:28it what i. So I am using a very easy program just only to explain that how the values get
04:39stored inside the locations of array variable and how from those locations we can take out
04:46the values and do the work. So with the help of scanf we can able to read the values and
04:54store inside the different locations or different position of array variable. Once the values
05:03stored now I have to display it on the screen. So I give a message here display salary of
05:15employees. For display I am going to again apply the loop. So we can make the same program
05:33by use of single for loop that also I will explain but at present I am using the for loop
05:41for two times just so that you can easily understand and do not get confused that how
05:49the values are stored and then displayed. So here I will use what printf then what inside
05:58it the specifier that is what percent d, comma and where is the value inside the cell and I
06:07is going to take to the position of an array. I need some space also after each value so I
06:16will use what printf and some space. So I hope everything is fine I have made the program.
06:22Let us click on build and see if there is any error so there is no error. I am going to run
06:31it also and I am going to input the salary of three employees. This one is of the first,
06:38this one is of second, this one is of third. Let us press the enter so it displayed also.
06:46Now how the things work let us understand that. When I declare the variable cell3 what
06:56happened behind the scene at runtime it's going to allocate three spaces cell0, cell1,
07:04cell2 like this. Now what I have to do I have to input the salary of the three employees inside
07:11those locations so I have applied the loop here. Let us run the loop the value of i is 0 it will
07:20come to the decision part 0 less than 3 the condition is true with the help of scanf inside
07:27the array variable cell we are talking about the location 0 as the value of i is what 0.
07:34So inside the 0 location that is here the value of i is what 0 so inside here will store the
07:43salary. So the salary is going to store here like this then what will happen the value of
07:54i is going to increment by 1 less than 3 the condition is true it will come here to the
08:02scanf. Now we are talking about the second position of an array so the next value the
08:10next salary of the second employee that is going to store inside the second or inside
08:17the second position or you can say the it starts from 0 so in the first position of an array that
08:23is here this okay. So what we have done we have stored the first employee salary at the
08:35jth position now we are going to store inside the first position of an array the salary of
08:41the second student okay so that is going to store of the second employee okay the salary of the
08:49second employee is going to store and what will happen
08:58this loop will carry on the value of i is going to increment from 1 to 2, 2 less than 3 the
09:05condition is true now this time we are talking about the second position of array that is this
09:13one okay. So the salary of the third employee the salary of the third employee is going to store
09:21inside here okay now suppose we have we have input from the keyboard for 3000 so that is
09:26true okay. So all these salaries are stored and what will happen this loop will carry on the
09:36value of i is going to increment again now from 2 the value will be what 3 so you have to remember
09:45the remember the values of the variable so now the value of i is what 3 it will come to the
09:51decision part 3 less than 3 okay the condition is what false. So this loop will terminate but we
09:59have got the salary of the three employees inside the memory space in zeroth in the first in the
10:07second. Now what I have done I have again applied the loop for the display of the value okay so I
10:14have given a message first display salary of employees and then I have applied the loop let
10:21us run the loop the value of i is 0, 0 less than 3 okay the condition is true from the zeroth
10:28position of an array as the value of i is 0 from the zeroth position of an array the salary of the
10:37first employee that is going to display on screen from here that is going to display on a screen it
10:45will display on screen from here from the zeroth position okay. Then the value of i is going to
10:56increment from 0 to 1 okay 1 less than 3 the condition is true okay now we are talking about
11:03what the first position of an array of here okay the first position of an array. So what is there
11:12in the first position of an array let us see here is the salary of the second employee that will be
11:20display on the screen as we have used the printf okay. Then what happened the value of i is going
11:29to increment now the value will be what 2 it will come to the decision part 2 less than 3 okay 2
11:36less than 3 the condition is true. Now from the second position as the value of i is what 2 okay
11:43from the second position that is from here from the second position of an array the value is
11:51going to display on the screen okay. Then again this i is going to increment okay the value of
12:00i will be what 3 it will come to this position 3 less than 3 the condition is what false the
12:08loop will terminate. So in this way we will get all the values okay. So the next program I am
12:15going to explain with the help of a single for loop and how it will work that I am going to
12:22explain. Thanks for today.

Recommended