PV178: Programming for the CLI Environment Seminar: Week 1 Ondřej Krajíček, et al. Institute of Computer Science and Faculty of Informatics Masaryk University March 2, 2007 * S Q Examine basic C# program (console application) B Explore the Microsoft Visual Studio 2005 IDE (abbrev. MSVS8) B See how MSVS8 handles projects Q Create, build and debug simple C# program B Examine the resulting assembly B Loops and conditions B Sample applications Basic C# program i 2 3 using System ; namespace BasicProgram 5 class SimpleClass { 9 10 } 12 i public s t a t i c void Main ( s t r i n g [] args) / / Do something here. * s ˇ line 1: using statements are used to declare namespace references * line 3: namespace statement is used to define a new namespace * line 5: SimpleClass class definition * line 7: Main method definition - program entry point Task: Create an application which outputs the number of command line arguments and lists the individual arguments. The output is made to the standard output stream. The sample code is available in learning material for the PV178 course in the IS. Download the archive, uncompress it to the folder where you have R/W permissions (such as your home directory), locate the Seminári.sin file and open it in the Visual Studio IDE (such as by double clicking on it). ˇ code is organized into projects and solutions * project represents is a set of source files, resources, configuration files, etc. * the result of project compilation is an assembly * project is a compilation unit * solution is a group of (inter)related projects * for complex tasks, having just one project may be insufficient * solutions and projects are managed using Solution Explorer * sample code contains one project ( S e m i n á r i . v c p r o j ) and one solution (Seminári . s i n ) ˇ syntax highliting * code folding (manual: # r e g i o n , #endregion) * ItelliSense * code completion, tooltips * pioneered by Microsoft, adopted in all major IDEs now (Borland, IBM, etc.) * controversial but useful - many "programmers" rely on it .uilding the sample project build: to perform all steps necessary to create a project output * build individual project or entire solution * "Build" menu in the IDE * build is incremental (builds only missing or changed parts) clean: to clean (remove) all intermediary and result files produced by build process rebuild: clean and build try building the sample project try to change something in the source code to produce an error (e.g. change c l a s s to CLaSS in the Program.cs file) * s ˇ "Debug" menu * usually: stepping an execution of a program to examine the state to detect and fix errors * try running the program (Start Debugging and Start Without Debugging) - whaťs the difference? * stepping through source code (Step Over, Step Into) * breakpoints (break program execution when it meets specified conditions) * try to create a breakpoint (F9, context menu or double click the leftmost grayed column in the code editor) * try to start the program with the breakpoint set C# Loops i w h i l e ( c o n d i t i o n ) { 2 / / loop body 3 } i do { 2 / / loop body 3 } w h i l e ( c o n d i t i o n ) ; i for ( l o o p var d e f ; c o n d i t i o n 2 / / loop body 3 } l o o p var u p d a t e ) { * s i foreach (loop v a r i a b l e def i n sequence) { 2 / / code execute for each item in sequence 3 } Essentially a high-level construct for looping over containers, sequences, lists. Semantically equivalent to while, but offers simpler syntax. condition statements i i f ( c o n d i t i o n ) { 2 / / do something 3 } i i f ( c o n d i t i o n ) { 2 / / do this if condition holds 3 } else { 4 / / do this otherwise " } * S A prime number (or a prime) is a natural number greater than one whose only positive divisors are one and itself. Eratosthenes (276-194 B.C.) was the third librarian of the famous library in Alexandria and an outstanding scholar all around. He is remembered by his measurement of the circumference of the Earth, estimates of the distances to the sun and the moon, and, in mathematics, for the invention of an algorithm for collecting prime numbers up to a given integer. The algorithm is known as the Sieve of Eratosthenes. Idea: Number p is a prime iff p is not divisible by any prime lower than p. So we find the lowest undiscovered prime and flag all its mutliplies as nonprimes and continue with next lowest unflaged. Heapsort is one of the best general-purpose sort algorithms, a comparison sort and part of the selection sort family. Binary heap is a binary tree with two additional constraints: D The shape property: the tree is either a perfectly balanced binary tree (all leaves are at the same level), or, if the last level of the tree is not complete, the nodes are filled from left to right. B The heap property: each node is lower than or equal to each of its children according to some comparison predicate which is fixed for the entire data structure. Heapsort creates a binary heap and then performs extract_min operation until the heap is empty. ˇ s