 Welcome to this lab session. In this lab session we will first look at Unix, small commands of Unix and then we will write a program using the WIM editor and then we will compile this using the GCC compiler. Later on you will have a bigger program to write in G edit, a more user friendly interface of to write programs and all and then we want students. Okay so now let's start with the login procedure, some of you might have already done it but basically on your screen the opening screen of Linux to type your username that is already available with you and then type your password. This will bring you onto a screen which will have maybe one or two bars the starting the upper bar and the lower bar. So it will be similar to the screen that you are seeing right now. But here you need to start something that is called as the terminal. The terminal is a box through which you can actually pass a command to the operating system to execute any program that you want. Now since we want to do the WIM programming you will have to go to applications, accessories and open the terminal. Now there are many ways of opening the terminal, some of you might not be seated on a Ubuntu system in which case you will have to do a alter F2 which will open a run application box in that you will have to type the Genome-terminal. Okay when you type the Genome-terminal it will open a very similar box to what opened right now. Okay I think most of you will be able to do this, have you opened the terminal? Okay now in the terminal well I have increased the font size but you will have a very small prompt. So the prompt is made up of two things mainly. The first part that is before the ampersign is the name, the user name that you have used for logging into your system. The second part after the ampersign is the name of your computer. Most of you will have an OSL dash a number as your computer name. Then you have a colon tilde and then a dollar. So this dollar basically signifies the prompt. Now let us come back to our slides. So we will start with terminal and please be relaxed we are not assuming any Linux knowledge here so you will be shown all the things that are to be done. So as I said there is an ampersign and a dollar sign, anything before the ampersign is your user name, anything after it is the machine's name then there is a path. So the tilde that was here is the path. This is the path of your current directory. Now you need to create a directory of your own name. To create the directory you will have to type the command mkdir on this prompt please type mkdir space your name. You can give any name that you wish and then press enter. So basically all of you have logged in into a directory which is placed at slash home slash your user name or something similar to that. Inside this directory everything by the way in Linux is called as directories. It is similar to folders in Windows but slightly different that it has certain permissions and all. So you want to create a directory of your own name inside this directory you will store most of your programs. So when you create the directory let me also show you. So I will do an mkdir say Ashutosh 1 because Ashutosh might already exist. So when I have done this the prompt comes back we are not inside the new directory that we created. We are just at the same place where we were before. This place is by the way tilde, tilde is the your home directory. Now you can move inside your directory using the command cd space whatever the name of your directory. So please execute that command cd space whatever name you had given to the mkdir command. Using this you are basically moved from your home directory into the directory that you right now created. Most of the programs that you will write right now will be placed inside this directory and you will execute them from this directory itself. Now your first program will be done in an editor. So after you move into your directory basically you will be creating programs inside the directory. You will be using the editor known as Vim to do that. So you will be creating your first program now in the Vim editor. So to start Vim you have to type Vim and then give the name of your program. So maybe give rock1.c or whatever you want. Now in your handouts I think we have the prog1.c am I right? Basically it will be looking similar to this. It is a very simple c program. In this program you have one print statement and then some 10 print statements repeated and then another print statement. Now in the Vim editor do not start typing immediately just listen. Vim editor is a very powerful tool but it operates in two modes. There is a text mode and there is a command mode. So in the text mode you can type the program however in the command mode you can do other things for example moving between the lines and deleting lines and appending them etc. So when you open Vim editor all tilde's will be showing you have to just press the I key. When you press I you go into the insert mode. Now you can start typing since you are using Vim you will be also able to go up and down using your arrow keys. And that was not true when you were using a previous version of Vim. With this you can actually type the whole program. You may also require certain Vim commands something like x but when you want to execute a command you have to first press escape key so that you enter the command mode again and then execute a particular command. A command is executed just by pressing a single character or maybe two characters. So for example if you want to delete a character now I am in Vim mode you are usually in insert mode then it will show as insert at the bottom of the screen. I will just press an escape key now nothing is showing in my insert part and then I will press an x the I that where the cursor was gets deleted by pressing x showing you again by pressing x the character under the cursor will get deleted. Now if you want to insert something again you can press escape where escape is not needed here. You can press I again insert mode is entered and then you could write alright there are many commands in Vim you will be using only a couple of them here x might not even be used if you write it correctly also backspace might work in your version of Vim. So basically you will be fortunate if backspace works well so basically there are many commands in Vim most of the commands are given at these websites you may look up them later on. However there might be a question in your mind as to in the world of these very good editors that we have why do we still need Vim. So basically first of all Vim is very powerful much more powerful than maybe those simple editors that are available secondly there might be cases when you will not have a complete GUI available to you even in that case Vim will work only when command prompt is available only when command line setup into a system is available still Vim will work properly and then that has to be used to edit certain system files etc. So knowledge of Vim or rather knowledge of its existence is good alright. So I think everyone has now completed the program the finalized program will look something like this. Now you can come out of Vim by pressing escape again and then giving colon wq the program has been saved whatever you wrote has been saved and is now ready to be compiled. Now just to check whether or not the program that you wrote still exists you can just type the command cat prog1.c. Clicking the command cat basically standing for concatenate will print the whole program on the screen as it is. Now if it were multiple pages all of them would have scrolled across the screen there are other commands available like less and more which can show you a paginated output of the text file that you have but we will look at those commands only later on. So now basically you have a program ready you also verified that the program has been properly saved. Now you have to compile this program to compile you have the compiler called gcc which you are going to use. So type gcc prog1.c now if everything goes right that is if you have written the program correctly and gcc is properly installed on your computer it will only show a prompt again it will not show any errors or any warnings. Here you have done this the gcc compiler has created an object file a dot o file dot output file well it is known as a dot out by usual gcc compilers but you can always give a minus o option which tells it what program what file to output into. So we could write something like gcc prog1.c minus o prog1.o so prog1.o is the output file very similar to the exe file that we have in windows the object files will compile directly on will run directly on the computer. Now if you have used only the first command you will be typing something like dot slash a dot out this a dot out file was created automatically by the gcc compiler when you compiled it using the gcc command. Now by the by running a dot out we have basically run the program and it had shown all the things that the program was supposed to do okay so now this was about a simple program that used only c syntax and you could use gcc properly okay so now we will be using a slightly better editor I would not say a more powerful one but one that you will find bit better to use so we will be using g edit okay now press g edit and then give a new program name so program 2.cpp use the extension dot cpp because we will be using a program we will be writing a program that will require the cpp construct okay so when you open program 2.cpp you will see a g edit screen that is similar to windows notepad screen except it has some bigger icons over and at the top now you have to write a program that will calculate the area of a circle given its radius this program is also given in your handout in this program since we are going to use cpp pay attention to the first two lines we have the hash include IO stream we do not have a dot at extension given there and then we have the using namespace std both of these things are important and are slightly different from what you might be already used to IO stream basically is an header include file you include it from the directory in which you have the Linux headers and this has a namespace a namespace is a kind of box in which you can put certain variables these variables will be accessed only in the context of that namespace and nowhere outside the context now this has a benefit that if you use these variable names in anywhere outside the namespace you can still not cause a conflict with the original namespace. So basically now we are using the std namespace things that are inside the std namespace are examples of them are see out and see in so you will be writing a program similar to what is displayed here okay so after this program is written completely we will just compile the program now compiling this program using the gcc compiler will not work it will give certain errors this usually happens be if your Linux headers are not properly resolved gcc does not understand where to look for your libraries and for the include parts a better of course there is a solution to this you could use you could properly resolve all the headers by installing the Linux headers packages and so on but another way is to if you have g++ installed g++ will have all these dependencies already resolved for you so that the program will perfectly compile in g++ so just type g++ the program name dot cpp of course since you are using iostream you will have to have a dot cpp file a dot c file won't work now to run it we are again going to type dot slash a dot out now this a doubt out what's created by the g++ or the gcc compiler hence whatever a dot out that was present previously has been now erased and has been replaced by this program so let us give some maybe 43 okay and whatever this is the area of the circle the pi r square okay so basically this was a small program by which you will have got certain insight into how we use g edit for writing programs and vi in the previous program well now if you have done the compilation and everything is okay now let us move to an example program that we have conducted in the last semester for the CS 101 students at IIT Bombay CS 101 as you know is the first year program so all branches students are present in the program and this was an interesting assignment that was given to them the assignment was solved by the students within 3 hours or so though of course they had a week to submit their assignment but let me just read out the assignment I think you have the handout the print out of this entire text is with you so I will read out the assignment this assignment is courtesy of professor Anade who has allowed me to show it here we had this as a fourth assignment in our course so fourth assignment was approximately the fourth week at Sir had said previously turtle was used in most of the programs but since you do not have the requisite background for turtle programming in this assignment we are not going to use turtle we will have plain C program what we need you to appreciate here however is the kind of intuitiveness inside the program and the insight that the students will require to do something of this sort so I will just read this out the goal of this lab assignment is to get an estimate for pi so in the previous program you already used pi to calculate the area of the circle now we will assume that pi is defined as area of circle of the radius k divided by k square if we can find a way to estimate the area of the circle then we will be done the basic idea is as follows a point in the plane or in higher dimensions also that is if it is a sphere and so on it is said to be a lattice point if both its coordinates are integers since we expect an area to contain roughly a lattice points by counting the number of lattice points in a region we can get a rough estimate of its area so basically if you have a square then you have a fixed number of points which are 1, 1, 1, 2 and so on so if you count all the points inside that rectangle you get the area of the rectangle because each of the point is supposed to be of unit area whatever the unit may be the same idea can be used here however circle does not have a boundary that is rectangular however we can still say that a circle is made up of multiple pixels or multiple squares put at its boundary and other squares filled inside it therefore this technique will work now this technique will work better if you have a very large radius of the circle so we will count the number of lattice points in a circle c with radius k centered at the origin but note that the number of lattice points contributed by each quadrant of the circle will be same therefore we can actually repeat it so we can just count the number of points in the positive quadrant and multiply to get the estimate of the area of the whole circle so now we have reduced the problem from calculating the area of a circle to calculating the area of just the quadrant that lies in the positive quadrant so this gives us the estimate for pi as the number of lattice points in c from the positive quadrant multiplied by 4 divided by whatever the radius square so the key question is how to estimate the lattice in the part q of c in the positive quadrant there are two ways of doing this you will implement both of these ways now the kind of question that we have framed does not basically belong to any textbook this has been thought of it is a original question so now this questions answer will never be found on the internet even if students are given say a week full of time they can at max only copy from other people and that we can obviously detect but they cannot copy from the internet or somewhere else so they have to think about this question and answer it so now let us look at the two techniques that were made available to them first is the brute force method we know that every point i, j in q satisfies the condition 0 is less than equal to i, j is less than equal to k k is the radius therefore any point i, j that lies inside will satisfy this condition of course the converse is not true so keep a variable say called counter which is initialized to 0 the counter will basically count the number of lattice points inside that quadrant then we consider each of the points i, j satisfying the above condition say you do nested loops or whatever you want and increment counter whenever the point is inside at the end of the counter at the end the counter value should be equal to the area estimate for the quarter circle from this we print the estimate for pi notice that determining whether a point is inside c can be done using a simple arithmetic operation we can just find out whether it is inside or not because we know that 0 is less than equal to i, j is less than k okay now the second method it is slightly of course it is much better but it is slightly different the second method is based on the following observation let the point let the points with y coordinate j be set to constitute the jth row now instead of counting the number of lattice points we first determine the rightmost lattice point in each row basically observe that the circle has only its boundary that is very important for us so now if we can just move around the boundary going here and there only by 1 pixel or so we can actually estimate the all the points inside anything to the left of it from the origin is always inside the circle therefore the way we move should be just around the boundary 1 pixel inside or 1 pixel outside and then always say that all points to its left up to the origin line are all inside the circle so this is the basic idea here let the points with y coordinate j be set to constitute the jth row instead of counting the number of lattice points we first determine the rightmost lattice point in each row if the rightmost point is ij then we know that the ith row will have i plus 1 points that will lie in queue because this ith point was the last point on the circle therefore all points less than it are inside determining the rightmost point is further simplified by the following ideas first the rightmost point on the row 0 is clearly k comma 0 because k is the radius and we have placed the circle at the origin in general given i comma j as the rightmost point in row j we know that the rightmost point in row j plus 1 will be a point i i naught comma j plus 1 that is a new point where i naught will be close to i and no larger so it will be just inside the circle so we first check if i comma j plus 1 is the rightmost point then whether i minus 1 comma j plus 1 is the rightmost point and then i minus 2 comma j plus 1 and so on this way we will determine all the points by just checking all the points on the border and then calculating the lengths of the lines to the left of this point with us get the area of the entire quarter now again when we get the area of the quarter we just multiplied by 4 and then divided by the square of the radius that is how we will get the estimate for pi now when you will do this program this by the way is a very effective method of finding pi only if you have a radius which is very large if you take a radius very large then you obviously increase the amount r square and therefore you might want to consider a larger data type to store your radius if you consider your radius to be some 10 or 20 the estimate for pi won't be that good therefore take your radius to be something like 50,000 or so but then your data type should be large enough to accommodate the square of the radius so basically we suggest using long or double data types see how the solution for this problem it supposed to be and what are the principles behind this so basically here we have a circle which is placed at the origin and now we want to find out the area of the quadrant on the positive x and the positive y okay so the basic equation that we will be using for this is x square plus y square is equal to k square okay now this is the equation of the circle so anything with x square plus y square is less than equal to k square will lie inside the circle okay now since we are looking only at the first quadrant we can increase the x from x goes from so becomes equal to kind of 0 to k similarly y goes from basically it goes from 0 to k now when x is increased what we will get is points over here so basically we will be doing this line okay now the moment we go outside the circle this equation will fail okay when this equation fails we just change the y coordinate and check for the line just above it again just when we go outside this equation that is when this equation fails we stop at this point at this point and then go above one more line this way we basically find the area of the whole quadrant alright now this was the brute force method instead of doing this in this fashion we can do it in another way that was what was given in the answer the second approach okay we will just rub out these things inside now you know that this circle lies in the first quadrant okay now this first point over here its coordinate is already known its coordinate is x is equal to k and y is equal to 0 at this point it will always lie on the circle therefore all points inside it meaning all points with x less than k will always lie inside the circle therefore the area of this line this line that I just drew the one that I made thick now is equal to k now there is no such notion like an area of a line but I am just doing it so that we speak in the computer language basically line mathematically will have no area it will have only length however here since there is a finite pixel we will have an area because the line basically becomes a rectangle there okay so one pixel by whatever the length of the line is the area of the line therefore it is also equal to the length k now at this point it is okay now you can just increment y by 1 and then check if x is equal to k and y is equal to 1 I will just erase all these other things we will just check whether x is equal to k and y is equal to 1 lies inside the circle now this won't lie inside the circle so now we do a x minus 1 now what we have is x is equal to k minus 1 and y is equal to 1 this point may or may not lie inside the circle depending upon what your radius is and what is the granularity of the pixels that we are seeing okay so now if this point doesn't lie inside the circle if it's still outside the circle then we do a 1 more minus 1 here and keep on doing till we get a point which is just inside the circle when we come inside the circle so basically we are moving in this direction now when we enter this point on the circle then we can say that all points lying to lying to its left are inside the circle again here we have a fixed value of x say x is equal to k minus 2 and y is equal to 1 we add this value to the previously stored x is equal to k value for the line below for this line and then we have a total of k plus k minus 2 okay and we go on so in this fashion to complete the to completely find the area of the whole of this whole quadrant so basically I have shown slanting lines you are going horizontally across the entire circle and you know the area of the shaded region now this technique is far more superior than the previous brute force technique because you do not require to consider every point from the original line up to the end of the circle you just consider very few number of points at each of the boundary locations so basically this becomes a almost linear technique linear in y basically but it is a circle y is equal to x is equal to k in the maximum therefore the number of iterations you do is bounded by so the iteration is equal to y multiplied by some fixed constant c number of times when you had to check whether the point lies inside or outside the circle so to make things simpler you can use a small is inside function which will basically tell whether this point lies inside the circle or not which will do this calculation again the same equation this equation must be calculated if it is less than equal to it returns either true or if it is outside it returns false or something of that sort so given a point x comma y coordinate you will always find out whether a particular point is inside the circle or not okay so this way after you calculate the area of the quadrant you can just so area of the quadrant is suppose q which is given by this equation you can just multiply it by 4 okay and that will become the area of the circle area of c all right now once you have the area of c calculating the value of pi becomes trivial it is just this area divided by the square of k all right there is a program in your home directory the home directory can be accessed by if you are already in your directory that you created just do a cd space dot dot something like this cd space dot dot okay I will just enter into my own directory again okay now you are at your home directory there is a program known as threads dot cpp now usually you compile all your programs using something like g plus plus threads dot cpp okay now if you give something like this here it shows two errors these two errors are because a particular library is not linked currently with this file this program is a fairly complicated program it basically creates multiple threads inside a inside one process and then these threads are run for some time after they complete they all the processes join all the threads join and the process ends so since this requires a library known as p thread library and since the g plus plus compiler right now doesn't know how to use this library it has shown these errors now by the way previously when we had a problem we changed from gcc to g plus plus here we do not have that kind of a problem here we need libraries therefore you have to give a minus l tag minus l specifies that a particular library has to be loaded when this program is run so please type minus l p thread no space between them it is minus l p thread so it basically reads minus l p thread okay when you give this there is a library known as p thread already present in linux headers it will be along with this program it will be linked together therefore the output that you get now will have all the references required from the p thread library brought into this program and then it will work fine so now this has compiled properly you can just press a dot out and see what happens now this program is fairly complicated you won't be typing it or even maybe looking at it yourself but this is a good example of a threading a simple threading program which just forks five threads and then at the end of the process all of them join together using the p thread join command join function and then the program exits okay so along with minus l p thread there are many other libraries that are required to run specific functions which are not completely part of the gcc environment already sometimes it is beneficial to put all these library parameters into one macro kind of thing and then run that together that is done by make file but we won't be covering make files here however you can always feel free to add any more number of libraries so this was a real time library then this is a math library and so on it will always start with the minus l command and be then appended with the name of the library you can find the names of the library is required on the internet as as you go and use a few functions so basically whenever you find a function you want to use that function along with the synopsis of the function which library it requires will always be provided to you will be I mean you will come to know which library is required okay after this after I complete Avdut will show you how to debug programs that is using the ddd software.