 Hello, hi everyone, I am Sanjay Gupta. I welcome you on Sanjay Gupta Tech School. So today we are having day 21 of this C and C++ coding boot camp and yesterday I explained the concept of structure briefly and in today's session, I will be explaining few more concepts related to structure. So we'll be implementing couple of programs as well, right? So in yesterday's session, I told you like how you can implement a structure and it's variable and how you can do some implementation. So today I will be creating arrays of structure, right? So in today's session, you will actually understand the application of structure, right? So before starting the session, I just want to give my introduction. So if you go through this slide, you will get to know. So if you are following me from other boot camp, so I think you know about me very well. But if you are for the first time on this channel, so you can just go through with this slide. I am running Sanjay Gupta Tech School as founder and instructor doing career coaching as well for lots of folks and worked in Salesforce, Traction, Aperio, having 16 plus years of experience and helping students, freshers and professional so that they can ramp their career in Salesforce ecosystem or in IT industry. So I have created a telegram group. Okay, before that, I just want to say consistency is the key. So if you're following this boot camp or any other boot camp, so do regular practice because if you do regular practice, then only you will be able to understand the concepts very well, right? Okay, now I have created a telegram group where lots of folks are connected. And if you face any difficulty while learning C or C++, so you can scan this QR code and in that telegram group, you can ask any question. I will be there to answer your questions. And you can follow Sanjay Gupta Tech School on YouTube, LinkedIn, Instagram, Telegram and all the important links are available in video description as well. So if you want to find all the session links together, so there is a session tracker which is available in the video description. Okay, so now I'm going to start with the topic. So we are discussing structure in C programming. So yesterday also I discussed about this topic. I hope you followed that session. So today I'm going to implement one more program related to that, right? So yesterday I implemented this program like we have a student structure where name, roll number and marks variable is created. And I just demoed you how we can receive information of a student and how we can display that. Now in today's session, you will get to know how we can create more than one variable of structure, right? So just go through with this requirement where it is mentioned like write a program to read height of two persons in feet and inches and print their sum, right? If after addition inches are greater than equals to 12, then we need to convert them into feet as well, right? So this is the requirement that we need to implement, okay? So for height, we have two things. One is feet and one is inch. So I'm going to create a structure, okay? So I am just erasing this code, like I'm just going to modify it. So name of structure will be height and here I will be having in feet and inch. So these two members will be there in the structure, right? Now here I will be creating struct height, then h1, h2 and h3, okay? So this way I have created the variables. So here you can see h1 will also be having memory for feet and inch, h2 will also be having memory for feet and inch and h3 will also be having memory for feet and inch, right? So after creation of structure, you just need to create its variable and structure variable can access any member of the structure, right? So now I need to read first members or first person height. So enter height of first person. So here I'm going to write h1.feet and h1.inch. Then I'm just copying and pasting it. So enter height of second person. So here I am writing h2.feet and h2.inch, right? So what will happen? h1 is a variable of structure. So this variable can access feet and inch. So first person feet and inch, whatever height is. So those feet and inch will be stored in this parameter. And for second person, this h2 will be storing feet and inch, okay? So this way we will be having the input from the user. Now we need to perform the addition operation. So I'm going to write h3.feet equals to h1.feet plus h2.feet. So h1.feet means first person height feet and h2.feet means second person height, the feet value will be added and it will be stored into h3. Then we need to write h3.inch equals to h1.inch plus h2.inch, right? So this way addition will take place, okay? Now if you want to display the results. So what I can do? I can write feet equals to person d and then h3.feet. So after addition, the feet, addition will be available into h3.feet. So that's why I'm using h3.feet here. Now I just need inches. So I'm writing index again and here I'm writing inch equals to person d h3.inch, right? So we just did some addition operations and then the result will be displayed with the help of h3.feet and h3.inch. Now I'm going to run this code, okay? So I'm executing it. So here I need to enter the height of first person. So I'm entering five feet and seven inches. Then I need to enter height of second person, right? So second person height I'm going to enter. So five feet, eight inches, right? Now you can see the result. So feet was five and five. So total feet is 10. Inch was seven and eight. So total is 15. So we all know mathematically if inches are equals to 12. So it is equals to one feet. So from these 15 inch, we can subtract 12 and we can add one into feet. So that 12 inches can be converted into feet. So actual result will be 11 feet and three inches. Okay, now we need to do that. So here after addition operation you can apply if condition. And inside this if condition what we can write we can check value of inch. If it is greater than equals to 12. So what we can do, we can increase feet by one and we can decrease inch by 12. So this will be the logic for conversion. So we are increasing feet by one and we are decreasing inch by 12 because 12 inches equals to one feet. So after this calculation result will be appropriate. So again I will be entering the same data. So I'm clicking on a run again. I'm entering five feet, seven inch and five feet, eight inch. So you can see the result. Feet is 11 and inches three, right? So earlier it was 10, 15 but right now it is 11, three. So this way I hope you are able to understand the uses of structure. So unnecessary, you don't need to create lots of variable. So common variable you can create under structure and then that structure variable you can create and you can use it, okay? Now after implementation of this program I just want to have another topic that is structure versus union. So similar to structure we have another data type. Similar to structure we have another data type that is known as union, right? So here you can see both are declared similarly. Similar to structure you can create union and here we have different type of variables like ABC. A is of int, B is of float, C is of care. Similarly we have union where we have the variable and different data types. Then at the bottom you can see we have memory allocation. So this is the difference. If you are using structure, so in structure if you create any variable. So here we have created a variable n. So just after structure declaration you can create a variable. So for this n, this memory will be allocated and each member of structure will be having separate memory block, right? Each member of structure will be having separate memory block. But if we talk about union, so in case of union any one member will be occupying the memory and how we will identify the member. So the member which will require highest amount of memory. So equivalent to that member, memory will be allocated and other members will be sharing that memory, right? So this way if you want to save your memory then you can use union. So in union the limitation is at a time you can store value into one member. So here we have three members. We have A, B and C. So either you can store value into A or B or C. If you store value into another variable so previous value will be erased because we are sharing same memory, right? So sometimes to save memory and if you already know like we just need to store value into any one of the member and if we replace it with another value so previous value we don't need at that point of time. So in that case you will use union. So now if I move forward here are some key differences between structure and union. So if we compare structure and union so both are basically user defined data type. Both structure and union are user defined data type, right? Why they are user defined because the declaration we need to define like what all members will be there in structure and union. Then structure basically allocates separate memory blocks for each member. So if you are having three members three blocks will be there. If you have five members so five blocks will be there. In case of union memory will be allocated to the member who requires highest amount of memory and other members will be sharing that memory location, right? So this is the basic difference between structure and union if we compare the memory thing, right? And we can store values to each member of structure at same time because we have different memory locations but in union we cannot do so because in union at a time we can store value into one member, right? So whenever you want to save memory and you want to reuse the memory for different different members so you can use union and if you have enough memory you want separate memory for each member then you can create structure. So declaration for both the data type is same but the uses are different. So if you are a BTEC student so for you this is an important question that comes in the exam and if you sit in the campus placement and interviewer is asking about CNC plus plus so this whole bootcamp will be beneficial for you to revise all the concepts in one go, right? And if you are non-technical folk and you are just learning your first programming language so for you all the concepts which we are discussing in different sessions so all those are very much important for you. Now we need to learn what is array of structure. So array we already discussed in previous session like if you want to store a similar type of elements then we create array. So now we have created a structure, right? And if you want to create array of structure so that is also possible but before that we need to understand the requirement like in which situation we will be creating array of structure. So in yesterday's session I demoed you how to create student structure so that we can store information of one student now in today's session I will be showing you through the same structure how you can store information of 10 students, right? So for that we will be creating array of structure. So here you can see we have name, roll number, marks so we need to process it for 10 student and then we need to filter out name and roll number of those students whose marks are greater than equals to 75, right? So I won't be creating much variables and you will get to know how it is being implemented. So first of all I'm going to create roll number then I'm creating name, character array and then for marks I'm using float. So this student structure we created yesterday as well and the same I'm creating today, right? Now the difference is yesterday I created one variable from the structure like struct student s so this way I created one variable yesterday. Now what I'm going to do I'm going to convert it into array. So this is now basically array of structure. So this s is one variable which can store 10 student information and those 10 students information are different like roll number, name and marks having different data type. So first you will be creating the structure and then you can create its array and the same concept is available in object-oriented programming as well. So in object-oriented programming like we create class and after creating class we can create its list that is similar to array, right? So if you are learning Java or Apex then you can understand otherwise I will be explaining these things in C++ also. Okay, so here we have created an array. Now to iterate this array we need one variable that is I so I'm just writing here enter roll number, name and marks of 10 students. Now we need to implement a loop. So I equals to zero, I less than 10, I++ and inside this for loop we are going to receive the data. So now here we need to write s of I dot roll number. So s is array, array and then I will denote the index number because we have 10 elements that we need to store and at each index we will be having three members, roll number, name and marks. So after dot you need to write roll number then we can receive name, right? So this way with the help of this loop 10 students roll number, name and marks will be received and at different indexes they will be stored. So initially for roll number, name and marks index will be zero. So at zero index all three information will be stored then I will be incremented to one. So on one index three information will be stored. So this way there will be 30 blocks. So for each index there will be three blocks and the results, sorry, the input values will be stored accordingly. Okay, now we need to filter out the values. So this is the print f information of students having marks greater than equals to 75. So here I'm writing for I equals to zero, I less than 10, I plus plus. Now here I need to write if condition. So if s of I dot marks greater than equals to 75. So s of I means that particular index. So initially index will be zero. So s of zero dot marks, zero index marks if it is greater than equals to 75. So what we need to do, we need to display roll number and name. So this way I think you will be able to understand. So initially roll number and then name will be displayed. Only those roll numbers and names will be displayed where marks is greater than equals to 75. So this way you can see how we implemented this code. So first of all we implemented a structure. Then that structure is converted into array of structure. So this is basically array of structure. Then with the help of this loop, we are receiving input and then we are processing that input data. Right, so initially I'm going to execute it for four times. Initially I'm going to execute it for four times. So array size is four and both the loops are going to execute four times. Okay, so now I'm going to run this code. So here you can see it is asking for roll number, name, okay. One thing I didn't modify it, so it should be four. Okay, so just correct it like it should be four. So here we need to enter four students information, not 10. So roll number one, name I'm entering Ajay and marks I'm entering 200. Then second student roll number two, Sanjay marks 100. Okay, now here you can see it is giving incorrect information. Right, so there is one catch that we need to see. So one thing I use percent D, so I need to use percent F here because marks is of Thai float, right? And after get us, I just need to use get care because whenever you are entering something and I think get us will be working fine. Let me just check it. I'm going to run it again. So entering one Ajay 200, two Sanjay 300, okay, something wrong here. M percent S of I dot roll number, okay. I just declared it as int, I just need to remove it. Let me just run it one more time. This time also it is not receiving all the data. Okay, I'm executing it one more time. So yeah, this time it is working fine. No, but it took first two students data and it is showing that as well. So actually there is some thing that is not working fine with this get us. So maybe instead of this, I'm going to use scan F percent S, M percent or maybe directly S of I dot name. Name, okay, I'm going to run it again. So I'm entering name Ajay roll number one, marks 100. Then Sanjay 200, then Vijay 300, then Ram 400, yep, now output is correct. So instead of get us, you can use this scan F percent S. It would work fine. So all the students are having marks greater than 75. So their information is showing if I run it again. So this time I'm going to input different values. So let's say 100, Vijay is getting 50 and Ram is getting 70. So you will see two students roll number one and two, right? Who are getting marks greater than equals to 75, right? So this way I hope you understood the concept. Now we have one more example. Like we need to read name, number of pages, price of 10 books. And now we need to filter a name and number of pages of those books whose price is between 500 to 1,000. So here I'm having page numbers, the name, it should be book and then price. Then it should be book. Then I'm using B of four. Then here I'm writing enter, name, pages, price of four books. So first of all, we are reading name. Then we are reading pages, page number and then price, right? And here information of books having price between 500 to 1,000, right? Now here I need to write B of I dot price. So if price is greater than equals to 500 and B of I dot price is less than equals to 1,000. So this way you will be framing the condition. So here we have two conditions. Price should be greater than equals to 500 and price should be less than equals to 1,000. Then we can show pages name. So here we can write B of I dot page number and B of I dot name, right? So this way the program is modified. So again I'm going to run this code so that I can show you. So we need to enter the name. So let's say first book name is C, number of pages 1,000 and price is 800. Then C plus plus 400, 7,000, then Java, 1,300. Then Salesforce, 2,000 and 1,500. So here you can see it is showing two books, C and CPP because these books prices between 500 to 1,000. So C is having 800 and the CPP is having 700. Java is having 300 and Salesforce is having 1,500 which are out of range, right? So this way I hope you are able to understand the uses of area of structure. Okay, so this is what I planned for today's session. I hope you understood whatever I explained. So do practice whatever scenarios I have implemented and next week I will be having more sessions on different topics. So you have session tracker with you. So if you go to the session tracker, so here all the topics are listed. So in the session tracker here we have a tab, code in C and C plus plus. So next topics will be pointers, then functions and then array, sorry, file handling. So next three weeks then we'll be jumping onto the C plus plus, okay? So this is a common sheet of Sanjay Gupta Tech School where all the tabs are available, different boot camps are available here. So you can follow them as per your requirement. Okay, so with this note I take your leave and we'll see you in the next session. Thank you for watching this session.