2.1 Review of Scientific Programming in C and Fortran

Video in TIB AV-Portal: 2.1 Review of Scientific Programming in C and Fortran

Formal Metadata

2.1 Review of Scientific Programming in C and Fortran
Title of Series
Part Number
Number of Parts
CC Attribution - NonCommercial - ShareAlike 3.0 Unported:
You are free to use, adapt and copy, distribute and transmit the work or content in adapted or unchanged form for any legal and non-commercial purpose as long as the work is attributed to the author in the manner specified by the author or licensor and the work or content is shared also in adapted form only under the conditions of this license.
Release Date

Content Metadata

Subject Area
00:09 Rappture with C and Fortran 00:26 Example: app-fermi in C 01:41 What is the interface? 03:29 Same interface - Use C Language 03:56 Generate program and Makefile 04:16 Add physics to the generated code 05:04 Add physics to the generated code 05:18 Build the program 06:07 Running the code 06:53 How your program gets invoked 10:03 Not Much Overhead 11:38 Fortran version 12:27 Same interface - Use C Language 12:36 Add physics to the generated code 13:15 Add physics to the generated code 13:26 Build the program 13:52 Running the code 14:27 How your program gets invoked 15:52 Not Much Overhead 16:28 Reference Documentation 17:42 Assignment #6: Rappture interface for C or Fortran
Point (geometry) Metropolitan area network Code Sheaf (mathematics) Computer simulation Drop (liquid) Urinary bladder Computer programming Formal language Order (biology) Right angle Whiteboard Game theory Simulation
Logical constant NP-hard Greatest element Machine code Code Ferry Corsten Interior (topology) Multiplication sign Mathematical singularity Decimal Set (mathematics) Parameter (computer programming) Shape (magazine) Function (mathematics) Mereology Arm Computer programming Formal language Mathematics Array data structure Strategy game Different (Kate Ryan album) Oval Data acquisition Endliche Modelltheorie Series (mathematics) Error message Arc (geometry) Physical system Metropolitan area network Computer icon Simulation Logical constant Theory of relativity Mapping File format Real number Computer simulation Drop (liquid) Virtualization Maxima and minima Variable (mathematics) Substitute good Type theory Message passing Telecommunication output Normal (geometry) Right angle Energy level Cycle (graph theory) PRINCE2 Quicksort Simulation Resultant Reading (process) Point (geometry) Web page Trail Standard error Functional (mathematics) Divisor Computer file Real number Letterpress printing Maxima and minima Similarity (geometry) Drop (liquid) Computer icon Value-added network Number 2 (number) Tabu search Revision control Goodness of fit Prototype String (computer science) Energy level Differential equation Integer Compilation album Standard deviation Scaling (geometry) Planning Calculus Line (geometry) System call Compiler Word Loop (music) Pointer (computer programming) Personal digital assistant Statement (computer science) Video game Freezing Library (computing)
Machine code Parsing Water vapor Computer programming Sign (mathematics) Computer cluster Different (Kate Ryan album) Computer configuration Computer icon Metropolitan area network Block (periodic table) Keyboard shortcut Computer simulation Maxima and minima Bit Hecke operator Arithmetic mean Quicksort Hacker (term) Mathematician Simulation Spacetime Point (geometry) Random number generation Computer file Letterpress printing Maxima and minima Control flow Drop (liquid) Computer Element (mathematics) Number Energy level Punched card Compilation album Form (programming) Default (computer science) Distribution (mathematics) Counting Plastikkarte Line (geometry) Directory service Cartesian coordinate system Limit (category theory) Compiler Loop (music) Personal digital assistant Statement (computer science) Musical ensemble Library (computing) Greatest element Ferry Corsten Code State of matter Multiplication sign Decimal Function (mathematics) Parameter (computer programming) Mereology Arm Formal language Mathematics Bit rate Positional notation Data acquisition Flag Series (mathematics) Position operator Programming language Simulation Ext functor Price index Variable (mathematics) Type theory Right angle Energy level Resultant Ocean current Software engineering Statistics Functional (mathematics) Game controller Time travel Field (computer science) Power (physics) Twitter 2 (number) Wave packet Programmschleife Causality String (computer science) Integer Analytic continuation Hydraulic jump Condition number Machine code Equals sign Planning Particle system
Logical constant Execution unit Range (statistics) Source code Computer programming Software bug Medical imaging Sign (mathematics) Different (Kate Ryan album) Exception handling Metropolitan area network Touchscreen Block (periodic table) Gradient Keyboard shortcut Parameter (computer programming) Maxima and minima Arithmetic mean Quicksort Hacker (term) Simulation Spacetime Point (geometry) Random number generation Computer file Letterpress printing Maxima and minima Drop (liquid) Mass Computer Declarative programming Value-added network Number Goodness of fit Term (mathematics) Ring (mathematics) Energy level Data structure Compilation album Punched card Standard deviation Matching (graph theory) Physical law Operator (mathematics) Counting Line (geometry) Symbol table Compiler Word Loop (music) Personal digital assistant Statement (computer science) Library (computing) Building Greatest element Code State of matter Multiplication sign Set (mathematics) Parameter (computer programming) Function (mathematics) Counting Mereology Magnetic stripe card Formal language Mathematics Positional notation Fiber (mathematics) Email Logical constant File format Real number Price index Variable (mathematics) Type theory Sparse matrix Vector space Right angle Energy level Resultant Link (knot theory) Real number Heat transfer Discrete element method 2 (number) Twitter Wave packet Power (physics) Revision control Operator (mathematics) Integer Analytic continuation Machine code Cellular automaton Planning Film editing Doubling the cube Logic
Computer virus Complex (psychology) Multiplication sign Equaliser (mathematics) Mereology Computer programming Formal language Mathematics Uniformer Raum Core dump Compiler Error message Metropolitan area network Computer file Gradient Electronic mailing list Drop (liquid) Lattice (order) Control flow Variable (mathematics) Type theory Process (computing) Right angle Energy level Figurate number Curve fitting Point (geometry) Slide rule Functional (mathematics) Mobile app Statistics Computer file Time travel Letterpress printing Maxima and minima Control flow GDB <Programm> Drop (liquid) Student's t-test Discrete element method Theory Wave packet Number Frequency Term (mathematics) Alphabet (computer science) Booting Punched card Forcing (mathematics) Weight Debugger Counting Line (geometry) Density of states System call Compiler Word Pointer (computer programming) Loop (music) Software Personal digital assistant Statement (computer science)
Metropolitan area network Computer file Information Grand Unified Theory Directory service Function (mathematics) Line (geometry) Computer programming Value-added network Formal language Revision control Mathematics Word Computer configuration Personal digital assistant Right angle Quicksort Gamma function Library (computing)
Area Point (geometry) Metropolitan area network Computer icon Greatest element Computer file File format Multiplication sign Maxima and minima Mass Directory service Arm Computer programming Compiler Formal language Word Hausdorff space Uniformer Raum Personal area network Right angle Gamma function Error message
Metropolitan area network Building Computer file Closed set View (database) Variable (mathematics) Mereology Rothe-Verfahren Computer programming Declarative programming Formal language Compiler Medical imaging Word Personal digital assistant String (computer science) Text editor Right angle Quicksort Gamma function Compilation album
Metropolitan area network Word Oval Statement (computer science) Bit Line (geometry) Arithmetic logic unit Graph coloring Arm Compiler Formal language
Metropolitan area network Functional (mathematics) Grand Unified Theory Line (geometry) System call Arm Computer programming Number Compiler Inclusion map Type theory String (computer science) Right angle Drum memory
Metropolitan area network Loop (music) Multiplication sign Letterpress printing Hecke operator Line (geometry) Arm
Point (geometry) Building Computer file Code Multiplication sign Letterpress printing Control flow Mass Shape (magazine) Arm Computer programming Software bug Different (Kate Ryan album) Single-precision floating-point format Square number Gamma function Metropolitan area network Debugger Counting Line (geometry) Type theory Word Loop (music) Software Personal digital assistant Statement (computer science) Right angle Figurate number Quicksort Table (information) Writing
I how online simulations and more work for nanotechnology bourgeois but so in
the next section of a really go through a review of scientific programming in C and Fortran it's pretty hard for me to like teach you the whole of the world's languages and I'm not trying to do that but what I'm trying to do is show you some example code just so that you get a sense of what's there and also you can always look back in the notes and cheatin copy things out because you know probably stuff is very similar so we still you'll see at once in order to
kind of walk you through it and give you a problem to look at we're going to create a Monte Carlo simulator after this play again because it see the plane go thank you go like the the fair county fair or something and there's this game where there's a bunch of pegs on board drop something down the tags and the the point had starters around then eventually falls into of spot at the bottom right and now you do this on the price is right right if you get into the middle spot you like win the big prize that the grand showcase and nobody can ever get exactly on that 1 spot but anyway so we're going to do is build a simulator that can simulates that motion and the
idea is where follow calling you drop it from the top comes down in each time it hits a page we're going to assume that there isn't even a 50 50 chance that it will go laughter right now of course in real life it's not exactly that but because the pegs might be slightly often mean there's all kinds of other factors but are very simple model just kind of chooses a 50 50 chance when it hits a peg and then it goes down and hits the next square on and as a 50 50 chance going laughter right so what we can do is drop thousands of points in simulate thousands of paths through this and at the end we can count up where all the coins land so we're gonna keep buckets at the bottom and we're gonna drop thousands of virtual coins down through the through this system and we'll see where most of them tend to end up on any of that they call this Monte-Carlo simulation in fact it it's used a lot in scientific stuff and the call Monte-Carlo simulation because like gambling cycle the money spin the wheel and you see what happens and here we're spinning wheel like thousands and thousands of times turns out you can use the same strategy if you want to figure out how electrons move through but little transistors you can do the same thing we should the electrons in 1 at a time you say why don't know exactly what's going to happen but for this particular track it's gonna bump into it adenoma and it's going to get hit by a full non and going to go through the oxide and you you do all this random stuff and if you do that for thousands and thousands of electrons turns out you can actually simulated device pretty accurately as 1 strategy for doing simulations on other strategies involve like calculus and differential equations and all of a sudden you can appreciate the Monte Carlo think and the whole lot easier to think about so that's my
general set up you can imagine you know your advisor hands you this program tells you that's how it works and here's the code and what I wanna do is not so much explain to you how the plane goes simulator works but what I can show off the features of the language in case you saw a program like this that implement something like that right so 1st of all we look at the language program 1 of the 1st things you see near the top estate a a series of pound include statements of the pound includes statements ways of bringing in the libraries that you need for running a program and what I've got here are are 3 very standard libraries STD I always the standard input output STD live is just a bunch a standard library functions and math that age is a all the math functions cost Monte-Carlo simulator really doing a lot of math functions here so we need that and you can bring in other files to usually when it's the standard file you put it in Diamond braces that's the compiler likes that looks for standard places but if you you can also bring in whatever files you want just by putting a founding quotes and so what everyone of bringing coding in Canada as if it was included in line you use a pound include for that planet to find all the functions of variables and stuff you can also define constants and so in this case are playing go as has is going have 9 levels of pegs so we're we're got system with 9 levels you might write the program hard with 9 everywhere and then your advisor comes back and says why would try 11 levels and you like what we have to go back and fix your program right so sometimes instead of of hard coding numbers in it's better to put parameters in like that this axis if it's hard coded of what the compiler does with a pound defines everywhere in your code that it sees the word levels it will literally substitute 9 in place of that just before it compiles everything so all it's doing is a textual substitution of whatever you got up there wherever it sees levels and by convention in C language people use all capital letters for that because they want to be easy to spot in the code where the substitutions are happening you don't want to confuse you could define all kinds of stuff and like you could redefine the word chart in C language and then all hell would break loose in your program because it may be making substitutions and try and figure out what the compilers doing and very confused so people tend to use all upper case letters for for the pound fine just make it real obvious that this is just a text substitution thing that just to emphasize that point you can see the 2 spots in my code arrived at the word level such as get substituted with 9 so I can size my arrays according to levels and I can look through the loop according to levels and all that right now we're going down into the actual code of the program on main program in sees looks like this it's defined as a function called main and it has to have it on 2 arguments and integer oxy and watch our star star or the other stars in languages are references to pointers in this case this is a pointer to a pointer of characters that's how you represent array of strings basically in C language and if you don't understand all that that's OK just copy that because that's where your function prototype needs to look like in Maine that's what you see languages don't need to worry about that arcsine obvious stuff unless you actually use the arguments in the program the arguments when he called program the command line you get all the extra words those are the arguments and artsy tells you how many arguments there on the command line and on the gives you all the different strings for the arguments on the command line and so anyway you just make a function like that called main nattering program and always returns an int the integer returns as an integer status code that says whether your program succeeded or failed so if it if it returns 0 everything's OK and returns anything other than 0 somewhere on and on Unix there's all kinds of different status codes that tell you what might have gone wrong in a program with different numbers that can return so you wanna write hello world or whatever you start with a function like that right now just like in Matlab yesterday we need to get some input into the program and the way that a lot of people get input into the program again the prompt the user the asking questions and they read things from for example standard input so the way you can do that in C language there's a printer function formatted print print f in this case I just gave a simple string number of drops the backslash enters a new line so just read that as a carriage return or a new line so it's going to print number of drops to a question mark and and another the next line and then the standard function is a way of reading and from standard EN stand at sort of the opposite of proliferative prints out scanner freezing but it reads in according to a format the format here is % D means look for a decimal number and put the result in the word map and stand at the and returns a value which tells you how many things was able successfully stand so if I'm in good shape it will read a number of decimal number it will put it into variable called max and then I'll return the value 1 and I'll know if something didn't return the value of 1 when something went wrong somebody typed in x y z type anything or something went wrong and so in this case I'm looking at scale saying well if it's not equal to 1 then I print out bad number and then I exit from there's all these different flavors just like yesterday with Matlab as we can see this an after enough flavor for print have after enough prints out to a particular file in this case standard error as a way of putting out messages so but again all of these are very similar print after spreads out to the normal standard output at with standard error prince of the standard error but in both cases you just give it a string like that that number or something maybe a new line at the end and it prints out and then exit exit 1 will exit the program and with a status code of 1 is telling returning from the main program of 1 basically stops to program and signals an error that I wrote that down there so whenever you're ending your program and in relation to return 0 or eggs at 0 x is 0 means OK I always remember that way zeros OK and everything else is failure of 1 to 10 1 1 million and 27 whatever everyone return as as a failure of right now this is how you do a for loop in C language and and it's a little different than some other languages the for loop in C language kind has 3 parts to it and they're separated by semicolons the 1st part sets up what happens before the loop starts so in this case it says i equals 0 so at the beginning of the loop of sets i equals 0 and then as it's going through the for loop again and again and again it checks the middle part it checks to see if i is less than the levels plus 1 so as long as i is less than levels plus 1 it keeps going through the loop and then the last part is done said right at the end of the loop to go on to the next version so in this case it says i + + which is the C language way of saying bump the icon is like saying I was I + 1 people got tired of saying I was I + 1 all the time so they created i + + 1 to increment so so
this is what is going to do is to start out with the value i equals 0 it's gonna run through the loop say count 0 0 and then it'll bump to the next 1 and then I'll check and see up and understood that room there so keep going count 1 a 0 count to 0 Cup 3 0 it'll keep going all the way through the number of levels that i've defined and remember way back in the top 1 change the size my program i can redefine levels to be 27 and do a different simulation and this will just keep going as much as it needs to so all I'm doing there's going through that array of counts in this case and saying all the buckets to empty right those are the buckets at the bottom of my plea thing and I'm zeroing them all out to make sure the empty on 0 1 thing to point out in Fortran in Matlab all the indices start from 1 but in C language they start from 0 so gotta remember like all your a start from 0 in C language right so here's kind of the mean of my Plecos simulator and this other for loop I start out with a drop value equals 0 and I keep going until drop is less than Max Sum dropping and dropping and dropping these points and n each time I increment the drop out drop plus plus all I'm doing there's just making sure I'm doing it so many times so basically were dropping point through and at each point in the levels were around were going through all the different levels the deer and 48 generates a random number and checking to see if it's less than 5 or greater than 5 less than 5 and I go to the minus 1 side if it's greater than 5 I go to the plus 1 side this is kind of a funny syntax in C language and occasionally a few other languages of borrowed it it's a really compact way of doing it statement so what this does the rand 48 generates the random number and checks to see if it's less than 5 the question marks says if that's true then use the value minus 1 and the colon says it's faults then use the value 1 so it's a real compact way of writing it if statement look at that and if it's true use the value minus 1 if it's false use the value 1 so in our case that's like they're calling it the pagan were deciding strongly left right minus 1 plus 1 either way in the language you can write really dense code that way and 0 for better or for worse so what I'm doing is basically with that statement and figuring out the the plus or minus 1 randomly and then and then under my position I got a statement parse plus equals plus or minus 1 that means parse equals past plus 1 or particles parse minus 1 so my past position is going to shift left right so you might see like cold like that if you're looking for a C-language program and I want to make sure you've seen this statement the foreign sort how that works by the way is I'm going through a guys of this if I'm not talking about something and you wonder what something is feel free to ask and they just kind of taking over a general tour through the language so this is that part plus plus equal sign that I was telling about music powers plus equals 1 it's equivalent to saying particles past + 1 again the guys wrote languages got tired of typing stuff out and they invented a kind of a shorthand notation and that's been borrowed now into a bunch of different languages so you may have seen that in another language OK so the for loop just keeps going and keeps dropping coins through and until until dropped reaches the maximum count so maybe 500 thousand and that was the parameter that we ask the user for how many drops to we wanted do you could type in a thousand and there we go through this loop a thousand times each time starting at the top generating random number and working its way down through the number of levels until finally reaches the bottom of the certain position right and then at the very end of this program and you can see this little block this is where were printing everything out this is the output for a program on 1st of all you can see there's a lot to highlight the other comment line so in C language we want put comments in your code the way you do it is with slash star and then some stuff and star slash and and that's good to know and it's a little tricky because sometimes you know people forget that slash star and start typing a bunch of stuff and they forget to close it off with a star slash this type the star slash they convert it to characters or something that Comanches stays open the C language will ignore all the text until it sees that closing comment this star slash at the end there so that a lot of times if you don't put that in there if you mess it up then the compiler will just ignore three-quarters your program because of looking for the end of of what it thinks is a gigantic common right but modern languages now like C + + used 2 slashes at the beginning of a line Arabia pounded beginning of a line that makes it easier than you just have a common for the rest of the line but in C languages stays open until you close the comment like that and so watch out for that it's going to become it's your code right otherwise 6 months from now we put comments in your code you're actually writing a note to your future self it's like time travel so because 6 months from now you go back to this program to be like no what was I doing and especially when they're tricky part your code you wanna put a note in there to remind her future self they don't don't edit this don't delete this because it'll cause caught later or something future self fact so this but underneath the comment and praying out a headline statistics and I'm going through again another for loop going through all the levels in my twinkle program and print out the values the results on again I'm using the press statement to print out a string bucket and then some decimal number colon and then another decimal number and a new line so where you see the % D that's going to get substituted with some decimal value some integer value and and the 2 variables that are getting substituted and count survive in the counter array that the i'th element of the count rate so I'm printing out just like you see over here but it's 0 and then with the count and but 0 2 were whatever bucket 1 and what's the cow in bucket 1 so what so those 2 variables get substituted in water into the various % fields and then at the very end of my program and I can either say return 0 locus amoenus a function or I can say X is 0 which is equivalent basically terminates the program returns the 0 status code number 0 means OK that's good and that stands for the important if your program returns a status code of 1 and rapture runs a program gradual state your program failed will tell the user this program failed right so you have to make sure you return an exit the 0 axis status code otherwise other programs like rapture that depend on your program will say invalid didn't work so well run by so we got through it that's the C language code now the next step is to actually if somebody handed you that program you gotta run it you have to do something with it and we see language we have to compile the program down to machine code so there is a program called GCC for example that the new free
compiler and see you can use whatever compiler you want but and here we got gcc installed so GCC is the compiler and you can give it some flags and arguments the dashed g argument is important says debugging I wanna turn on debugging so I can get my program to figure out what the heck is wrong with it and so that's a real important option when you're compiling and the next thing we do is we give it the C-language program play . si that we wanna compile we tell it to put the output dashed following play go and program called go so when it's done compiling it will create an executable file called that's what you run and then at the very end we add a dash element which is the math library you your programs depend on other libraries a lot of times every pound include that you bring in probably brings in a library and so at the end you get add all those dashed l em dash L rapture whatever when you're compiling stuff to bring those libraries in the the free program are so having done that now if I've compiled my program I this executable Copeland go and I can run it by saying that slash plane go from the reason say . slash a lot of times people don't have the executable path set up to look in the current directory see it if you say just think of what they say Ottawa . 0 as you like right here dummy . slash plane go this 1 right here so that slightly goes way of saying this play go executable I just created dot is your current working directory and in the current working directory Run plane go and it'll say in number of drops and you'll type in 500 or something like that and then it'll go off and start spinning through that loop is is and the end it'll print out the statistics because it gets down to the bottom and it prints out all that protest stuff so to show you bucket 0 but it was bucket to at all that is interesting because our simulators not very sophisticated but it kind of works right middle few ever been to the carnival and looked at the plane goes simulator but when you're dropping like wherever you're dropping the coins tend to kind of pile up in the middle of that simulator and in fact I think of you know enough math you can figure out the gas in distribution and so what we're doing is we're doing a Monte Carlo simulation which if we drop enough coins will eventually produce the gas simulation which mathematicians will tell you that's the correct answer so there's our simulator so just to summarize everything that we just saw and maybe a few other things to and this is what the C-language program C language sort of Construx look like if you want to do a conditional things you want to an if statement in C language it looks like that you can say if and then some conditions and some statements and C language also has if like x is greater than 0 this else if x is less than 0 this and then there's also when all else fails to that and we see language all those bits of code all surrounded by curly braces in other languages like Python for train you don't have that but the language everything it's all wrapped up in curly braces like that so you have to be careful about those curly braces there's also something called a switch statement in C language the switch statement is sorta like and if it says if X is case 1 and do this and break if x is in the case to or default then do all this stuff and so and so forth so this which kind looks at the 1st value x matches it up against 1 of the cases and jumps down and runs you notice you always get a break out of the case because otherwise it'll do the statements and just keep going if I another break statement there it would you case 1 and fall through and decays to decays 3 and keep going so watch out for that the problem of other looping side of things like 2 different ways to look you can do a while loop while is not 0 just keep doing that bunches statements again and again and again you can do budget statements and then check at the bottom of the loop while the do edges statements and keep doing it as long as x is less than 10 so you have the while the do while form is there we talked about the for loop setting the condition checking it and incrementing as we go and then there the statements break and continue right we already talked a little bit about break your where breaks you out of whatever you're the middle of doing and continue is sorta like break but it it doesn't break you out of the for loop it'll take you back to the top and have you do it again so when you hit a continuous statement in a for loop it just gets rest and goes back up to the top and starts over a lot of times people put that inside and if they'll they'll start a for loop and they'll say well if this is true then skip the rest of this and go up to the top and keep going again so of those breaking continued statements are really useful in loops because they can get you out of loops turn-taking on next case so that your cheat sheet of all the different programming statements and against which years now and can I show you the same program but now in Fortran I think I see how different it is informed training and 1 of the 1st things you'll notice is that everything is shifted over by 6 spaces every everything in Fortran program starts in column 7 and that's because back in the old days in the 18 hundreds like when they used to use computers and of guys reversing this these gigantic but card punch machines ever see that that would basically it's like a giant typewriter that has paper cards they go on the side you have been to doing computer history museum you know well you if you went to the Computer History Museum and you saw the giant keyboard you put in the paper card and it turned out that the 1st like 6 columns you program was made up of a series of cards each card was 1 line in your program so by the for water right this whole program and over the whole deck a stack of cards representing the program and they always reserved the 1st 6 lines the 1st 6 so I'm sorry for 6 characters of each line of for these kinds of control structures which I'll show you down below so for trends vary in t kind of programming language based on these punch cards that they used to use will talk about some of the control characters like the plus and C in the 10 in just a minute but you start program is a program plane go and make sure it's over in the 7th column otherwise they will work only and the other thing about punch cards is that punch cards can only go out to about 80 characters so if your Fortran compilers really strict in trying to be very you know old-fashioned then it will stop you any characters because that's where the punch cards ran out of space of modern Fortran compilers of course can go beyond that and some of them do but some compilers actually force the character limit and that give you we're compilers if you make long lines right now about those funny characters if you're in the middle of that statement and in Fortran and declaring my variables here I've got an integer max and drop an iron parsing and declaring all these variables and I'm running into my any character limit right so I do and trying to go beyond 80 characters well you can continue on on the next line but you have to put a plus sign there and it has to be exactly in that spot so in column 6 if you put a plus sign or many compilers will let you put any character in that spot usually it's just a plus sign by convention but any character in that spot says this is a continuation of the previous line so so you can read that is the 1 you know continuing on
integer magstripe bypassed how levels and so forth in C language even just sort of keep going but in Fortran you have to be real careful about that the plus sign stuff because of all the punch card here's another magic character if you start with a character in the very 1st column that marks the rest of the line is a comment and usually people put a C there what common kind reminding you that this is a common but again most Fortran compilers would say any character in that spot makes a comment line and so I put the work I put a C there and I said set all counts to 0 and that's not programming stuff that's just the note for my future self of what I'm trying to do here and then finally in C language we had curly braces that can help put all the code together in Fortran when you write a loop you have to use numbers so so I can number statement like c can continue I can number that statement and then I can set up a loop by saying OK do everything until 10 i equals 1 go up to levels plus 1 keep going blah blah blah so this is the way you write a loop in Fortran is you have this is do I do continue thing you have to number the continues statements so the rest of the spaces in between the 1st column which is for comments and the last column which is for line continuation you have what for other spots there where you can put in line numbers but ended up hopefully you don't have to put too many of men turns out that matter when you call that statement 1 statement fiber statement tender statement 9 9 9 or whatever Fortran doesn't care you got 4 columns there use numbers and then you just match of all the numbers for your loops many interesting is like a a walk through the Computer History Museum and I forget how many of you guys actually use for Fortran before know we have tested 2 data we as yesterday and then I asked about your grandfather's alright alright I remember well it's still but it turns out that the reason for dressed up around so you made the you know now we're making fun of rivers grandfather's but the reason for transtec around is because it's actually a very efficient language in terms of mass people spent years and years and years developing great math libraries in Fortran and then they decided you know for Fortran sort of an old language right but it turns out there's tons a code written in Fortran and that is very good because of all the math libraries and I wouldn't be a bit surprised if you sit down with your advisor and he says OK here's your code you like grade is a Python no so that know it's fortunate and you like create words that lecture that was the point thing that I was supposed to be listening to catch a a couple more things that you know the basic structure of this program and for Fortran has variable declarations but it also is old enough that they used to do this thing called implicit declaration of the declarations used to be in Fortran anything that started like in the range of I'd and was assumed to be an integer value so if you create a variable called J for trend was seen as an integer k is an integer of course right we actually see a lot of code using I Jason case and things like that as images so in Fortran you'll soon that those are integers if you pick values like a 2 eight-year-old old easy you get a real number so I fight choose the T 4 temperature that would be a real number but that's a real easy thing to do you can some people back in the sixties that that was a great idea but it turns out when you're writing a large complex programs that's the easiest way to shoot yourself in the foot is that you declare something that you want to call 1 thing and it turns out that actually makes it an integer so in every program that I wrote I always turn that off it's better to be explicit about what you've got so by saying implicit none that says don't assume anything about my variables all tell you about every single 1 of them so if I say implicit none now it's up to me to define all those variables into levels integer max integer drop double precision or number all that stuff and and that's safer because that way if I forget to declare a variable compiler get mad at me 1 assume that has an integer value or something like that this is sort of a way of defining constants just like with the power defined in C language instead of the pound find you can set energy levels and parameter levels equals 9 parameters the thing in Fortran this as I write this thing is sort of a constant value but I might change at the next time I compiler and that I mentioned before about this is due to the fact that you statements like tend to kind of tie everything up so well in in Fortran I'm going through this loop here eyes 1 2 3 4 5 and I'm going through setting all the counts to 0 so in your mind you can imagine it lists where count 1 equals 0 cut to equal 0 . 3 equals 0 remember I said earlier in C language all the indices started from 0 in Fortran in Matlab indices sir from 1 so that's why we're starting from 1 year to 2nd half my program for trainees this is really crazy notation for things like less than greater than on the other is the greater than symbol on the keyboard 1 is that I think back in the old days there wasn't 1 there so if you go back far enough that it makes more sense support train has this crazy . L T and I L E and all that notation so here's your cheat sheet of what it means when you're looking at an if statement in this case I I use more about link the if statement to express it it's a little easier to read if my random number is less than 5 . 5 I'm sorry then parse equals 5 minus 1 otherwise possible sparse plus 1 that's exactly what I was doing in language except in C language was 1 line of code in Fortran account is longer look more readable but longer and cities all those crazy operators and there's even logical operators Franzen wars and all that the other thing is when you're writing stuff out for transfer of funny and Fortran has all this crazy stuff for formatting so in easy thing to do if you don't care about formatting is you can say right 6 thomas . 6 is 0 you should know this 6 is the unit number 4 standard out everybody knows that right so when you wanna write to the screen you write to 6 on if you wanna reading you read from 5 and they're all these file descriptors that were hard coded back in the day with for and so anyway you'll see this a lot of when you see people saying right 6 Thomas star you can just read that is pronounced that's people use that all the time is just write it out to the screen right 6 from stars here I'm writing the 6 again but now instead of starting using a specific format using format 99 and again you can see and using line numbers so which format format online 99 in that format says I want a printout it and then enjoy 5 spaces in the colon and an integer 5 spaces so again I'm sick it's telling the computer program exactly how I want the output to appear with these letters and integer with this linear spaces and all that kind of stuff and then those are the 2 values the iron accounts of either of the 2 values that get substituted into that format like so again that's the antique version of the pronounced
trash right and is your cheat sheet to similar g cheaper Fortran on if you wanna write in the if statement for training it's similar to see but it wasn't the curly braces it's if and then the parentheses then much stuff and and if so use then end if instead of the curly braces defined marker code but there is an if then else if then and in this case else if 1 word but else and then also fails you can have those statements to so those are the if statements there is a switch statement enough for train just if statements but that's fine but there's not really a while the others back in the day before they invented the while loop so what people used to do was using if they go to handle maybe program instructors probably told you never use go to that's good advice never use go to except when you're writing a Fortran program because it's the only way to write a while it so you can you can mark at the top of the while loop is something like 10 or 20 year or whatever the number and you say if X is less than 10 and do a bunch of stuff and then go back up to the top and keep going and keep going until finally you break out of the loop and loop is done you can do the same kind for do while thing where you start with a continuous state and you will do a bunch of stuff and then you check the if at the bottom of the loop just like a do while and go back up to the top and then we already saw the for loop where again using line numbers to mark the bottom of the loop and going through the the for loop is the start and the end and the incorrect so this says go from 1 to 10 steps of 2 1 3 5 7 9 2 5 so we made it through Fortran program to show so now we just have to compile it but they give you Fortran compilers called G Fortran modern 1 and again just like the C-language program we say gee for forgery and dashed G says I want debugging just like language turn on the debugging forming an idea the name of the program play go . after and then I say dash 0 . go on in for training could also have libraries but it turns out you don't need to Fortran builds a lot more libraries the math libraries and everything so you can add other libraries on in Fortran but most of the time you don't use many libraries as Fortran builds in and so now I've got the same thing and you notice it called go and probably from this point on you can't really tell when I'm running that program play go I can't really tell how was written when was written in C or Fortran on the inside I wouldn't know it just works the way it works but just like the other C language version of the Fortran version last mean number of drops cell type and 500 and then it'll judge a judge or do its work and then it'll print out the results 1 thing you notice because I said I want an integer 5 spaces I get a very rigid looking output with no spaces in here but but maybe it's slightly different MIC language version but but anyway saying stuff and really if I had the same random numbers I should get exactly the same results course my random numbers might change each time around the program so it changes a little trash alright so now you seen see and naive C Fortran I wanna go back and talk about how we build programs and talk about makefiles law that we saw makefiles just a little yesterday and I think it's important thing for you guys to know because you know you'll you'll be writing your own programs that they involve a compiler at all you should have a Makefile for your program why because when compiling this program GCC level and then I edit the program because there's a bug in it and then I compile it again and then I added again and you just keep going doing this right every time as a building program you you edit you compile you test you edit you compile you test it you keep going through that loop and if you keep having type a big long gobbledy-gook line of complicated stuff is really irritating it's easier to get wrong but also the more complicated your programs such as 1 line you have to type you might have to do 50 lines like that that you have to try to build the whole thing so so the compiling part can get really complicated in a hurry instead instead of typing all that stuff by by hand after the compiling UNISIG make and when you see make there's a program and Unix that looks for something called a Makefile and it follows all the instructions inside of it so if I say make the make program looks for a make file and the very 1st thing it finds a something called plant go and so says OK although that so how build plan go the 1st line says clean go colon plane good that that means clean-coal depends on the plea go . C so if you added singled out see the Makefile is smart enough to know that you need to rebuild the program so the Makefile automatically looks at the dates of all the files and says all you've just change the program then if you tell it to make it recompile because it knows I need to rebuild this because it depends on that so make follows all the dependencies in your code it knows your your program depends on this header file this source file and everything else and the way you tell it without calling go depends on legal that see then the next line down the tab over and the tabs really important because a lot of times people write a Make file and they put a bunch of spaces because they're copying example that they see somewhere if you put in spaces Make file gets really mad has to be a attacked why 100 always the seventies and it seemed like a good idea at the time right who knows where they were smoking about that but you put in a tab and then you put in the command that you would normally executable that so just like you would typed on the command line GCC dash G blah-blah-blah block file also has a bunch of
different syntax so that instead of typing things exactly you can put in all these weird dollar apps and things like that and I'll I'll save that for another day in terms of complexity but a very simple file just says please go depends only good at sea and whenever you need rebuild it just run this GCC line to rebuild you could have other things in the Makefile 2 I'll also the target call clean and viruses make clean then it'll do that clean depends on nothing and when I say make clean it's going to remove the dash F which means force all the Daidalos and think 0 so if I had a bunch of . 0 files sitting around those of things that are compiled by the compiler it'll clean those up and get rid of the co program clean everything up so that I can say make to build my program and I consider clean to get rid of my program clean everything up tight so very simple Make file and you can make a lot more complicated right so now having done that I can edit my program and I can make an edit my program and I can make and just keep doing that as a whole lot easier because I don't have to type all that gobbledy-gook and don't have to remember weight I recompile that no I don't know if I just make it'll it'll either do nothing or recompile everything or whatever it needs to do to make it so if you do make twice no changes does nothing and then finally I can say may clean at the end and it'll clean everything up it'll remove the Daidalos in the plea go whatever I told it to get rid of the clearing up trash but you almost ready for your lab assignment almost and what happens when somebody gives you program and he finally managed to get it to compile and you try running and then work what happens that so suppose I type and it does this new type in number of drops as a safe awkward does ever everything that Segfault poured out there so that basically means something somewhat horribly wrong in your program you're probably trying to address memory that doesn't exist or using a null pointer or something and then the program just can't continue didn't know what to do so it just stops dies so at this point you figure out what are I didn't get up on the rack and figure out what went wrong so there's a program in unix called gdb that goes along which CCG for training it's the new free debugger so it's border that works with all the compilers and if you compiled remember we talked earlier about minus G so few compiled with minus G then you can use GDP and a work right so GB . go will start RGB revealed gdb prompt in parentheses and you can type in some simple commands 1 simple command is l and it will list your program now so the type L you'll see the program and you can remind yourself 0 yeah aligned 13 was the part of function you can tell it to stop there and run and so forth so l lists your program if you wanna tell to stop somewhere like 1 13 you can say break break 13 or B I think works too but anyway he was a great 13 and I'll set up a break point so that when you run the program and stop there and let you check everything out before you continue want to buy so I've got this breakpoint now I say run it'll run the program and will stop at 13 waiting for me to do something I can look at some variables I can print some stuff out I can do n n is next if I type and it'll be due the next statement and then stopped due the next statement and stop even kind walk away through the program just by saying n n n n 1 statement at a time when I say N year it executes print and you can see it's a number of drops and it starts at the next day so if I say and again it away from you type something in and continue 1 so when I say any again now it's doing the Athens just sitting there waiting I typed 500 and then is done with the statement it continues on right so I'm kind of walking my way through the program and I can convince myself as I'm saying and that everything's working I can get will look like this and see OK i is 0 what is look like i is 1 what is count look like and I can keep going next next next through the program and I can convince myself next next next if if I print please is print by P print I that I can see OK now I'm at the case where the loop is 1 in lupus to lupus 3 so you can print stuff out and you can figure out what where you are and what's going on in the program and why its core dumping her hung up or whatever you can also do really fancy stuff with but there's a lot more to the new debugger you can do things like say break at 24 if drop equal equal 3 which is really cool is that where you can do a bunch of stuff and it'll get halfway in the loop and then it will break as opposed to you going next next next until you're blue in the face right so you can do things like that C is for continuing say it'll continue to the next break point working in the program and so for example it continues until it hits that next breakpoint stops and then at that point I can print out other variables drop and so forth so in general this is your little cheat sheet for denuded wonder if you say L and line number Alicia program if you breaking given line number it'll stop you can say run and give it some command-line arguments and run your program and you can say N for next and as 1st step and see for continuing and P for print and all that so that your cheat sheet of things when you're stuck inside the new debugger and you're wondering what to do these are the commands that you can use to to work it out by so
standard your lab assignment and you can work your way through yourself this is going to be edited lovers leader particularly slide you at this was his theory of so many of the congratulations you've inherited a program that almost work so this is the situation you're going to face where the last student grad student or whatever wrote a program in your advisor says 1 is start with this and then go from there right in your advisor doesn't know because he hasn't tried it but it turns out when you try it is completely broken it doesn't work 1st of all you can get it to compile them and 2nd of all when you actually do get it to compile it doesn't work right so you gotta figure out you never seen this thing before and you gotta figure out what it's doing how to get it to compile and how to make it work on this program is a lot like the Clean program that we looked at this a little different it's a letter frequency count and so the idea is you run this program that slash letters and it'll say type in a sentence and you type in hello world and then it'll go through all the letters in that sentence that you type into account of statistics so for 1 thing it account that the 2 words it ignores punctuation by the way I just looks at alphabetic characters but it'll count there 2 words hello and world so it prints out to words and then prints out the letter frequencies there's 1 the there's 1 in new tools and 3 Alison goes through and counts up all the letter frequencies right so that's what it's supposed to do in your advisor will swear you that it works and it does that in last 2 networking and you'll saddle what you and all your problem is that you can make it work right of so I that's your challenges you like I can make it work I can make anything work so well and do is download 1 of these to be the 1 you can use w and then type in the past and are either 1 of those will work wrapper letters that C or letters that after if you prefer language use letters that see if you want to walk on the wild side with the punch card Time-Machine then use the letters that are for for Fortran either 1 of your 1st job is going to be make a make file and that should always be your 1st job someone hands you a program that you have to compile you say what I can do by hand or make a make file so I want to create a file to compile in the letters that C or letters that after 1st job and I want you to use the file to compile it and as soon as you try to compile it you're going to find errors in the program so you're going to go through at least get it to compile and once you get to compile and run it you're going find it doesn't work and then I want you to get into the debugger and try to figure out why doesn't work and at the end of this you will have done all of that you will meet Make file you will have and fix the program to the point where compiles you'll have gotten into the debugger you'll figure figured out everything that's wrong with the program and you'll have a working program that looks like this so let me walk you through the solution that I've got for this particular assignment so in my boot
camp 2012 directory and I've got nowhere was in the right directory thing it by 2 there's 2 flavors of this exercise others walk through the C language version to the most people were using that and so there is a a C language version here and then I'll just copy that and because this is sort of the solution array of from make a new directory then copy letters that C into new right so I've got this program now letters that C and I got I try to make it work so the 1st thing I want to do is create myself a Makefile because I'm gonna be compiling again and again and again and it just makes it easier to have to make files on a tight
bunch of crap so my makefile I I say I've got a program called letters that of that depends on letters . C and then the next line down I can have over the tabs real important otherwise make its own magnetic your file and then I say what my compile command is going to be GCC dash GU member dashed G is important because I wanted to but this program to always put the dashed g in there to get the debugging info I think a few of you might have stumbled into that and gdb GB wasn't showing meaning useful false because you you're missing the minus G option so I'm going to compile letters dash C and put the output into a program called letters and put in the math libraries or whatever else I need I'm also gonna make myself books on a clean target and the clean target just says if I wanna start over and I can get rid of all the Dido's if there are any there will be in this case and then the word of the compare letters that I compiled so that's my makefile and as long as it's called
Make file with a capital M like you see here and as long as it's in the same directory as my program and as long as the format with the
tabs and everything that we talked about and then I'll be all set to
go so now I can say make any goes ahead and runs the compiler program and and that shows me all my errors so that is good that I got a Makefile can only be making this a few times and I'm gonna go through each 1 of these things always start at the top because when when C language or any compiler encounters an error will tell you the air and I'll try to move on and sometimes that error causes another area which causes another and if you start at the bottom you be like hopelessly confused so start at the top and fix the 1st error and keep going until they tell you tried again at some point right so the 1st thing it tells me is that online and 29 there's a lot of variable called N words it says any words is undeclared so going to my
editor and I go to line 29 and it tells me that the N words here is undeclared so I have to declare all my variables right but I'm like wait a minute right up here it says into words equals 0 in 10 words equals 0 and yet it's telling me and words as undeclared that is like the craziest thing in the world because it's declared that you notice that in this particular editor just for readability I turned off syntax highlighting a lot editors have syntax highlighting building on so you can kind of see what's going on in a file syntax highlighting is a very good thing but when we show
you another view of the same thing if we go into the assignments and see and new and we pull up my solution my assignment this is what you know this is your brain on drugs this is your brain on syntax highlighting syntax highlighting will will highlight various keywords so you noticed like all of year variable declaration tightly into the top image in char in in those are all in green you notice words like foreign while are all in red you notice all of your you're character strings are shown here in purple right so it's really easy when you're looking at something with syntax highlighting to see all the parts your program you can see the way the compiler sees it this way the compilers look in your program and this 1 the other thing is that the compiler does is it draws all comments in blue so great here for example this is a common is highlighted in blue right and up here we have some comments that are highlighted in blue and you notice this is highlighted in blue as far as that compilers concerned that declaration is a comment why is it a comment well remember in language common stock with slash star and they keep going and keep going and keep going until it finds the closing star slash that's really easy mistake to make in C language you have a comment this sort of goes and goes and goes and consumes your whole programs in this case a the variable declaration and it's like getting it hadn't wanted to do that
and I could do 1 of a couple of things I can I can end the comment here and then all of a sudden now you know this is color differently so as soon as I added that closing comment there in words equals 0 shows up as if the compiler recognizes it and some people also put the books used to mine VI some people also put the young star slash star slash at the end of each line and slash at the beginning just to avoid problems like that and so you can also do your comments like that and try to keep them 1 line at a time and but anyway that really matter the the important thing is that it's actually nice to have a syntax highlighter when you're very confused about something because it will show you what the compiler seen so
let me compile again and you notice I get a little bit further now it's telling years that line 31 and saying expected a semicolon so that line 31 it's saying it expected a semicolon and you notice there is no semicolon and compiler saying it expected a semicolon still you with the problem is in C language every statement has had a semicolon at the end of it you can see that on the line above there's a semicolon but there's no 1 here so that's OK I can fix it and then I make again it's telling me letters is undeclared aligned 35 and so i go line
35 is saying see greater than 0 see less than letters and see what I thought I had letters and use that somewhere and if I go to the top of my program and I search for letters this there right but actually n letters so I I forgot this was to be n letters so everywhere else I was smart enough to say and letters but not their suffix that and make again I'm getting further almost there line 47 it doesn't like got terminating quote characters so 47 0 OK that's because it's telling I didn't terminate the strain and yet it's right I'm missing a quote character remember with pride after I have a string that says what i want printed out letter character of some number of new line and then these the next things under the rest of the function call those are the things I want substituted in place of the % so I was missing coca that just like the compiler says and I make now it finally works for a Nikon right so I typed letters and it's not doing anything but I can type that's OK Hello world is not doing anything right now people ask me why isn't doing anything and the answer is I don't know as the program right how do you ask the program you get into gdb when something doesn't work your friend is GDP his AGB letters and if you say L it'll show you your program so can reminds you what's there and set a breakpoint line 22 because I wanna get here and figure out what's going on I should say type
in a sentence on the run that not
getting the line 22 where the heck am I if I hit Ctrl-C it will tell me
where I am it shows me underlying 19 I mean not line 22 yet and if I that never you for next next next next
next next next I mean this loop where it's going through and initializing the count and if I pronounce i is 0 and if I go through a few more times and print out i is still 0 it's like stuck here never going on in the loop what's going on well for i equals 0 starts at 0 while i is less than n letters it's true I our banks was the i plus plus a little right cluster the bond to the next value on the loop fights so I can quit that and and it looks that
letters but letters that C and so now I go back there and fix my loop so I'll put in the plus + socio lupus closer look like that right was 0 i is less than whatever and then i + + that moves on to the next to the next value so now I have to make again right because if you go try run your program again nothing's changed if I'm writing letters it's still stock that's because I have to make again I have to make the brand new copy and now when I run letters it's actually doing something different so I can type in a B C D E F and well alright this sort of there so it's sort of working now I can see it saying a B C D E F there's 2 words but when I go to print out just tell me there's a B and is at the end of the day after day we see what's going on there why is it working I don't know as so in GB and then go through the whole program and on a break down here at like 44 run at the tight ABC D E F OK now at this point I'm about to print out and words which is too that's good but I'm also about to print out the count I look at the count and you can see ABC D E F is good so it should work so now I'll continue doing it the Council right but it's not bringing and what's going to on let me take a look more closely on the run it again start over a B C D E F and now this time and this step through I'm stepping through and I'm praying I is 0 and the print out tau sub 0 or I can also say count so by looking at 1 so here's my print statement I can't even pronounce things like i plus a kind of check everything that's the character 97 which if you look up in the ASCII table is a lower case a right so and I can print out count so 0 and again I convinced myself that that's it so I printed out wait a minute it's single letter B and it's saying 1 like I thought I convinced myself that I was about to print letter a and a print out letter B and now I'm going through my count if I print out i it's said to going from 0 to 2 and print out letter be like what the heck but if you look look closely we go back through this OK and that too and I do that now at 3 and now I meant for doing it like the I counters all goofed up and if you look look closely you'll see right here there's an i + + so when I was praying stuff out I print out i + saying and integrated out I count survive and in the plus + after it says right after I turn the count go ahead and move on and then when I got to the top of the loop that moved again so every time through the loop it was kind of bonding I'd like to bumping it once what I wanted to and bumping it 1 more time when where wrote this program screwed up so anyway go back to my program and take that out and try to get array and now we can try other cases because I never trust my program and everything works so that's how you build and debug and that's like the bread and butter of what you do when you're building when you do in software right are sometimes you get a program that you inherit that you didn't have anything to do with the new gotta figure out sometimes you created the mass but it's the it's the old you it 6 months ago you and hopefully 6 months ago you wrote something of comments to can help future you figure it out but that's it you make yourself a file to make it easy to compile you get into the debugger and you walk through your program line by line and that check every square inch of it there's so many programs of so many bugs because nobody looked you know you just assume if your program runs it works properly really whenever you write a program should go through what should execute once if you if you make sure if you convince yourself once that everything looks good then you probably in pretty good shape but a lot of people just write code assume it's gonna work perfectly the 1st time that and never does


  915 ms - page object


AV-Portal 3.20.2 (36f6df173ce4850b467c9cb7af359cf1cdaed247)