 Hello and welcome to the NPTEL course on an introduction to programming through C++. I am Abhiram Ranade and today's lecture is also about arrays and the reading for it is from chapter 15 of the text. So here is an outline. So the first topic that we are going to look at is how to represent textual data. Then we will talk about two dimensional arrays and in this one of the examples will be how to create polygons in simple Cpp. And then we will talk about accepting command line arguments. Textual data we have already talked about a little bit. So we said that the care type was specially meant for storing single letters. Now obviously the natural step would be to use an array of care to store sequences of letters. So for example you could use an array of care to store words, sentences, paragraphs, maybe a character string like India, maybe a character string like C++ is nice with spaces inside it and the textual data representation that I am going to talk about today is derived from the C language. C++ has nicer representations and we will learn about them a little bit later. But whatever we learn today will also be useful even in that nicer representation. So character arrays we already know, well we know arrays and therefore character arrays are not really different. So as always we define an array by giving the type name, the name of the array and then in square brackets the size. So here I am defining two character arrays, one array called name of length 10 and another array called address of length 50 and both have the type care. So such arrays can be used to store character sequences or character strings as they are more often called. And you may, you often use an array of a larger length to store strings of smaller length. That is because often you do not know how long the string is that is you want to store say in the name, in the array name because you may not always calculate what is the exact name of this person before storing the name of the person into the array. You may just allocate a large enough array for storing say the name of a person. Now the arrays are, character arrays are really not any different from other arrays but there is a difference in the way they get used especially there is a protocol for storing character strings that is inherited from the C language and here is what the protocol is. So if you want to store a character string into an array you store the string in the array starting from element 0. After all the characters in your character string are stored in the array you also store the character whose ASCII representation is 0 often written as quote backslash 0 quote. This character is often also called the null character. So what you store actually is the string that you want to store followed by this null character. So the null character sort of signals that look the portion of interest has ended and indeed the way everything happens with character arrays is not using the defined length of the array but rather the idea that only everything until the null is a part of the actual string and what comes later is supposed to be ignored. So in fact you will see that when you are processing character strings stored in character arrays you do not really talk about the length because the length is never specified explicitly. So you just process until you find the null character. Now you can create character arrays with initialization. So for example you might write char and 1, 20 so you are allocating an array of length 20 and in which you are storing agenta and you are allocating an array n2 without giving the size in which you are storing allora. These definitions are analogous to the definitions that you have seen say for defining arrays of integers. The initialization form for character strings is slightly different. You do not need to enclose individual characters in braces but you can just put up character strings. Okay, so how does this work? So n1 will be created with length 20 and it will be initialized as follows. So capital A will be put in n10, j will be put in n21, n11. The next A will be put in n12 and so on. So a, j, a and ta will be stored in array elements n10 through n15 but that is not all. In n16 the null character will be stored. So this is this as I said this is just according to the protocol that we talked about earlier and the elements n17 through n19 the remaining elements of this array agenta will not be initialized and it is expected in fact that if you are going to process this array these elements will not be looked at. So only the elements until the null character will be looked at. N2 will be created of length 1 plus the length of the string allora. The length of the string allora is ELLORA6 plus 1 so 7 the size of n2 will be 7 and it will also be initialized to the string allora followed by the null character. So just to draw a picture n1 will look like this. So this will be index 0, this will be index 19 and then in n1 we will store a, j, a, n, t, a and then this null character. So this is 0, 1, 2, 3, 4, 5 and in 6 the null character will be stored. N2 because the length of N2 is not given N2 will be created so that it uses the minimum length. And so what will it look like? It will look like ELLORA and then the null character. So this requires 7 characters and in fact exactly 7 elements will be allocated. So element 0 through element 6 and I should point out that the syntax n120 equal to agenda looks like an assignment but that is not true. You cannot use this as an assignment, you can only use it as an initialization. You can assign individual elements of the array. So you could write n1 of 0 equal to quote A quote, n1 of 1 equal to quote j quote and so on but assignments will be one element at a time and there is this feature of assigning a lot of elements simultaneously is reserved only for initialization. So we have declared arrays and maybe we initialize them but we surely want to read data into arrays. So this is the first way in which we are going to be able to read data. So for example we have a character array buffer of length 80, suppose it has been defined as shown here. The first way to read into it is to use C in greater than greater than our standard mechanism and this for the purposes of reading into character arrays works in the following manner. So it reads one word and what is a word? Well it is anything which is terminated by white space. White space is a space character or a new line character, tap character. Any of these appears then the word will be considered to have ended. So whatever that one word was typed in by the user will be taken and will be stored into this array buffer from the beginning and it will be terminated by a null character. So suppose we use our types C++ is nice. So there is C++ space is space nice and after that nice say there is a new line. If you remember you have to hit a new line before the computer actually starts looking at what you typed. So say here you hit a new line. So what happens in this case? So in this case as we have said one word is read. So the first word is C++. So that C++ is read. So three characters are read and they are stored into buffer 0, buffer 1, buffer 2 and following them the null character is stored. Now Cn greater than greater than buffer does not mention the length of the buffer because if you remember the name of the array is only the starting address. So this is needed of course. You need to know where to start putting in the data that you are just reading. But there is also a problem here. So if the user types more than 80 characters then this will cause an index out of range. C++ will attempt to store whatever you typed into say index 80, 81, how many ever characters you need and that will cause an error. So what should you do? So we will have a more safer way to read, we will show that in a minute. But the standard idea is to just allocate a large enough array or the simple idea I should say. So quite possibly if you want to write a really good program you should probably not be doing this. This is sort of a quick. This is a way to write a quick trusting program. I should also mention that the command Cn greater than greater than buffer works only for character arrays. In fact buffer is a constant. So normally Cn greater than greater than is supposed to have a variable after it. So normally if you type in a symbol that symbol had better be a variable. But for character arrays there is a special case made by C++ so that it is interpreted as put whatever is read into this array starting from the 0th character. So if it is not a 0th character, if it is not a character array however then C++ does not allow you to read things in this manner. So that will actually cause an error. Say buffer is an integer array then you write Cn buffer would cause an error. Now here is a different way to read into character arrays. So again we have our same definition char buffer 80. The safe way to read into it is this. We say Cn dot get line buffer and 80. Here you can notice that we are specifying the length. So C++ will actually make sure that the reading does not happen beyond the array boundary. So this will read a line and this time it is not terminated by white space but it is terminated by a new line. And if the line is very long then C++ will only pick up the first 79 characters. So whatever is picked up will be placed in buffer and it will be followed by the null character. So this is safe because you do not attempt to write beyond the end of your allocated region. And another difference between this and what we had earlier is that the line that you read may contain spaces. So if you want to get in spaces into what you are reading this is the way to do it. As an example if the user types C++ is nice as before and again after nice there is a new line so a computer starts processing it. This time that entire text would go into the buffer followed by the null character. Printing char arrays is simple. So if you have a char array again the same char array then say you somehow put data into it. We have seen one way of putting data which is to read into it but there could be other ways which we will see soon. And I should just point out that the way you put data into the elements of any array is the same. So I can put data, I can make assignments to char buffer 0, to buffer 0, buffer 1 just like I make assignments to the elements of an any array. So suppose you have assigned some value into buffer and you have done it in the right way which is that the string that you have stored has a terminating null character. So if that happens and then you can write c out less than buffer. So this would push the content of buffer onto the screen. And how much content? Well everything until the null character. So for example earlier we initialized n1 to be agenta and n2 to be allora. So if you write c out less than less than n1 then agenta will be printed because as it is said over here everything until the null character is to be printed. And for this it is important that your buffer actually contain the null character. If it does not contain the null character then c++ will just go on printing whatever there is after that address. Again in a sense this is an unsafe, slightly unsafe mechanism but it is not that unsafe because it is in your hands as a programmer to make sure that it contains the null character. The length of the array is not important because only everything until the null character is going to be printed. And I should also point out that if buffer is an array of a different kind then what would get printed would be the address of that array because as you may remember in general the name of an array stands for the address, the starting address of that array. So here when I say c out, so this is something special intended for care arrays. Now how do you process character arrays? So we are going to do a few examples of this. And the general principle however is that the array length is ignored and instead we process all the elements till we find the null. And it is expected that the array will contain a null if you are going to do this kind of a processing. And this is the most common idiom that we do not really keep track of the array length but we process everything until the null character. So let us write a very simple program. So this program is going to read in a name from the keyboard and we are going to count the number of letters in the name. So here is a program. So what does it do? Well first it defines this array name of length 80 to store whatever you are going to read. Then it prints out a message saying please type your name. Well for short it is just saying name. And then I could have used the safe variant but just for simplicity and compactness I am using the unsafe variant and this variant will read the first word. So whatever you type delimited by the null by a white space will go into name. And then the next line is just going to print that out. So it is going to first print the message you typed whatever and then whatever you type the first word from it will get printed. The first word will be taken, only the first word will be taken into name and only that will be printed. After that we are going to search through the name array. This search is sort of similar to what you have in other arrays but it is different in the sense that you are not going to go, you are going to start at 0 but you are not going to go till the end. You are going to go only until you find a null and this is the body of the vile and in the body you are only going to increment L. So as a result after this entire code executes L will be the index of the null character or since our counting starts at 0 L will be precisely the length of the name. So that is what got printed over here in the last bit. So here is a quick exercise for you, write a program that reads a word and prints it, prints its reverse. So just to clarify what you are expected to do, you first find the length of the word using what we just saw and then examine the characters in the word from the last character coming back. So again now this is not, the second part is sort of standard array stuff. It does not use this convention about the null character or anything like that but anyway, so the first part does and so do write this program. So what have we discussed so far? So we said the text is stored in an array of care, the length of this text is not explicitly stored and this is not a rule as such, it is just a convention but it is a very, very stringently followed convention. Here the last real character, a null character or the ASCII character with value 0 is stored. Again this is also a part of the convention and sometimes it is this last null character is called the sentinel, sentinel means guard so it is sort of guarding the actual text that you have stored and it is standing at the end and guarding its sort of. So next we are going to look at functions that operate on character strings and also something called character string constants. So we will take a break.