33 - ONE DIMENSIONAL ARRAYS DECLARATION, INITIALIZATION AND ACCESSING - C PROGRAMMING

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello friends welcome to our Channel so in the previous session I have seen unconditional statements that is break end continue so we have seen the usage of break and we continue and with small examples and in this session let us have a look on Alice so in the previous sessions we came to know that our variables what is a variable and how to declare a variable and what is the use of variable so a variable is a nickname for a number location so that we can assign some value so that we can store the values in the memory location that we call it as variable so at a time only one variable in that in a variable at a time only we can store a single value so in order to store multiple values under a variable name that we call it as Anna's so let me explain with an example so that you can better understand so if I want to store marks of a student marks of a student a so there are six subjects so I am going to store the max of 60 subjects for a student game so here we need to use six variables right let us take another example if I want to store the temperature of a city hour-by-hour that means probably having 24 hours in a day so I want to store the temperature hour-by-hour soul within 24 hours so there the heat request 24 variables so each variable is having individual temperature let it be there are hundred students in a class and I hope to I want to stole the register number of this hundred students in a class some CAC so I need hundred variables so in this example we'll cross six variables that hotter complexity in this example we require 24 variables we need to declare 24 variables and we have to assign the temperature for each other in the individual variable and here we have to declare my variables so in this case it is difficult or it is more complex to declare more number of variables in the program so for this ultimate thing we'll declare it as errors so here array is a group of elements with a similar data type or same data right so we can store all the six subject marks and the single variable we can store these 24 variables in a single name right so we can store these six variables using single name 24 variable single name similarly hundred variables with single name this thing we call it as parents so an array is defined as a group of elements with the same data type so we can store a number of variables that means we can store a number of elements but the only thing is all the elements must be of same time today either it may be individual or a fruit or a double or a character because these are other fundamental difference No so that's why we call it as headed it's different this group of elements meet same data and here we call it as Aaron mean instead of using the variable we'll call it as a nickname and the giving the name to the array and the rules to be followed when creating a name to the added is similar to the variable that means you should not match with any one of the keywords and it should be maximum of 32 characters and one of the Hoos we have followed by creating a variable all the rules will be applied for the form of this a name also and here there are different types of errors one is numerical added another one is character Numerical added and i can't remember the name itself indicates this numerical eric it contains all the numerical values either it may be in terms of integers or afloat and here it is a character array that means it stores the characters so here as we have discussed in the first sessions the character is any letter in the keyboard we call it as a character so capital letters small letters and numerical data zero to nine and all the special symbols will comes under this character so we can use it as a character I so that means here integer float or we can also use a table so all these are the numerical characters numerical errors and here the camp at a time which we can declare with a candidate and we call it as a character errors now let us look at the numerical array and then character it and these hands are having dimensions so these errors can be represented in dimensions that is one dimensional and two dimensional array three dimensional array four dimensional array and and so on so in general we will call it as a 1-dimensional Eric two-dimensional array and if it is both more than two we call it as multi-dimensional so these dimensions are one dimension one dimensional array two dimensional array and what do damage look so these errors will be having at inventions we can implement these errors using these dimensions we can implement by using a one dimensional array two dimensional array or 20 dimensional array then depends upon the problem statement so here also let us look one by one let us consider this one dimensional array first and then two dimensional then three never coming to this one dimensional Paris one dimensional pairs here if dimension is represented as square brackets square brackets so that we call it as subscript let me call it a substrate if we use the One Direction array we will use one square brackets that is single subscript if we use a two dimensional array we will use two square brackets that is two subscripts so if we use a three dimensional array we will use three subscripts so multi n dimensional and we having young subscripts so where we have to place this subscripts so immediately after writing the array name we have to place this subject so by counting these subscripts we can say whether it is a one dimensional two dimensional or n dimensional so example this array can be represented as if I had a name is marks we can write it as marks and immediately after writing the name of ma we have to include the square braces so this is called one dimensional right next to damage to that semantics so he'll to remix letter we are reading two subscripts here one subscript here two subscripts and original error we will write am subscripts so as it is a one dimensional array we have to use only one subscript so this can also be called as single subscript this is also called as sinking subscript now what is the syntax for one dimensional F so we need to use a datatype followed by add a name so evident can be anything so better to avoid the keywords and square brackets inside the square brackets we have to write the size of the array so this is a magnet we need to declare the size as the declaration itself so without using this size array cannot be declared so syntax for declaring an array is data type and a name followed by size of there this size shows maximum number of elements dr. allan emkin stroke so data type for valiree name and size size means maximum number of elements that is Erik angstrom so this Delta tree can be anything any data type of our fundamental data rates it can be integer it can be floats it can be double and it can be kept so different is integer float and double we call it as numerical errors if it is a character data type we call it as a character so example take it amounts of a student so six subjects marks we need to store the six subjects marks for a student a little bit student name is yay so integer so max array of integer datatype have a name that is e of some six so here six is the maximum number of elements that they can store this is a declaration of array so this is the declaration so this is also similar to that of variable that means we need to declare everywhere every variable which are used in the program as the beginning of the program itself similarly we have to declare the adder also before using this add in the program at the beginning itself we have to use that is similarly available but here the only difference is had I can store a more number of values but a variable can store only one in bed that is the only difference between a variable N and and here as it says that it can store my maximum number of elements more number of elements so we need to specify the size of the N and this we call it as index index values here inside the square braces the number we call it as index values and this index values for air is more is from 0 to n minus 1 that means the index value will start always from 0 so the starting element of array is the index value of the starting element yes always zero the index value will start from zero so here six elements were saying that six so he hated those six elements so one of the six elements so first element is always starts from zero so zero one two three four and five so total six elements so that's what I am saying that sit if Yin is a size of an array it can store 0 to n minus 1 elements that means total n elements will be placated so 0 1 2 3 4 & 5 so how these can be represented how elements can be represented so this is called an array array element means individual of six elements so as I said that yeah of 6 is an array which helps to the maximum of 6 elements so 6 values so that each value will call it as element so we can say that it can store 6 elements under the same name so how to use these elements how to access these elements or how to represent these segments energy so one of the six elements of you have six are yeah this is an index value it necessarily always starts from 0 so Y of 0 you have 1 yeah - you have 3 you have food and you have 500 600 1 2 3 4 5 6 so this is a first element second element third element fourth element Fifth Element 16 so inside the subscript the number we call it as index so index values are from zero to five and the elements are from one to six not get confused and the name is common yeah you have 0 you have 1 F 2 F 3 here 4 and here 5 so how these elements can be accessed you know who how much memory it will occupy so as we know that integer if it is integer data type the variable is integer datatype so it will have occupied two bytes of memory that depends upon the company similarly here also if it is integer nether day each element will have in two bytes memory so that depends upon the compiler and always this memory is allocated in a continuous locations if the first element memory is thousand the second memory location will be thousand two third 1004 next thousand six thousand eight thousand ten thousand twelve one two three four five six sorry only six elements so index values are zero one two three four five elements so you have zero elements are you have zero yeah of one you have to yeah of three yeah of four and a of five so this is address this is index values this is elements so the memory will be always occupied in a continuous memory locations for this arrays hope you understood this declaration and representation now let us have a look on initialization how this address will be initialized initialization of Eros how this address can be initialized so any variable can be initialized in two ways one is it company another one is a one-man so for the runtime initialization we know that we will use a function called scanf function well coming to this data visualization that means after the initialization and a compile time itself a normal variable how to initialize a normal variable we have seen in the previous sessions so if he ain't yeah is a normal variable we we will use this compile type initialization as a is equal to 6 directly by using this assignment operator will be less n a value to the variable name so this statement implies the value of 6 is assigned to the memory location with the name yeah similarly here also in the comparative initialization Alice will be initialized with some values using assignment operator so let it be so we here we can combine both declaration and initialization in a single line you're a scientist index fast datatype add a name and clearly we have to specify the size size is equal to that is a second operator open the curly braces not a square braces curly braces and write down the values value 1 comma value 2 comma and so on so likewise we can assign the values directly to the array elements using assignment operator see example let us take that much of a student here the gel let us take our name as a marks so marks of 6 so there are 6 subjects so I have to take the marks for six subjects so months let's take a small anything it doesn't matter so marks of six the prints we are creating or we are declaring the array marks with the integers so that all the six elements must be integer datatype and here we are giving the size at 6 so it can maximum it can hold 6 elements under the name of marks by using the assignment operator is equal to over the grace right on the marks separated by comma so 70 comma a P comma 65 Tama 90 comma 95 85 so like this we can initialize the values to them and so at which place these values will be a same that implies as we said that the individual elements of this max is max of 0 is equal to the first element 70 will be assigned to max of 0 that makes for statement next marks of 1 second element a tick marks of two third element 65 marks of three mighty marks of 495 not so fine 85 so this single line assignment statement will assess individual elements of the array so individual elements of this array is 0 1 2 3 4 5 so does its 16 bits because index starts from 0 so here otherwise the first element will be assigned to the first value will be a string to the first element second value will be assigned to the secondary bit third 4 5 & 6 so like this way the initialization will be and if an array is declared but not initialized that means if we declare eat marks of six so just we have to kill but not initialized so Kayla I am saying that the initializing means we assigning the values to the variable here in this statement we just declared a variable that means we declare an array but we should we are not asserting any value to the elements of the earth in this case initially all the elements 0 1 2 3 4 and 5 so all the elements will contains garbage values garbage values so in the previous sessions we know that we came to know that the importance in what is meant by a garbage value say what is that so an average values are nothing but a dummy values so it gives any inappropriate results so by default in mark 6 is a normal declaration without any initialization so a memory is allocated to all these six elements and by default some garbage values will be assigned to all these six elements so these garbage values will be a same until you initialize any one element of the array so if you initialize any one element among this file all the remaining elements will be Z becomes zero the things if we initialize in marks of six is equal to 50 so this was a valid statement in march 6 of z equal to 15 here 6 is a maximum number of elements that we can store in an array so here we are writing only 50 and single value we are giving a single value so that single value will be stored in the first element that fames marks off view so in the max of 0 the 50 will be stored and what about the 1 2 3 4 and 5 so here one element is initialized so even though one element is initialize all the remaining elements will be replaced the garbage value with zeros so all the remaining elements will be zeros so if you have just declared the array but not to visualize every single value single element then all the elements will be having are these values and if you initialize any at least one variable that means it well at least one element all the remaining records will be zeros so that's very important so image declared but not initialized garbage values initialized at least one element all the remaining elements will be zeros and we can declare we can we can initialize these elements and the compile time in Nepalese so first one is this one this is the first case and it may be the second case so the values written inside the curly braces will be assigned in the index format so the first of all with either the first value will be assigned to the first element that means index 0 and if you write here three values 50 60 70 that implies 50 will be stored in 0 60 will be stored in first element and 70 will be story tournament and all the remaining elements will be new so because March 6 all the remaining elements will be 0 and we can also initialize this address without specifying the size if you initialize it in the company that means if you enjoy feeling like my in marks of 6 into marks we are not specifying any size is equal to 70 80 90 so this is also a valid statement because we are assigning the three values to the array marks so the compile will that the company will treat this array is of size 3 here we are we are sending bullet three values so we have not specifying the size here so the compiler will treat this as mass is an array which can store the maximum of three elements so this is also a valid statement and this can be done only if the initialization is done in the company itself so if you are assigning directly using assignment operator it is optional to mention the size of an array so these are different cases different ways we can initialize an array using at the company table now what about the run time initialization of a one dimensional array so these are all the one dimensional bare areas because as I said that here we are using what is one subscript that makes only one square braces so it isn't one dimensional array so let us have a look on run time initialization random initialization that means we are using scattered function right so similarly as a variable we have seen that we are able to write scanf if it is an integer that occur we need to write the control space for a percentage D comma address operator address operator is called a nitrous operator it goes yay so this is a hammer syntax for initialization of a variable at the one time no we we are going to see the one time initialization of an and you know so there are different cases so two different cases normally so we can directly initialize each element using scan of the pins if we declare in marks of three so it implies marks easier learning and threes a maximum number of elements that that marks Eric in store now we can write a scanner so three elements percentage D it is an integer data that so we have 19 percentage D percentage D ostentatiously so pre image because here we are having thanks form of three elements we can read it three elements and use the ankus operator right the marks so at which we have to you store the value marks of zero first one second hammers and Mike's off one come on Amazon marks off so like this we can initialize the values to the array elements at the front end so here we use so very simple because here only there are three elements maximum number of elements that is three so it is very simple to write a scan of single scan so coming to the earlier examples that if you want to store the additional numbers of a hundred students this initialization is complex because we have to write hundred variables in the scanf statement 100 control specifies and hundred variable names amperes and hundred variable names so when it is not possible to write all the hundred variables so what is an alternative so instead of writing am present marks zero FS and marks 1 Amazon marks - we can implement the same statement multiple times we need to implement or we need to execute the same statement multiple times that means up to the size of Eric so this can be done but you see loose concept so we have seen different loops in the earlier sessions that is further up we have seen while loop and dowhile so we can use this iterative statements for a runtime initialization of one dimensional so we can write further try the index values I is equal to already the index value starts from zero so zero so up to vein we have a flexible so honey less than n that means n minus 1 up to n minus 1 I plus plus write out the scanf statement here scanner ostentatiously because it is a judicial candidate come on right on the mark hammer st. marks of I so instead of reading the elements you don't want to tell you we are repressing it is it with a look variable I so we have to use and look variable I and I I ranges from 0 to n minus 1 so this is a simple code for one time initialization of one dimensional and hope you understood so here with two bits into this we can initialize any address and then the mountain so by using become close friends and I am present marks of each element so that means assigning of the value to the each element of an adding so it is somewhat difficult if more number of elements in the array so further purpose we are avoiding the second logic that is we are using the iterative statements for assigning the values to the eros at the one time hope you have touched on this now let us have a look on how to initialize and how to next we have seen that the declaration and initialization now how to print their arrangements so display or print the array image so how to do this display our printing so you know so we have to use the interactive statement because if there are more number of elements similarly we can write the here printf as we know that the output statement is a plaintiff write the controlspace well so in the previous example in we are seeing that int marks of three so for this we can display percentage D percentage D percentage D common marks of zero-carbon marks of one common marks of two so the max of 0 will be treated here marks of 1 will be printed here marks of 2 will be printed here so if there are more number of elements in the array this statement will be more complex for that purpose again we have to use an iterative statements for displaying them all the elements of the array so again we can write the single statement printf percentage D marks of ie when I ranges from 0 to n minus 1 right on the iterative statements for I is equal to 0 I less than 10 I press place so first I is equal to 0 max of 0 next 1 or less than the N so if it is 3 here 3 we are writing here 3 I less than 3 0 max of 0 will be printed next increment I is equal to 1 well as then three marks of 1 will be printed I Christmas to to less than three marks of 2 will be printed i plus plus three 3 less than 3 so it is a false so automatically the control comes out from the loop so like this we can use we can display our printer on the display screen hope you understood this initialization declaration and printing the relevance of Vanhanen and we should be very careful where the initialization or accessing the elements because if you because marks of three the first three elements in an array if you try to access the marks of fourth element that means here there are there will be elements from zero one and two right so here we have retained marks of three that makes for the three events that is max of zero marks of one or two and if you are able if you want to access much of three the compiler will not generate any error but the result will be inappropriate so once again I am saying that so if you declare the air you get maximum number of elements and if you are going to access the other than the maximum beyond the maximum element the compiler will does not create or generate and editors it gives them inappropriate so we must be very careful by the initialization and accessing the arrangements because we will not get any errors so here marks of three that means three elements marks of zero marks of one marks of two and if you want to access the marks of three the error will not be occurred so inappropriate person will be coming so hope you understood this declaration declaring the one dimensional array and in the initialization of one dimensional array at the runtime at a compile time and how to access this one dimensional array and how to display the elements of one dimensional so let us from here and then the next to session I will explain the two directional area and a simple program showing them so reading the elements of an array and displaying the elements of them there are some here and if you like my videos like it and share share with your friends and subscribe to my channel and if you are having any doubts regarding this C language you can post your doubts in my comment session so that I will clarified and I will try to clarify you are those within a short video and thank you very much for your support and encouragement thank you
Info
Channel: Sundeep Saradhi Kanthety
Views: 140,978
Rating: 4.9297862 out of 5
Keywords: sundeep, saradhi, kanthety, cp, computer, programming, computer programming, c programming, c language, arrays, one dimensional arrays, two dimensional arrays, multi dimensional arrays, numerical arrays, character arrays, declaration, initialization, accessing, elements of array, index of arrays, array initialization, array declaration, accessing elements of arrays, example for one dimensional arrays
Id: HyDE7T6YPsU
Channel Id: undefined
Length: 41min 33sec (2493 seconds)
Published: Wed Sep 20 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.