C++ Essentials: Arrays, Pointers, Strings & Vectors for Intermediate learners | Dr. Reza Shahin | Skillshare
Search

Playback Speed


1.0x


  • 0.5x
  • 0.75x
  • 1x (Normal)
  • 1.25x
  • 1.5x
  • 1.75x
  • 2x

C++ Essentials: Arrays, Pointers, Strings & Vectors for Intermediate learners

teacher avatar Dr. Reza Shahin, Ph.D. | Lecturer | Researcher

Watch this class and thousands more

Get unlimited access to every class
Taught by industry leaders & working professionals
Topics include illustration, design, photography, and more

Watch this class and thousands more

Get unlimited access to every class
Taught by industry leaders & working professionals
Topics include illustration, design, photography, and more

Lessons in This Class

    • 1.

      Introduction c++ for intermediate learners skillshare

      2:06

    • 2.

      Continue keyword

      6:21

    • 3.

      Break keyword

      5:00

    • 4.

      goto keyword

      4:04

    • 5.

      Array

      7:12

    • 6.

      Array (Exercise #1)

      8:31

    • 7.

      Array (Exercise #2)

      11:29

    • 8.

      Array Function

      10:45

    • 9.

      5 Array Function Exercise1

      13:04

    • 10.

      6 Array Function Exercise2

      14:07

    • 11.

      7 Array Examples

      15:02

    • 12.

      8 Array statics

      15:44

    • 13.

      9 Array statics exercise1

      25:19

    • 14.

      10 Array statics exercise2

      15:24

    • 15.

      11 Array sort ascending order

      4:15

    • 16.

      12 Array sort ascending order 2

      1:59

    • 17.

      13 array sort descending order

      3:36

    • 18.

      14 array reverse

      2:16

    • 19.

      15 insert an array in the beginning of another array

      12:27

    • 20.

      16 insert an array at the end of another array

      2:41

    • 21.

      17 insert an array in the middle of another array

      10:15

    • 22.

      18 Constant array

      3:27

    • 23.

      19 two dimensional array

      10:22

    • 24.

      20 two dimensional array exercise1

      10:50

    • 25.

      21 two dimensional array exercise2

      12:02

    • 26.

      22 Multi dimensional array

      13:01

    • 27.

      1 Pointers

      5:55

    • 28.

      1 1Pointers function0

      4:49

    • 29.

      2 pointers function1

      3:46

    • 30.

      3 pointers function2

      5:07

    • 31.

      4 Pointer array exercise2

      9:33

    • 32.

      5 pointer to two dimensional array

      9:14

    • 33.

      6 pointer to two dimensional array 2

      2:33

    • 34.

      7 array to function pointers

      8:00

    • 35.

      8 array to function pointers 2

      6:36

    • 36.

      9 pointer to a function as a parameter of another function 2

      13:22

    • 37.

      10 pointer to a function as a parameter of another function

      6:54

    • 38.

      11 Dynamic memory allocation1

      8:08

    • 39.

      12 Dynamic memory allocation2

      9:25

    • 40.

      13 A pointer to another pointer 1

      3:41

    • 41.

      14 A pointer to another pointer 2

      7:40

    • 42.

      15 Type Casting 1

      3:28

    • 43.

      16 type casting 2 void pointers

      5:00

    • 44.

      17 type casting 3 void pointers

      3:18

    • 45.

      18 type casting 4 void pointers

      6:30

    • 46.

      19 type casting 5 void pointers

      6:51

    • 47.

      1 Strings

      0:56

    • 48.

      String 1 length

      3:32

    • 49.

      String 2 access characters

      2:21

    • 50.

      String 3 append

      3:00

    • 51.

      String 4 insert

      6:09

    • 52.

      String 5 erase

      1:56

    • 53.

      String 6 replace

      1:42

    • 54.

      String 7 substring

      3:31

    • 55.

      String 8 clearing

      1:02

    • 56.

      String 9 reverse

      2:21

    • 57.

      10 string sorting

      1:53

    • 58.

      11 array of string 1

      4:29

    • 59.

      12 array of string 2

      7:32

    • 60.

      13 array of string 3

      2:10

    • 61.

      1 vectors 1

      3:05

    • 62.

      Vector 2 sum of all numbers 12

      4:44

    • 63.

      Vector 3 access elements

      1:25

    • 64.

      Vector 4 size

      3:03

    • 65.

      Vector 5 modify elements

      1:35

    • 66.

      Vectors 6 popback

      2:43

    • 67.

      Vectors 7 clear

      1:11

    • 68.

      Coding exercise vectors 8

      7:39

    • 69.

      Vectors exercise 9

      3:16

    • 70.

      Vectors exercise 10

      6:40

    • 71.

      Vector 1 insert and display 11

      8:00

    • 72.

      Vector 2 sum of all numbers 12

      4:44

    • 73.

      Vector 3 max number in a vector 13

      7:27

    • 74.

      Vector 4 reverse 14

      7:27

    • 75.

      Vector 5 remove even numbers 15

      7:53

  • --
  • Beginner level
  • Intermediate level
  • Advanced level
  • All levels

Community Generated

The level is determined by a majority opinion of students who have reviewed this class. The teacher's recommendation is shown until at least 5 student responses are collected.

1

Student

--

Projects

About This Class

Are you ready to take your C++ skills to the next level? This course is designed for intermediate learners who have a solid understanding of the basics of C++ and want to bridge the gap to advanced-level programming. With a structured curriculum and hands-on examples, this course will empower you to write more efficient, scalable, and robust programs.

What You'll Learn in This Course

This course focuses on mastering the key intermediate concepts of C++, including:

  • Arrays: Explore one-dimensional, two-dimensional, and multi-dimensional arrays with real-world applications.

  • Pointers: Learn how to manipulate memory efficiently, pass pointers to functions, and work with pointers in arrays and structures.

  • Dynamic Memory: Understand how to allocate and manage memory effectively using advanced techniques.

  • Strings: Perform string manipulation and use file handling to manage data in real-world scenarios.

  • Vectors: Dive into complex data management with vectors.

Highlights of the Course

  • Real-world examples: Apply concepts like sorting, searching, and statistical calculations using arrays.

  • Practical exercises: Work on a large number of practical coding exercises to improve your coding skills.

  • Comprehensive curriculum: Understand the interplay of arrays, pointers, and structures in developing robust C++ programs.

  • Step-by-step guidance: Build confidence with complex topics through clear explanations and hands-on coding.

Why Start This Course?

  • Tailored for intermediate learners: Designed to solidify foundational concepts and prepare you for advanced-level programming.

  • Hands-on approach: Gain practical coding experience by implementing complex concepts step by step right after its corresponding lecture.

  • Bridge to advanced C++: This course serves as a pathway to mastering topics like object-oriented programming and advanced algorithms.

Who Should Take This Course?

This course is perfect for:

  • Intermediate C++ learners who want to advance their programming skills.

  • Students preparing for advanced C++ courses or real-world programming challenges.

  • Aspiring developers seeking to understand C++ memory management, advanced structures, and efficient coding techniques.

  • Hobbyists or professionals eager to refine their problem-solving skills and take on complex programming challenges.

Prerequisites

To get the most out of this course, learners should already be familiar with:

  • Basic C++ programming, including data types, if-else conditions, loops, switch statements, and functions.

Embark on your C++ programming journey and equip yourself with the skills to solve complex problems, build scalable applications, and prepare for advanced programming concepts. Start today and take a step closer to becoming a C++ expert!

Meet Your Teacher

Teacher Profile Image

Dr. Reza Shahin

Ph.D. | Lecturer | Researcher

Teacher
Level: Intermediate

Class Ratings

Expectations Met?
    Exceeded!
  • 0%
  • Yes
  • 0%
  • Somewhat
  • 0%
  • Not really
  • 0%

Why Join Skillshare?

Take award-winning Skillshare Original Classes

Each class has short lessons, hands-on projects

Your membership supports Skillshare teachers

Learn From Anywhere

Take classes on the go with the Skillshare app. Stream or download to watch on the plane, the subway, or wherever you learn best.

Transcripts

1. Introduction c++ for intermediate learners skillshare: Hello, everyone. Welcome to this course. This is Raza Sha hin, a PhD in Computer Science from University of Gustav val in France, and I perform my postdoctoral fellowship at the University of Polytechnic Montreal in Canada. Welcome to the course of C plus plus for intermediate learners. In this course, we are going to review arrays, pointers, strings, and vectors. In particular, with respect to arrays, we're going to learn about one dimensional arrays, two dimensional arrays, and three dimensional arrays, and multidimensional arrays in the end. We're going to combine arrays and functions, and we're going to learn how to combine two different arrays to create the first array. Then we are going to talk about pointers. Why do we use them and how do we use them? We're going to combine the concept of pointers with functions, pointers with arrays, and we learn about dynamic memory allocation. And finally, we learn about typecasting in pointers section. Then we move on to the strings where we learn about different practical functions of strings. For example, we learn how to measure the length of a string. We learn how to append a new element to the string. We learn about accessing characters, erasing an element, and replacing a reverse function. Finally, we will move on to vectors. We're going to have a throughout comparison between vectors and arrays to see when do we use each one of them. And we're going to learn how to modify vectors, how to measure the size, how to use the pushback function, and how to use the pop back function. Throughout this course, we're going to teach you the concepts, and right away, we're going to have coding exercises so you can see that we're going to have practical coding exercises and functionalility of what we learned as the concept and theory. So we're going to apply them right away in practical exercises. Thank you for watching. I'm looking forward to seeing you in the course. 2. Continue keyword: Hello, everyone. Welcome back to another video. In today's lecture, we are going to discuss about the continue keyword. In the previous lecture, we learned about the break keyword. So in certain situations, when a condition is met, we just tell the program, stop the execution, break the program and get out of, for example, a loop. Here we are going to use continue keyword. In this case, there are some cases that when certain conditions are met within the blocks of code, we tell the program, if this condition has met, continue. So just escape this part. Don't execute, don't do anything. If this happened, just escape. Okay? So let me illustrate this with an example. We would like to print all the odd numbers 1-10. To do that, we simply write a four loop that starts from one, and as long as I is lower than or equal ten, and I is going to increase one by one. Then when we have this condition because when we have this four loop, we would like to have the odd numbers printed only, we define a condition. So we define a condition saying that if I, this number is divided by two, and it's remainder because we are using modular sine. It's remainder equals zero, it means that the number is even. And in this case, just continue. Don't print anything. And if this situation is not the case, then print the I for us. So let me give you an example. So initially, I starts from one. I equals one, and one, when it is divided by two, it's remainder is it equal to zero or not? It's not equal to zero. Therefore, it's an odd number. When it's an odd number and it's not equal to zero, it is going to print the I value, which is one here and then print one space here. And then it goes up, I equals two now, and then I equals two, it says, Okay, 2/2, its remainder equals zero or not. Yes, in this case, it is true. Then continue, escape this part. Don't do anything. It will go up again. So it will increase the value of I by another one value. So I is now 33/2, its remainder. Is it equal to zero or not? It is not equal to zero. Then this condition is not passed, so this part is escaped and it's going to print the I here, which is number three and then one space. Next one. I equals four because this loop is going to continue until I equals ten. So I equals four enters the loop, 4/2, it's remainder. Is it equal to zero? Yes, it's going to enter this condition then, then it's going to continue. Just escape like nothing happened. Go back up again and increase the value by another one value, and then in the next run, I equals five. Okay? So let me run the code. Okay, great. So we are printing odd numbers 1-10. It is printing odd numbers 1-10. Then there is a four loop here, and then it is having this condition. If there are even numbers, don't print them, just escape. So just continue like nothing happened. However, when the numbers are odd, because if numbers are numbers remainder, when the numbers are divided by two, the remainder does not equal zero. It means that they are odd numbers. In this case, just print them. So all the odd numbers 1-10 are printed here. So this is basically how we are going to use continue keyboard. It's a very useful keyboard and it's a very easy example of how we can use them. There are different ways to execute this program and implement this program. For example, we could write simply instead of writing this condition here that to detect the even numbers, we could say if the number is divided by two and its remainder is not equal to zero, then here simply print I. Okay? We could also have that C out, I Oops. Sorry, C out, I, and L. Okay? We could do that. This we could also do. So when you would like to implement a program, there are different ways to do that. It's up to you how to do your coding, but usually we try to code in a way that is efficient and that we program in a way that we increase the program readability. So if the code is passed to the next developer, they can also easily understand what you were doing during your code. So there are different ways to do one task. Here, we could simply use continue keyboard and do what we wanted to do. Thank you for watching. Try to practice this coding exercise. We're going to use continue and break keywords later on on this course. Thank you. 3. Break keyword: Hello, everyone. Welcome to this lecture. In today's lecture, we are going to talk about a reserved keyword for C plus plus that is called break. So basically, when we would like to exit the loop, in certain conditions, we're going to use this keyboard or when a certain condition is met by the program, we just want to tell the program if this condition is met. Okay, stop the execution and just exit the program. So in these cases, we're going to use this break keyboard. Here I have an example to illustrate the usage of the brake keyboard. We would like to write a program that gets input numbers from the user, and this getting input numbers from the user is going to continue until the user enters minus one. When the user enters the number minus one, we are going to break the program. It means that the program is going to continuously ask the user enter a number, enter a number, enter a number, and the user is going to continuously enter different numbers. However, when the user enters minus one, it is a sign for us based on this program. It is a sign for us that, okay, the condition is met, let's exit the program. Let's stop the execution. Therefore, we use the brake keyboard. Let me illustrate it with an example. So here we are initializing an integer. We are declaring it, and then we are telling the user enter numbers and type minus one in case you would like to stop. Then we are writing an infinite loop here, which is using y loop and the condition is true. As long as the condition is true, it is going to repeat this y loop. And in the Vi loop, we are going to store the number that is given by the user. And if the entered number by the user equals minus one, break the program. We use the break. So exit the loop if minus one is entered. And if not, if any other number is entered, just print out the number that entered by the user, and when we are out of the Villoop, please just print loop that. Let me run the code and we see the details. Okay, enter numbers. So I run the program. We are now here. It is executing enter numbers, type minus one to stop exactly this line. I would say 20. It says, You Enter 20. And again, it is waiting for me to write another number. I would say, again, 20. It says, you enter 20. I would say 12. It says, you enter 12, 15, 17, 18, 22. Whatever you put here, it is going to continuously ask from you that give me a number unless I type minus one. So when I type minus one, it says Loop ended. Why? Because when I type number one, number one is stored using this line, and it checks to see if the entered number equals minus one or not. In this case, it is equal, then it's going to break. When it breaks, we get out of this loop and it is going to print loop ended. So this is how we use the break keyword in C plus plus, and generally, it is very useful in different programs that we are going to practice in the future whenever a certain condition is met. Okay, get out of the loop, get out of this ELs condition. It is very useful for us. So it is good to start using it. Whenever it is possible, so you get used to its usage and you get comfortable with the syntax and the way to use the brake function and to see if you're doing everything correctly or not. Therefore, please practice this exercise yourself as well and try to write this program several times so you can exactly remember when and how you need to use the brake keyboard. Thank you for watching. 4. goto keyword: Hello, everyone. Welcome back. In today's video, we are going to review the usage of the continuing keyboard and we're going to learn about the go to keyboard. So we would like to write a program that asks a user for a positive number. If the user enters minus one, we would like to terminate the program, and if the user enters any negative number, we would like to let the user know that negative numbers are not allowed and let the user continue entering the number. This program is going to continue until the user enters minus one. To do that, we declare an integer variable. Then we show the user saying that enter a positive number and minus one to terminate the program. We're going to define a I loop, which is an infinite one in this case here because the condition is true. And then as long as the user is not entering minus one, this vi loop is going to repeat because it is an infinite value, why because within the parenthesis, the condition is true. So we read the number from the user. We mentioned we check if the number equals minus one or not. If the number equals minus one, it is going to use the go to keyword and it is going to jump to end. Where is end end this year. Okay? So it is going to escape all these parts, and it's going to jump here and print program terminated. So this is how the go to keyword will work. And in case the num is lower than zero, it is going to print out numbers, negative numbers are not allowed, and then continue. When it continues, it means that do nothing, scape and since there is an infinite value, it is going back up again and let the user choose another number and then check if it's minus one or not, et cetera. So let me just run the code. I would say 20. It says, you enter 2015, 17, 18, whatever, 20 -20. When I say -20, it says numbers are not at exactly like here. And then and then I am allowed to write another number like 21. Okay? This is going to continue until the end of the word unless I enter minus one. When I enter minus one, program terminated. So when I enter minus one, it is going to jump to the end and then write it here. We could also use the break function here. Could simply mention if this happened, break the program. Usually, when we are having different situations, we are going to use different keyboards. In this case, we wanted to practice GoTo, but as we mentioned earlier, there are different ways to execute one program. It depends on your taste, what you would like to do with your code, how efficient and readable your code you think you might be. By using one keyword or another. So it depends on you, but this was a review of the continue keyboard and also the usage of the go to keyboard. So please go ahead and practice. And while you are doing different coding practices throughout this course, think about the places that you may be able to use break keyboard, continue keyboard or go to keyboard. They are very useful try to practice them. So basically, when you practice, you will get used to using them where necessary. Thank you for watching. 5. Array: Hello, Evan. Welcome to this lecture. In today's lecture, we are going to introduce arrays. So arrays are basically fundamental data structures in C plus plus and any programming languages. And they are important because they allow us to store multiple elements of the same time in a single block of memory. So let me give you an example. So we would like to write a program to store five numbers in an array and then print them. In order to do that, previously, we know from the integer data type, we could simply just declare and initialize one number, two numbers, three numbers, four numbers, but they were initialized and declared separately. Here, using arrays, we can do them. We can do it and like declare all of these numbers in a single block of memory, how we're going to do it. Okay. So this is how we declare and initialize an array. So this is the name of the array which is numbers. This is the size of the array, meaning that this array is having five elements. And then this is the data type of the array, which is integer. And when we want to declare and initialize an array, we're going to say that, for example, the data type, in this case, int numbers, the name, and within the bracket, we put the number of element of the array, and then equals, and then we write down the element itself. So previously, we used to write it something like int A equals ten, for example, It B equals 20, for example, et cetera. And when we wanted to print them, we were printing them separately. We were just typing it like the value of A is, for example, A and L. So this way we were printing A and the same way we were printing B, for example, here we write down B, here we write down B. So this was the old method that we use to print elements and declare and initialize them. However, using an array, we can just declare and initialize all of them in a single block of memory just like this line. Okay, so we would like to write a program to store five numbers in an array that we did. And print them. In order to print an array because there are multiple elements in the array, we need to use the four loops. So in order to use the four loops, we are going to iterate over each element of the array and print them one by one. Let me explain you in the code. So we say the numbers in the array and NL, we go to the next line. Then we have our four loops here. So It It I equals zero. It starts from zero, and it goes to five because we have five numbers, five elements in this array, and then we increment the value of I one by one. Then we see out numbers, which is the name of the array. Within the bracket, we mentioned the I because it's going to print the first time. Let me just type it down here. In the first iteration, I equals zero and it enters the loop, then we have the numbers zero, so it will be printing the first element of the array number. When it is printed, it goes back up and I is incremented by one value. Then I now equals one. Then it comes back and enter the loops and it will print the number one. And the second element because here we are starting from zero. Now one equals the second element of the array. It is going to print the second element of the array, and then it goes back up again and I is incremented by one value. Now I equals two. When I equals two, we are having the numbers and the third element of the number. So the first element of the array number equals ten. So in the first iteration, we're printing ten. In the second iteration, we are printing 20. In the third iteration, we are printing 30, and it continues until we have Number three, it equals 40. And finally, number four equals 50. So please note that we started from zero. So this is why we are ending at four, but all in all, you will see that five elements are being printed here. Therefore, all of the elements of the array is expected to be printed. Let me run the code and see how it works. Okay, great. So as we can see here, the numbers in the array, so this line is printed. Then it is printing ten, 20, 30, 40, and why there is a space between them. The reason that there is a space between them is that because when we are printing the element, the I element of array number, we are having one space here. So this is why we are seeing that ten and then space 20 space 30 space 40 space 50. So this is why we are having the space. So to summarize, we learned that we can have we can have an array to store multiple elements of the same type in a single block of memory like this. And when we are going to print elements of an array, we need to use four loops to do that and why we need the four loops because they many elements. There might be several elements, a few elements in an array, so it's not a single element. There are a few elements that if we want to have an efficient code, we can just iterate over each one of the elements and then print them instead of just manually printing them one by one. Thank you for watching. In next videos, we're going to do some coding exercises regarding the arrays. 6. Array (Exercise #1): Hello, everyone. Welcome back. In today's video, we're going to do some coding exercises for the arrays. So we would like to write a program to find the largest number in an array of five integers. First, we learn how to declare and initialize an array. Here, we need to declare and initialize an array with five elements, and the elements should be integers. So we know that the in the declaration, we write down int, then the name and then five elements. And we just randomly put some numbers, five elements for the array. And then we need to know how to find the largest number in this array. To do that, initially, we initialize one integer has to be the first element has to be equal to the first element of an array. This is just an assumption. We're going to first assign the first element of an array to this max integer initially, and then we are going to perform some tasks and some iteration over the different elements of the array, and then we are comparing each element of the array with this max integer. So if a number in an array, if an element in an array is larger than this max value, we are going to assign that number to max. To do that, we need a four loop. So here we declare and initialize the array. Then we are initializing the max to be the first element of the array. This is just an assumption. We can put it to be the second element. The last element. This is just an assumption to start from somewhere. Then in order to find the maximum element in the array, we are going to have a four loop. So we're going to have a four loop that is in I equals one I lower than five I plus plus. Because already the first element is assigned to the max. We are not going to iterate over that. We could do it, but it's redundant because already we know that the first element equals max, so we're not going to iterate over that. So this is why we suggest to assume that max equals the first element. However, you can iterate over all of the elements and assume that max equals any of the elements. It's okay. So we are iterating from the second element till the end, and we are defining an I condition here. We are saying that the element, if the element I of the array number is larger than the value stored in integer max, if this is true, enter this condition and assign that value to our max integer. So for example, let me just explain how it works. So initially, max equals 45. And then for the first time when we enter this loop, we have the numbers one, which equals 12. And in this situation, when I equals one, we have numbers one. So number one means that the second element of the array numbers and why we are writing down one here, why we are saying the second element because in C plus plus, we start from zero. The first element is it's written like this. Zero element of number means the first element. Therefore, the first element, when we're writing the one value here, it means the second element. So it starts from zero. When it's one, it means the second element. So it is entering the loop with I equals one, I equals one when it is talking about the one element, the second element of the array number, it equals 12. So it is saying that 12 is 12 larger than Max, no, so it's not going to enter the loop because max is already 45, so it's not going to enter this if condition. Then I value is incremented by one and I equals two. Then I equals two, let me just write it down in the next line. So max still equals 45. We are discussing the third element of the array number, which is 78. So I equals two meaning that it is second, it is the third element of the array number. Equals 78. So it says that 78 is larger than 45. Yes, it enters the condition and then assigns number 78 or the third element of array numbers to the max. So now max is updated. After implementing this part, max equals 78. Okay? Then this condition is finished, we go up and we increase the value of I to B three. So now max equals 78 numbers. This is going to give us the fourth element of the array, which is 34. So it is basically in this condition saying that is the fourth element of array number, which equals 34, larger than the integer max which is 78. No, it's not, then it's not going to enter this if condition. And then max remains the same. Sorry, max remains the same and numbers we increment the value of I by one, which is its maximal value now because four I must be lower than five, not equal, lower than or equal than five. So now I is four, it's going to discuss the fifth element of array number, which is 56. So it is basically asking is 56 larger than 78? No, it's not. Therefore, it's not going to enter the Ils condition again. And when everything is done, so in the next round, we're not going to enter the four loop because now I is incremented by another one value. It will become 55 is not lower than five, then it's not passing this condition and it's not going to enter this four loop anymore. In the end, it is printing. The largest number in the array is Max and Mx equals 78 as we analyze the code. So let's run the code and see how it works. Okay, great. So here we see the largest number in the array is 78. So this is the procedure, how we can find the largest value in an array. If we want to find the smallest number in an array, we simply just reverse the code and we're just putting number lower than max and if this condition passes, we're assigning that number I into the Max. Thank you for watching. 7. Array (Exercise #2): Everyone, welcome back. In today's lecture, we are going to do another coding exercises for arrays. We're going to write a program that takes five numbers as input and store them in an array. And then it calculates their average. To do that, first, we initialize an array with the size five. It is named numbers and its type is double. And in order to have an average, we first need to know their sum. So we're initializing a double sum equals to zero. It is initialized to zero. In order to get a lot of data from the user, not just one data, one input, but as large as the elements of our array, we need to use a four loop. In order to do that, we mentioned, okay, enter five numbers, and then we enter the loop. I equals zero and I as long as I is lower than five, we are going to iterate in this four loop again and again. And in each iteration, we are mentioning that, okay, now enter the I element of the array. So the first time, so the first time, it's going to mention, okay, numbers zero, numbers one, numbers two, Number three and finally, number four. So it is going to ask you enter five numbers, and then you're going to enter five numbers continuously and you're separating them with the enter bottom. So the first number you are entering, it is going to be stored in the first element of the array number using this line. And the second number you are entering, it is going to be stored in the second element of the array number again here because I is incremented one by one. So the first time I zero, I enters and then you're writing down the number zero like this. Then I is incremented and it's now one, then what you're entering is stored to the second element of the array number. Then I is incremented one more time, and it is two. Now, what you're writing is what you're entering is going to store in the third element in the fourth element, and in the fifth element of the array number. So this is how the procedure would be when we are going to store some elements in the array. And finally, after each iteration, it is going to store the sum of these numbers. So this sum was initialized to be zero. But whatever you're entering, it is going to be added to sum. So the first time, let me just run the code once. It says Enter five numbers. I would say, one, two, 579. Okay? So we're not going to talk about this line yet. Let me just talk about the above line. Okay, now it's good. So it says, Enter five numbers. So I enter one. I entered one. One is now the first element of the array. Sum was originally equal to zero, we initialized it. Now one is added to sum. Now sum is one. Then it is asking me, so they are going to be done after another, one after another. So this line is going to store, and then this line right after that, is going to assign the number to stop. So then I'm entering the second element, which is two, and it's going to be stored using this line. Like we are saying that, okay, now I equals one, I equals one means that it's going to store the second element, and for the second element, I'm entering two. So for the second element, I'm entering two and sum was already one, and now two is added to that one, so the sum equals three now. Then I increased by another value for the next round that it enters the loop, now I equals two, and it is what I'm entering here, like number five is going to be assigned to the third element of array numbers. And five is going to be added already to the already value of the sum which is three because the first time was one, the second time was three. So some initially it is zero, then it is one, then it is three. And then let me give you the details. I will write down the next steps. But when I equals zero, we're going to write down the first element of the number and we mentioned that we have the first element equals one. One is stored as the first element of the array numbers and initially sum equals zero and one is added to sum. Now sum equals one. Then I is incremented. Now I equals one. When I equals one, we are going to enter the second element, which is two. So we enter two, the second element of the array is stored and then two is added to this one because sum equals s plus numbers I here. So having this equation is equal to have some plus equal numbers I, okay? So they are the same equation, this equation and this equation. Okay, so some was already one. So was already won. And we added the number two here. So some in the next round, some equals three. Okay? Then I equals two because in the next iteration, I equals two, when I equals two, I entered number five, which will be the third element of the array. So the third element of the array is stored as to be number two, Then sum was already three. Now I'm adding sum equals three plus five. So then sum becomes eight. So the next value of the sum will be eight. And then we are increasing the value of the I another time and another time until the end, so we are entering all elements and each element is being accumulated in the sum one by one when they are added. And when everything is finished here, we are just printing the value of the average. How we are initializing a double called average and the sum is divided by the number of the elements of the array. In this case, it is five. And then we're printing out the output. So based on our input here, like one, two, five, seven, nine, let me just print also the value of sum here, see out the value of sum equals, then we're printing sum here and the so one, two, five, 79. Okay. Let me just 12, five, seven ops nine. Okay. So here we are seeing that the total value of sum is 24, and the average is 4.8. And if you would like to see the increment value, the increment value of sum in each iteration, I can simply print it here. So we can see how sum is incremented. One, two, 579, one, two, 579. Okay? So now we are printing the incremental value. So initially, sum was zero. Then I added one. So s became one exactly as we wrote here, some became one. We added two as the second element, so the sum became three. Then we added five. So the sum became eight exactly like we calculated here. So basically, we're saying sum was zero first. We added one value, it became one. We added another two value, it became three. We added another five value, it became eight, another seven value, it became 15, and another nine value, it became 24. And then this number is divided by five. The total number of elements in the array, and the average is calculated this way. So this exercise was very easy and straightforward. However, in order for you to get used to how arrays work and how we enter, we can read the elements of an array as the input values and then how to work with them, how to print them, how to calculate the sum and average. It would be nice for you to do some coding exercises and start implementing these coding exercises that I have solved so far, so you can first master the basics of an array before we move to further exercises. Thank you for watching. 8. Array Function: Thank on, welcome to another lesson. In today's video, we're going to learn how to integrate arrays and functions. So we know that in order to reduce the amount of repeated information in the code, we need to write functions. So simply in the int main function, we call another function or many other functions that do certain things for us instead of just repeating those operations in the main body of the code. To do that, we use functions. So for example, if we would like to know the sum of a few numbers, we can define a function that does the sum of two numbers, for example, for us. And every time we call the function, it is going to give us the sum of those two numbers. So this would help us to write down the operations to do the summation only once. However, we can call it several times instead of writing the summation operations several times in the main code. So this is why we actually need to write the functions. For example, here is a void function that we have here. Simply, we're just calling the function and everything, everything else is taken care of. So in this in this section, we're going to pass an array as an argument to a function and the function is going to do some operations with this. So what is the operation? We are asked to write a program that prints all the elements of an array using a function. So the statement itself says that, okay, print all the elements using a function, so don't do it in the int main function, but define a separate function. Order for you to review the basics of functions, there are three things that we need to take care of when we are looking at a function. The first one is the name. Behind the name, it's the type of the function, so it's a void function. It can be ink function, float function, double function. But when it's a void function, it is not going to return anything. If it's a function of any other kind, for example, here we have a int. We're going to return some value. If it's a float or whatever, there is a return value to be passed. But for a void function, we have no output. It's just going to do certain tasks for us or maybe print something for us, and that's it. So first thing is the name, then the type of the function. And in the parenthesis, we have the arguments of the function that are separated with a comma. So we would like to write a function that takes one array, and it's going to print the array. To do that, we declare and initialize the array like this. We learned it before. So we are saying that the name of the array is numbers and the type of the array is int. However, the size of the array is not indicated, but it's okay. I could tell five. It's okay. But even if I don't tell you that the size of the array is five, it can determine it itself because there are five elements in this array. So it's up to you. You can write it down or it's okay if you want to escape it. Then we need to know the size of the array. There is a predefined expression in C plus plus that gives us the size of the array of one element, one integer, et cetera, and it's the size of using this, we calculate the size of this array. How to do it. This is a formula. So we initialize one integer called size, and we divide the size of the array by the size of the first element. Why do we do that? So by doing this, for example, we are having an array of five elements. And then we are calculating the size of the full array, and each element is going to, for example, take four bytes of the memory. When we have five elements, it's going to take 20 bytes. And then when we divide it by one of the elements, we are going to understand how many elements it's having. For example, assume that each element is consuming four bytes of memory. So when we have five elements, it's going to have five times four, it's going to have it is going to consume 20 bytes of memory. So on top, we have the number 20, and it is divided by the size of element one, which is four, then 2/4, the results would be five, which is the number of elements in the array. This is how we're calculating the number of elements in one array, the total size, the total bytes that are using the memory, divided by the first one or any of them, like the second one, the third one because they are going to take the same amount of memory each of the elements. This way, we are calculating the number of elements, and we assign it to size. Then we are calling a function, and we are passing two arguments. Why two arguments because this function is defined and initialized with two arguments. The first element is the array. The second argument is the first argument is the array and the second argument is the size of the array. So here we are passing as the first argument, we are passing the array itself, and then it's size. Okay, we are calling the function. We go up here and we enter the function. It is getting the array on its side. So it is C outing, printing array elements, R. Then in order to print all the elements of an array, we need to iterate over all the elements. To do that, it's easy to use four loops. So we're going to use a four loop, but why do we need the size? Because we would like to automate things. Because of that, we can mention, Okay, it's going to start from zero as the first element of the array, and it is going to go up to the size of the array. So this is why we need the size. And then we're going to increase the value of I one by one. To do that, let me run the code once. So we see the result, and then we continue with analyzing the code. Okay, great. So here, it is saying that array elements are ten, 20, 30, 40, and 50. It's printing everything. So this is what we wanted, and there is one space between these, each of the elements that space comes from here. So let's start by analyzing the the code again. The first time that we enter the array, we enter the four loops, sorry, I equals zero. When I equals zero, it is going to print the first element of the array using this line ARR, I element of the array. In this case, because I equals zero and C plus plus starts from zero basically, and we are here starting from zero. Therefore it is going to print the first element of the array. And the first element of the array is ten. Okay. Then it's going to go up again, we increase the value of I by one. So I is equal to I is now equal to one, and then we are going to print the second element of the array, which is 20. And then again, it goes up again and increase the value of I by one value. So now this time we enter the loop, I equals two, and we're going to print the third element of the array, which is 30, et cetera until the end. So this is exactly what we got as the output here. So basically, instead of printing instead of doing all of these tasks in the main function, we are just simply calling a function that does this for us. So our int main function remains very clean. So this is the first thing, and the second thing I would like to highlight is the size. So we learned how to calculate the size of an array. So now we know if we have an array of ten elements, we know how to calculate the number of elements in that array. This is one thing. And the second thing is that we passed the size instead of, okay, calculating, okay, this array is having five elements. So instead of writing size, I'm going to write five. Instead of doing this, we automated things. So the size is simply replaced here. Why? Because every time that you're dealing with an array and you're calling the function, you don't have to change this number anymore. So because you may have another array with the size of three, ten, 15, et cetera, so you're automating things, whatever the size is, the function is going to work instead of just manually changing the size here. Thank you for watching. In the next video, we are going to do another coding exercise using functions and arrays. 9. 5 Array Function Exercise1: Hello, everyone. Welcome back. In today's video, we're going to write another. We're going to code another exercise, and in today's lesson, we would like to write a function that counts the number of even at odd numbers in an array. So we need to write a function that in the end iterates over all the elements of a function and tell us how many odd numbers and how many even numbers are included in that array. To do that, we need a function to make the code very clean. So we start with the int main function. We initialize one array. We buy now, we know how to declare and initialize an array. Then we calculate the size in order to calculate the size of an array we calculate the size of the array divided by the size of the first element. So this way, we know how many elements are included in this array. And then in order to count the number of even and odd numbers, we just declare two new integers like even count and odd count. So then we call our function and we are passing four arguments. The first one is the array. The second one is the size of the array, and the third and the fourth one are the integers that we declared here that they are going to do the counting for us to count the number of event numbers and odd numbers in this array. So let's go up here. When we call the function, we are coming up here. The name of the function is here. It's the void type. In the parenthesis, we see the we see the arguments. We see four arguments here separated by comma. So the first one is the array of type int. The second one is the size of the array by type int. The third one and fourth one, also they are type int. However, they are passed by reference. So let me review the concept of pass by value and pass by reference one time here. So you see these two values are passed by value because there is no and sign behind them before them. It's a pass by value. So basically in pass by value, we are making a copy of the original numbers or arrays, and we send the copy of them to the function and let the function to work with the copy. So any modification that is made to the array or the size integer is going to be locally implemented here, and it's not going to have any impact in the array and the size in the main function because this function is working with a copy of the original value because it's a call by value, okay? However, when we are doing the call by reference and the sign is having this and behind before the name of the integers, we are calling by reference, and calling by reference means that it means that we are giving the address where these two values are stored in the memory, and it is going and directly working with the original values. Therefore, it is making modifications to these values. So any changes any changes that are done here, is going to impact on these two. So this is the basic concept of call by reference and call by value. And why we are passing these two why we are passing these two arguments using call by reference because this function is actually going to count the number of even and odd numbers for us. Therefore, it's important to know the output value in this main function as well. This is why we need a call by reference. But for the first two values, we don't need any output from this function for these two arguments. Therefore, we are just calling by value. Okay. Now that we know the difference between call by reference and call by value, we are initializing these two past numbers to zero because they were only declared and they were not initialized. We are initializing them here. In order to iterate over all the elements of an array, we need to use four loops. So we start from zero and we go up to the size, we know from before, and we are increasing the value of I one by one. So we are going through all the elements one by one. And then in order to see if a number is odd or even, we need to define ILS conditions. To do that, we say if the I element of array is divided by two and its remainder using modular sign here, and its remainder equals zero, it means the number is even. Therefore, increase the value of even count by one because we found one even number. And if this condition is not going to pass, go to the else because if the number is not even, it is for sure an odd number and increase the odd. Okay, let me make some further analyse of the code here, so it would be easier to follow. So we are starting we start to go through all the elements of the array here, and the first one is ten. So I equals zero, we enter the loop. Here, I equals zero, we enter the loop, and it is investigating to see if the first element, the I element of the array, which is the first one in this case, because I equals zero. If this number is divided by two, its remainder equals zero or not. The first element of the array is ten. So 10/2, the remainder will be zero. Yes, so the remainder equals zero. Therefore, it's an even number. So we say even count equals one now. Then when this condition is passed, we're going up again and now I equals one. Therefore, we are investigating the second element of the array. So we are saying that basically we are entering with I equals one. We are basically discussing array in the bracket one, which is the second element of the array, which is 15. And we are basically saying that if 15 is divided by two, it's remainder equals to zero or not in this line. Okay? We are saying that 15/2, the remainder equals zero. No. When the answer is no, it's not going to enter this part. It's going to escape because it's not passing the condition. Therefore, it is going to enter the else part, and it is increasing the value of odd count equals one now. And now that it's done, we go up again, we increase the value of I one more time because it is going to iterate until I is lower than the size of the array. And the size of the array is one, two, three, four, five, six, seven. So I is going to take 0-6. So I equals two, and then we are discussing the third element of the array the third element is 20. We are basically in this condition discussing if 20 is divided by two, the remainder will be equal to zero. Yes? Yes, this is true because 20 if it's divided by two, the output will be ten and the remainder is zero. Therefore, we are going to increase the value of the even count another time. So this time it is equal to because previously it was one and then we are increasing another time in this line, so it equals two. Okay, now I equals three, we are basically discussing the fourth element of the array. So the fourth element is 25. I equals three, it enters. It says that ARR in the bracket number three, which is the fourth element of the array. If it's divided by two, it's remainder equals to zero or not. So here, basically, 25, if it's divided by two, the remainder is not going to be equal to zero. Therefore, it's not going to enter this condition and it's going to enter s. And when it enters s, it's going to increase the odd count integer by another one value. So odd count previously we had it here. I was equal to one. Now we are going to add one number to it, so it is going to equal to two. So basically, this is the logic of the code. Now if I run, you will see how many odd and even numbers we have in this array. So basically, one, two, three, and four even numbers are included in this array, and we have three odd numbers in this array. So let's run Okay, great. We see exactly the number of event numbers, a number of odd numbers. Okay. So here we do all of the calculation that we call the function here and we do all of the calculation here. After everything is finished, we are coming back to the main function, of course, and we are going to print number of even numbers where we are printing the even count, a number of odd numbers, we are going to print odd count, we see that the number of event numbers is four equals to four, and odd numbers equals to three. This is what is what we expected actually. And the important thing just to summarize and highlight is to do this by calling by reference because these two numbers are going to change here. We are actually calculating their value one by one here. We are increment their value, and then we are printing the final value that we got here in the main function. So if it's a call by value, it's not going to have the output, the correct number because it's not going to change. When the function is finished, it's finished and everything else is deleted. Thank you for watching. Please make sure that you do these coding exercises yourself because it's very important. They may see and they may look easy and understandable. But you will never make errors until you do it yourself. So please go ahead and do it yourself. Please make errors because it's important to know where the errors come from, and then you can improve from there. And you can always come back to these videos and review the logic and analyzing the code, so you will find exactly how you need to improve your coding. Thank you for watching. 10. 6 Array Function Exercise2: Hello, everyone. Welcome back to another coding exercise session. In today's video, we are going to do coding exercises with respect to the arrays and functions. So we would like to write a program that checks whether a given number exists in an array using a function. So basically what we understand from this statement is that we need to write a function that checks if a given number exists as one element of the array or not. So to do that, we have the int main function here. We are initializing and declaring one array. We know from before how to do it. We are calculating its size to see how many elements this array is carrying. In this case, it's going to be five. And then we are initializing the target, we're declaring the target integer, which is the given number that is going to either be decided that its number is it included in this array or not. So this target is going to be read from the user. Like, I'm going to enter one number as, for example, seven, and we need a function to see if this number seven is included in this array or not? Is it one of the elements of this array or not? So this function is basically going to do that. So we initialize the target integer and we are sending a message to the user saying that enter a number to search, and then we are storing it using a syntax and basically, we are checking here if search element, the search element is the function that we defined to do the checking for us, and we're passing three values, three arguments, first, the argument, first, the array, second, the size and third the target number. So we are simply here, we're saying that if something is true here, just print that we found that number, that target number. In the array, otherwise, print that it's not in the array. From this if condition, we understand that this function is having is of a boolean type. Why? Because we are saying that if this statement is returning true Go ahead and print the target number and mention that it is found in the array. Otherwise, say that this target number is not included in the array. So let's go and see how we are doing this calculation using search element function. When we call the function here, we go up with three arguments. Array, its size and the input target number by the user. We go up here and we see that the type of the function, as we discussed, it's a bullion, so it's going to return something for us. It's going to return either true or false. Basically, we can guess. So if the target integer is one of the elements of the array. It's going to return true. If it's found, if the element is found in the array, it is going to return true, and otherwise, it's going to return false. So when it is going to return true, it's going to print that the target number is found in the array and when it's going to return false, it's going to mention that target number is not in the array. So basically, this function, this is the name, this is the type which is Boolean, and these are the arguments of the function that are separated using commands. So the first one is the array, the second one is the size of the array, and the third one is the target number. And all of them are passed by value because we are not going to make any modification to them. We're just going to work with their values. This is why we're not calling them by reference. So in order to see if one number, one integer is is actually equal to one of the elements of the array. We need to iterate over all the elements of the array to see if one of them matches this target number. To do that, we use a four loop to iterate over all the elements of the array. So we start from zero, and we go up to the size of the of the array, which is five in this case. And one by one, when we are passing through, we are saying that, Okay, is this element equal? Is this target number equals to element I of that array. So let me just analyze the code further. It's more clear for you to understand. So let me just mention that, for example, I choose the target number to be like 18. Okay? So I would say target equals 18. So when target equals 18, initially, I equal zero and it's going to enter the four loop. So when I equals zero, it is going to investigate whether the first element of the array equals target or not. And this means that we are checking to see if five equals 18. How? So I chose 18 to be the integer target. I equals zero, and when I equals zero, we are investigating when we are investigating this condition, we are basically investigating this. So the first element of the array is equal to the target number. And basically, it means that the first element of the array is five and the target equals 18. Therefore, we are mentioning if five equals 18. So if this is the case, it is going to return through. So it is existing. It exists and it's going to return through here. And then we are getting out of the loop and we are simply printing that, okay, the target number is found in the array. But in this case, this is not the case for us, okay? So as this is not the case, target is still 18 because we are not going to change it. It's one time chosen value. Then it is going to investigate I equals one, which happens to be something like this. So when I equals one, we are simply saying that, okay, the second element of the array equals to target or not. In this condition, we are investigating this. And since the second element of the array is ten, we are basically investigating if ten equals 18 or not. This is not true. So when it is not true, again, it is going up and increasing the value of I by another I by another one value. So now I equals two, which means that we are going to investigate the third element of the array. So when we are investigating the third element of the array, we are basically mentioning is 15 equal 18 no, it's not true again. So it's going to go to another round of the loop this time. Again, target is 18, I equals three, I equals three, and it means that we are investigating the fourth the fourth element of the array, meaning that we are saying, does 20 equal 18? No. Then for the last time, we are going to increase the value of I another time. So I equals four now, and then we are discussing the fifth element of the array, and we are basically saying I 25 and 18, are these two numbers are equal? No, we didn't find any answer to return true because neither of these equations are correct. So it's not going to enter this if condition. When it's not going to enter this if condition, it's not going to return true. And then when it is exiting this condition and this four loop is finished because we reached its upper limit, which is four. Why? Because when I is four, it is lower than the size of the array which is five, four lower than five. When we are increasing the value of I, another time I becomes five, Then five is not lower than five. Therefore, it's not going to do another four loop. So we exit here and we are simply saying that, okay, return false because we didn't find it. So basically, let me run the code and enter the target 18, we expect to see. We are expecting to see 18 is not in the array. So let me run. So enter to search, I'm saying 18. Okay, it is saying 18 is not in the array, and how it is deciding whether it's in the array or not using these analysis, we're understanding it. But what if we are mentioning number like ten as the target value. If we mention number ten as the target value, so let me just revise these numbers. Okay. If the target value is ten, I starts from zero. It is comparing the first element of the array, which is five with the target number, which is ten now, and we are saying, Okay, does these two numbers are equal or not? These two numbers are equal or not? So it's no. The answer is no. Therefore, we are going up again in the fourth loop statement and we are increasing the value of I by another one value. So I equals one now because it starts from zero. Okay? I equals one. So basically, when we are investigating this condition, we are basically saying that we would like to see if the second element of the array equals to the target value which is ten. Basically, we're saying, does ten equal ten? Yes, the answer is yes. When this makes sense, when this condition makes sense, it actually enters this condition to return true. When it returns true, the work of the function is done, it's finished, and here it is simply mentioning that, okay, target value is found in the array. So because when it returns true, this if statement becomes true, and then it enters this part, it enters this part, and when it enters this part, it's going to print it. So let me just choose the input target value to be ten. Okay, it says that ten is found in the array. So basically, this is how we did the coding exercise, and we analyzed the code. It would be nice for you also to start doing these analyses for yourself to make sure you understand all the steps one by one. This helps you to remember how the program works and it will be beneficial for you in the future. When you are writing your code, you can imagine, okay, if I write it this way, it's going to work this way. So this is what we want or this is not what we want, so I'm going to change or modify the path, et cetera. So please go ahead and do this exercise yourself because as we progress, there are many coding exercises that we are going to do. So try to start the coding exercises and do the coding exercises yourself as you proceed throughout this journey. Thank you for watching. 11. 7 Array Examples: Hello, everyone. Welcome to another video. In today's lesson, we are going to declare and initialize one array, and we're going to do some simple manipulation that are important using array. So basically, we know how to declare and initialize an array. So the name of the array is X of size five, and it is of type integer. There are five elements in this array one, five, eight, 12, and 15. Order to print the array, let me just comment the rest of the code. So in order to print the array, we do it using a four loop. We start from zero. We go up to the size of the array and then we are printing the I element of X. So first time I zero, then one, two, three, four, and all the elements of the array is printed. Okay, great. So this we know from before. I equals zero. When I equals zero, it's like this X zero, then I equals one, X one. So when X is zero, it's going to print the first element of the array like this. Then when I equals one, it's going to print the second element of the array X, which is five, et cetera. So we know how to do this. This is just to show you how we print the elements of the array. But if we are printing, if we would like to print specifically one element of the array, we can simply do something like this. So we name the array and then we are going to call the, you know, element of the array that is in braces. So in the braces, we're saying zero, it means that call the first element of the array. Then we are having in the braces, we have the number one, which is going to call the second element of the array, then the third and the fourth and the fifth elements of the array. This way, we also can print the elements of the array. But if we are only willing to print, for example, the fourth element of the array, so the fourth element of the array is this one because C plus plus starts from zero. So this is the first element, second, third and the fourth element of the array is X, the name of the array. And in braces, we type down the number three. So it's going now to print us the fourth element of the array which is 12. So this is another point. These are very basic operations, but I think it's important to know them before we dive into more advanced parts of the array. So basically, we can manipulate array elements. So initially, the first element of the array was equal to one. Here, I'm assigning number ten to be the first element of the array. So if I print again the array, we will see. Let me just print also the array here. Okay. So yeah, we see that when we initialize the array, the elements were like one, five, eight, 12, 15, we printed it. It is here, it is printed here. Then using a line, we are trying to separate them from here. And then we assign ten to be the first element of the array using this line. And as you see, instead of having one, we are having ten here. But the other elements remain the same. So we could assign it to the second element, for example. Okay. So when we are assigning it to the second element, the first element is the same. The second element instead of being five, it's ten now. We could also manipulate more elements like X equals 68, for example. Okay. The fourth element. So in this case, we are changing two elements. The first element is the same in both arrays. The second element was five initially here was five, and now it's ten because we assign ten to the first element. The third elements are the same. The fourth element, initially, it was 12. Now then we set it to be 68 and the last elements are the same. So this way, we can also manipulate the elements of an array manually. And the next interesting thing that we can do is the following. So we can initialize one integer called index to be zero instead of writing X, within braces zero equals 26. Instead of writing this, like above, we mentioned the example, we can write it like this. It's nicer to write it like this. So within the braces, instead of writing the actual number zero, we can define an integer that is zero, and then we put the index integer in the braces. So this way, we are also able to manipulate the array. Let's see. As you see, the first element was initially one and now it's 26 because 26 is assigned to the index element of the array X and index equals zero. Therefore, the first element of array x equals 26 now and we're printing the value of index in the end to know the value of the X. Let's do some further modifications here also, so here, I am writing down that index index was zero, right? And we printed it, it's zero. Here, index is zero. So if I again type X index, equal 91. It's going to because X index was zero, it's going to put 95 to be the first element of X. However, using this line, we are first increasing the value of index by one. So index was zero, now it becomes one. And then 91 is assigned to the second element of the array x. Let's run and see. Okay. So the first array, the second array is this one. So the first element of the array is set to 26 and the remaining elements are the same. We know it from here. However, when we are increasing the value of index first using plus plus and then we write down the index in the braces, it is going to first increase index from zero, which was here to one, and then 91 is assigned to the second element of X. It is something like this now equals 91. Okay? Because now index becomes one. So this line is exactly like this line. Be index was zero, we increase zero by one value, so it becomes one. In the braces, it becomes one. And when it becomes one, it means the second element of the array x. So when we're talking about the second element of the array X, 91 is assigned to it. So either writing this line or this line, they are going to do the same thing. And here, when we print again the index, we see that the index equals one because we increase its value. However, if the plus plus is after the index, this is interesting. I plus plus is after the index, so we see the second array was 26 was set for the first element. So we know from before an index is zero. But when the plus plus is after index, it means that 91 is assigned to the index element of the array X, and in this case, index equals zero. So when it simply means X zero equals 91. So this line equals this line. However, when 91 is assigned to the first element of the array X afterwards, the index value is increased by one. You see? So when we have the index, after that, we have plus plus, it means that 91 is going to set to the indexed element of the array X, and in this case, index is zero. Okay? 91 is going to assign to the first element of the array X. When the assignment is done, then increase the value of index by one. So this is why we are seeing that the index value is one, however, 91 is assigned to the first element. So it's important to know where to put the plus plus depending on what we expect it to be. So let me just keep it afterwards. So 91 is going to assign to the first element of the array x, and then index is going to increase by one value, so index becomes one. Let's do the last example here. When we are doing the last example here. Let's go up and review everything. So we initialized one array. We're printing the array, and this is the original array. Then we are changing the first element of this array, we initialize index to be zero. Then we say that the first element of the array X equals 26, and then we print the array. We print the array, we see both arrays are having the same last four elements, but the first element is modified because of this line. This line equals this line exactly because index equals zero. And then we print the value of index, it's zero. Then we're mentioning that, okay, now index is zero. Therefore, assign 91 to be the first element of the array x. Why first element? Because index is zero, it's going to assign it to the first element. When the assignment of 91 to the first element of array x is done, then increase the value of index by one using these two plus plus sign. So 91 and then we print the array. So this way, 91 is assigned to the first element of the array, we see that it is modified 26-91. But when we are printing the value of index, it's going to print value one. And in the end, since index now equals one, when we are writing this line, it means that they put the 68 to be the index element of the array X, and index equals one. Therefore, it's going to set 68 to the second element of the array X, and then it's going to print the array. It's going to print the array. So we see that the 91 stays the same, but 68 is here as the second element of the array index equals one. So this was a very good coding session, I think, because there are simple things simple manipulation that we are going to do in later lectures. However, this lecture is a basic for manipulating the different elements of an array and working with plus plus index or index plus plus. So these are important to know a priori to solving further advanced example. Thank you for watching. 12. 8 Array statics: Hello, everyone. Welcome back. In today's video, we're going to discuss the static variables and arrays. So basically, when we define a function and when we call the function, the function is going to execute certain blocks of code. And when the function is finished, whatever that has been done in the function is going to be removed from the memory. That we know before from before. However, in order for the function to retain some part of the values that we initialized in another call, it is necessary to use static variables. Let me give you an example. So we would like to write a program that uses a static array to count how many times a function is called. So let's just eliminate the static part for the moment. We solve the problem using normal situation, then we integrate the static variables. So we have one int main function here and we have another function. It is called count calls, and it's a void type, and it passes no argument. This is why in the parenthesis it's empty. So the first time we are calling this function, this function is this function starts to be executed. So we here initialize an array named call count, and it is of the integer type. And the value here shows that it only has one element, and its element is zero. So it only has one element, and that one element is zero, o? Then in order to manipulate different elements of an array, we can use the following code line, this one, so basically, we are calling the array, we are calling the array, and then we are calling the element of the array. When we are saying the zero, it means the first element because C plus plus starts from zero. So if this array had two variables, two elements, for example, and we were writing one instead of zero here, we were accessing the second element or the last element of the array. So here we can access the first element of array call count using this line. So now we learned how to access different elements of an array. So in order to access the first element of array call count, we are using this code. And by using plus plus, we are incrementing the first element of array called count. So previously, it is defined to be zero. Now we are incrementing the first element of the call count array therefore, it should be one now. So we are adding one value to zero. So the first element now is zero. Then it is saying that function called, and it is going to print the first element of the array. Previously, we know when we wanted to print different elements of an array like all the elements of an array, we used to use a four loop. So we iterating over all the elements of an array. However, if we would like to access and print maybe or manipulate a specific element of an array, simply we can use this. So because now we are writing C out, it's going to print the first element of the array call count. Here, we are manipulating the first element of the array call count by increasing it by one value. We could decrease it also. So there are different things that we can do. Oh, the function then it is going to print the first element of the call count array. After this manipulation, we expect the printed number here would be one rather than zero. Why? Because we increased its value by one here. Then this function is finished. This function is executed and we come back here. So this line is also executed. We go to the next line. When we go to the next line, again, the function is initialized. Sorry, the array is initialized to be the type of d. It is going to have one element and its element is zero, then we are going to increase this first element of the array called count. So we're implementing it by one value, and when we are printing it, it is going to show us again one and another round. So we're going to call the function again. We are doing the same thing. So we are going to print three numbers. One, one, one. Let me just call the function and then start the program. Okay. Here, we see that for the first time, it is calling the function. It is declaring and initializing the array, and then it is incrementing the value of the array here, and then it is printing. So it is printing one function called one time. When we are calling the function again, when we are calling the function again, it comes back here, it initialize it declares and initialize the array again, it is incrementing its first element and it is printing it. Okay. However, when it is finished, it is removing everything from the memory. This is why the next time we're just having the function called one time and then the first time. Let me print the also see out. I think it's nice to have this line, so it would be easier to see the increment impact. We are saying that function called before manipulation, call count, zero, and L. Okay, let me run. Okay. So every time that we call the function, it is initializing and declaring one array. Then we are printing its first element. So function called before manipulation, the element of the array, the first element of the array called count is printed, which is zero. Then we are increasing it, we're incrementing its value by one. So the output then will be one. When we are printing it again, it is one. So we see that an increase of one time is done. We finish execution of this function and we come back here, we go to next line, we are going to call the function. Again, everything is going to start from zero again. So every time that we call the function, everything starts from zero. We are re initializing again the array, we are declaring it. We are printing its first element, then we are increasing the first element. However, if we use a static keyboard here before the type of the array, when we run, you see, the first time we run, it is going to declare and initialize the array. So far, it's the same thing, but we see the output here. So we added only one keyword static here. When we are printing the function called before manipulation, it is printing zero, which makes sense because the first element is zero here, so it is going to print zero. Then it is incrementing the first element of the cold count array by one, and then we are printing the first element of the call count array here. So we are saying function called one time. Then we come back here and we call the function again when we are calling the function again, here we are printing that function called before manipulation for the second round is one. You see the difference. So it could retrieve what the first element of the array was in the previous call. So it is not printing zero again. It is printing one, which was the value of the first element of the cult count function after the manipulation using this line in the first cult in this line. But here we are calling it for the second time, it is still remembering that we one time has done one modification on the first element and the first element is one because we did one manipulation. So when we add the keyword static, the memory usage is different. So it remembers that we had already declared and initialized this array once we manipulated once and incremented its first value 0-1. Now it is printing its first value using this line which equals to one. We are going to increment the first element of the call count array in the second call, then we are going to print it. So when we are printing it, it says function called call count. So print the first element of the call count array. It is going to print two. Why? Because it remembers that when we started, it was one, then we increased it by one, and it is now two. Code is executed, we go to the third line. And when we go to the third line here, we will see that it is again, remembering what has been done in the first call and the second call of the function. Now it's the third call, and it is going to print function called before manipulation. It is printing the number two. And then we are modifying it again. We are increasing its value by one, and we are printing it again, and it says, function called three times. So there are two things to remember from the call calling the function and using static variables. So the first thing is that when we have the static variables included, the program remembers what has been done before. And it uses the same memory. This is why it is remembering it. When it is initializing an array of static type, it is going to initialize it and it's going to be there forever as long as we are using this function. And it's not going to declare and then delete the memory in the end. So when we are using the static, it is going to remember. When we are not using a static, it is going to initialize it, and when the function ends, it's going to remove it from the memory. So when the first call is ending here, it is going to remove everything. This is why in the second call, it's not remembering anything. This is the first thing to consider, and the second thing is that it is very useful to, for example, initialize an array using static variable to have a counter like mentioning, Okay, I would like to declare a function. In that function, there is an array that is of static int, and we know whenever we enter the function, we are going to manipulate one data, for example, here. And in the end of the program, when you have 200 lines, you may need to call this function again and again. Using static variables will help you to understand how many times this function is called. So it could also help you to have a counter there because it's going to remember previous calls. And finally, the same thing could be done using call by reference. So instead of using static int array, we could simply define some arguments here and pass these arguments using call by reference. Therefore, the function was going to originally use the arguments that are passed in the same place of the memory, it was going to access the same place of the memory and then do the modification directly using those arguments. However, in terms of memory usage, there are some differences between using static array or like calling a function by reference. These topics are also very important in memory management that we are going to explain in this course, but in another section. So just keep that in mind. This can be done also by using a call by reference, for example. It's a bit different how we do it. So basically, we need to remove this static keyboard and we're going to define some arguments here, and we pass the arguments using this function and we have the sign before the argument to mentioned to give the program the sign that we are using called by reference. So this could be done both ways using static variables or call by reference. But in terms of memory usage, there are some differences between these two. However, those differences are going to be discussed in another section in the same course in this course, but in another section about memory management. Just keep that in mind, we're going to dive into the details later on in this course. Thank you for watching. 13. 9 Array statics exercise1: Hello, everyone. Welcome back to another video. In today's video, we're going to do one coding exercise using arrays and static variables. However, before going through that coding exercise, I'm going to give you a little bit of concept about one type of arrays. So we start in the int main function. These parts are commented, so this is for our coding exercise. We're going to discuss them later on this video. But here, something that I would like to mention is that we learned how to declare and initialize an array. So this is an array of size five with the type integer, and all of its elements are set to zero. So when I print the print of the elements, when I want to do that, I write a four loop. It starts from zero up to five when I lower than five and then increment the value of I by one time in every iteration. Then we are printing the I element of array in each round. It's when I equals zero, it's going to print the first element of the array, then it's going to print the second, the third, the fourth, and the fifth. Let me just run this code. Okay. So it is very straightforward. So far we know already how to do this. We're printing all the elements of this array. And this array is of size five with five identical elements. So instead of writing the zero everywhere here, we can simply write it once and saying that it's size five, and we only write one element of it because the rest are identical. And let me run it again. Okay, we see that we are having the same results. So when we are writing and declaring and initializing an array with the same elements, when you write down the size and only one of the elements, it's going to perceive exactly like this. So this array, when it is declared initialized and initialized, is the same as this array. Okay. But in order to have shorter codes, we are going to use this version of declaring it. This is a point that I wanted to mention before we dive into the coding exercise. But something to mention is that only this is true when the elements are zero. When the elements are zero, you can simply use this way of declaring and initializing if the elements are one, it's not going to work. Let me run. Okay. So as you see here, it's not going to consider zero for all the elements, but one for the first element and zero for the remaining four elements. So by default, C plus plus compiler is considering zero for the not declared elements, and for the other numbers, you need to give it to it. So if I mentioned three also here, it's going to consider the first element to be one, the second element to be three, and the rest that are not declared here to be zero. So something to just keep that in mind. So if you're putting zero here, it means that all the elements are zero because you said that it's an array of size five. You put zero, so it knows that all the elements are zero. However, if you put one, two, et cetera, it's going to consider one as the first element, two as the second element. And if you're only adding two of the elements, the remaining elements are considered to be zero by default. So this is something to keep in mind while working with arrays. Okay, let's dive into the coding exercise. We would like to write a program that maintains a static array to store scores of a game and updates them in each function call. So we have one main function here. For example, let me just explain the first two calls and then the rest. And simply, we are calling a function called up the discors which is a void type, and it is getting one argument. And here we are just giving the argument as discors let's say. To the function. We are passing this argument to the function and the function basically is initializing one array of type int and static with the size of five and all the elements are zero. And also in order to keep track of position to update, we are initializing one integer of type static to zero. So basically, the index is zero and the size of the array is five. So as long as we are entering and calling this function. So if the index is lower than five, which is the size of the array, assign the value X, which was passed to the function as an argument to that index of the array scores. What does it mean? So when I call the function for the first time, using the Argument ten, I call using Argument ten. Okay? Here I'm calling the function using Argument ten. Here X equals ten, index equals zero, okay? So when index equals zero, we are basically here accessing the first element of the array, and the array is let me write it the other way. So the array scores is having five elements, and all of them are zero. Okay? So basically, with this line, when index is zero, we are having the array scores equals to ten. Okay? So here we are calling the function with the argument of ten. So here, this function is called with the argument of ten. Therefore, X equals ten. And initialized we initialized index to be zero, and we have the index zero. And when the index equals zero, using this line, we are assigning the X variable or ten to the first element of the array score. Why first element? Because index equals zero and index is put in in the in the file in the in the brackets of the score. So it means that we are assigning ten to be the first element of the score. And then we have the incremental value. It means that ten is assigned to the first element of the array score. Then we are incrementing when this assignment is done, then we are incrementing of the value of index by one. So we're manipulating it. And if index is not lower than five, it's going to print, scoreboard is full, because we can only insert five score into the scoreboard because the size of the array is five. So basically what we're doing here is done then we are saying current scores. We have a four loop, and we are printing all the elements of the scores. So we're going to call the function twice. Let me run the code and then explain further the code. Okay, great. So here, we're calling the function with the argument of ten, ten equals X. So initially, all the elements of the array were zero. This is not the initial array, the initial array is this one. Initially, everything was zero and the index was zero. It says index lower than five, it enters, it assigns ten to the first element of the array score. Why first element? Because index is zero and basically using this line, we are accessing the first element. So when the first element is equal ten, this happens. So ten, instead of having the first element to be zero, we are having the first element to be ten. Okay. And everything is done here in this if condition and we are printing the array here. So we say current score, we're printing all the elements of the array. And the code execution for this function is done. We come back to the main function, we go to the next line when we call again the function using the argument of 20. So in this case, we are having call using argument 20 X, therefore equals 20, index equals one this time, scores one equals 20. So this time, we are calling the function using the Argument 20. And since the array was initially initialized and declared using static keyword, it remembers what has been done before. So it remembers that we did a manipulation to index. So index was earlier zero. But in the first call, it became one. Why we incremented its value here by one. So it says that now index lower than five, yes, because one is lower than five. And why not zero? Because this is a static index. Okay? So the first time it was zero, but because it is static, it remembers that in the first call we manipulate and modify the value of index 0-1. Now it considers that index is one. So one lower than five, yes. Therefore, it is going to put an assign the value X, which is 20 in the second call. 20 is going to be assigned to the second element of the array score. Therefore, the second element of the array score is going to be 20. What was the first element? It was ten. Okay? So when the 20 is assigned to the second element of the array score, this part of the I condition is finished. Then we are printing again the current score. So when we start from zero, the first element of the score, we're printing it again for the second time because it's the second time we call the function. It is going to print ten because already from before, it remembers, right? It is a static variable that used to initialize and declare this array. The first element is ten. Now we have the second element equal to 20. In the first time, we only had ten here, and the second element was zero. But when we call the function for the second time, the second element is set to be 20. Okay. And then when the second element 20 is assigned to the second when X equals 20 is assigned to the second element of the array score using this line, after this is done, we are going to increase the value of index one more time. When the plus plus sign is after index, it means that X is going to be set to the index index element of the array score, and when it's done, then the index is going to increase by one value. This simply means that plus plus is after index. So let me just remove the plus plus. So you see the difference, it's more tangible. Okay. So when we remove plus plus, when we remove plus plus, we're calling the function using the argument ten. And initially in the first call index equals zero, so zero lower than five yes. It is going to put ten as the first element of the array score because index is zero, and then it's going to print all the elements of the array core. Therefore, it's going to print this one. And everything is done, we are going to call the function the second time and we are going to call it using the argument 20. So in this case, X equals 20. However, since we didn't increase the value of index in the previous call, index is still zero. So it says zero lower than five, yes. So it is going to now index equals zero. Therefore, it is going to assign 20 again to the first element of the array score because we didn't manipulate the score, the index value. It is zero. But when we are having the plus plus for the next round, it's going to be increased, okay? Okay, great. So let me uncomment the other lines as well. So when I uncomment the other lines, we see that when the second call is finished, the third call is done using number 30 and 30 is going to be the third element because we are increasing the value of index one time by one point at a time. So let me also analyze this so basically, it's going to be equal to one. So now call using argument 30 X equals 30, and this time equals two scores two equals 30, okay? So basically, when we are putting the number 20 to be the second element of the score, afterwards, we are increasing the value of index. So index will become two in the next round of call. So when the third round of call is done using the argument 30, index is two, so it started from zero and now it's two. And therefore, when it's two, it's going to assign number 30 as to be set for the third element of the array score. And then it's going to print and then again, it's going to call the call the function again with the argument 40 and 50. So therefore, the array is progressively being modified until it reaches the last part. So when the array is already full, we run the code again. So you see here, when we reach to this part, the array is already got the numbers that we want, and the size is only five. And we were counting the side, keep tracking of the updates using the index, if you remember. So in the last round, index equals five. Index equals zero because index equals four because it starts from zero. So index equals zero. Index equals one, index equals two, three, and four. Then index equals four, we have done five modifications. Okay, zero, one, index equals zero, one, two, three, and four. So all the five elements of the array are updated because it's an array of size five, so all the elements are updated. Therefore, when we are increasing, when we are, for example, calling the function here, call using argument, for example, 50 X equals 50, index equals four, okay? Therefore, scores four equals 50. When this is done, when this is done, Using this line after having the index equals four here and 50 is assigned to the fifth element of the array score, then we increase the value of index one more time. Index becomes five. When index becomes five, in the sixth call of the function, index is five. Therefore, it says that, okay, if index lower than five, it's not going to enter and not going to pass this condition, why? The reason is that five is not lower than five. Therefore, it's going to print the s, which says, scoreboard is full, and then it's going to print the full array for us. It was a very good example of how static variable is useful and is going to help us to have what we have in the output to update progressively, for example, the array. If we didn't have the variable static. We couldn't have the output something like this. So you see that progressively, everything is updated, and in the end, we have an array with scoreboards. We have an array that is like a scoreboard and it's storing all the elements that we want. However, if we were not having the static variables, things were different. So let me just remove to show you how it's going to work. Okay. So the way that it was going to work was like this when static variables was not there. So we're calling the function for the first time, for example, the value is ten. So we have an array of size five, all the elements are zero, then index is zero. Index is lower than five. Yes, we are going to then assign number ten to be the first element of the array score, and we are going to increase the value of index later afterwards, and then we are printing the array. Everything is done. When we print the array and we reach to this point, whatever that is done and modification that is done in the function, it's going to be deleted. So now we are calling the function for the second time. Again, it is going to initialize an array from zero again. Then it's going to initialize one int called index to be zero. Then it says that, okay, zero lower than five, yes. So put 20 for the first element of the array score and then increase the value of index and print everything. So First of all, since we are not using the static variables, it is not going to remember that in the first round of the call for the function, we increase the value of index. Why? Because index is simply one int and it's not static? Because it is not a static. It's not going to remember what was the value in the previous call of the function for the index. So index is going to be reinitialized and re declared in every single call that we have for the function. This is Y 20 is stored here, 30 is stored here, 40, 50, and also 60 is stored here because whenever one function call is finished, everything is removed from the memory. Like nothing happened. And then we are calling the function again. Everything starts from the beginning again and it's going to perform all of the task. And then when it's printing everything in the end of the function, it's going to remove all the memory again. So it's going to call the function for the third time for the fourth time, et cetera. So this is why it is important to use static variables if you would like to see the progress of how everything works using an array. Thank you for watching. Please, please go ahead and do these codings yourself yourself because it's very important to do the coding yourself and get to those bugs and errors and start thinking of what went wrong. That is the point where you find your weaknesses and you're going to improve from there. Thank you for watching. 14. 10 Array statics exercise2: Hello, everyone. Welcome back. In today's video, we would like to do another coding exercises using static array. Okay, so far, we learned how important it is to consider static array in certain situations. In this video, we're going to write a program that uses a static array to store user input values across multiple function calls. So to do that, we have the main function and then another function that we're going to call repeatedly during in our main function. So the name of the function is store user input. It's of the void type, and its argument is an integer called value. So in order to call it for the first time, we call the function with the value of five. Here we are initializing and declaring a static array with the size of three, and all of the elements are zero. And then we are initializing and declaring an index to be static in integer set to be zero initially. So this is index is used to track verb to store the next input. So in the first call, index equals zero. So when we are encountering this if condition, we say, okay, zero lower than three. If this is the case, set the value, which is the argument of this function, which is five in the first run in the first call of the function, set the value five as the first index of the user input array. After doing that, then increase the index by one. So in the initial run, it's like this equals to five. It means that set the value five to be the first element of the user input array, and then the final index will become one. Initial index equals to zero. Why initial index equals to zero because we initialize it and why the final index equals to one because after we set the five to be the first element of the user input array in this line, we are having plus plus sign here. So when we are having the plus plus sign, it's going to increase the value of index by one. So the final index after this line will be equal to one. Then we are simply printing the value. Value is number five because this is the argument of the function, which is defined here and then here, and then it is being simply printed here. So whatever the number is being assigned to the first element of the array is being printed here. So we know what has been passed and what has been inserted to the array just for the sake of checking, okay? Then we have this part as well. If this index is not lower than three, it's going to enter this s part. There it is printing array is full. So stored values or it is going to print all the elements of the array using a four loop, and the four loop is going to start 0-3 which is the size of the array, and it's going to print all the elements of the user input of the user input array. So when I equals zero, it's going to print the first element. When I equals one, it's going to print the second element, when I equals, for example, 22, it's going to print the last or the third value. One time, the first call of the function is now finished. Program is executed, so the program goes to the next line and it's going to call the function again with the argument of ten. So now value equals ten, but because we are using static array and index, now the program remembers that one time, we already called the function one time, and it remembers that we did some modification to the first element of this array and also to this index number. So now that it enters this function, it goes to this if condition here, but it is not considering index to be zero, but it is considering index to be one because it remembers that we did some modification in the previous call for this function. So this is why actually the static variables are useful. So initial index, in this case, equals one, and basically it is saying user input inputs one equals ten. The final index equals two. So it basically says that, okay, the index now is one, one is lower than three, that is true, so it is going to enter this part. When entering this part, it's going to assign the argument, which was ten in our case to be the second element of the user input array. Why second element? Because the index now is one and C plus plus starts from zero. Zero when index is zero, we are talking about the first element. When index is one, we are talking about the second element. So it is going to assign ten as to be the second element of the user input array. When this has been done, we are going to increase the value of index by one, so the final index will be two again. Okay. And we're going to call the function for the first time. Now the initial index equals two because it is an static int index. It is declared to be static int. So the program remembers the modification. And also because the array is static, it remembers the modified its elements, so it's not going to start from zero again. And now user inputs two equals 15 and final index will be three. How come? Okay. Let's see. The initial index value is two, two is lower than three, it's going to enter. So the argument that is passed to this function is 15. Therefore, the value is 15, the int value is 15. 15 is going to be assigned to the third element or the last element of the user input array, after which we are going to increase the value of index, which was two, and now it's three. Okay. In the meantime, we were always printing the values that were passed to this function. So now index equals two. And when we finish execution of this line, final index is three. So when we call the function one more time, with the argument 20, it is going to say three, which is the final value of index, is it lower than three? No, it's not. When it's not, it's not going to execute this part of the code, and it goes directly to the s part, which is this part. So when it comes here, it is going to say array is full. Why it is full because the size of the array was three, and we already had stored three values in it, therefore, and three is not lower than three, therefore, it is not executing this part, and now we see that three values are stored in the array, as you see here, five, ten and 15, and the size is three. So it says array is full and it is printing all the elements of the array here for us. So let me just run and see the result. Okay, great. So in the first call, the value stored is five. So as we discussed, the first element of the user input was set to be five and the value stored was five, and the second value was ten and then 15. And when we are passing the 20, it is not printing the 20. Instead, it says array is full because it is entering this Ls part. And then we are printing all the elements of the array which are five, ten and 50. The only reason that with every call, the program remembers what we have done and knows that the elements are different values and the index is updated progressively and also the array is updated progressively is because we are using the static keyword here. If we don't use static keyword here, you will see the difference. So you will see that it is printing five, ten, 15 and 20. Why? Previously when we reached 20, we saw that we saw that it was mentioning that the array is full, but it is not saying that. Why? Because index is zero when the first call is done. Zero lower than three. Okay, and five is assigned to the first element of the user input array. We are increasing the value of index, and so the index equals one, and it is printing the value just like here. Okay? And when we are calling the function the second time using this number, it is forgetting everything about the index value and the user input array because we are not using static variables, it is not going to remember. So when the first call is done, everything executed, it is going to remove everything from the memory, so it's not going to remember. So if I try to simply just print the array in the end of this function, we will see the numbers are different. So when we are printing the array in the end, we see, Okay, let me just put N L. It's nicer. Okay. So when we are calling the function for the first time, this is five. Five is set to be the first element of the user input. Therefore, this line is printed, which is seen exactly here. And then we are having this print of the array. So the first element is five. The other elements are still zero, and the program execution is finished. Then we are calling the function again with the number ten. So it is going to consider index is now again re initialized to zero because it is not a static. So ten is assigned again to the first element of the index of the user input array. Just like here, and just like here, you see here, and it is when we are printing the array, it is considering ten to be the first element. The first reason is that index is again, starting from zero. Therefore, it is assigning ten to the zero. And the second reason is that it is re initializing the array to be zero. So this array was zero, zero, zero with 30 element, not 50, zero, like here. Why? Because it is not a static. But when it is static, o. When it is a static, let me just comment this line. When it is attic, in the first call in the first call, it is setting five to the first element of the array, and then we're printing the array here, 500. Then we are calling for the second time, it already remembers that the first element was five and an index the final index was one. Therefore, now ten is going to be assigned to the second element because now index is one and it is not considering this line because it is a static. So it is going to assign ten to the second element of the user input, and only the third value is remaining zero. In the third call, 15 is going to be assigned to the third element of the array user input. So this is how static actually variables work, and this is how a good to how good it is to use them in specific situations that we would like the program to remember previous calls for the function when we are using an array, for example, or an integer value. Thank you very much for watching. If you haven't had the chance to rate this course yet, we would appreciate it to rate this course, and it is also very important for us to give us constructive feedback based on which we can improve our future courses for future students. Thank you very much. 15. 11 Array sort ascending order: Hello, everyone. Welcome back. In today's video, we're going to write a program that provides an array and sort the array in ascending order. To do that, we need to include predefined library in C plus plus, that is called algorithm. So we include algorithm to do the job for us. Initially, in the first step, we need to declare define one array for ourselves with the elements that we want. It depends on what we want. It can be having two elements, five elements, ten elements. It depends on what we want to do. Then we're going to understand the size of the array. We learned previously how to compute the size. So we basically have the size of function in C plus plus, this is a predefined keyword. So we divide the size of the array by the size of the first element of the array, so it is going to be something like if each element of the array is going to have four bytes. So the size of the array would be one, two, three, four, five, six, six times four bytes will be 24/4, so it is the answer will be six, which is the size of the array, which is true because one, two, three, four, five, six, we will see that we have the six elements in the array. So this way with this formula, we're going to compute the size of the array. Then in order sort our array, we're going to use one predefined library function in C plus plus, that is called sort. And within the parenthesis, there are two parts that we need to mention and they are separated by a comma. So the first part would be the name of the array. The second part would be the name of the array plus the size of the array. So let me first compile and see how the result would be. Then we're going to analyze the code. Here, also, we are providing a four loop that we're starting from zero and we're going up to the size of the array and we are printing all elements of the array to see the sorted array. So as you see here, the array starts with five and finishes with six. Then I compile. Okay. When I compile, we will see that one, two, five, five, 69. So the array is now sorted. But why we need to define the sorted array using these line of code. So this is a predefined library in C plus plus that consists of two parts. The first part is the name of the array. The second part is the name of the array plus a size. So instead of the size, in this case, the calculated size would be six as we discussed. If we are going to instead of the size we type three, it is going to sort only the first three elements of the array. Let's see. Okay. So the first three elements are originally five to nine. When we sort them, it is 259, and the rest of the array remains unsorted. So in order to sort the array, the full array, we need to type size here. But if we are going to only sort the array for the first three elements, simply just type three, simply just four first elements you would like to be sorted. So you just simply type four here. So it's okay. Depends on what you want to do. Was a very good example for us to learn how to use the sort function to easily sort our array in Ciplus plus. Thank you for watching. 16. 12 Array sort ascending order 2: Hello, everyone. Welcome back. Now we learned how to write a program that gives an array and sort the array in ascending order. Now we would like to declare and define an array and sort it in an ascending order. However, only for example, the last two numbers or the last three numbers, you would like it to be sorted. So previously, we learned if we would like to make the sorting for the first three numbers instead of size, we will just type three, so it is going to sort the first three numbers. We see that, okay, five to nine is now 259, and the rest are remaining the same. However, if you would like to sort only the last three numbers of an array, we add number three in the first part. It is going to simply sort the last three numbers for us. So now we see that the first three are staying the same, exactly identical. But the last three numbers are having a sorted value in ascending order. So if you need to sort an array from some part of it on art, you just put the number in the first part. Of the call function sort. And if you're going to sort the array for the first few elements instead of size, which calculate sort the full elements for us instead of this, you just put a number like two, three, four, for example, if you put four, it is going to sort the first four numbers of the array. Thank you for watching. 17. 13 array sort descending order: Hello, Evan, welcome to another video. In today's video, we're going to learn how to write a program that provides an array and sort the array in the descending order. So previously we learned how to do it in ascending order. Now we're going to learn how to do it in ascending in descending order. To do that, we already know how to define and declare an array. And then we calculate its size. And then in order to sort it, we still in descending order, we still use the sort function that is predefined by the C plus plus library function, and we need to include the algorithm library. In the first part, we put the name of the array. In the second part, we are putting the name of the array plus the size of the array. And in the third part, meaning this part, we are going to use another library function of C plus plus that is called greater. And then int and two empty parentheses and one empty parentheses. This would do by default, the sorting for us in the descending order using a predefined C plus plus library. To check how it works, we will just compile and see. So please note that the array is five, 29156. Now I compile here I'm printing all the elements of the array, so we need to have a result of a sorted array in descending order now. Okay, great. So we are seeing the results. So in order to sort an array in the descending order, we can simply call the sort function. In the first part, we are calling the name of the array, then the name of the array plus the size, and in the third part, we're going to write down greater and this function is called using this keyword is going to call the function predefined function of the E plus plus to simply do the job for us and makes our life much easier. And then we're just printing out whatever that is in the array. So this way, using this line of code, we can easily sort the array in the descending order. And also, we learned to play with this number. So if I put numbers plus four, if I put numbers plus four, you will see that only the first four numbers are sorted in the descending order, and the rest are staying the same. So this is why when we want to sort the full array, we are using the size because the size is going to give the number of all the elements of the array. So this element is having six elements. So size will be six. So when numbers plus six means that the full array is going to be sorted in the descending order. Thank you for watching. 18. 14 array reverse: Hello, everyone. Welcome back. In today's video, we are going to write a program that reverses all the elements of an array. To do that, we need to first declare and define an array. We calculate its size, and we're going to simply call the reverse function that is a predefined function by C plus plus, but we need to include the algorithm library. So when we call the reverse function, there are two elements to consider. The first one is the name of the array. The second one is the name of the array plus size. And simply there after that, we are printing the array to see if it has done some changes or not. So let me just run the code. So the original array is one, two, three, four, five. The reverse array is five, four, three, two, one. So now, as you see, one is exchanged with five, two is exchanged with four and three remain the same. So when we have the size here, it is going to reverse the full array for us. But if we put, for example, four, it is going to reverse the first four elements of the array. So let's try Okay, because we put four and not the size or five, it is going to consider the first four elements of the array for the reverse. So one and four has been changed. Their place has been changed and two and three are exchanged. However, five is remaining the same. And the reason is that because we didn't include the full array, in order to include the full array, we just simply put the size. And if we are going to reverse the array for the first two elements, three elements, four elements, and five elements that depends on us. You can simply just manually change this size and put the numbers, and then the code will go and reverse those elements of the array. Thank you for watching. 19. 15 insert an array in the beginning of another array: Hi, everyone. Welcome back. In today's video, we're going to do some coding exercises regarding the arrays. So we would like to write a program that inserts one array in the beginning of another array. So this is our target array, so R two, and we would like to insert R one in the beginning of the R two. So we first declare and define both arrays. We compute their corresponding size. Then we are going to declare one array, a third array with enough amount of size that can contain both arrays. So if each one of them is having three elements, this new array needs to have at least six at least have the size for six elements. By default, I just put it ten to make sure we are above the number that we are targeting. So we declare an array a third array with enough size to be able to include both arrays. To do that, firstly, we would like now to fill this new array with the values of the first and second array. To do that, we copy all the elements of the array one to the third array. So we're going to iterate over all the elements of the first array. So in order to do that, we computed the size, and the size would be three here and also three here, okay? And we know how it works from previous lessons. So basically, we're going to write a four loop that starts from zero and it goes up to three. So as long as I lower than three, it is going to iterate over this loop. So I start from zero, then I is incremented by another value in another iteration. In the last iteration, I is going to be equal to two because two is lower than the size which is three now. Let me also just size one equals three. So if I equals three, it's not going to enter this loop because three is not lower than three. Okay? So in the first round, it is going to enter this loop and it is going to assign the first element, which is ten of the array one into the first element of the third array and then increase the I. So I is now equal to one. It is going to put the second element of the array one. This is the array one to the second element of the third array. And finally, it is going to put the third element of the array one, which is 30 here as to be the third element of the third array. So after doing this, this array is copied in the third array. So now what we need to do is just to continue copying the rest of the elements which are the elements of the array two into the combined array. So we would have a new array containing both elements of both arrays. And the first array is going to start the array, and the second array is going to continue because we wanted to make sure we are defining an array, a new array that inserts these three values in the beginning of this array. So now the first array is copied to the third array, and now we're going to copy the second array into the third array. So I still equals zero. I equals one, and I equals two while size equals three, size two. Size two. Okay. Now the first three elements of the third array is filled with the elements third of the first array, sorry. Now as a fourth element, we need to add the elements of the second array. As the fourth element of the third array, we're going to insert the elements of the second array. To do so, we need to iterate over all the elements of the second array, and it starts from zero. It goes up to two for the values of I and the size two is calculated to be three, which is calculated here. So now that we are inserting the elements, for example, when I equals zero, we are adding the first element of the second array to be the size one plus I element of the third array. But size one equals three. So it means combined size one plus I, in this case, in the case where I equals one, I equals zero, it is going to replace these numbers plus it equals two. So in this case, it is going to put because I equals zero, it is going to put the first element of the second array to be the fourth element of the third array. Why fourth element because the first three elements were already filled with the previous array. Now here, I equals zero and size one equals three, so it is going to right away, start with the fourth element. We know in C plus plus, we start from zero. So when the size is zero, it means the first element, and when it is three, it means that it's the fourth element. So the fourth element of the combined array is filled with the first element of the second array. And when I equals one, we will have something like three plus one. So in this case, it is going to put the second element of the R two as to be the fifth element of the array combined. And finally, when I equals two, it is going to simply put the third element of the second array to be the sixth element of the third array. Basically, after this loop, the combined array will look like this combined array is having ten elements because we initialized it to be this way. It looks like before this loop, let me just before this loop, it was looking like this, meaning that all the elements of the array were zero. Now all the elements, we could also, for example, we could do something like this to make sure that it works, okay? Or we can also eliminate that. Then what we can do is to, where was I? Yeah, I was here. So before doing any changes, all the elements of the array were equal to zero. Now, what we did using the first loop, we inserted all the elements of the first array in the third array. So the third array looks like this now, and the rest of the elements are zero. And after this loop, this array looks like this, as you see here. So here we are having a new array with ten with the size of ten with the elements of the first array and the second array. Basically, this is the element. These are the elements of the second array. Basically, we inserted the first array in the beginning of the second array, which was our target. So in order to print these values, we can simply write a four loop that is iterating over all the elements of the array combined. So I is going to go until the size of the combined size of two elements or simply just the size of the third array. It's not important. When we put this size one plus size two here, it is going to print only the first six elements of the combined array. Let's see. Okay, great. As we see here, it is now having the first element the first three elements, all the elements of the first array and then all the elements of the second array. So this way, we can insert an array in the beginning of another array. It is very straightforward. You need to be careful about the second loop that you put the size one plus I here, so it is going to insert the first element of the second array as the fourth element of the third array if each array is having three numbers because the first three are already filled. So basically, a general formula would be to start from the size one, like the size of the first inserted element plus I. And here we will have the I element of the array two to be set to the size of the first element plus I to be set for deferred array. So I here it was like four elements, still, that formula would work because size one equals four. So this is the only thing that we need to be careful about, and this way, we can insert an array in the beginning of another array. Thank you for watching. 20. 16 insert an array at the end of another array: Hello, everyone. Welcome back. In the previous lecture, we learned how to insert one array in the beginning of another array. So initially, we defined two arrays, and then we initialized a third array, and the third array was going to, in fact, merge these two arrays. So we were having this target array, and the first array was being inserted in the beginning of the second array. However, after discussing this lecture, dovile we are able to know how to insert an array in the beginning of another array. But we could see also the other way around. So what if we would like to insert an array in the end of another array. So in this example, we had this array, two that R one was inserted in the beginning of this array. Seeing it the other way around, we could mention that the array two like to be inserted at the end of the array one. So this example can be very helpful for doing both tasks. So you can either mention that writer program that inserts one array in the beginning of another array, meaning that array one is inserted in the beginning of array two or we could write a program that insert array two at the end of array one because when I run the program, we see the resulting array is ten, 20, 30, the first array, then 40, 50, 60, the second array. We could either say the first array is inserted in the beginning of the second array or the second array is inserted at the end of the first array. So both of them are the same. It depends what we're going to do with the program. This video is just to show you we could see things differently for one specific coding task. In the next video, you will learn how to insert one array in the middle of another array. Thank you for watching. 21. 17 insert an array in the middle of another array: Hello, everyone, welcome to another coding exercise session. Today, we would like to write a program that insert one array in the middle of the second array. So this is our first array. This is our second array, and we would like to write a program that inserts the first array in the middle of the second array. Okay, to do that, we declare two arrays. The first one is one the first array is ten, 20, 30, and the second one is 40, 50, 60 70. And we are computing the size of two arrays. We are initializing one new array that is called combined. We call it third array. And in order to find the middle point of the second array, we divide the size of the second array by two. So this will give us the middle point. In this case, it has a size of four. When it is divided by two, it is going to give us number two. So after this number, we're going to fill these parts in the you are going to copy these two elements in the first array. So let's begin. So here we are computing the middle point, like we are having four elements here, 4/2, will be two. So Middle equals two, I equals zero, I equals one. These are the scenarios. We iterate over the second array. We start from zero and we go until I is lower than middle, which is two here. So I is going to get two values, zero and one. Therefore, is going to perform two iteration. In the first iteration, when I equals zero, we will have the combined zero equals to zero. Basically, it is assigning the first element of the second array to be the first element of the first array, meaning that 40 is going to be assigned as the first element of the array combined. Then in the second iteration, combined one equals two. It means that the second element of the second array, which is 50, is going to be assigned to be the second element of the third array. So now, if we print our third array, it is going to have it is going to have these two values, 40 and 50. So basically, our third array is holding after this loop is holding 40 and 50. And the goal is to insert the first array in the middle of the second array. So now the 40 and 50 is there. Now we need to copy ten, 20 and 30 here, and then copy 60 and 70 here. So now we are going to iterate over the first array. To do so, we are having one iteration here. So the size one equals three, and there are scenarios where I equals zero, I equals one and I equals two. So I starts from zero, and it is going to continue until it is lower than size one, which is three. So it is going to have value zero, one and two. When I equals zero, we are basically having the middle size plus zero equals r10. So basically, in this line, the first element of the first array, which is ten is going to be inserted as the third element of the third array which is combined. So now, combined previously combined look something like this. After the first iteration of this part of the code, it is being equal to ten as well. So ten is added to the first element to the has to be the third element, sorry. Then we are having here combined two plus one equals R one first element. The second element of the R one now is assigned to be the fourth element of the combined array. So it is going to put 20 here, and we know when the sum of this part is three, it means the fourth element because in C plus plus, we start from zero. So the index, this one is zero, one, two, three. So when the index is three, it means the fourth element. And in the last iteration, we have combined two plus two equals this equation, meaning that the third element of the first array which is 30 is going to be assigned to be the fifth element of the third array combined after this execution. Our combined array looks like this. It is having 40, 50, so the first two elements of the second array, and then it is having all the elements of the first array, and we need to insert the last two elements of the second array as well. To do that, we need to write another four loops and here we know middle equals two and size two equals three. Okay? So size two, sorry, size two equals four. Yeah, because in the second array, we have four elements. So having said that, I is going to start from two and it is going to go until four. So when I equals two, and when I equals three, these are the only two situations where I is going to get some values because I equals two is lower than size two, which is four. And when it is three, it is still lower than size two, which is four. So I starts to be two. When I equals two, we will have something like size one, equals three plus two and something like this. So it is going to assign the third element of the array two, which is 60 to be the six element of the third array combined. It means that combined will look like this. So already, it was having 40, 50, ten, 20, and 30. Now we are adding the six element to be the third element of the second array. So 60 is going to be here and then when I equals three, we will have the formula, something like this are two, it means the fourth element of the second array must be assigned to be the seventh to be the seventh element of the combined array, which means that the 70 the fourth element of the second array, 70 is going to be assigned as the seventh element of the array combined. So basically, after the third loop, we are having the combined array or the third array looking something like this. Now here we are just printing out the combined array. Let me compile Okay. As we see here, our new array is having the first two elements here, then all the elements of the first array, and then the rest of the elements of the second array. Basically, in this lecture, we learned how to insert one array in the middle of another array. Thank you for watching. 22. 18 Constant array: Hello, everyone. Welcome back. So far, we learned how to declare and how to define an array like this. For example, this array is named config. It is of integer type, and it has three elements, ten, 20, and 30, and we learned that we can modify different elements of the array. For example, here, we are changing the second element of the array which is 20 to be 25, and then we are printing the modified array like this. So let me just run and see how it works. Okay, as we see here, the original array was ten, 20, 30, but we modified the second element and we replace it with 25, and now we're having ten, 25, and 30. So we basically know how to modify one element of the array. But there are sometimes that we may make mistakes throughout our code, our different blocks of codes in the program that unintentionally we change one element of the array, but we didn't want to do that. In order to avoid any modification for elements of the array, we can use the constant array. So a constant array is declared as to be constant. So all the elements of the array are constant. It means that it is not allowed to modify any elements of the array after initialization. To do that, we can simply use this keyword, constant array. So this way, this array cannot be modified anywhere throughout the code. And as you see here, there is an error because it says we have a constant array that is being modified here. Therefore, it retains an error. So when we have a constant array, it is not possible to modify it throughout the code. So in case you are using an array and for any reason, you would like it to be constant and not to be modified unintentionally, for example, throughout different blocks of code, you are advised to use the constant keyboard, so CONST in order to keep everything safe and not allow the modification. Now, when we compile basically, we get the original value for the array, and the reason is that we remove the modification part. And when I uncomment the modification part, you see that this returns one warning here, saying that variable config declared constant here. It's a warning, and there is an error saying that it is of type constant and you are trying to modify it, so it is not possible. So this is a very good way to keep everything as it is and to keep everything safe if you would like not to allow C plus plus to let you modify your array throughout your course. Thank you for watching. 23. 19 two dimensional array: Hello, everyone. Welcome back to another lecture. In today's lecture, we are going to discuss two dimensional arrays. So far, we learned about one dimensional array, and now when we would like to store our data in a matrix or a table, we need to use two dimensional arrays. In order to do that, let's review what we learned so far about arrays. Here, we are declaring one array that is named my array. It's of integer type, and there are three elements in this array, and the elements are one, one, and one. This value tree is the number of elements, but also the number of columns that we can see in this array. This is the first column, this is the second column, and this is the third column. However, because there is only one row here, we didn't declare the number of rows here. Example of a two dimensional array would be this array. So we would like to write a program that stores three by three matrix of integers and print its value. In order to do that, it means that we are going to have three rows like this and three columns like this. So the first number corresponds to the number of rows, and the second number corresponds to the number of columns. And here, it's the name of the array that is matrix, and here it's the type of the array which is of type integer here. So in order to declare a one dimensional array, we only type number of columns, but here in order to have a two dimensional array, for example, first, we declare the number of rows and then we declare the number of columns. In order to print two dimensional array, we know that we need to use four loops. In order to have a four loop that could do the printing of different rows and columns, we need to use nested loops. Basically, nested loops are used when we have an outer loop and an inner loop. So the outer loop is going to count the number of rows and the inner loop is going to count the number of columns. For example, here we are sending the message saying that the matrix is, and in the outer loop, this one is the outer loop. Outer loop starts from I equals to zero, I lower than three, I plus plus, and then we have the inner loop here that corresponds to columns. J starts from zero, J lower than three, J plus plus, and we are going to print matrix, which is the name of our two dimensional array. Row I column J. To do that, let's see how it works. So initially, I equals zero when the outer loop is performed, and the inner loop J equals zero as well. So it is going to print matrix zero, zero. Meaning that the value that corresponds to the first row and the first column, which is one. So it is going to print one. Then I still equals zero, but J is increased by one value because we are still in this loop. This loop is going to continue until J J is not lower than three and does not pass this condition anymore, then we are going to the outer loop to increase the value of I 0-1. So now I equals still zero and J equals one. So we are printing matrix 01, which equals to first row and the second column, which is two. And then I equals zero, J equals two, J is increased by another value, two is lower than three, so we still are in the inner loop, and we are going to print matrix zero, two. This happens to be the first row which is this place and the third column, which is this number, so three. So now that J equals two, if we increase the value of J by another one value, J will be equal to three, so three is not lower than three, so it does not pass this condition and it cannot enter the inner loop another round. When this loop is finished, we are going to go to the next line using CO and dell then we go up again and now the value of I from zero has increased to one. Now I equals to, J again equals to zero because now with the I equals one, we enter this inner loop and J is going to while I equals one, J is going to take the values of 01 and two again. So now we're printing matrix one, zero, which means the second row, which is this part and the first column. So the first column would be second row and the first column would be four. Then I still equals one, J equals one null. We have matrix one, one, meaning that the second column, the number corresponds to five, then I equals one, J equals two, we have the matrix of one and two. This means that we are going to discuss the second row and the third column which corresponds to value six here. And finally, when J again reaches its maximum number, it is going to go to the next line using the CO and L, and then I value is going to increase 1-2, so two is lower than three. So this is the last time that we are entering this outer loop because the next time I equals three and three is not lower than three and it does not pass this condition in the parenthesis. So this is the last time I equals two, J equals zero matrix 20, and this would equal to the third row and the first column. The third row, which is this line and the first column would correspond to number seven. Then we have I equals two J J equals one, we are going to print matrix two and one. So the third row and the second column corresponding to number eight, I equals two and J equals two matrix two and two, so it is going to print the third row sorry and the third column corresponding to the value of nine. So now we learned how to print this two dimensional alley. Let me run the code and see how it works. Okay, great. So we see that here, we initialized a two dimensional array, and then we started going through these two dimensional array that is card matrix row by row. So in the first row, when I equals zero, we iterate over J 0-2. So we're printing values one, two, and three. Then we go to the next line. Where I has increased by another value, and then we are printing four, five and six. Then we go to the next line, and I has increased again by another value. So I equals two now, and we are printing seven, eight, and nine. So using this coding exercise, we not only learned how to declare and define two dimensional array, but we had the chance to review the nested loops and show how using nested loops, we can print a two dimensional array and also how we go through the iteration of an outer loop and an inner loop in nested loops. Thank you for watching. 24. 20 two dimensional array exercise1: Hello, everyone. Welcome to another coding exercise session. In today's lecture, we are going to write a program that stores two by two matrices and calculates their sum. In order to do that, we first declare and initialize our first matrix that is called matrix one. So it's a two by two matrix. And in the first row, there are two values. In the second row, there are two values. So there are basically two rows as it is shown here and there are two columns as it is shown here. Then we have another two by two matrix which corresponds to this one, we have five, six in the first row and seven, eight in the second row. Finally, we have a third matrix that is also two by two matrix. So it's two rows and two columns matrix that is going to store the sum of the second and the first two dimensional arrays for us. In order to calculate the amount that we need to put in the third matrix, we need to know how to make the summation between two matrices. So in order to do that, we first need to know. So this number, the first draw first column of the first matrix, is going to be added with the first row and the first column of the second matrix, and it is going to be in the first row and the first column of the third matrix. So imagine that we have now a matrix that corresponds to our third matrice here. So this number, which is the first row and the first column of the third matrice, which is sum has to be six, which is the sum of five and one. Okay? So by adding the first first row and column of the second matrix and first row and column of the first matrix, we are going to build the first row and column of the third matrix. So then the second column of the sum matrix is going to be six plus two. Why? Because it corresponds to the value of first row second column of the second matrix and the first row second column of the first matrix. So it is going to be eight. Same policy goes with the second row, first column number and the second row second column, which is eight plus four coming from this four and this eight, which means that it needs to be 12. So this is the sum of these two dimensional arrays. However, in order to do it in C plus plus, what we need to do is to iterate over both matrices and set and assign the summation of each number to the third array. In order to do that, we need a nested loop. So a nested loop starts from zero goes until I is lower than two and J starts from zero until J is lower than two. So basically, firstly, I equals zero, J equals zero. In this case, the first row and the first column of the sum matrix is going to be equal to Matrix. One and matrix. Two. So basically what when I equals zero, J equals zero, therefore, the first row and the first column of the sum matrix will be equal to the first row and the first column of the first matrix, which is one now, and the first row and the first column of the second matrix, which is five now. Therefore, the value will be six. Okay? Then still I equals zero, J equals one, so we will have the first second column of matrix sum equals to the sum of the first row second column of matrix one, and again, the first row, second column of the matrix two, which means that number two plus six, which is here. So this equals eight here. Also, we had one plus five equals six. Then J, because in the last iteration, I equal zero and J equals one. Now, J, if J is increased by another one value, J equals two. So two is not lower than two is not lower than two, so it's not going to enter another time in this inner loop. And therefore, this inner loop has done one full iteration. So it is going to go up again and increase the value of I by another value. So I now equals one and J equals zero, so we are going to have this here. Let me just type it down first. So it will be Okay. So now I equals one meaning that we are discussing the second rows, the second rows. J equals zero, it means that we are discussing the first column, second row, first column, second row, first column, second row, and first column. The second row and first column of the third array, which is sum equals to the second row and the first column of the first array plus the second row and the first array, first column of the second array, which is seven which equals ten, which we have here. Then I equals one, J equals one. So we have the sum one, one, matrix one, one, one, plus matrix 211. So this means that we are now talking about the second row, second column. So this number 12. Now, the number this 12 number is calculated based on the second row and the second column of the second matrix, which is eight and the second row and the second column of the matrix one, which is four, so it will be four plus eight. It's equal 12 this way. We are assigning numbers to the matrix sum. And then we are simply just print the values that are assigned to the sum matrix here. So in order to do that, we have another nested loop. I iterates, the outer loop iterates the number of rows, which is zero and one, so we have two rows, and J inner loop is going to iterate for the columns. So J will get the value zero and one, so there are two columns. So let me run and see Okay, great. So basically, we see that the sum of one and five equals six here that is printed here. The sum of two and six equals eight that is printed here and here. So three plus seven equals ten, which we have, and four plus eight equals 12, which we have here, and we wrote the detail six, eight, ten, 12, here as well. And here we are just printing the matrix. So in this lesson, we learned how to declare two dimensional arrays that are having two rows and two columns and how to calculate their sum. We analyzed a nested loop here that is assigning the values based on the summation of the first matrix and the second matrix row by row column by column to the third matrix, which is the sum. And then we learned how to print the final matrix that is called sum. Thank you for watching. 25. 21 two dimensional array exercise2: Hello, everyone. Welcome back. In this lecture, we are doing a coding exercise regarding two dimensional arrays. So we would like to write a program that stores marks of three students in four subjects and calculate their average mark. So when we have three students, it means that we are having three rows and each row is corresponding to different marks in different subjects. So we are having four subjects like four rows here. So the first row first subject means the first subject of the student one, the second subject of the student one, first subject of the student one, and the fourth subject of the student one. Second row corresponds to the student's two mark from the first subject to fourth subject, and third row corresponds to the third student from the first subject to the fourth subject. This is the matrix or two dimensional array that we have here of integer type, there are three rows here, as you see, and there are four columns here, as you see it here. So this is the declaration and definition, and we would like to calculate the average mark of the first student, then the average mark of the second student, and then the average mark of the third student. In order to do that, we are going to use a nested loop because we learned if we are going to iterate over the data of a matrix or a two dimensional array, we need a nested loop. The outer loop will work as we are iterating over the rows and the inner loop works like we are going through the different columns here. So we have three rows, starting from zero, we are going to have the first row, second row, and the third row. So I is going to get value between zero, one and two. So three rows. Then we are having the inner loop, which is starting from zero going until J is lower than four. Meaning that it's going to get value of zero, one, two, and three, meaning that there are four subjects. After the outer loop, we are initializing and declaring an integer called sum where we are going to store the marks of each students individually, for example. When we start, I equals zero, meaning that s, meaning that we are discussing the first student. So and J equals to zero, it is going to put the sum will be equal to sum plus marks zero and zero. It means that sum equals sum equals zero because initially it was zero plus the first row and the first column, meaning that the first subject of the first student, so it is 85. Okay? So the sum equals 85. Then I still equals zero, meaning that we're still iterating over the marks of student one. However, we are going to discuss the second subject of the student one. It means that sum equals sum plus marks zero and one, it means that sum equals already from the previous iteration, we calculated the value of sum, so it was 85. So here, it is not zero anymore. It is 85 plus the first row and the second column of the matrix marks, which is the first row and the second column first row and the second column of the matrix marks, which is 90 and doing the math, we see that 90 plus 85 will lead to having a sum equals 175. And then we are having I equals zero, still discussing student one, J equals two, sum equals sum plus marks, zero and two, it means that sum equals 175 that we calculated in the previous round plus the first row and the third element. So first row and the third element of the matrix marks, which is 78. So by adding this, we get to the number of 253, and finally, here, I equals zero student one still. J equals three, so sum equals sum plus marks zero and three means that sum equals 253, which is calculated in the previous round and the first row and the fourth column first round row and the fourth column, which is 92. Is calculated here. So 92 will lead to have the overall sum of 345. So at the end of the inner loop that J equals three, here we saw that the value of sum equals 345. Now the inner loop is finished because if J is increased by another value, J will be equal to four and four is not lower than four. Therefore, it does not pass this condition, therefore it's not going to enter this inner loop one more time. So now the value of sum is calculated to be 345. This inner loop is finished, but before going up to increase the value of I by another time by another one value, we are going to calculate the average of the student one so we're printing average mark of a student one equals sum divided by four, and why four because there are four subjects. So it is going to print the average of the first student. When it is printing the average of the first student, it is going up. Now, it increase the value of I by one. I equals one, meaning that we're calculating the second student marks and J equals zero. Then again, I equals one student, two J equals one, et cetera until J equals three. Then we calculate the average of the second student and then we again, increase the value 1-2, meaning that we are discussing the further student. J starts from zero and goes until it is three here again goes until it is three, then we're calculating the average of deferred student as well. Let me compile Okay, great. So here we saw that the average mark of the student one is 86.25. The second student is 84.75 and the last student is 89.5. So we learned that this is a declaration of a two dimensional array or a matrix that has three students each are having four subjects marks. We iterated over each student the outer loop is for rows and the inner loop is for columns. We calculate the sum of each row corresponding to each student marks. When we had the summation here, in the end, we calculate it. We divide the sum by four because there are four subjects here. And we print the average of the first student. Then the inner loop is finished, the inner loop is finished. The value we go up again and we increase the value of I by one value. So I previously it was equal to zero. Now it is equal to one, one lower than three, enters. It re initialized the sum equals to be zero. So now it is zero. We are concerning the solution for the second student. It is zero, we're starting from zero again. And then J goes 0-3 and storing the sum of the four marks of the second student, then calculating the average here again, and the same again goes for the last student. So it was a very good example to learn how to work with a two dimensional array and how to calculate the average of each row. In an example, we calculated the average of marks of three students. But basically, we were calculating the average of each row. So as you see, it is very useful and it is very nice to use C plus plus to do that for us. So if you have some time, please go ahead and do some practices. Do this practice several times, the very same practice several times. So you will get to use you will get used to the syntaxes and also you will get used to finding where the errors come from, so you can improve from there. And in case you haven't had the chance to rate this course yet, please go ahead and do it because it would be beneficial for us and other people can recognize this course if you find it valuable. And also, we would appreciate if you give us some constructive feedback on this course, it will help us to provide a better course in the future for future students. Thank you for watching. 26. 22 Multi dimensional array: Llo everyone. Welcome back. In today's lecture, we are going to discuss multidimensional arrays beyond two dimensions. So we would like to write a program to store sales data of three departments, each selling four products over five months. And we're going to display sales of one specific product across all departments and months. So this information three department, four product, and five months show that we need a three dimensional array, and the array needs to be something like this. So the dimensions are like department, product, and month. To do that, we initialize the array in order to initialize and declare a three dimensional array, this is the name of the array. This is the type of the array and we see here there are three dimensions. The first dimension is for the number of department. The second dimension is the number of products and the first dimension is for the months. Here, we see that we the first department is discussed in this part, then it is separated by a comma. Another department, the second department is discussed here, the separated with a comma from the third department. This is the first dimension. The second dimension is the number of products that are four. Therefore, we are going to have in each department, we are going to have four rows. Each row is representing one product. So again, here we are having four rows. So here is also product one, here also product two, product three, and product four. And also in the last one, we are having the same thing product, sorry, product one, product, three, product, two, and finally, product four. So we are having three departments like this. You see here, each of them are having four products, each one representing a row. So you see each department matrix is having four rows, and also each department is having five columns, meaning that product one, first month, product one, second month, product one, third month, et cetera. Then we have, for example, product two from month one until month five. So this one, for example, represents the data of the department one, product four, and the fourth month. This is how we navigate through the initialized matrix. Now when we initialize this matrix, we would like to display, for example, the sales of the product 2/5 months across all three departments. Okay? To do that, we mentioned that the product, we initialize and declare one integer called product equal to one, because it's based on the zero indexing for each product, and we are mentioning that, okay, sales for product, so it is already one. When it is added to one, it is going to print the number two, which is the product two that we are going to iterate over. Then in order to print values in in a matrix or in arrays, we need nested loops. So here, we're iterating through different departments. This is the outer loop and this is the inner loop. So the outer loop is iterating over all the departments, starting from zero until depth is lower than three because it will be zero, one and two, meaning that there are three departments. And then it is printing the value of the department plus one, Y plus one because it is a zero based index. So when initially, the value of the depth equals zero. So zero means we don't have department zero. We have zero plus one, it will show the department one, then one plus one Department two and two plus one department three. And then we enter the inner loop that iterates through the months. So months we mentioned that we have five columns and we are having five months. Okay. Here we are also having it. Therefore, it is going to start from zero until month is lower than five, meaning that it is going to get the value zero, one, two, three, four. It means that there are five months. And then it is going to it is going to print the index of the month, for example, when month equals zero, zero plus one means that the first month is printed. And when the first month is printed, we are going to have the matrix of code sales. The first department, let me just analyze the code here. So depth equals zero, product equals two, equals one. Sorry, because it shows the second product, and then we have the month equals zero. So basically, when we reach here, in the first iteration, we are having these values, and when having these values, it means that we are going to have the matrix of sales in the first department, second product, and the first month. Then still depth equals zero, product equals one month equals one sales will be 011. So it is going to get the value of the matrix sales, the first department, second product, and the second month. So this value corresponds to the first department, second product, first department, second product, and then the index of month is zero, meaning that the first month, so it is going to print 20 here it is going to first department, second product, and the second month means that it is going to print the value 22, and then it is going to continue until dep equals zero product equals one, and month equals four, and it is going to print the value of the first department first department, second product, and month five, which is 28 28. After this has been done, so the inner loop has gone through all demands 0-4. F zero, it starts and ends to four. And then it is going now it's the time to increase the value of the debt. So now debt is equal to one. Still product equals to one because only the second product is being discussed and month equals zero to four again. And then we go for the debt equals two Product equals one, and month equals 024. So this way, we are iterating over all the departments and all the months of the second product. We are basically printing all the values of the second product over five months in three departments. Okay? So now let me run the code and see how it works. Okay. So as we see here, we have the first department, second product over five months. So it is 20, 22, 24, 26, 28, 20, 22, 24, 26, 28, and this is the first department. Then we have the second department exactly like the second product, and we have the third department as well, which equals to the third department here. So this is how we are obtaining the data now as we see here, we are having a nested loop consisting of two, four loops. If we were going to iterate over all the products, we should have had three loops there. So one outer loop, one middle loop, and one inner loop to go through the department, then products and then months. But because the products was a constant here and we were only a wanted the data for the second product, we consider it to be one integer value always equals to one. This is why we don't have three, four loops here. This is very practical example. Please go ahead and do the exercise yourself because for example, if you're in a sales department and you're doing some data scientists works, for example, you need to call all the data and store it somewhere to do some statistical methodology on the data. For example, this is how we can do it in C plus plus, for example. So please go ahead and do the coding yourself. It is very important to get to know the shape of the multidimensional arrays and how they work. And we could have, for example, a four dimensional array adding one layer of complexity, for example, department product month, and then we could say store. So there are, for example, two stores each consisting of three departments and each department, we're having four product over five months, for example. This is another layer of complexity. As you see, we can have multidimensional arrays to store data complex data for us to be able to organize our data in a database. Thank you for watching. 27. 1 Pointers: Hi, welcome to this session. Today we're going to talk about pointers in C plus plus. So pointers are one of the most important concepts in C plus plus and are used to directly manage and manipulate memory. While this may sound advanced, the core idea is simple. So a pointer is a variable that stores the memory address of another variable. We would like to illustrate this using an example. But before going through the example, we would like to review some part of the code. So we know how to initialize and declare one variable of type integer. For example, we assigned the value ten to the integer named X, and then we are going to print the output using CO syntax. So when I run, I see the value of X equals ten, which is the assigned value. But this value when it is declared, it is stored somewhere in the memory. When it is stored somewhere in the memory, and if we would like to find the address where this integer is exactly stored, we can simply type a syntax call like and and the name of the integer that is declared above. So here by having this line, we are able to find the address of the integer X. So we are going to find exactly where this X is stored in the memory. Let's run. Okay. So this is the address of the variable X that is equal to this line. So this is the address where it is stored. Now in pointers, we are going to assign the address of a variable to a pointer. So the pointer can store address, its address, and it can directly use the address to modify or manipulate the variable. Here, we would like to write a program that demonstrates the use of a pointer to store and retrive the value of a variable. In order to do that, first, we declare and initialize a variable here. And what we're going to do is the following. So we are giving the address of the variable to be equal to the pointer that is defined here. But how do we know that it is a pointer? Because there is one star before the name of the pointer. So when we have this type of declaration, it means that it's a pointer, and we are simply just giving the address of the variable X here. So so far, we printed the value of X, the address of X, and now we are going to print the PTR variable that is assigned the address of X. As you see here, when we are printing the PTR, it exactly equals the address of X because this is what it is assigned to. So PTR, this pointer is directly accessing the address of where X is a stored. However, if we are going to use a pointer that shows that is shown by start then PTR, we are going to access the value of the variable X. So we expect to see number ten when we print this line here. Let me run Great. So we first printed the number of the variable, then it's address, then using the address that is stored in the pointer, using PTR, reprinted, again, the address in which the X variable is stored. Then in order to access the value of X, we simply use the dereferencing the pointer which is stored the PTR so we can now see that we are having access to the variable X. So now we can see that the variable X is declared. Is address is given to a pointer, and the pointer can access the address of X using only PTR, and if we are dereferencing the pointer, meaning that we are having a star behind it, it means that we are directly accessing the value of the assigned variable. So this is just the beginning and introduction of pointers. Please go ahead and do this very simple exercise for yourself and try to play with it. It gives you a good introduction for starting this section of pointers. In the next sessions and videos, we are going to dive deeper into different examples and coding exercises of pointers and show how they are useful. Thank you for watching. 28. 1 1Pointers function0: Hello ive. Welcome back. In today's lecture, we are going to do ON coding exercises with respect to the pointers. We would like to write a program that defines a function to double the value of a variable using a pointer. Previously, we know how to do it using a call by reference. However, here we are going to do it directly with a pointer. To do that, first, we are going to initialize one integer called X to be ten and we're going to print its value before doing any modification. We expect that it will print the value ten. Then we are going to call our function. So when we are calling the function, we are passing as the argument, we are passing the address of the variable X. How do we know that we are passing the address because we are having this and sign before the name of the variable. So let's go up. So here we see that the name of the function is double value. It's of void type, so it's not going to return anything, and as an argument, it is accepting pointers. So when we are passing the address, this function is going to have access to the memory address to the memory address of where this variable X is stored, and it can also do modifications to the value of X itself. So what's going to do is that it is going to whatever the value of that X is showing this star PTR is going to be multiplied by two, and it is going to store here. So basically, we learned that when we have something like this, we can also write it like this. So writing it like this is more advanced, but we can also see this equation with this line. So it's only your choice, but you're going to see this example more frequently. So basically, whatever the value of star PTR is, in this case, it is ten because the address of the variable X, which is ten is passed as the argument. So the star PTR is going to access the value of the variable itself when it is PTR, it is going to have access to the address. When it is star PTR, it is going to have access to the value. So here we are having star PTR dereferencing the pointer. So the value of X is being multiplied by two, and it is being stored in the X. So ten times two means 20, so 20 is going to be stored in the dereferencing the pointer. So in the star PTR, which means X. And when the functions work is finished, the execution of the code finishes this line and is going to the next line. So here we are saying after doubling the X, let me just compile. Great. So as we discussed, ten is assigned to X. We are printing X before doing any modification here. Then we're calling the function and passing the address of X as an argument. So when we have access to the address, using a pointer, we can access to its address using the PTR or StrPTR to have access to the value. In this case, we use the StrPTR, this means that we are having access to the variable X. Its value is ten. So ten times two means 20. So 20 is assigned to a star PTR or X. So basically, this line is exactly like writing X times two. So it means that we are having ten times two, equal X equals 20. So this is what it means. So we see that using a pointer, we can do some direct modification without having the call by reference here to any variables in a function outside of the main function. Thank you for watching. 29. 2 pointers function1: Hello, Evan. Welcome back. We're going to do another coding exercise using a pointer, and we would like to write a program that uses a function to modify the value of a variable using a pointer. To do that, we know that we need a function to do that for us. But in the int main function, we first initialize one variable named X. Then we are going to print the value of X before doing any modification and we expect here to print the value of 25. Then we are going to pass the address of this X variable to this set 200 function. So this set 200 function, the name is set 200. It's of a void type, and its argument or its input is a pointer. So by passing the address of the variable X, this function can have access both to the address and to the value of the variable X here. So here, this function is going to simply put 100 to the variable X. Why we are saying that star PTR means variable X because we are dereferencing the pointer here, and the pointer is pointing to the variable X, Y, because its address is passed when its address is passed, and if we use PTR, it's going to print the address. When we use star PTR, we are going to have access to the value. So now the pointer, when we call this function by address by its address, the pointer is pointing to this X variable, and now we are saying that, okay, now assign 100 to that X variable. This line simply means that we are going to put X to be equal to ten. To 100. But if we want to do it directly with a variable, we need to do it by a call by reference. Using a pointer, we can directly access the value of the variable and make some modification here. So let me just now run and you will see the result. Okay. So here we are initializing the variable. We are printing its value. It is 25. Then we are calling the function and we are passing as the argument. We are passing the address of this variable X. So it basically means that this pointer is pointing to this value variable X. By using the star PTR, we are dereferencing the pointer, meaning that we are setting the 100 to this SRPTR, it means that we are putting 100 to the X variable directly. And then we are printing the variable X after called the function. Please note that we couldn't directly modify the variable X using this line because if we wanted to do so we had to use a call by reference. It is a very important note to have in mind that we need a call by reference to directly modify this variable X here. Or we can do it using a pointer, as we mentioned in here. Thank you for watching. 30. 3 pointers function2: Hello, everyone, and welcome to another session. In today's session, we are doing a coding exercise using pointers and functions. We would like to write a program that swaps the value of two variables using a function and pointers. We learned previously how to do it using a call by value or call by reference. So a call by reference, we can directly modify the variables. But in a call by value, we are modifying a copy of the original variables. So whatever that is printed in the function is the modification and the modification is not done in the int Min function. So this is what we learned from the C plus plus course beginners. Now we are going to do modifications using a pointer. So in order to do that, we declare two variables X and Y, and we would like to swap their values so now we are printing the X and Y values. We expect that it should be five and ten X equals five and Y equals ten. Then we call the SWAP function here and we are passing the address of the X and the address of Y. So this function is going to have access to the address of both variables and directly to the values of both variables. This function is called swap. It's a void type, and it is accepting two arguments, two pointers as the arguments, and they are separated by a comma. In order to swap their values, we are defining a new variable called TEMP means temporary. So the first argument here represents the X and the second argument here represents the Y. So A star A, so pointer A is having access to value X, which is five, and pointer B is going to have access to the value ten, which is Y. So now when we say star A, it means X, or it means five. So star A is assigned to this temp, meaning that five is assigned to this temp. And then star B is assigned to star A, meaning that ten is assigned to star A. And finally, temp is assigned to star B, meaning that because it got the value of five, five is assigned to star B. Basically, we defined a new variable. We put whatever it is in A, we're putting in this one, and then we are putting whatever that is in B in A, and then we are putting this temp into B. So we used a third variable to swap the values of this A and B. And after this function has finished its execution, we are going to print the value of X and Y, and we see that the values are swapped directly. Okay, great. Let me analyze the code for you one more time. So we are having two variables here and we are printing their values. X equals five, Y equals ten, so we are printing them. Then we call the swap function. It's a void type. We are passing the address of two variables X and Y. So A represents X, and B represents Y. So whatever that is in A or X or simply means five is going to be assigned to temp. And then whatever that is in star B simply means Y or ten is going to be assigned to star A, meaning that X is going to get the value ten like this. And then Tem was five. Now, then temp is going to be assigned to Star B. Meaning that five is going to be assigned to star B, meaning that now star B equals five and star A equals ten. So initially star A was representing X, whose value was five. Now it is still representing X, but its value has changed. The same goes with D B, as we see the result here. After swapping X equals ten and Y equals five. It's a very good example that we can do modification using a pointer directly directly pointing to the variables. Don't forget to do these exercises yourself to get used to syntaxes of the pointers in Cy plus plus. Thank you for watching. 31. 4 Pointer array exercise2: Hello, everyone. Welcome back. Now we are going we're going to do another coordin exercise using pointers. So we would like to write a program that using a pointer, we are going to print an array and access all the elements of array. So write a program that access an array and print its elements using a pointer. So this is what we are going to do. Know how to initialize an array. So it is an array of type integer. There are five elements in this array, and we are pointing to the first element of the array. So the first element of the array is this is how the first element of the array is accessible using an and sign here and sign here before the name of the array, we are basically having access to the address where the first element, the first element of the array is store. So we are giving the address of where the first element of the array is stored. So using this line, we are pointing to the first element of the array. So the pointer knows where the first element of the array now is stored. So now, in order to make some clarifications, let me print the PTR, only the PTR, and then the address of the first element of the array. So we expect that they should be the same. Okay, great. So they are the same. Why? The reason is we are giving the address of the first element of the array, the value one to this pointer. So when we are using PTR, it is going to print the address where the first element of the array is stored in. And using this line and ARR and zero, we are going to print the a address of where the first element of this array is stored in the memory. So we see that everything works perfectly, and now we are going to print the value of the first element of the array, just using the array syntax directly, and also using dereferencing the pointer using a pointer, we are going to print that. We expect the values should be the same again. A. So we see that we wanted to assign the pointer to point at the first element of this array, we see the first value is one using this line it is printed, and the pointer is also pointing to the value, the first element of the array whose value is one. So the pointer works perfectly and everything is good now. So let us now print the elements of this array. Using the pointers and four loops. To do so, we know because we need to iterate over all the elements of the array, we need a four loop, and a four loop is going to start from zero until I is lower than five. So basically, this means that I equals zero, I equals one, I equals two, I equals three, I equals four. And there are five elements in the array. So this represents the first element, second, third, fourth, and the fifth element. We're going to print element I plus one, Y I plus one because I equals zero, but because C plus plus starts from zero, we can simply just add one to show that we are talking about the first element. And then we would like to print the value we are going to print the value of the first element of the array plus I. That plus I means, let me just type down the details so you would have a better understanding. So now I plus one is printed, then we have the star PTR, the pointer plus I, this means that when I equals zero in the first iteration, zero plus one is printed, and then the pointer is one. The pointer is pointing to the first element now because I equals zero, so the pointer is going to pointing at the first element. So basically, now, let me just run and see the first line result. Let me just comment this part so it would have more clean output. So we see that when I equals zero, it is printing the element one, which is this part element one. And we are pointing at the first element whose value is one. And then we see here that the pointer is pointing to the first element, and no value is added to this because I equals zero, so it is still going to point at the first element. When I equals one, we will have element I plus one, this means that I equals one, so one plus one, this means two. And then pointer is already pointing at the first element plus one. So it is going to point at the second element this time. So let's compile as you see here, when I equals one, so we are having the element two, and pointer was already pointing at the first element. Now I equals one, so I is added to the pointer pointer is going to point to the second element. So then I equals two element two plus one means that it's pointer pointer equal to one already in the initialization, it is going to have access to this one, and it is going to be added by two because of this line because it is added by two, now I equals two. So pointer is going to point to the third element. So let's run Great. We see here, when I equals two, we have the element three, so element three, and pointer was already pointing to the first element plus I and I equals two, so it is going to point to the third element whose value is three. So using this loop and then I equals three, and I equals four as well. So we see that using a pointer, we can iterate over an array and access to all the elements of array, and we learned how to point to the first element of the array. And if we would like to for example, have access to the second element of the array using a pointer, for example, now the address of the second element of this array, which means two is being assigned to the pointer. So pointer now is pointing to the second element of the array. But for the sake of simplicity, we started with the first element. Depending on what you want to do, you can simply change the pointer is pointing to which element of the array. Thank you for watching. 32. 5 pointer to two dimensional array: Hello, everyone. Welcome to this coding session. In today's coding exercise, we would like to write a program to access and print the elements of a two dimensional array using pointers. But before doing that, I would like to review some concepts of the pointers in C plus plus, and then we will dive into solving this program. So we learned how to define initialize variable and then pass the address of the variable to a pointer, and we learned that if we dereference the pointer here, if we dereference the pointer, we can access the value of the variable and if we simply use PTR, we are going to have access to where this pointer variable is stored in the memory. So this line is going to give us the value of the variable X, which is ten, and this line is going to give us the address where this variable X is stored in the memory. Let's run. Rate. So we see here that the value of X is printed, which is ten, and then the address is printed here. Okay, so this is a simple example to just review the concepts. Now we would like to dive into the coding exercise. Okay. Now, we would like to have a two dimensional array just like this. Let me just comment the above part because yeah, okay. So here, we are initializing one matrix, two dimensional matrix. There are two rows. There are two rows here and three columns, and there are elements associated to each row and column. So each cell has one associated value there. So in order to have a pointer, to this array, things are a bit different. So in order for a pointer to point to this array, we are writing into the type of the pointer. So in parenthesis, we write down the star PTR, and then we type the number of columns of that array. Equals to the name of that array. So this is how we are going this way, we are going to store the address of the first row of these two dimensional array matrix. So this line is going to help us to have the address of the first row of this matrix. And since PTR points to the entire row, so this PTR is pointing to the entire row, we access elements. We must access elements using PTR IJ. So indexing is necessary. So indexing when we have the indexing, automatically dereferencing the pointer and accessing we can access specific elements using star PTR, like here, would give you the entire row of the array and not a single element here for the array. So previously when we were having star PTR dereferencing pointer, we were having access to the value of X, but because it was a single element, here we have a pointer that is pointing to the full row to all these three numbers. Therefore, we must use the PTR I and J to point at each specific cell, each specific value. So for example, if we type down PTR 00, it is going to point here. But if we type down PTR 01 like this, 01, it is going to point to number two. So therefore, it is necessary to point at each element using this formula, and you see that initializing the pointer for a two dimensional array is a bit different. Here, in order for you to have a better understanding, I am printing two values. Above, we printed the syntax when the pointer was pointing at one single element. But here, if I run you see in both cases, it is just returning the address where the first row of the pointer is stored. So either using SRPETRD reference pointer or simply the PTR, it is going to give us the address. But how can we access the elements? In order to access the elements, as I mentioned above, we need to use INJ. To do that, we need to have a four loop nested for loop. So the outer loop is going to point to the rows outer loop, and the inner loop is going to be for the columns. So now in order to print the data, for example, in the first time, I equals zero, J equals zero. We are going to print PTR zero, zero, then I equals for this line, I equals zero everywhere, but J now equals one. We will have the PTR zero, one, and then J equals two, we will have PTR 02. So we are going to have the prints of one, two, and three. Why? Because PTR 00 means that the pointer is talking about is pointing at the first row first element here. So the value of one is printed, sorry, the value of one is printed. Then one space is printed here. Then I is still zero, J equals one, so it pointer is pointing at the first row second column, which is two here, and then first row third element three. So when J reaches the number two, this loop is not going to be repeated. Why? Because when we increase the value of J three, three is not lower than three, so it's not going to pass this condition and it's not going to enter this inner loop anymore. We have one NEL here, so we are going to next line, and now I is increased 0-1 by one value, so I equals to one. And again, J equals zero until two. So it is going to basically print first this number and then this number and then this number. And how we're accessing it using simply PTR INJ. So this is how we're going to have access to elements of a two dimensional Redmon run. Okay, great. So let me just comment this part, so we will have a clean output. Great. So here we see that we are able to print the value of these two dimensional array. And simply the way for doing this using a pointer is that we type the name of the matrix. We type down the number of column and dereferenced pointer in parenthesis and then the type, and then simply just printed using PTR and INJ that are the row and columns in the two dimensional array matrix. Thank you for watching. 33. 6 pointer to two dimensional array 2: Hello, everyone. Welcome back. In today's coding exercise session, we would like to write a program to change one element of a two dimensional array and print the elements of it. So we learned how to initialize two dimensional array. So here we are having two rows and three columns, we're defining a pointer that is pointing to the first row of the array here. So it is pointing now to this part. And to do that, we need to have the number of columns when initializing the pointer. In order to access elements of a two dimensional array using a pointer, we learned that we need to do it using INJ. So now that we would like to change, for example, this value, instead of having six, we would like to have seven. So we are going to type down the second row and the third column should be, for example, seven or should be nine, okay? And then in order to print again the two dimensional array, we need one outer loop representing rows and one inner loop representing columns, then simply using PTR INJ. So now if I run, you will see Okay, great. So the original array was 123456, but the modified array is 123459. Why? Because we directly changed the value of the second row third column here. It was originally six. Using this line, we changed it. So this was a good example of how to modify one element of a two dimensional array. Please go ahead and do these exercises yourself to get used to defining a pointer, pointing to a two dimensional array, and also if you haven't had the chance to rate this course, we would appreciate it. If you could do it for us. It would be very beneficial for us. And also, we would appreciate if you give us some constructive feedback, it can help us to improve our teaching experience and having a better courses for future students. Thank you. 34. 7 array to function pointers: Hello, everyone. Welcome to another coding exercise. In this session, we would like to write a program that defines an array of functions pointers. So let us dive into it right away to see what we mean by functions pointers. So here it is our main function here. We are declaring an array of function pointers. So here I have four functions. The first function is at, it's of type int. Two arguments are there, both of them are integer, and it is going to return because it's not a void type. It's anything apart from void. There should be one return. So it is returning A plus B. Then we have the subtract function. It is basically the same thing, but it is going to return A minus B, then multiply function that is going to return A multiplied by B, and then we have the divide function. First, we check to see if the B is equal zero or not. If it's not equal to zero, the second number, we are going to return A divided by B. Otherwise, if B equals zero, we're just saying that division by zero is not allowed so we return an error. Okay. So these are our four functions that we defined at subtract, multiply, and divide. So these four functions names, their names are defined like an array, and we have a pointer pointing to these four functions. Basically, we are having of type integer. This is our dereference pointer. There are four elements in it, and the four elements are these four functions, and the arguments are two arguments separated by a comma and both are of the type integer. Now the function pointers are defined, and now we have two variables. The first one is X, the second one is Y. We're going to set ten to be equal to X and five for Y. And in order now to call the function using our pointer, we can simply do it like this. So imagine that we have this is the first element. This is the second element, third element, and the fourth element, and we start from zero in C plus plus. So in order to call the first function, the add function, its index is zero, so we're going to use zero and then the name of our pointer, we are simply just using the name of the pointer directly, and we are passing X and Y as the arguments. We can define any other variables like Z, I don't know, H, whatever. So we can just pass two variables to the first function of the function pointer. So the first function is add and when we are saying zero, it means that we are talking about the add function and we are using our pointer name, so it directly goes and pass the X and Y to the add function. And here, it's going to calculate the A plus B and see out the value for us. Let me just run the code. Okay, so A plus B is 15, it's correct. Now, what if we would like to call subtract? So we simply just point to the second element with the index one. It is going to do the job for us, and if we would like to, for example, do the division, we are going to now have the division result. Okay, it's two, so it's correct 10/5, it's two. But in order to avoid repetition, we can call all the functions together. To do that, we need a four loop. In our four loop, we are going to start from zero and we will go up until I is lower than four. So here we are calling the functions one by one, when I equals zero, displace will be operations zero, add function is called, then I equals one operations one. So the subtract function is going to be called. What I mean is something like this. When I equals zero, I equals zero, for example, I equals one, we have the operations, one, so subtract is going to be called. Then we have operations when I equals two, for example, and then I equals three, we have operations, operations three, and the divid function is going to be called. And after each call, we are going to the next line. So this line is going to print result of operations. We are calling the number of the element. So we are adding one because it is starting from zero, we would like to have 1-4. So when it's zero, it is printing one and then two, three, and four. And then we are calling the functions one by one, add subtract, multiply, and divide and passing the argument, X and Y, and print the result because we are using the CO. Let me run Okay, great. So here we see first, it does the A plus B, so it calls the add function, it's 15. Then do the subtract, it's five, ten minus five, then ten times five, it's 50 and then 10/5, it's two. So simply we can have in order to facilitate our coding, we can instead of, for example, having four pointers and each pointer is pointing at one function. We can have only one pointer and this pointer is pointing at an array of functions, and different element of this array is one function. So the first element is one function, the second element is another function. The third element is another function, et cetera. This way, we can save a lot in our memory usage when we are having a long blocks of code, too many blocks of code, so this is going to help us very much. Thank you for watching this video. 35. 8 array to function pointers 2: Hello, everyone. Welcome back. In today's video, we're going to do coding exercise, and we would like to write a program that defines an array of function pointers. We did the same thing in the previous exercise. However, in this exercise, we're going to read some values from the user. So we are going to get some values from the user and use those values in order to do the operations. So we basically have the same functions here, and we are having the same function pointer here, and we have a display menu. When we run the program, we are asking select and operation. So the user is going to select AD if they want. They are going to select one if they are going to use the add function, and if they are going to use the stub track function, they are going to enter two, enter three for multiply, enter four for divide. And this code is simply just the number. You see here, backslash N is going to the next line and then printing the two, which is the number. Again, backslash, it's going to next line. Backslash N, it's going to next line. So we are defining one integer called choice. Whatever the user chooses, we are going to store it. First, they would like to see if the user is typing the correct number. So choice if the choice is lower than one or greater than four, we are returning invalid choice because it's not among the options and the program execution is done, and if not, so if they correct something 1-4, we are going to initialize X and Y and ask the user to give us two numbers. So whatever they enter, they enter the first number, they enter, they push the Enter button, and then they enter the second number and then they push the Enter button. So we store those two entered numbers in X and Y using syntax. And then finally, we are going to define one integer called result. And based on the operations that they entered, we are going to call the function the corresponding function and pass two arguments, the X and Y that the user gave us. Why we have the choice minus one here because in C plus plus, we start from zero. So operations zero means at one means subtract, two is multiply, and three means division. So zero means add. But if the user is entering one, because we told the user to enter something 1-4. So if the user wants to call the add function, we are going to enter one. But here, if we put number one, we are calling the subtract function. Okay? Be careful about it. So one minus one will be zero and the add function is going to be called. Basically, it is because we would like to be synchronized across all the blocks of code. We are using the minus one here, and then simply we are just putting the value of this function is called using this pointer and these two numbers that the user gave us are going to pass as the argument to that function, and the result is going to be stored in the inter result. Then we are printing the result. Let me just run and see and analyze the code further if needed. So yeah, so we are here, select an option. We have one ad, backslash N, going to next line, then two subtract, backslash N, going to next line, three multiply, et cetera. So I would say one and I enter. When I put one, it checks if the number is lower than one or greater than four. It's neither. So it is in the correct range. It is going to show the message, enter two numbers. I'm going to enter ten and enter 12. It is going to return the result 22. So this is when I mentioned add, when I mentioned number one, this number one is stored here in this integer choice. And this integer choice, which is one now, is going to be here. So one minus one is going to be zero. It means that we are having something like operations. Zero, pass the argument, X and Y. And then we have the operation zero, so the index zero means the first element of this function pointer and the first element is the add function, and the add function is simply taking these two arguments and adding them and returning them because it's of integer type. It's not a void type, so there is a return value, so it is returning the value. And the returning value is going to be stored in the result integer, and we are showing the result. Let me run the code one more time. I call two, so I would like subtract. I would say the first number is 20 and the second one is 13, so the result is seven. So this is basically how we can have a more interactive coding exercise with the user, so you can also participate and check your code and you see that it's becoming more nicer when it is interactive. Thank you for watching. 36. 9 pointer to a function as a parameter of another function 2: Hello, everyone. Welcome back. Now we would like to write a program that processes an array of integers using a function passed as a parameter, and the program should allow applying different operations, for example, doubling the value or finding their square. To do that, we have one main function. We initialize an array we get the size, we compute the size using the famous formula, and then we're printing out the original array. So far, nothing changes. It's like previous lessons, like the lessons in the array section. So using a for loop, we print the original array. So this contribution of this program comes from here. So we are calling a function called process array, and we are passing three arguments. So the first argument is the array itself. It's the array. The second argument is its size, and the third argument here is calling a function. So it's a double value. Let's go and read the process array. So when I go and read the process array function, it's of a void type, so it's not returning anything, and it is accepting three arguments. So the first argument is of type of array, it's of type int and it's array. The second one is integer, so it's a variable. It's just a number, so it is called size. So it's the size of the array, for example. However, the first part is the pointer that is for deferred argument. And the pointer is accepting one variable itself. So one argument is within this pointer. So when we are calling the process array, we're passing the argument. We're passing the array, the first argument, passing the size, the second argument, and we are passing the double value. It's a name of a function, so it is going to be treated as a pointer, because this deferred argument of this function is a pointer. So now this pointer is pointing at this double value. So what is a double value function? Here we have two functions, double value and square value. The double value is going to get one argument. It's an integer, and it is going to return the double of the variable that it is received. So in case that it receives, for example, three it is going to multiply it by two. So three times two, it is going to return the value six. And square is going to multiply the argument by itself. So if you pass the number three, for example, it is going to have the multiplication of three times three, so it is going to return the value nine. So here when we are calling the process array, we are passing the array because as the first argument, as the second argument, we are passing the size of the array and then the name of the double value function. Because the first part is the array, it's correct. The second argument in this function is the size of the arrays of integer, so it's correct. And the third part is a pointer. So now that it's a pointer and we are passing the name of a function, for the pointer. So now the pointer of this function is pointing at this function. And it is going to return this value. And what is the value that we are passing? The value that we are passing is these values. So it's values. These are the values of the array. But how when we are passing all these arguments to the process array, we're going to go through a four loop because when we would like to access all the elements of an array, we need to iterate over all the elements one by one. To do that, we need a four loop. So the four loop starts from zero and goes up to the size to the size of the array, which is already passed. So as long as I is lower than the size, this loop is going to be repeated. And for whatever the value of the array I is. So for example, let me when I equals zero, it is going to call operation R zero, then I equals one, it is going to have operations one, for example, and the R zero equals this one. R one equals this. Okay. So basically what we are going to do here, we start from I equal zero are calling when we are in the I equal zero in this loop, we are running and executing this line, and this line means this line, for example, when I equal zero. So we are basically mentioning that the first element of the array now needs to be treated using the operation pointer and the operation pointer was pointing at this function because we call this function, double value. So the first element of the array named ARR, which is one is passed as argument here for the double value function and it is going to be times two multiplied by two, and it's resulting value is going to be now assigned to the first element of the array. Previously, the array was one, two, three, four, five. After this line, the array becomes two, two, three, four, five. Then we increase the value of I by one value. I equals one, I equals one, we enter the loop again to do this line. We are having it here. We're mentioning the second element of the array ARR, which is two is going to be the argument of the pointer, and the pointer here is pointing at the double value function and the double value function is getting the second element of the array, which is two. So two times two, the result is going to be returned. It is going to return the value for us. So it is going to now after executing this line of code, the array would look like this, okay? And the size of value, size of the array is five, so I is going to go 0-5. And we wrote what is going to happen for I equals to one and I equals to zero. So it is going to go through all these steps. Basically, it is doubling everything like one will be two as we had here, two will be four, as we had here, three will be six, four will be eight, and five will be ten when we call the process function using this syntax. And then we are printing now the double value of the function. Let me run. Okay, we are seeing that the original array is one, two, three, four, five, which is correct here because we were printing the original array here in the code. Then we call this process array function. And when we call this process array function, in the first iteration, the array becomes like this. In the second iteration, it becomes something like this. And in the last iteration, it is going to look like this. Eight and ten, which is exactly like here. So we computed the first element, the first two elements, so two is here, so two is here. We computed the four here, so the four here, and the rest is when this one for the I equals zero, I equal one, these we analyzed here, then I equals two, I equals three, and I equals four. So we learned now how to call the double function using the process array function. Now it is an array. It is having an array as the argument. But what if we would like to do the square? So if we would like to do the square here, sorry, if you would like to do the square here, We are having a function named square, right? It is just going to the difference is only for the math part. The rest is the same. So when we are having the square function, when we would like to make it square and when we would like to call this function, basically we are writing the same thing. We are calling the process array. We are passing the array, we are passing the size, and then instead of double value function, we are calling the square value function. When we are calling the square value function, this third argument of the process array is going to point at this square value function now. Doing so means when we are having a four loop here, we are saying that, okay, when I equals zero, we are going to execute this line, and this line means that the first element of the array, which is one is going to the pointer is going to point to the first element and it is going to call this square value function. So the first element is passed here, and the first element times first element, it is going to be one. So now this time, let me write it down somewhere else. So square value, and this one was for double value. When it is doing that, I equals zero until five, and it is going to have the value one here. Then it is going to then I equals two I equals one, sorry, when I equals one, it is going to call the second element, second element is passed to this function two times its four and then it's nine, then it is 16, and then it is 25. If I compile, Okay. So we see the original array is one, two, three, four, five, and the square one is 14 exactly what we have here, one, four, nine, 16, and 25. Why? The reason is we are having the process array. We are having three arguments. The first one is the array itself. The second one is its size, and the third one is a name of a function. But because the third argument here in this process array function is a pointer, now this pointer is going to point to the named function which is square value, and this function is going to take the element of the array that is passed to it and return its square. So in this case, it is going to pass the first element of the array to this, for example, function and it is going to square it. Thank you for watching. 37. 10 pointer to a function as a parameter of another function: Hello, Evan. Welcome to another coding exercise session. In today's session, we would like to write a program that defines several arithmetic operations and uses a function pointer to pass one of these operations to another function, which then applies it to two numbers. So to make it easier, I'm going to explain through the code so you would understand what it wants us to do. We have the same four functions here at subtract, multiply and divide and they're going to simply add two numbers, subtract two numbers, multiply, and divide two numbers. We know from previous examples this. In the int main function, however, we are only declaring two variables, and then we are calling the calculate function. The calculate function is accepting three arguments. The first two arguments are two integers that are called A and B. These are separated by one comma, and this is the first argument. This is the second argument, and this is the third argument. Please note that the arguments are being separated using a comma. Okay. The first two arguments are easy. So these are simple. We did that before. They are just two integer variables. However, the third one is a pointer, and this pointer is now an argument of the calculate function. Previously, we defined a pointer that was point, it was an array of function pointer. So different functions were defined as an array, and this pointer was pointing to this array, and each element of this array was a function. Previously, it was something like that. But now this pointer has become one argument of the calculate function itself. Now, what's the difference? The difference is that when we are going to call the add function, for example, we don't need to mention we don't need to mention, for example, operations zero and then X and Y. Previously, we were calling the add function like this, but now we don't need to do that. The difference is here. So that pointer is now an argument of the calculate function. And if we would like to call the add function, we call the calculate function. In the first two arguments, we are passing the variables that are like ten and five, which are X and Y. Then we call the name of the function, and this name is going to be assigned to this third part of the argument. So the pointer is now directly pointing to that at function. And when it does, it's going to call the function passed as a pointer. So it is going to now the result of this line is this add function is going to be called using the X and Y variable, and it is going to add X plus Y, write X plus Y, return this X plus Y for us. So let me explain it one more time with another example. So here we are calling the calculate function. Three arguments are passed, each separated by a comma. Okay? The first two arguments here are these two. So there are variables of integer type that are X and Y here for us that are having the value ten and five. And the third argument here is defined to be a pointer. And in the third argument that we are passing, we name the function and then call the calculate. We name the function that we would like to know we would like to be operated. So we name the function and when we do that, when we put the name of this subtract function here, this pointer is going to point at this function. So it's going to by this line, it is going to call the subtract function. And then pass A and B, which are X and Y because X and Y are A and B here and A and B here. X and Y are going to be passed to this subtract function. Why? Because the pointer is pointing to this function. Why it is pointing to this function because the third argument is a pointer, and we named the subtract function as the third argument. This is Y. Then it is going to do the subtraction and return the result for us. So it is going to subtract A do the A minus B and return the result for us. Let me just run the code. Okay, great. So here we see that we have the calculate and calling the add function, it is having the addition, ten plus five is 15, subtract is five, multiplication is 50, and division is two. So this was a very good example of a function that takes a pointer to another function as a parameter. Please, please go ahead and do these exercises yourself. They are very useful. They may seem simple for some people. They may look simple for some people, but they are complex in nature and very tricky. So please go ahead and do these exercises yourself as well, so you get used to calling using a pointer to call an array a function or a variable. It just comes with the practice. So practice makes perfect. Please go ahead and and in case you had any problems, you can write to me in the question box, and I will be happy to help you out. Thank you for watching. 38. 11 Dynamic memory allocation1: Hello, Evan. Welcome to another coding exercise session. Now we would like to write a program that dynamically allocates memory for an array. Previously, we learned that we can declare and initialize an array like that. So this is the name. This is the type. This is the name of the array. This is the type of the array. This is the number of elements, and these are the elements. And when we have one element that is zero, it means that all of the ten elements are equal to zero. So here we see that a declaration and an initialization of an array is done in a normal situation. But there is a drawback to this type of declaration. So we are having an array with ten elements. So we are using, for example, ten bytes of our memory but what if we are not sure how many elements that this array needs to have? So for example, if one user is going to use eight elements, one user is going to use five elements, one user is going to use two elements of it. One way is to declare one array, that is big enough, that is large enough that can have enough memory to, for example, be used for a user that is going to use eight elements of it, and for another user that is going to needs an array with five elements and the last one needs an array with two elements. But here we are declaring an array with ten elements. So we're not using our memory in a very optimized way because sometimes we can ask the user to simply give us the elements of an array that they would like to have. In this lecture, we're going to do it later on in a few minutes. But before that, I am trying to show the drawback of this declaration of array. So this declaration if we are having different users that are using our program, so one user may need to have an array with ten elements, with eight elements, one with five elements, et cetera. So either, we need to have an array that is large enough that we make sure that it is larger than the size of the arrays that are going to be chosen by our users, which is not a very efficient way. Or we can use another way and declare our array dynamically using pointers to let the user choose the size of the array. If one user is going to use our array and they are needing only an array with eight elements, we are going to dynamically allocate eight elements for an array based on the input of the user. Another person is going to use our program. They need five elements, so we are going to declare array with five elements. So as you see, we are giving dynamic memory allocation based on the user preferences, so it is a bit more customized. To do a dynamic allocate memory declaration for an array, we can simply declare it like this instead of this. This is the name of the array, and this is of pointer type integer, and the size of this array is going to be put here, it can be five, it can be eight, it can be anything. And this is the type. So basically, we are declaring a pointer that names array that can hold the address of the array and dynamically allocating the memory for an array in the elements with this size and return the base address of the allocated memory. So this is how we can do it. Okay? But we first need to ask the user what is the size of the array that you're looking for? To do that, we are initializing the size. We are reading the size from the user, and we are declaring and initializing our dynamic array here. And then when we have the size from the user, we are allocating an array with the same size as the user mentioned. And we are having a four loop to feed the array. So for example, the user says that I want an array with three elements. We dynamically allocate an array with three elements, size, and then we are having a four loop from I starting from zero until I is lower than size, and we are reading the elements of the array from the user here in this line, because we're calling the name of the array and this is the IF element. So when I equals zero, it is going to store whatever the user is entering in the first element, then second element, then third element. And here we are simply just displaying whatever the user chose for the array. And finally, when we are declaring a dynamic array, we need to make sure in order to deallocate the memory or free the allocated memory, we need to make sure we need to use this syntax. Delete the name of the array. So the allocated memory is now deleted. Let me run and we analyze the code further. Okay. So here, it says that enter the size of the array. I would say like three. And now it is showing me okay. Enter, Enter size element. So the size was something that I entered. Now it is printing Enter three elements because I entered, I would like to have an array with three elements or the size of three. So basically, whatever I'm going to enter, it is going to store as the first, second and the third element of this array using this four look. So I'm saying the first element is four, the second element is seven, the third element is two. And basically, here, in this four loop, it is printing the stored array that I used for my array. So I entered that I want an array with the size of three, that the first element is four, then seven, then two, and it is now printing array elements are four, seven to exactly what I entered. So if another user wants an array of, for example, five, they can have it like an array of size five and the elements are one, 25, so it is allocating an array with the size of five. This is how we can use dynamic memory and use efficiently the memory allocation. The important thing, the declaration, we get the size from the user, so it is customized, and then we need to deallocate or free the allocated memory at the end of the program. This is basically how the dynamic allocation of an array works. Thank you for watching. 39. 12 Dynamic memory allocation2: Hello, Evon. Welcome back. In today's coding exercise session, we would like to write a program that dynamically allocates memory for a two D or two dimensional array. Let me first run the program and see how it works, and then we start analyzing the code. So basically, it is asking to enter the number of rows and columns. So I would say two rows and three columns. It says, Enter the element of the two dimensional array. I would say one, two, three, four, five, and six. And then it is printing the elements for us. So it is having two rows, as you see, and there are three columns. So the first three numbers are stored in the first row because there are three columns, and the second three numbers is going to be stored in the second row. But here is an example of dynamic memory allocation. So I chose to have like two rows and three columns. One could use, for example, three rows and like two columns, for example, and then entering the number oops, sorry. So then entering the numbers like this. So it is customizable. If we were going to use the normal declaration of the array, we should have chosen an array that is an array with a size with the size that is large enough to consider all of these sizes and can contain all of the sizes for the input. However, using a dynamic memory allocation, we can have our array customized based on the user preferences. So now I want an array with three rows and two columns and can do it and you want another array, it can do it. So this is dynamic allocation, so efficiently, we are going to use the memory. To do this program, we need to have two integers, rows and columns, and we're going to read them from the user. Then we are going to have a four loop for the declaration of the two dimensional array. Basically, this row is array of pointers where each pointer will later point at the beginning of a row of integers. For example, if we are having in the input, if we are having two rows, something like this, this line will help us to have an array of two row with the size of two, which is the input of ova. We assume that it's two. So it is going to give us an array of pointers. Each pointer is pointing at the beginning of each row. So now in case that the number of row equals two, we are having two pointers. Each of them are pointing at the first element of the array. And then we are having a four loop here that is going it's a loop that is used to go through each pointer in the array. So basically, it is going to go through this pointer in the first row and the second row because it is starting from zero, and it is going to continue as long as I is lower than row. So in case row equals two, I equals zero and one going to enter this loop, okay? Then it is going to allocate memory for each row. For example, each row for each pointer in the row, like we had a pointer here and a pointer here, memory is allocated for a row of integers with columns element. So if the number of columns are three, it is going to allocate memory, this size of memory of t. If the number, for example, columns are four. It is going to allocate four elements of memory for this pointer pointing at four, starting at four. So this is how we are declaring a two dimensional array, one more time, I'm going to review. So first, we are creating an array of pointers where each pointer is going to point at the beginning of each row. So if row equals two, we are having two pointers, one pointing at one and one pointing at four. Then we are having a four loop going through each pointer in the array. So we are having two rows and I starting from zero. As long as I is lower than row, we are having a four loop because for each row, we are going to assign a memory. To do that, we need the number of columns. So in this case, the number of columns equal three we are going to give an allocate memory for a row of integers with three elements. This is how we do it. And this policy is extendable for, for example, three dimensional array, et cetera. We are going to have another four loop here. Then when we are having the declaration, we're going to initialize the array. So we are saying that enter elements of the two dimensional array. So when I equals zero, we are going to have J equals, for example, zero, one and two. If we assume rows equal two, and columns equal three, this is going to be the first iteration of I and all the iteration of J. So whatever you're going to enter, it is going to put it in the first column, then first row second column, then first draw third column. So this is why when I enter one, two, three, it is having the first row is stored with one, two, three. Okay, because we are having three columns only. Then when it is done, it is going to ask the I is going to increase. So now I equals two, I equals, sorry, I equals one, and J again goes from 001 and two. Therefore, we are having the second row, first element, second row, second element, and second row third element. And this is why when we are having the four, five and six here, okay? And then we are going to have another nested loop, one outer loop here, outer loop, and one inner loop that is going to print all the values for us. Again, I equals zero is for the rows, and this is for the columns. I equals zero, J equals 0-2, and it is going to print 123. Then we have DL because this loop is finished. We have DL here. It is going to next line, and now it's there. And then we are going to print when I equals one and J equals zero, one and two. So it is going to have four, five, and six printed. Let me run again. So I would say the number of rows two, the number of columns three. And when I enter one, two, three, four, five, six, we are seeing that the elements are printed exactly as as we have here. This is basically how we can dynamically allocate the memory for an array for a two dimensional array. But something to remember is to free the allocated memory. Here, because it is two dimensional, we are going to have a four loop to delete the memory allocated for our array, I for the columns, and then the whole array itself of the array of the pointers. Thank you for watching. 40. 13 A pointer to another pointer 1: Hello, everyone. Welcome to this coding session. So in this coding exercise, we are going to discuss a pointer to another pointer. So if we need a function that updates the value of a pointer itself and not the value it points to, we need a pointer to a pointer. So for example, in this example, we are demonstrating how a pointer to a pointer works to modify address stored in a pointer access and modify a pointer. So we call this a pointer to another pointer or a double pointer, we can say. Initially, we learned that this is how we initialize one integer and we are giving the address of the integer to the pointer PTR. So by having this and sign, we are giving the address. And then this is the name of the first pointer. We are giving the address of this pointer to another pointer, and when we are having a pointer to a pointer, please be careful that you need to have two stars there. So let me just comment this part and start printing this part. The value of X is now going to be printed and then the address of the first pointer and the address of the double pointer. So here we are printing the value of X. And this is the address of X that is stored in the first pointer in our pointer, and this is the address of a first pointer that is stored in the second pointer. Okay? So this is the value of X as it's printed let's review one more time. And this is the address of the X address of the X that is stored in the first pointer, okay? And this one is address of the first pointer that is stored in our second pointer or double pointer. Okay? You see that the addresses are different, okay? And now, basically, how it works is going to be like this. So basically, we can use our double pointer to modify the value of X because this pointer is pointing to this pointer. The double pointer is pointing to the first pointer and the first pointer is having and it points to the variable. So by modifying this by modifying this, like we are typing down double pointer equals 100. We are going to have the X variable modified. Now, as you see here, the value of X, we're printing it here, it is 100 instead of 42 that was initial either. So basically, this is how a pointer to another pointer works, and we learned that the address of X is going to be stored in the first pointer also, and the address of the first pointer is going to be stored in the second pointer. However, directly using the double pointer or the second pointer, we can access the value of the variable here and directly modified. Thank you for watching. 41. 14 A pointer to another pointer 2: Hello, Evan. Welcome to another coding session. In today's coding exercise, we would like to write a program that access array elements using a double pointer or a pointer to another pointer. So we are having one simple one dimensional array here that is named ARR. It's of type end and it is having three elements. And then we are having a pointer to the array. So this is how we're defining a pointer to the array itself, to the initial to the first of the array to the beginning of the array. Okay? And then we are having a pointer to the pointer. So a double pointer is defined here and we are pointing to the first pointer. Not that when we would like to point at the beginning of an array, we don't need the sign in here. We just point at the name of the array. So basically, you just need to write down the name of the array. Then we would like to print and access its value. To do that, we need a four loop because it's an array of three elements. To do that, we are having I starting from zero as long as I is lower than three, so I is going to get the value zero, one and two. And then basically we are having to have this line of code to access each element of this array. Because we are using a double pointer or a pointer to another pointer, we are having two stars here, but one story is outside of the parenthesis. So the first story is outside of the parenthesis because it is dereferencing the value obtained by adding I to the star PTR to PTR. Let me make an example. So basically what it does step by step is that this part of the code, it is dereferencing the PTR to PTR to get the value of the first pointer which points to the intart of the array. Okay? And this is dereferencing the first pointer. And then it adds the value I to the pointer value effectively moving to the IF element of the array. When I equals zero, I equals zero here, this one is going to dereference the first pointer that is pointing at the first element because I equals zero. Now that it is pointing at ten, and we are dereferencing the double pointer because we are adding because now this part is going to point at the first beginning element when I is revealed, and then we are dereferencing with another or second star, the double pointer here. Okay? And basically, this is what I explained here. So D references the resulting pointer to access the actual value of the IF element of the array. So when I equals one, this line is firstly pointing and dereferencing the first pointer. And we are adding the I, so now I equals one. So this one is pointing at the second element. And therefore, we know that the result of this parenthesis is the pointing at the second element, and then we are adding the second star here or the first star before parenthesis here to dereference the double pointer here, that dereferencing the resulting pointer to access the actual value of the I element of the ara and then I equals to two which means that this one is dereferencing the first pointer, and by adding the number two because it is pointing at the start of the array by adding 21 and two, it is pointing at the three and using this code, the full code, now we are dereferencing the resulting pointer, which is now at 30, so we can access the actual value. So let me run Okay, great. So you see here that it is now printing ten, 20, and 30, okay? But let's do another test. To do another test, now I'm putting both of these stars in the parenthesis to see how it happens. So basically, what it is going to do is that it is dereferencing directly, the first element because now it is pointing at the first element. It is now pointing at the first element. So basically, here with this, we are simply de referencing this one that is pointing at the first element, meaning that this part value is ten. Then ten is being added to the value of I, which is one time zero, then one and then when I run. You see that it is not printing the values of the array. It is printing ten, 11 and 12. Why? Because in each iteration, first, it is dereferencing the first pointer that is pointing at the first element only. And then it is adding the value of I, which is value 0-2. This is why here we are having first the in the first iteration, this part equals ten plus zero, we're printing ten. Then this part equals ten again because it is pointing at ten, then plus one because now I equals one, and this is printing 11 and then 12. This is not dereferencing the movement of the pointer. We are just dereferencing fully the first pointer. But having one star outside means that this part is going to dereference the pointer and also move the pointer to the next element using this I here and then dereference the full value of the number when it is pointing. When I equals zero, it is pointing at ten, when I equal one, it is pointing at 20. When I equals two, it is pointing at 30. And then when it is pointing this part is pointing at 30, now we are dereferencing the resulting pointer that is pointing at 30, so we can see the actual output here. Yes. So this is what we were looking for. Thank you for watching. 42. 15 Type Casting 1: Hello, everyone. Welcome back. In today's video, we're going to do some coding exercises regarding the type casting. So basically, it means to convert the nature of a datatype and its value. To give you an example, we simply initialize one float variable here that is called NUM and the value associated to this datatype is 5.75. Then we initialize one integer number called result and assign the value of NUM which is a float to the result. So basically in the output, when we print the value of the float number, which is num, we should see 5.75. However, when we assign this float number to an integer number, it is going to round the number to be able to print an integer value. Let's see. Okay. So here, we see that the float number is 5.75. However, when it is assigned to an integer number, the value is five. So basically, one float number is assigned to an integer number, or basically when we are converting the type of the data, it is going to round the number, so it is possible to print the integer number. And every time it is going to round down unless we tell it to do the round up, it is going to round down the number by default. So round down the number of 5.75 is going to be five. Now it is printed here. But instead of doing that, another way to do it. So another way to do it is to use typecasting. Typecasting the num directly. So typecasting is like this is the keyword for it. So the static underscore cast, then we have the type integer. So basically, this num variable is originally a float number. Here, we type down the destination, the target data type, which is integer in our case right now. And then we use the keyword static cast. So it is going to directly print the value of the non variable as it is an integer. So now we can run Okay. So here we see that we're getting the same result. So in order to do it directly and avoid declaring a new variable and then assign the previous variable to the new variable here, we can directly use typecasting, static cast, and then the target data type in here, and then this is the name of the variable that we would like to perform the typecasting over it. So basically, it's a very easy concept, but it is very useful. Going to do some more coding exercises with respect to the typecasting and also provided to expand the exercises to pointers, for example, using typecasting for pointers. Thank you for watching. 43. 16 type casting 2 void pointers: Hello, Evon. Welcome back. So in today's coding exercise, you would like to use a void pointer to access the value of a variable. Okay. So we would like to write a program using a void pointer to access the value of a variable. Basically, we haven't worked with the void pointer so far, so a void pointer is a pointer that we don't know it's data type. It's a void. So it can get any type. But in the end when we would like to access the variable, we need to do typecasting in order to understand the type. Example, here we are initializing one variable called X. It's of integer type and the value assigned to it is 42. Then we are assigning the address of that variable to avoid pointer. But however, when we would like to print out the value of this variable using the pointer, we need to use typecasting. Why? Because it's a void type. But this variable is originally an integer. And in order to access and print its value using a pointer, using a void pointer, we must use typecasting here. Basically, the formula for using the typecasting as before, we learned it. This part is simply the keyword. This is the a target data type, which is of type int because this is the variable is int, so the pointer should be int as well. Then we have one star afterwards, and we have the pointer in the parenthesis, and one star before the parenthesis will help us to access the value of the variable X here. So let me run Okay. So using this code, now we learned how to use a void pointer and then access the value of the variable of integer type. So when it's a void pointer, we must use a typecasting. To do the typecasting, simply, we have the typecasting keyvod, the target data type, and also the name of the variable that we have here is the name of the pointer. We have one star after the target type and before the parenthesis here. So in particular, this part, what this part is doing is static int PTR. This part is simply converts the void PTR to a pointer of type int. So this part is written why? Because originally the pointer was of a void type here because we initialized the void star. Okay. We write down this part in the code in this parenthesis to convert this void pointer of type integer. So basically what this part is doing is to converting the void pointer to an integer pointer, okay? And also when this part of the work is done, we are having one star before the parenthesis, and it simply does after casting the PTR to an integer type, after casting it from a void to an integer here, the D reference operator, which is this star is used to access the value of that memory location as an integer. Okay? So basically, this part is converting from void to integer. And then this star behind this parenthesis is going to dereference the operator star, the operator. So therefore, the operator star is used to access the value of that memory location. So basically, this is how we use typecasting for the pointers. Thank you for watching. 44. 17 type casting 3 void pointers: Hello, everyone. Welcome to another coding exercise. In today's video, we are going to write a program that excess the value of the variable Y using a void pointer. So here we are initializing one variable that is called Y. It's a flow type and its value is 3.14. Then we are assigning its address to a void pointer. In order to access this variable, first, we need to convert this void pointer to a float type because when it is void pointer, it cannot directly access. So in order to do that, we need typecasting. To do the typecasting, simply write down the static cast keyword. Then we are converting from void to the float, which is the target type. And this part of the code here basically does the converting from the void to the float. So this is the target a variable. This is the pointer. So this is a target data type, and basically we are using float star because initially it was void star, so we would like it to be float star. So this can make us to access the float variable. Then we have one star here that is going to dereference. It is a dereference operator that is used to access the value the value at that memory location as it is used. Let me explain it one more time after running the program. Okay. So here we see that we have a void pointer. We're accessing directly the variable Y here. However, in order to access a float variable using a void pointer, this void pointer needs to be synchronized with the data type of the variable which is float. To synchronize it, we need to convert it first. And then when we would like to convert it, we need to use the typecasting. So we type down static cast and then in this part, we are going to type the target data type. So this is a void type and the target is to have access to a variable that is of type float. Therefore, the target data type will be float. Then we are just typing down the variable that we would like to perform the typecasting over it. In this case, it's a pointer. So it was void star to declare a void pointer, but we would like it to be a float star, which means that it's a float a pointer to a float variable. And then we are having this dereference operator that we use to access the memory location where this variable is stored in. 45. 18 type casting 4 void pointers: Hello, Ivan. Welcome to another coding exercise. In today's coding exercise, we would like to write a program that uses one void pointer to access different variable types. So we are initializing three variables here. One of integer that is called A equals ten. We have the float B that is 5.5, and we have one character called C that is Z. A, we declare a void pointer. But in order to access these three different variables using a pointer, previously, we had to define three pointers. One to be an integer pointer. So it should have been something like this, my first pointer, and then we assign the variable A and then a float Sorry, a float pointer, my second pointer to assign the address of the variable B, and a car pointer, my third pointer. See. Okay? Now, then when we are having these data types, we could print the first variable, and we could use, for example, my first pointer and Dell. Okay. This way, we were able to let me just run the code. Okay. This way, we were able to access an integer pointer using integer variable using an integer pointer. So basically, we are defining three pointers here to access three different data types. So here I printed out the value for the first pointer. Then we could do the same, print the second variable it is my second pointer and they'll then print the third variable my third pointer. Okay? So basically, in order to access three variables of different data types, we had to initialize three different pointers of the same type of the variables. So my first pointer is pointing at the integer variable A. My second float pointer is pointing at the float variable B. My first character pointer is pointing at the variable C. And here I am printing the values of these variables using their corresponding pointers, okay? So when I run, I see the result. See the result here. So first variable is ten, then 5.5 and then Z as we want it. However, in order to have a more efficient code, we could declare a void pointer, so we don't know the type apriori, so we just declare it. First, we assign the value, firstly assign the address of the variable A to this void pointer. And then when we would like to access the variable A, we need to do the type casting and we learned it before. So we use the static cast keyword, the target data type. Initially, the pointer is void star, and now it should be interstr because now it is going to access an integer variable. It is needed to be converted to an integer pointer, and this is the target variable which is here a pointer, and then dereferencing the whole parenthesis in order to access the variable A. Then we are storing the address of the variable B into the PTR, the pointer. So this is a float type. So when we are using the typecasting, we simply write down the keyword and then we are having the float star because initially it was a void pointer, and now it is going to be a float pointer, and this is a pointer because we're doing the typecasting the name over the pointer. So the name of the pointer is here, then we are dereferencing the whole parenthesis because we would like to access the variable B. Value, and then we do the same thing for the variable C, we do the typecasting to care and we're printing. So let me run. Okay. So we see here we are having ten, 5.5 and Z. However, instead of having three pointers, we are having only one void pointer. Is a very efficient way to manage the memory because one pointer is going to get less amount of the memory as compared to three pointers. So we see that the practical usage of a void pointer is very very respected and very useful and fruitful if we are not knowing that if we would like to have a pointer for different data types or we are having a pointer initialize and we don't know that it is going to point only the integers or integers and floats and doubles and characters. So in this situation, we are going to have a void pointer to access simply all the variables that you would like using a void pointer. Thank you for watching. 46. 19 type casting 5 void pointers: Okay. Hey, everyone. Welcome to another coding exercise. Today we're going to write a program that uses a void pointer in a separated function to do the typecasting for us. Okay? So we are having one main function and one print value function here. We start from here. We are initializing three variables, A, Y, and Z. A is of integer type float. Y is of type float, and then Z is of type character. Okay? And we are simply calling the print value function. We are passing the address of our variables and one keyword. And let's go up and see the function. The name of the function is print value. It's of type void, so it is not going to return anything. And as arguments, it is taking two arguments separated by a comma. The first part is a void pointer, and the second part is a type that is of a character. Basically in the first call, we are sending the address of the variable X. So it is getting the variable X. When the first call is done for the function, it is going to store the address of the variable X in a void pointer, and then it is getting the character I, meaning that it's of integer type. Here we are having a switch function. It's a conditional function that says that if the type that is passed, which is now which is of type character is passed, if it's I do something, if it's F, do something else and if it's C, do something else. So here we are having the switch over the type, and if the type in case that the type is I, it means that it's an integer. It's a keyword that we define ourselves. Then we are saying that, if I is passed, print out the integer value and then do the typecasting to integer because X is integer here we are sending the address to a void pointer, and in order to access the value of this variable X, we need to do typecasting from a void type to an integer type. So we send this I message to the function saying that if it is I, it means that it's an integer, and then you need to do the conversion from a float type, sorry, a void type, avoid type pointer to an integer because the target variable is integer. So we use simply use the a keyword here, we are having the target type, which is integer pointer, and this is the pointer here we are having because this part is this parenthesis is going to have is going to have the target variable. In our case, it's a pointer, and then we are dereferencing the whole parenthesis in order to access the variable here. So the first call, it is going to convert the pointer from a void pointer to an integer type, and it is going to print the value of X and then the execution is finished because there is a break afterwards. Then we are having the second call. We are putting the address of the variable Y into it into the void pointer as the first argument, and as the second argument, we are sending the keyword F. Keyword F means here float. And we are saying in case it is F, the second argument that is passed is F. Go and do the conversion from the void type for the pointer to a flow type for the pointer and use the static cast keyboard and then dereference the whole parenthesis in order to access the value of the variable Y, then we break, and the second execution of this function is finished. Then the third execution is going to be after the third call. Passing the address of the variable Z as the first argument for the void pointer, and for the second part, we're sending the C character, and in case that it is of C, we mean that it's a character, so the program is going to do the typecasting from the void pointer to a character pointer and then D reference the full parenthesis in order to have access to the value stored in Z. Let me run the code. Okay. So here, we see that we are having the initialization. We are calling the function for the first time. The address of X is passed as an argument to the void pointer, and then we are sending the I as it is an integer type. So we are having a switch condition here. Previously, we learned from the beginner course that switch is when we are having different cases, it is efficient to use switch condition rather than IL condition. And we are saying that basically switch, if we would like to review it basically in the switch condition, we are having different cases case, for example, one, two, and three. And after each case, we put the condition and then column, and we are going to execute whatever we would like to do. And we need to do we must put the break afterwards. So basically here, we're saying that it's of I. So it says that in case that the character type because the switch condition is type. If the type is I, enter this block of code and convert the void pointer to an integer pointer and access the variable X value. Then if it is F, come here and if it is C, come here. So we see that basically the void pointer could access all the data types with different types. Easily. So this is a very good way to practice void pointers. Please go ahead and do the exercises yourself because they may look very easy, but you need to make errors to see where your weaknesses are and improve from there. Thank you for watching. 47. 1 Strings: Hello, Evon. Welcome to this new section of our course. In this section, we're going to discuss about the strings. Firstly, there are very important functions, predefined functions in C plus plus to deal with strings that we are going to practice coding exercises and review some of the most important of these functions, and then we are going to dive deeper into the concept of string. So stay tuned. And please note that when we would like to work with strings in C plus plus, we need to include this header in the above of the code. So this header is important to be included when we would like to work with the strings. Stay tuned, we're going to go through the different string functions one by one in this section. Thank you for watching. 48. String 1 length: Hello, Evan. Welcome to the first lecture about strings. First, we would like to discuss what the string is in programming. So a string in programming is a data type. You see it here, it's a reserved keyword. It's a data type used to represent a sequence of characters or letters, numbers, symbols, or spaces. In simpler terms, a string is like a sentence or vort. For example, this hello wort here is a string because it contains a sequence of characters, and they are enclosed in double codes. So in order to initialize a string, we need the keyword string. It's the data type. It's the name of the string here, it's STR. In the double code, we are having our string here. So in order to be able to have the length or the size of our string, we can use the pre defined length function of the C plus plus. So using the name of the string, then point and the length with empty parenthesis, it is going to give us the size or length of this string. Let us run. So it is saying that the length is 13, but how? So this is one, two, three, four, five, six, seven, eight, nine, ten, 11, 12, and then 13. So as you see here, everything between this double code is counted. Even the space here is counted, even the comma here is counted, even the mark here is counted. So everything that is between these two double code is going to be counted. As to be the length of the string. So I would say my string, and then I'm telling you za. So this is our new string. And when the length of the second string is then we are having STR or sorry, my string that length and. So this is not. So now I initialize a new string type that is called M string, and in the double code it's za. So in order to have the length, I'm just putting the name of the string, point the length with empty parenthesis function. So it's redefined in Ciplus plus and it's going to count it. So the length of the second string is four because one, two, three, four, we have four letters here or spaces or numbers here, so the length is four. If we had one number here, it should have been five. Now you see the length of the second string is five. So basically, this length function is going to give us the size or the length of the string very easily. Thank you very much. 49. String 2 access characters: B Hello, everyone. Now we would like to access different characters in a string. So we have one string that is called STR and the string is hello. In order to have access to characters, first, we need to know their index. So basically, this string is having one, two, three, four, five characters or the length is five. But the indexing in C plus plus starts from zero. So the first character index is zero. Then this is the index. This is two, this is three, and this is the fourth index. So it is having five characters, but they are starting from zero. One, two, three, and four, starting from zero and goes to four. So in order to access the first character of this string, I mean H because it starts from zero, we are simply using the name of the string, and then we are having the brackets. And within the brackets, we simply type down the element that we would like it to print us. So they would like to print the first element, first character. And the first this is the index of the first uh, character. So the first character index zero is H, or we can use a predefined function in C plus plus that is at and within the parenthesis, we are saying the number of the index that we would like to be printed. So this one is basically one. So here we are having the index zero equals index one equals E, et cetera until the end. Okay? So when we're saying that STR and within the brackets, we're typing down the zero. It is going to print H and then when we are having at library function, and then in the parenthesis we are having one, it is going to go for the index one, which is E here. So let me run Okay. So basically here, we see that we are accessing the characters of a string using these predefined functions. 50. String 3 append: Hello, von. Welcome to another video. In today's video, we're going to learn how to use the append function in strings. So we are having one string. Hello, and the name is STR in order to append one word. At the end of the first string, we can simply say str dot append and within the parenthesis, we write whatever that we would like to be appended to the end at the end of the original string. So because it ends with O and there is no space, we put a comma, then one space, and then we type down the word so let me just run this part. It is saying that originally the string was hello. We appended this ama word part to it. So if I start right away with word, you see that there is no space between them. You see hello word, there is no space between them. So we put one space here. Now, there is one space here, and we put one ama. And now it is going to print hello camo and then word. So basically, using this append function, we can append whatever we want at the end of the original string, and then we were printing it. However, there is another way to do it. Another way to do it to type down STR plus equal, and then in the double code, we type down whatever we want. So basically, original string was hello. Then here in this line, we added coma and ort. So it is hello comma space, ort. Now, after the execution of the line, our string looks like this. So this is string. Now, so when we print it here in this line, we see, then we are having the how are you at the end of it. So basically, here we are adding one space, how are you. You see, basically using this code, we are adding the how are you, starting with the space, starting with the space at the end of our updated string. So let me run Great. When we run it, we see that first, the hello word is printed using this line here, so this part, and then we are having the how are you part added to it. So hello word, how are you? So in order to append something to the original string, we can either use append or plus equal. And within the double code, we add whatever we want. It will be added to the string to the updated or original string here. Thank you for watching. 51. String 4 insert: Welcome back. Now we would like to use the insert function in C plus plus four strings. Initially, we initialized one string called STR, and it is hello. We would like to insert one string to the initial original string. And when we call the insert function within parenthesis, there is one common. So there are two arguments to be mentioned here. First, the position that we would like to insert and second, thing that we would like it to be inserted. When I put five, you see here, one, two, three, four, five, there are six elements here and we insert it, it starts from zero, one, two, three, four and five. When we are having this part inserted at the fifth character and then we're having this. Let me run. Okay. So the original value the original string was hello with one exclamation mark. However, this part zero, one, two, three, four, five. So this part was the fifth car index of the original string. So when we are saying insert at five, it is now removing this mark here and inserting this coma here. So this part is inserted from the fifth index, and the fifth index is this mark, so this mark is now removed and is replaced by coma board, which is our sage here. If I type down like six, it is going to keep the first mark here. Okay. You see? So this is inserted at the place six. So this exclamation mark is still there, it is still here. Then from the sixth character, which is after that, whatever we are putting in the double code here is going to be inserted in the original array. But if I say, for example, like two here and run you see? So 01, and this is the index one. Index two is this L. Okay? As of there, so the zero index and the index one is printed here, which are H and E. Then whatever we put here is going to be inserted as of the second character. So the rest is now Kama and art, and then the rest of the original character is now being printed in the end. So basically, now this ama space ort is inserted as to be the second character. And when it is finished, the rest of the original is now being printed. So basically, let me put a space here. It is basically meaning that this part can be inserted anywhere in the original string depending on where we put the position, but it is not going to remove it, but it is going to simply insert in that position and the rest of the a string is going to be printed afterwards. You see here. If I mention like insert at one, it is going to print H first, and then what we are inserting coma space ord space and then ELL O with the mark here. If I say zero, it is going to first print what we are having here. So what we're having comma space word space is now printed, then hello is printed. Basically, whatever we would like it to be, we choose the position that we would like to insert it. In the original part, we wanted to insert it at the Index five. Index five was this mark. So this mark was here initially. Let me see how SDR Okay. So let me just show the original. Yeah, so the original is hello. But when we are inserting the comma space word space in the end, we are in the fifth index, it is putting this part. This is exactly this part, and then we are having the mark in the end. So using this function, we can insert whatever we want in string in the original string wherever we want. Choosing the a position before the cama and after the cama, in the double code, we put whatever we on. So let me change the example. So my last name is Shahin, for example, and here we are having zero, one, two, three, and then I would add it to four, and I have one space here. So you see the original string is reza that is printed using this line here, and the modified string is Shahin, which is printed, which is inserted at the fourth position of the original string. Thank you for watching. 52. String 5 erase: Hello, everyone. Welcome back. In today's lesson, we're going to use the erase function. So the original string is hello cama word with the mark in the end, and we would like to erase it. So erase is going to simply called the function erase, and within parenthesis, we need to have two arguments. First one is the position and the second one is the length of the erase. So the position that we would like to start the erase is position five, and we would like to remove seven characters starting from position five. So let me find the position five. It's zero, one, two, three, four, five. So as of this part, it is the position five. This is a position five. As of this part, remove seven characters means that one, two, three, four, five, six, seven. So the highlighted part, there are seven characters in the highlighted part as of the position five. So it is going to remove this part and then print the STR for us. Okay, great. So now it is printing the highlighted part as of position five and seven characters are being removed from position five. So basically, we simply can shorten and remove whatever we want from our string. And you see the last character is not removed and still is there because we were removing only seven characters. Or if we remove eight characters, this mark is also will be removed. You see here. Okay, so the last mark is also removed here. Thank you for watching. 53. String 6 replace: Hello, van. In today's video, we are going to use the replace function. So we have one string that is hello ort. Using the replace function, there are three arguments to discuss. So we are going to replace five characters, which is the middle argument, starting at position seven. This is the first argument and the third argument is going to be what we are going to replace. So starting from position seven, replace five characters, and this is the character to be replaced. Let me run. So you see here, starting from the position seven, position seven is here, zero, one, two, three, four, five, seven, starting from seven here, go and replace it. So we're going to have this part, but this part is going to be replaced with five characters, replace five characters. So one, two, three, four, five, so five characters are here. So these five characters are going to be replaced with what with the universe, which is the first argument here, that is universe. So in order to wrap up, we're going to start at position seven, and we're going to replace five characters and what we're going to replace this part, the third argument. So using the replace function in strings, thank you for watching. 54. String 7 substring: Hi, welcome back. Now we would like to discuss substring. So if we have one original string and we would like to form a new string from the existing string, we need to use this sub STR library function in C plus plus. So we initialize one string here. Let me just change the name for the sake of simplicity, my string. And let me revise this part and this part. Okay. So Maya string is here and My string two. Let me just revise the name here as well. Okay, so Maya string is the original string that is saying hello work. And we would like to form the new string, which we call here, May string two. In order to do that, we would like to form the second string from some part of the first string. To do that, we have sub STR function. There are two arguments here. So basically, in these two arguments, there are two numbers and they mean that extract five characters starting from position seven. So position seven is here. Let me find it zero, one, two, three, four, five, seven. So starting at position seven, take five characters. That will be one, two, three, four, five. So the ort is going to be extracted from the first string, and it's going to make a copy in the second string. But the first string remains the same. We are not modifying the first string, but only taking some part of the first string to be copied in the second string. So first, we are printing out the second string to see how it works. Yes, great. So we see that this word part is pasted, copied and pasted in our second string. So we made a new string. We formed a new string from an original string. And if we print the original string here as well, the original string, we see that the original string remains the same. So basically, we declared and initialized one string. We took some part of it, we pasted some part of it in a new string. Starting from position seven, taking five characters and using this library function, sub STR, we copy and paste this part of the first string into the second string. Then we printed out the second string, and we also printed out the first string to show that the first string is not changed at all. So here you see the substring is ored substring is word. This is exactly what we wanted, and the original string remains the same hello word, and we have the original string here. Thank you for watching. 55. String 8 clearing: And everyone. Welcome back. Now we would like to learn about the clear function in string. So we have one string called STR here and the string is hello. Before doing anything, we are printing this string here, STR and we expect to print hello. However, we can make this screen clear and empty and like it has nothing in it using a clear function in empty parenthesis. Then after cleaning, we see that the string is empty. Let me run. Okay, so here we see that before cleaning, it is hello. We were printing it in here, and then we are clearing the string. And then we're saying after printing, we have nothing here because after this part, we are making the string clear. So using the clear function, we can make the string clear and empty. Thank you for watching. 56. String 9 reverse: Hello, everyone. In this video, we're going to learn how to reverse one string. So we're initializing the string here. It is of name string, STR, and it's of type string, of course, and the string is hello. We would like to reverse it using the reverse predefined function. There are two arguments in this parenthesis. We simply type down the name of the string, which is STR dot begin and empty parenthesis and CR empty parenthesis. So in these two arguments, we are basically in the first part, saying that the start of the reverse and the end of the rivers. So we would like to reverse the full string. We are saying that start from the beginning of the string and end at the end of the string. Using this, we are going to reverse everything in the string. Let me run Okay. So here we see that hello is reversed and O and H have changed their position, and then this E and L have changed also their position, and this part remains the same because it's in the middle point. So simply with a reverse predefined function in C plus plus, we can define the beginning of the reverse and the end of the reverse that we are aiming to have and then we simply put the name of the string point or dot begin empty parenthesis and CR dot and parenthesis and empty parenthesis. So this way, we are calling the reverse function. Basically, the reverse function is something like this before the coma is the beginning of the reverse and after the comma, it's the end of the reverse depending on what we want to do with the string. But if we want to reverse the full string, we're starting from the beginning like here you type the name of the string, then begin parenthesis, and after the comma, we say the name of the string and end empty parenthesis, and that's it. Thank you for watching. 57. 10 string sorting: Hello, everyone. Welcome back. In today's lecture, we would like to discuss how to sort a string. Before doing that, we need to include one header. Please note that if you're working with the sorting of a string, not only you need to include the string header, but also you need to include the algorithm header in the beginning of your code. So let's dive in. So we would like to sort a string. The string is programming. Okay. Now when we want to sort it, we simply use the sort predefined function as here, and there are two arguments, one B for comma then one after comma. The B for comma is the beginning part of the sorting and after comma is the end of the sorting. Basically, because I would like to sort all the string and the fullest string, I would like to sort it. I type down the name of the string. Then then begin and parenthesis and after the comma, I would like to because I would like to sort the full string, I would say the name which is STR dot and parenthesis. So basically, what it's going to do it in ascending order, it is going to sort the string for us. So as you see here, now when we are printing the string, it is sorted, starting from A, then the alphabetically speaking, it is sorting the string programming for us. This sorting is very important, specifically when we are going to work with an array of strings that we are going to discover later on on this section, and we can see some practical examples of using sorting of strings in an array of string, stay tuned. 58. 11 array of string 1: Hello van, welcome to another coding exercise. So now we would like to combine the concept of array and strings, and we would like to learn how to have an array of strings. So we would like to write a program that considers having an array of a string and prints its element. To do that, we first have an array of a string called fruit. And within the braces, we can simply put down the number of elements or we can leave it empty. It's optional, and it is a type of string, and we have the brackets here. In the brackets, there are the elements. And each element because it's array of a string, each element is in double code. So the first element is Apple, then with the coma, it is separated from the second element, the third element, and the fourth element. In order to compute the size of this array of string, we learned from before this formula, the size of the full array of string divided by the size of the first element. So the full is that each one of these, for example, are taking four bytes. And when we have four elements, the size of fruits equals 16, multiplied by four because each are having four bytes and there are four elements, so 16. When 16 is divided by the size of the first element, which is four already. So the size of this array of string is four, and as you see, there are four elements here. So this way, using this formula, we can compute the size. I will say it one more time. Each element is taking, for example, four bytes of memory. So we have four elements here, each taking four bytes, meaning that the size of the array of a string in total will be four times four because there are four elements. Each of them are taking four bytes. So four times four will be 16. This part will be 16, and then it is divided by the size of the first element. So this is the fruit array of a string, and this index is discussing the first element, which is apple. So the first element, we said that each element is taking, for example, four bytes, and now 16/4 equals four. The size of this area of a string is four, and we can verify it by counting the number of elements. Yes, there are four elements here. So we computed the size. And now we would like to print this array of a string. So we say fruits in the array, and we have a four loop starting from zero as long as I is lower than size, and we're printing all the elements of the array of string fruits using I index. So I equals zero, it is going to print apple. When I equals one, it is going to print banana, I equals two, cherry, I equals three, it is going to print the dates. So let me type it down. I equals zero. Print apple, I equals one, print banana. I equals two, print cherry, and finally, I equals three print date. So this way, we're printing all the elements of this area of a string. Let me run. Okay, great. So we see that all the elements of this array of strings are printed like apple, banana, cherry, and date. So this is how we declare and initialize an array of strings. Previously, we were working with arrays, normal arrays that were containing numbers and valid variables like integers or floats. Now we are having sorry, now we are having an array of strings that are holding different strings or names or let's say, sequences of characters. And we declared them, we computed the size, and we printed them all of the elements one by one using for. Thank you for watching. 59. 12 array of string 2: Hello, everyone. Welcome to another coding exercise. So now we would like to write a program that sorts an array of a string. So this is our array. It's banana, apple, cherry, and date, and then we have the size of the array computed here. Using the sort function in C plus plus, we're going to sort the array of a string in ascending or alphabetical order. So basically, sort function, we practiced this previously in previous lectures in this section. Firstly, we are going to tell the name of the area of a string that we would like to perform the sorting over. And then we are giving the range within which we would like to the sorting function, perform the sorting. So we are saying that basically with this word, we are saying start from the first element of the word, something like this. And go until the end of this array of strings. N equals the size of the array. Basically, we are telling the sort function to sort elements from the first element until the last element. This is how we are telling the C plus plus compiler to understand what we mean. So this words here is talking about the first element, starting from the beginning, like here, until the end, like here, this plus N means until the end here. N is the size of the array. So basically, what it means to go for the index zero, one, two, and three, but N is four. What we are saying this equation here is means that go until four but not include four. This is why we have parenthesis here, but in the beginning, we have the braces. This includes the start, but it does not include the end. When we are saying plus N, N equals four, meaning that zero is this one and goes up until it is lower than the size of the array, which are including the first element, second element, third element, and the fourth element. So let me just compile and see Okay. So here, original array of the string is having the banana apple cherry date. But when it is sorted, we are having the apple first, based on the alphabetical order, then banana, then cherry, and then date. So it is now sorted using the sort function. And in the sort function, there are two arguments. The first argument is the name of the array of the string, and the second argument is the range that we would like to perform the sorting function. So the range we are basically here saying start from the beginning. When we are saying the words, it means start from the beginning and include plus N, which is the size of the array of the string, and it's not going to include the last element of the because N is four when it is saying that start from the zero, plus four means that go three times. It is not including the last N. N is going to iterate only three times, so it is going to first this is the element, then one, two, and three. And then we are having the sorting over all of the elements of this array of string. Then here, we're going to print everything of the array as we saw before. We have a four loop starting from zero. It goes until I is lower than N. When I is lower than N, it means that I is going to take zero, one, two, and three. Is zero, one, two, and three. So it is basically printing all the elements that are there. Okay. To be clear, let me give you give you another example. So I changed this string now. Now, it starts with banana and apple, then date then cherry. So the place of date and cherry are changed. Now we would like to sort this. So in order to sort it, we call the array of the string, and we have starting from the zero, the first element, and go four and not N, which is two, which is four. And we are just putting two numbers here because N equals four here, but I'm not putting N, and I'm just putting two to see how it works. When I compile, as you see here, it is only sorting the first two elements and not all the elements. You see the date and cherry are not in ascending order or alphabetical order. So when you put number two here, it is basically going to sort only the first two elements, meaning that the element that are having the index zero and one, the first two elements, we are going to put two. But if I put three here, for example, still among these three, among these three is considering these three and among these three, the sorting will be apple, banana, and date because D comes after B. So when we put three, it is going to sort the first three elements only and it is not going to go through the fourth element. When it's three it is going to consider element zero in the index zero, second element with the index one and the third element with the index two. But basically, if we want to have the full sorting of the full array, there are four elements. We're going to have four elements here and we're going to go for the indexes 0-3. So when I compile, exactly you see here that the first four elements are now sorted. So we have apple, banana, cherry and date in the end. And instead of these four, we can simply when we want to sort the full array, we can just put the, which is the size, which is also equal to four, but it's more professional to write the size here rather than directly putting the number there, so I compile again and we have a consistent result as you see here. So this way, we learned how to declare initialize one array of string and also get the size and sort it until wherever we want. So if you would like to sort the first two elements, just put instead of and put the first three elements, put the three and the first four elements or all the elements, just put the sides. This is how it works. Thank you for watching. 60. 13 array of string 3: Hello Ivan. Welcome to another coding exercise session. We would like to write a program that modifies an array of string. So we have our string here, and we are directly mentioning that there are three elements here with Index zero, one and two. So element with index zero is the first element, New York. The second element with index one is Paris and the third element with index two is Tokyo. So we would like to modify the second element to modify the second element, we're going to use the Index one. So cities, it's the name of the ara of the string, and we're putting the number one here. Meaning that we would like to discuss the first and modify the second element of this array of a string with the index one, and we are willing to replace it, replace Paris with London. So after that, we would like to print the array. In order to print the array, we see out the updated list of cities, and we have a four loop starting from zero, until I is lower than three, so it is going to take I equals zero, one and two, and it is going to print the IF element of the array cities, which means that in the first iteration, New York, then updated element, which is London and then Tokyo. Let me run. Okay, great. So we learned how to modify directly one element. In order to modify that. We type the name of the array of our string within the braces. We type the index that we would like to change. In this case, we wanted to change the second element with the index one, and we replace it with London, and then we simply print out all the elements. So we see the updated array of a string here. So the first one is New York. The second one is London, not Paris anymore, and the third one is Tokyo. Thank you for watching. 61. 1 vectors 1: Hi, everyone. Welcome to this section. In this section, we're going to discuss vectors. So first of all, we would like to mention Y vectors. Vctors are like arrays, but they are more flexible. So previously, we learned how to initialize one array. For example, here, we're initializing an array the name is numbers, and it's of integer type, and it has five elements, and we see the elements here. This is a normal array, we learned also how to declare and initialize a dynamic array. To do that, we get the size from the user, and dynamically, we're initializing one array here, customized to the usage of the user. So the user, if the user wants an array with eight with the size of eight, we are giving it an array with the size of eight because this size that is that we are getting from the user is the size that we are making the array with. So if another user is going to use our program, they want the size of five, we're initializing a size of five. However, the thing is that initially, we know that we need to settle down with one size for arrays. What if we initialize the array with the size of five and later on we realize that we would like to add one more and we would like to add another more, et cetera. So this way, using vectors, it's much easier. Modifications and flexibility of vectors are much better because in arrays, we cannot modify the size of an array once it is initialized. When it is initialized, for example, this example to have an array of size five, later on, we cannot modify its size to seven, eight, ten, et cetera. Also here, when one array is initialized, we cannot modify its size. However, vectors give us this permission to have initial size for it. And dynamically, after some time, if we want, we can increase its size. It's a very useful way of working with numbers or it's a flexible way of arrays. Vctors give us this flexibility to be able to increase the size or reduce its size as we are working with it. So if any changes required, we can do it with vectors. However, with arrays, the size are fixed. This is why vectors are important. In this section, we are going to discuss vectors and provide different examples and exercises, practical coding exercises to see how flexible vectors are. Thank you for watching. 62. Vector 2 sum of all numbers 12: Hello, Ivan. Welcome back. In today's video, we would like to write a program to calculate the sum of all numbers in a vector. We are having a vector of integer type called numbers. Then this vector is having five elements, one, two, three, four, five, and we are having a variable called sum. It's of integer data type, and it is initialized to zero. Have the calculation of sum of all numbers, we're going to iterate first over all the elements, and then each element is going to be added to this sum variable. For example, here I'm having a four loop. I'm having my iterating variable called num, and we're going to iterate through this numbers vector. In each iteration, this num is going to be added to the sum. So this equation here, plus equal num is exactly as this equation, sum equals sum plus num. Okay? So in the first, I would say like this, first iteration, sum equals zero plus one. Why? Because sum initialized to be zero, and num is the first element of the vector numbers because we are iterating over the elements of this vector, and in the first iteration, we are going to have our iterating variable num equal to the first element of the vector. Therefore, num equals zero. Thus sum equals one, okay? In the second iteration, we are having some equal one plus two. Why do we have that? Because we're having S plus num. So sum sorry. So S plus num, S equals to one because in the first iteration, we updated the value of sum, so it's not zero anymore. It's one, and Y two, because the num is iterating for the second round through the numbers vector and the second element of this vector equals two. Therefore, sum equals three. We have the third iteration. Sum equals three plus three. And the first three is the value of sum, which we computed in the last iteration has to be three. And this three is the num, which is the element of the numbers vector because it's the third iteration, we are going to take the third number. Therefore sum equals six. In the fourth iteration, we are having some equal six plus four sum equals ten, y six plus four because the sum from previous iteration equals six, and four is the fourth element of this vector. And finally, in the fifth iteration, we are having sum equal ten plus five. Therefore, sum equals 15. Why ten, ten is the value of sum in the previous iteration, and five is the last element in the fifth iteration. So the fifth element of the vector called numbers. So we expect to have the sum of all numbers in this vector to be equal to 15. And finally, after the calculation is done in this flop, we are going to show up the output. So we say this message in the output, the sum of numbers is, and then we print the sum variable. So when I run this code, we see this message. So the sum, the sum of the numbers is 15. So if I have, for example, this element to be 30, for example, when I run, we see the sum must be increased. So as you see, the sum is now 42. So depending on the elements of our vectors, the sum of all numbers in a vector, of course, is going to change. Thank you for watching. 63. Vector 3 access elements: Hello, everyone. Welcome back. So now we would like to write a program that declares a vector of string and print the first two elements of it. So here we are having a vector of type string, and the name of the vector is M vector, and there are three elements in the vector. So this vector is having three index zero, one and two. Index zero is for the first element. Index one is for the second element, and Index two is for the third element. Now we would like to print two elements of it directly. So we can simply do it like the first name is and we name the vector here. In the breezes, we are going to write the index of the first element, which is zero in C plus plus, we start from zero. This one is zero, then one and two, the indexes. Second one, we name the vector, and in the braces, we're going to simply put the index of the second name which is Bob, which index is whose index is one. Basically, using this code, we can access the elements of this vector of a string. So the first name is Alice, the second name is Bob. Here we learned how to access each elements of a vector. Thank you for watching. 64. Vector 4 size: Hello, Evan. Welcome back. In today's video, we would like to write a program that prints the size of a vector. So we have a vector of integers. Its name is Vtor my Vctor and it is having four elements, five, ten, 15, and 20. So we would like to display the size of the vector. To do that, we have the name of the Vctor that size with empty parentheses, and we're printing the value. So let me just comment this part first. So when we run, we expect to see four. Okay. So here, my vector size in empty parenthesis, we will just print out the number of elements of a vector. And in order to do some review, we can use the name of the vector dot pushback and within parenthesis, the value of the value that we would like to be inserted in the vector. So now I would like to insert 100 to be a new element of this vector. So I use the pushback dot pushback, and in the parenthesis, I type down 100. So now if I run, we will see that the new vector size is five. Why? Because one element is inserted to the original vector. So we learned how to compute the size of a vector. We reviewed how to push back one number to the vector. And again, we see that when a new number is pushed back to the vector, the size has increased 4-5. Now, in order to print the new vector, we can have a new integer called Num that iterates over my vector. Iterates simply over the vector, and we have the num here. We have one space, and then we have the and basically, I defined a for lo. We first initialize, we declare one integer called Num. It is going to iterate over the M vector, my vector vector. So it's a vector that the name of the vector is my vector. So it is iterating over all the elements and it is printing the elements simply. And we expect to see this new number that is inserted in the vector in this print because this is the original vector, but we modified it. So let's run. Okay, great. So now we see that the original vector was 510, 15, 20 until here, and we inserted one number 100 here with the pushback syntax. And now we see that the new Vctor is having five elements in it, and 100 is inserted as the fifth element. Thank you for watching. 65. Vector 5 modify elements: Hello, everyone. Welcome back. Now we would like to declare we would like to write a program that declares a vector and modifies the second element of it. Okay? So we would like to write a program that declares a vector and modifies the second element of it. So we have a vector of integer type. The name of the vector is numbers and it has three values. So we would like to modify the vector called numbers and the element index that has the index of one. We know that the index of one is for the second element because for the first element, the index is zero. So we're putting ten to be the second element of the vector numbers. And now we are displaying the updated vector using a for loop. Here we are printing the num. Let me run. Okay. Great. So basically, we see here the original vector was one, two, three. Then we modified one element, and now we're having 1103 as to be the new and updated vector. So this is how we can modify elements of an array directly. We could modify also the third element or the first element. It depends on what we want to do, but this is how we can basically use a syntax to do that. Thank you for watching. 66. Vectors 6 popback: Hello, Evan. So we would like to write a program that removes the last element of a vector. We have a vector of type integer called numbers, and there are three elements 100, 203 hundred. In order to remove the last element, we can use the name of the vector dot Pop back this in empty parenthesis, and this syntax is going to remove the last element of the vector, which is 300 here. And then we are going to display the vector. Again, here, let me run. Okay, so we see that the vector after removing the last element is 102 hundred. So now 300 has been removed. So if we would like to add, push back like ten numbers that push push back 12. So now we are push back in two numbers to the vector. And basically, let me show the message. After adding two numbers, here also, we are having this. So basically, we had the code until here, we analyzed it. So it is going to using this line, it is going to remove the last element, and we're going to display the updated vector that was 102 hundred. We saw it. Now I'm going to add two more numbers to the vector. So after 200, now we expect to have ten and 12. And then we are showing the vector. So after 200, we need to have two new elements. Okay, so vector after removing the last element is 102 hundred. And after adding two numbers, now the vector is having the size of four, so 100, 200, ten and 12 using pushback here. So this is exactly the flexibility that I was talking about in the beginning of this section. In arrays, we cannot have this flexibility. When the size is declared, it is declared and it is going to be fixed. We cannot modify it. However, we can see that in here in vectors, we can modify the size easily we can extend it, we can reduce the size, et cetera. So this is a very good exercise of vectors that give us this flexibility. Thank you for watching. 67. Vectors 7 clear: Hello, everyone. Welcome back. Now we would like to write a program that clears a vector. In order to clear a vector, we first need to declare a vector, so we have a vector of integer. It's called numbers, and it's 714 and 25. So there are three elements in it. In order to clean a vector, we simply are going to use one function that is called clear. Using numbers, which is the name of the vector, clear in empty parenthesis, we're going to remove all the elements of the vector. And when we run, we see that the numbers that size of the function to give us the size of the vector when we type it down, it says that there are zero elements in the vector. This is how vectors are This is how much vectors are flexible. And as you see here, we removed everything in the vector and the size is reduced to zero. This is the flexibility of vectors that we don't have them in arrays. 68. Coding exercise vectors 8: Hello, Evan. We would like to write a program that collects and calculates the average score of students in an exam. To do that, we first declare a vector of type integer named scores, and we have one integer called score and one integer card number of students, so num students. So first, we are showing the user a message saying that give me the number of students. So when it gives us the number of a student, we are going to have a four loop to read all the student numbers. So I is going to start from zero until it is lower than the number of students. So if the user is going to say that we have three students, we would like to discuss three students. So I is going to be zero, one and two, and as long as I is lower than three, it's okay. So we will have three iterations 0-2, and we are asking the user to give us the score of the first student of the second student and the third student in the first run in each iteration. So just assume that I equals zero, now we're getting the score of the first student. So the user enters, we store it in score, and then we are pushing back this score into the scores vector. So let me type it down. So when I equals zero, getting the score of student one. So the cor so now we're getting the number from the user. Just imagine user enters 100. So 100 is assigned to the integer score. Then this vector is now pushing back push back that score integer or let's say in the first run, it is 100, then I equals one, getting the score of student two user. Imagine user enters 98. Score equals 98 now, so scores vector that pushback it's going to pushb 98. And then I equals two getting the score of student three user enters, imagine 99. So score is now 99. This score is this score, so we're storing it here. Okay, and scores vector pushback is now 99. Basically, we are entering, we're pushing back three elements to the scores vector. And then we would like to collect the scores that we did, and we would like to calculate the average. To do that, we have one integer called sum that is initialized to zero, and we have a four loop like we have an integer called S. It is iterating over all the elements of the SCRs vector, okay? And then it is going to add all the elements of this vector to each other. S plus equal S, this simply means sum equals sum plus S. So sum initially is zero. So when in the first iteration, we have some equal zero plus the first number, which is 100. Then in the second iteration, we have some equal. So from the previous run, we had sum equal 100. Now it is 100 plus the second score, which is 28, 98, sorry. This is what we imagine that the user mentioned. And in the third iteration, because there are only three elements in the vector, so they really only three iteration. So sum equals 198, which is from the output of the previous run plus 99, okay? So when we do the math, it should be equal to 297. And then when this four loop is finished, so we have calculated the number, the sum of the scores, which is 297. Then we have a double initialized here called average, and it is typecasting the value of the sum to be a double rather than an integer, because we want to divide it to the size of the the size of the vector and it may have some decimals. So we typecast the sum to double from int, and then it is divided by the size of the vector, which is the total number of students. So it could also be divided to these num students because this is exactly the number the number of students equals the size of the vector. So then we are printing the average. Let me run so basically, it says, Enter the number of students. We are here. I will write three. So it says that it enters this part and it is saying that enter the score for the first student student zero, we have here I plus one because it's the first student having the index zero, we would we don't want to show the student zero here. We would like to show student one. So I plus one, showing that the index plus one, zero plus one, because it starts from zero. So the first student, we say 100, and then it says the second student, I would say 98, the third student, I would say 99, and it says the average is 99. So we are calculating the sum here and then the average it's a very basic example. It's a very good coding exercise, however, because it reviews many things in this small example. We reviewed typecasting. We reviewed a for loop and using the summation to have all the elements of a vector, we reviewed how to iterate over a vector and we reviewed the pushback syntax function for the vectors here. Thank you for watching. 69. Vectors exercise 9: Hi, everyone. Welcome to another coding exercise session. We would like to write a program that reads the favorite color of user and display them. So for that because colors there are names and there are a sequence of letters and characters. We need to have a string. To do that, we have a vector of type string, colors, that is named colors, and we have a string named color, and we have integer that is N. So we first ask the user to tell us how many favorite colors do you have? And for example, the user says two, three, et cetera, we are going to store it in here using C in function. And then we are going to ask the user one by one to give us the color that you would like to insert. So we are asking you to enter your favorite color, and we have four loop starting from zero until I is lower than N, and whatever the user is mentioning. We are saving it, storing it using the Getline syntax to the color, which is a string. And this color is now pushed back to the vector colors, every time. So there are initially, it says that I have three favorite colors, it is going to iterate three times. And then when it iterates three times each time it is asking, enter your favorite color. You say, for example, green, then another one blue, then another one red. And these colors are one by one added to this vector. And then we're simply just printing all the elements. We are having a four loop going through all the elements of the colors vector and we are printing them. So let me just print also d here. So imagine that it says, how many colors, how many favorite colors do you have? I say two, Enter, and then it is asking now enter your favorite color. So when I have two, I is going to get values zero, and I equals one. Okay? So now I say green, and then I say blue. So when I say green, green is pushed back to the colors vector, and then I enter, and then blue, I enter blue. So blue is now pushed back to the colors vector. And then we are displaying your favorite colors and we're going through all the elements, we're iterating over all the elements of the colors vector, and we're printing them. This is very simple, but we learned and we reviewed how to push back, how to read strings from the user, and how to print the strings from a vector of type string. In this example. Thank you for watching. 70. Vectors exercise 10: Hello, Ivan, we would like to write a program that allows the user to input numbers into a vector and check if a specific number exists in the vector or not. So for that, we have a vector of type integer. Is name is numbers, and we have one integer called num and one is the search num. So we ask the user enter the numbers, and we define a VI loop here, as long as it's an infinite VI loop because we have the condition here. True. So when the condition in the parenthesis is true, it means that it's an infinite valop we are going to iteratively get the number, the input from the user and push back that number into the numbers vector until the user enters minus one. If the user enters minus one, it means that I'm not going to enter anything more. So break and get out of this val loop. So it's an infinite loop that we need to break to get out of it. In order to understand when to break, we allow the user enter numbers and type minus one to stop. So the user will stop. So the program, this infinite Val loop will stop when the user is going to enter minus one. Then when the user finished entering all the numbers, the user is going to enter minus one, so we get out of this loop, and we are going to have a vector called numbers, and there are certain numbers in that vector. Done. So then we are asking a user to give us another number to search and see if the new number that the user is entering exists already in the already vector that we formed previously in the previous step. So the user is going to enter one integer number, for example, here, and we're going to store it in the search num and to check to see if this number exists in the vector or not, we need a for loop. But before that, we have a found boolean initialized by default to be false. We have a four loop. Iteratively, we are going through all the elements of the numbers vector, and we say if in each iteration, N is going to iterate over all the elements. So I N at some point, equals to this entered number from the user, the key number, the search number, just put the found equal to be true, which was initially false and then do the break and come out of this four, okay? And finally, we say I found is true, when we have the boolean in the parenthesis, basically by default, it means it is equal to one. So I found is true, meaning that if we enter this four loop, if we enter the if conditions, if found is true, display exists in the list, otherwise, display that it does not exist. So let me run the code and we go and analyze the code one more time. Says Enter numbers, I would say five, four, three, two, one, two, three, four, five, seven. You see, it is an infinite loop. Until I'm entering the number, it is going to take numbers. As long as I'm entering numbers, it's okay. But once I push minus one, it gets out of the loop exactly like we had it here. So when it is minus one, then it is going to get out of this R loop. We were in this Y loop so far. It says, Enter a number to search. I would say seven. It says seven exists in the list. Why it exists Because it checks all the elements and at some point, one of the element that is N equals to this search num, which is seven, and at that point, it is going to set true for the found boolean and break. So get out of this for loop, and it says, I found equals true, in this case, print exists in the list. So let me run one more time, and it says, Okay, one, two, three, and I say minus one, so enter number five, it says five does not exist in the list. So basically what it does here, we are storing all the elements we're reading and storing all the elements from the user. And when the user says minus one, we are getting out of this four loop. When we get out of this for loop, we ask the user to enter a search number. So we have five here. We are looking for five and the elements of the vectors are one, two, three, and we set the found boolean to be equal to false by default, and it says iterate over all the elements of the vector which are one, two, and three. And if each of these elements one, two, and three equal to five while iterating, set the two for the found boolean. And break. Otherwise, do nothing, leave it to be false. And in the end, if this found is true, you're going to see the message exists in the list. But here, it says does not exist in the list. You see, this means that five does not exist search numb, which is five does not exist in the list Y because found is not true. And found is not true because five is not equal to any of the elements of the vector that are one, two, and three. Therefore, it is going to stay to be false until the end. So it was a very practical example to review some stuff from before, from the beginning course and also to know how to read the elements of a vector from the user and how to comparatively check one number with the elements of an array. Thank you for watching. 71. Vector 1 insert and display 11: Hello, Revan. Welcome to this lecture. In today's video, I would like to write a program to input numbers into a vector and display them. Here, I'm having my main function. I'm initializing a vector of integer type called numbers, and I'm having two variables. One is N and one is input. Then I'm going to ask the user by this message, how many numbers do you want to add? So we would like to first ask the user how many numbers that you would like to add? Then we're going to ask the numbers that the user would like to have. For example, I would say, I would like to insert three numbers, and then the three numbers that I would like to insert are five, six and seven. After that, we are going to take the numbers from the user and store them in a vector. And once this process has been done, then we are going to iterate over the vector to display the numbers that are stored in our vector. So I start by showing this message. How many numbers do you want to at. For example, I would say three. Then we're going to take that number from the user, store it using C in in N variable. Then I'm having a four loop here, and my iterating variable is I starting from zero, and it is going to continue as long as it is lower than N, and we are going to enter the loop as long as I is lower than N. Then in each iteration, we are showing this message to the user. Enter a number. Whatever the user is going to enter, we're going to use the CN syntax to store it in the input variable that we initialized above. Then when it is stored in input variable in the very same iteration, we are using the pushback function in vectors to insert this input variable into our numbers vector, okay? And then when this process has been finished, we are going to show the vector by iterating through its elements. Here I'm having a four loop. My iterating variable is called num and NOM is going to iterate over the vector numbers, and in each iteration, we are going to show NAM in the output. So let me illustrate it with an example. For example, imagine that the user says that the person, the user is going to enter three numbers, so N equals three, and numbers for the iteration will be this one. So I equals zero, I equals one, and I equals two. So we're going to have three iterations here for this loop, starting from zero and as long as I is lower than and N equals three. So I equals zero, I equals one, and I equals two are the possible iteration numbers that our iterating variable I is going to take. Then in each iteration, for example, when I equals zero, we are going to ask the user enter a number and the user, for example, is going to enter, for example, five in the first iteration. So we're going to take that five and store it in input, and then we are going to push back input to our vector, to our vector. And R vector is numbers. To use the pushback function, we learned that we typed down the name of our vector dot pushback. Then in parentheses, we type down the variable that we would like to be inserted to our vector. Therefore, or vector. Therefore, our vector is having the value five after the first iteration. Then I equals one. We're going to the input is going to be six, for example, the user is going to say, Okay, the next number is six, then using, again, the pushback we are push backing the variable input to our vector, hoops, this is I. And then in the last iteration, when I equals two, user says that, for example, number seven, we are going to use the pushback for that variable. And then in each iteration, we are going to show the elements of the vector. So when, for example, in the first iteration, num equals five, then num equals six, then num equals seven. So let me run the code, and then we are going to analyze the code and validate our expectation here that we type down in comments. So when I run this code, see this message. How many numbers do you want to add? So how many numbers do you want to add? So this part is being executed. And I would say three, for example. And when I enter three, it is going to be stored in the variable N. So I enter, it says, Enter a number. So I would like to say the first number that I want to have in my vector is number five. It says, Okay, so here we are now, Enter a number. We are going to iterate from I equals to zero until it is lower than three. Then in the first iteration, I entered five, so five is stored in input and input is pushed back to the numbers vector. And the next one, I would say six. The next one, I would say seven. And when the three elements are finished, here we are having the loop that we are looping over the vector, and it is showing this message you entered, and then we're going to see out the value of the num variable, which is iterating variable in each iteration. So here it says, entered. This is this message, five, six, and seven, five, six and seven. This is exactly what we expected to see in the output. We could say, for example, I would like to enter ten numbers or five numbers. For example, how many numbers do you want to enter? I would say five. So it is going to ask me to enter five numbers because now it is going to have the I equals to zero, and as long as I is lower than five, so I equals zero, I equal one, two, three, and four. So we can enter five numbers. So first ten, then 20, then 30, 40, and 50, and then it is going to iterate over the vector and show everything in the output here as you can see. Depending on how many numbers that I would like to enter, I would like to add in the initial choose here, we're going to enter larger or shorter numbers for our vector, and then we're going to show the output. For example, if I want to enter ten numbers here, I mentioned ten, then it is the program is going to ask us for ten numbers to be entered. Then in the output, we are going to have, for example, ten numbers to be printed in the output. Thank you for watching. 72. Vector 2 sum of all numbers 12: Hello, Ivan. Welcome back. In today's video, we would like to write a program to calculate the sum of all numbers in a vector. We are having a vector of integer type called numbers. Then this vector is having five elements, one, two, three, four, five, and we are having a variable called sum. It's of integer data type, and it is initialized to zero. Have the calculation of sum of all numbers, we're going to iterate first over all the elements, and then each element is going to be added to this sum variable. For example, here I'm having a four loop. I'm having my iterating variable called num, and we're going to iterate through this numbers vector. In each iteration, this num is going to be added to the sum. So this equation here, plus equal num is exactly as this equation, sum equals sum plus num. Okay? So in the first, I would say like this, first iteration, sum equals zero plus one. Why? Because sum initialized to be zero, and num is the first element of the vector numbers because we are iterating over the elements of this vector, and in the first iteration, we are going to have our iterating variable num equal to the first element of the vector. Therefore, num equals zero. Thus sum equals one, okay? In the second iteration, we are having some equal one plus two. Why do we have that? Because we're having S plus num. So sum sorry. So S plus num, S equals to one because in the first iteration, we updated the value of sum, so it's not zero anymore. It's one, and Y two, because the num is iterating for the second round through the numbers vector and the second element of this vector equals two. Therefore, sum equals three. We have the third iteration. Sum equals three plus three. And the first three is the value of sum, which we computed in the last iteration has to be three. And this three is the num, which is the element of the numbers vector because it's the third iteration, we are going to take the third number. Therefore sum equals six. In the fourth iteration, we are having some equal six plus four sum equals ten, y six plus four because the sum from previous iteration equals six, and four is the fourth element of this vector. And finally, in the fifth iteration, we are having sum equal ten plus five. Therefore, sum equals 15. Why ten, ten is the value of sum in the previous iteration, and five is the last element in the fifth iteration. So the fifth element of the vector called numbers. So we expect to have the sum of all numbers in this vector to be equal to 15. And finally, after the calculation is done in this flop, we are going to show up the output. So we say this message in the output, the sum of numbers is, and then we print the sum variable. So when I run this code, we see this message. So the sum, the sum of the numbers is 15. So if I have, for example, this element to be 30, for example, when I run, we see the sum must be increased. So as you see, the sum is now 42. So depending on the elements of our vectors, the sum of all numbers in a vector, of course, is going to change. Thank you for watching. 73. Vector 3 max number in a vector 13: Hello, everyone. Welcome back. In today's video, we would like to write a program to find the maximum number in a vector. To do that, I'm having a vector of integer type. It is called numbers, and it has five elements, ten, 25, seven, 30, and 18. So we can see that the maximum number in this vector is 30, but we want to find it using C plus plus. So we would like to write a program that does the work for us, does the job for us. So we need to have one assumption. First, I would like to initialize a variable to be equal to the first number of this vector. And then we are going to iterate through all the elements of the vector and compare all the elements of the vector with this first element. We're going to say if the second element is larger than the first element, take that number and insert it in my variable. Otherwise, keep this first element. Going to then compare the third element with the first element, fourth element, with the first element and a fifth element with the first element. Therefore, we are going to understand which one is the largest one. If one element is larger than the first element, we're going to take that large element and insert it in our variable. Here is my max number variable. This is initialized to be equal to the first element of the numbers vector. So it is equal to ten, but we're going to change it. This is just an assumption. Then in order to find the largest number in this vector, we are going to iterate through this vector using no iterating variable, we're going to iterate over the vector called numbers. And in each iteration, we are going to check if this non variable is larger than our max number. It means that in each iteration. So max number equals ten, in the first iteration. And in the first iteration, num also equals ten because num is going to take the first element, First iteration. And now we are checking, sorry, we are saying if num is larger than max number. So num equals ten, Max number equals, let me copy it and paste it here. Maybe it's a better place to have it. Okay. In the first iteration, we are going to check if num is larger than max number. So num equals ten. Max number equals ten. So ten is larger than ten, no, because the answer is no, therefore, this statement is not true for the first iteration. And then we are not going to insert this if condition to execute this part of the code. So we're going to go up and we're going for the second iteration. So second iteration. In the second iteration, having max equal to ten because we didn't change it, but num equals to 25 because now it is iterating for the second time, so it is taking the second value. We're checking to see if num is larger than max number. So num is 25 and max number equals ten. So this statement is true because 25 is larger than ten. Therefore, because this statement is true, we are going to insert this if condition. And when we insert this if condition, we are going to replace the max number. So max number equals ten, and num equals 25. And we are going to assign this num to max number. So we're going to insert 25 to be assigned to max number, okay? Because we're saying on the left hand side, we are typing down max number. On the right hand side, we are typing Num. Therefore, this 25 is going to be assigned to max number. So now max number has been updated. Then we're going up for the third iteration, third iteration. Max number equals 25. We had it from the previous iteration, and num equals seven. So seven is larger than 25. No, this statement is not true. Therefore, we are not going to enter this if condition. We go up back again and we're going for the fourth iteration. Here max number equals 25 still. We didn't change it in the previous iteration, and num equals 30 now. So 30 is larger than 25. So this statement is true because 30 is larger than 25. So we are going to enter this condition again, and max number now is updated to be 30. Why? Because num is assigned to max number. So num is 30 and max number is 25. So 30 is assigned to max number. Therefore max number is updated, and now it is having the value 30. So we go up back again and we're going to iterate for the fifth time, and it's the last iteration because this vector only has five elements. Max number equals 30 num equals 18. So 18 is it larger than 30? No. Therefore, this statement is not true. We're not going to insert Enter, sorry. I was using the wrong word. We're not going to enter this if condition, and we're not going to go back up again. So why? Because we're only having five elements in this vector, and we iterated over all five elements. So this four loop is also finished. Now we are having the max number equal to the largest number in this vector with this solution method. Now we're going to show it in the output. So the largest number is, and then we are showing the max number variable in the output. We expect to see number 30, so number 30. This is how we can find the largest number in a vector. So we assign a variable to be equal to the first element of the vector, and then we are going to iterate through that vector and compare the numbers, all the elements of that vector with initialized variable. If they are equal, it's okay. If this first element is larger than other element, we are going to do nothing. But if one of the elements is larger than this initialized variable, we're going to assign that to be equal to our variable. So we're going to update our max number variable. Then in the end, we're going to have the largest number being assigned to our max number variable. Thank you for watching. 74. Vector 4 reverse 14: Hello, everyone. Welcome back. In today's video, we would like to write a program to reverse the order of elements in a vector. Here I'm having a vector called numbers. It's of integer type. It has five elements one, two, three, four, five. We know about indexing in C plus plus. They start from zero. So the first element index is zero. The second element is one, and the last element is four. Okay. First, I'm iterating through this vector to just print the original vector. So I'm having an iterating variable called num. It is going to iterate through the numbers vector, and it is going to in each iteration, it is going to print the elements of the vector. Then I'm going to next line using the DL, and after that, I'm going to reverse the vector here. And this part is not necessary because we're having the NL already. And then I'm showing this message, the reverse vector column. And in order to show the reverse version of this vector, we are going to have a four loop. I'm having one iterating variable called I. It's a integer type, and it is equal to numbers that size. So the size of this vector minus one, as long as I is larger than or equal to zero, this loop is going to continue, and in each iteration, we are reducing, we are decrementing one value from I. And in each iteration, I type down the name of the vector, we are printing out the index of that I. Here, for example, numbers that size, it is equal to five. We would like to have I starting from four, as long as I is larger than or equal to zero. And in each iteration, we're going to decrease the value of I. In each iteration, decrease the value of I by one unit. Okay? This is how we're going to do. So we're not going to have starting from zero until the last element of the vector to iterate fluid, we are going to start from the last element and get to the first element. This is how we're going to reverse this vector. Why we are having the numbers size minus one. Why? Because here in each iteration, we are having the I element of that vector being printed. Okay? So this is the index of that vector element. So for example, if we are having for example, number zero, so the name of the vector and then index zero, we're going to have number one. Okay? If we are having, for example, one, we are going to print number two, et cetera until end, and the last element of this vector is going to take the index four because we are having five elements and we start from zero. Okay? So the last element of this vector is having index four, which is equal to five. But the thing here is that. The size of this vector equals to five. So five and here if we are having I equals to five in the first iteration, we are going to ask for the numbers vector with the element of this numbers vector with the index five, which does not exist here. We don't have the index five. We are having only up until index four. Therefore, it is going to return an error for us. This is why we need to have minus one when we are going to have a four look to reverse a vector all the time because in C plus plus, we start the indexing from zero. This is why we are having the minus one. And then for example, here in the first iteration, we are going to print numbers and then index four. In the next iteration, we are decreasing Di, so we are calling with the index four, then index two, then index one, and finally, index zero. Okay? So because it is going to continue as long as it is larger than or equal to zero. So in the first iteration, it is going to take five minus one, I equals four, therefore, five minus one. It is going to print the last element of this vector, the fifth element, let's say, with the index four of this vector. So it is going to print five for us. Then when we go up, we are going to decrease the I value by one unit and we are going to have I equals to three. Then we are calling this vector and it's element with the index three. Index three is the fourth element, so we are going to the fourth element, but also it is equal to four. And then it is going to call we are going to decrease the value of I one more round, so it is equal to two. We are going to call the element of this numbers vector, which has the index two, which equals the third element, and in this case, we are having it equal to be three as well. Then the index one, then we're calling the element with index one and then finally with index zero. This is how we are going to reverse a vector. Initially, we printed out the original vector and then in order to reverse it, instead of iterating from the start until the end, we are iterating from the end until the start. This is why we're decreasing the element of the value of I, which is the iterating variable in each iteration. So we start with the last element, which is the size of the vector minus one. We explained why. Then we're going to print, for example, the last element. Then we're going to decrease the I value by one, then one to the last, and then another variable. We're going to get closer to the start of the vector in each iteration. So how do we reverse the vector? We're starting to iterate through it from the end until the start. Instead of the typical version of iterating through a vector which is going to start from the start until the end. So this is how we can do it in C plus plus. Thank you for watching. 75. Vector 5 remove even numbers 15: Hello, everyone. Welcome back. In today's video, we would like to write a program to remove even numbers from a vector. How can we identify if a number is even or odd? So if a number is divided by two, and the remainder of this division is equal to zero, the number is even. Otherwise, the number is odd. So if a number is divided by two, and the remainder of this division is equal to zero, the number is even. For example, 10/2, the output is five, but the remainder is zero. Therefore ten is an even number. Otherwise, the number is odd. For example, if we divide nine by two, there is an output, for example, four something. And then the remainder is not in fact, equal to zero. Therefore, nine is not an even number and it's an odd number. Now we would like to write a program to remove all the even numbers from a vector. Here I'm having a vector of integer type. It is called numbers. It has these elements like five elements or six elements, sorry, ten, 15, 20, 25, 30 and 35. So we would like to remove even numbers. What are the even numbers in this vector? So ten is an even number. 20 is an even number, and 30 is an even number as well. First, I print out in the terminal the original vector. So I'm iterating through all the elements using the num iterating variable over all the elements of this numbers vector, and in each iteration, we are printing out the element of this vector. Original vector is being printed out using this part of the code using a four loop. Then in order to remove the even numbers, we are initializing, we're declaring another vector called odd numbers, sorry, odd numbers. It's a vector and it's a integer type. Now what we are going to do is the following. We iterate again through all the elements of the original vector, which is numbers, and in each iteration, we check if our iterating variable is divided by two. Is remainder is not equal to zero. If this statement is true, we're going to push back that element, that num into our odd numbers vector. Let me illustrate it with another example. So here, in the first, first iteration, num equals to ten, okay? In the first iteration, we check 10/2, it's remainder. What is that number? It's zero. So we're checking to see if it's not equal to zero. Therefore, when ten is divided by two, its remainder is equal to zero. So this statement is not true, so it's not going to enter this part. So it is going to go up and start the second iteration. So the second iteration, num equals to 15. 15/2, it's remainder is not equal to zero. Yes, this statement is true because it's an odd number. If this statement is true, enter this I condition, and then here, push back that 15 into our odd numbers vector. So this odd number vector is going to have the number 15 now, okay? Odd numbers. This is our vector. So this is going to take the number 15 as its first element. We go up and we start the third iteration. So the third iteration, num equals to 20. If 20 is divided by two, its remainder is not equal to zero. This statement is not true because when 20 is divided by two, its remainder equals to zero. Therefore, this statement when it's not true, it's not going to enter this part of the code and go up, it is going to go up to get ready for the fourth iteration. So in the fourth iteration, num equals 25. 25/2, its remainder is not equal to zero. This statement is true because this statement is true, it is going to take that 25 and use the pushback function to insert it in the odd numbers vector. The second element is 25. We go up and get ready for the next iteration. So fifth iteration num equals to 30. 30, I 30 is divided by two, its remainder is not equal to zero. This is what this statement is saying. But this statement is not true because 30, when it is divided by two, its remainder equals to zero. Therefore, we are not going to enter this if condition and get ready for the next iteration, which is our last iteration because we are only having six elements, so we are going to iterate six times. So in the next iteration, num equals 3535/2, its remainder is not equal to zero. This statement is true. We are going to enter this if condition, and that 35 is going to be pushed back to the odd numbers vector. So here is our vector. Now we created a vector called odd numbers. It is exactly equal to the numbers vector, but after the removal of the event number. This is the vector. When it even numbers are removed, it turns to the odd numbers vector. And finally, we are printing out the vector after removing the event number. So we are iterating using the num iterating variable, iterating through the odd numbers vector, and in each iteration, we are printing out the element of this num numbers vector. So when I run this code, we see here the original victor being printed out here. So ten, 15, 20, 25, 30, 35, exactly these elements are being printed out. But then here when we are having another four loop to remove the event numbers, we are seeing that victor after removing here this message is being shown. This part is the creation of odd numbers vector. Here is the message shown in the output saying that a vector after removing even numbers, equal to 15, 25, and 35 exactly the things that we expected. This is how we can create a vector of another vector and remove the even numbers. We could remove odd numbers, for example. This could be another coordinate exercise. And in order to do that, instead of having this sign, we need to have this sign. So if for example, a number is divided by two, the remainder of this division equals to zero, push back that number into our, for example, even numbers vector. This is how we could address another example. Thank you for watching.