T.U. Cluj-Napoca - Computer Programming - lecture 1 - M. Joldoş 1
Computer Programming
“Learn science first and then continue with the practice born from that science”
Leonardo da Vinci
T.U. Cluj-Napoca - Computer Programming - lecture 1 - M. Joldoş 2
Outline
� Who and What
� Computing: discipline and profession
� Software and Piracy
� (Very) Brief Introduction to Computer Systems
� Program Systems
� Programming Languages
� Stages for solving problems using computers
T.U. Cluj-Napoca - Computer Programming - lecture 1 - M. Joldoş 3
Who and What
� Who:
� Marius Joldoş: lectures + laboratory supervision
� http://users.utcluj.ro/~jim/CP
� Laboratory supervision
T.U. Cluj-Napoca - Computer Programming - lecture 1 - M. Joldoş 4
Who and What
� What: Computer Programming
� Lectures: 2 hours/week, 14 weeks – every Tuesday, 08:00 hours, G. BariŃiu 28, room 40
� Laboratory work: 2 hours/week, 14 weeks, as scheduled for each half-group, DorobanŃilor 71-73, room D1
� Self-study: 84 hours
� Worth 5 credits (1/12 of a years total)
T.U. Cluj-Napoca - Computer Programming - lecture 1 - M. Joldoş 5
Where are the rooms?
T.U. Cluj-Napoca - Computer Programming - lecture 1 - M. Joldoş 6
How you identify the place
� Panorama from the opposite sidewalk
busbus stop, stop, 46B46B
T.U. Cluj-Napoca - Computer Programming - lecture 1 - M. Joldoş 7
How you identify the place
� Cross the street (on-demand traffic lights)
T.U. Cluj-Napoca - Computer Programming - lecture 1 - M. Joldoş 8
How you identify the place� Yellow
building
at the front
� Enter
� Climb downleft
� Door on theright is for room D1
� Door in front is for roomD2
EntranceEntrance
T.U. Cluj-Napoca - Computer Programming - lecture 1 - M. Joldoş 9
What: Computer Programming
� What you will acquire� As theory
� To describe algorithms in pseudo-code
� To modularize an algorithm
� C(C++) foundations (w/o objects...)
� Some algorithms
� As abilities� How to design and implement algorithms in C(C++)
� An adequate programming style
� Master some algorithms (numeric, set)
T.U. Cluj-Napoca - Computer Programming - lecture 1 - M. Joldoş 10
What: Computer Programming
� Lectures� 1. Introductory matter (i.e. this). Problem solving stages.
� 2. Brief introduction to algorithm descriptions in pseudo-code.
� 3. Programming style. Digital representions.
� 4. Variables. Functions
� 5. Preprocessing in C. Standard I/O. Expressions
� 6. Statements. Pointers and pointer operations. Memory allocation/deallocation
T.U. Cluj-Napoca - Computer Programming - lecture 1 - M. Joldoş 11
What: Computer Programming
� Lectures
� 7. Midterm. Pointers and functions
� 8. Pointers and functions
� 9. Recursivity
� 10. Data types: structure, union, enumeration
� 11. Files
� 12. Numeric algorithms sample design andimplementation
� 13. More program samples
� 14. Wrap-up and review
T.U. Cluj-Napoca - Computer Programming - lecture 1 - M. Joldoş 12
What: Computer Programming
� Laboratory work� 1. Pseudocode
� 2. Using Codeblocks IDE. Standard I/O
� 3. Expressions
� 4. Statements I
� 5. Statements II
� 6. Functions
� 7. Modular programming
� 8. Midterm lab test
T.U. Cluj-Napoca - Computer Programming - lecture 1 - M. Joldoş 13
What: Computer Programming
� Laboratory work
� 7. Pointers
� 8. Recursivity
� 10. Character strings
� 11. Data types: structure, union, enumeration
� 12. Files – high level
� 13. Wrap up
� 14. Laboratory test
T.U. Cluj-Napoca - Computer Programming - lecture 1 - M. Joldoş 14
What: Computer Programming
� Course info resources� Handouts (pdf, slides, on course web page)
� Lecture notes (printed sample)
� Laboratory guide (pdf, on course web page)
� http://users.utcluj.ro/~jim/CP
� http://172.27.207.2:81/CP (local in room D3) and
� Brian Kernighan, Dennis Ritchie The C ProgrammingLanguage. Prentice Hall, 2edition,. 1988
� Herbert Schildt, C++ the complete reference, Osborne McGraw Hill, 1995
T.U. Cluj-Napoca - Computer Programming - lecture 1 - M. Joldoş 15
What: Computer Programming
� To get your credits
� Attend classes
� Study, learn
� Do the assignments (3 or 4 problem sets)
� Grading
� Laboratory evaluation (LE) + written exam (WE)
� LE ≥ 5 and WE ≥ 5
� Formula: 0.35*LE + 0.65*WE
T.U. Cluj-Napoca - Computer Programming - lecture 1 - M. Joldoş 16
Computing: the Discipline and the Profession
� Computer Science is concerned with� Information processes, with the information structures, and
� procedures that enter into representation of such processes, and
� with their implementation in information processing systems
� Relationships between information processes and classes of tasks that give rise to them.
T.U. Cluj-Napoca - Computer Programming - lecture 1 - M. Joldoş 17
Computing
� Computer Science is concerned with� Digital computers and the phenomena surrounding them.
� Principles that underlie their design and programming,
� Effective methods for their use in different classes of information processing tasks,
� Theoretical characterizations of their properties and limitations
� Explorations and experimentation with new computer systems and with new domains of intellectual activity where computers can be applied.
T.U. Cluj-Napoca - Computer Programming - lecture 1 - M. Joldoş 18
Computing: the Discipline and the Profession
� NOT� Programming
(medium of expression in computing)
� Use of computers by professionals in other fields
� Sub discipline of electrical engineering, mathematics or other.
� YES� Study of algorithms and computers
(theories, abstract models, mechanical realizations, reliability and verification, measurement and efficiency, linguistic description, social context).
� Mathematical discipline (its theories and style of investigation and communication are necessarily rigorous).
� Scientific discipline (theories and abstractions are continually evaluated and tested in an experimental laboratory setting).
� Engineering discipline (design, analysis, efficiency, reliability, and correctness are central in all its nine subject areas
� Direct and profound impact on the quality of life and society at large.
T.U. Cluj-Napoca - Computer Programming - lecture 1 - M. Joldoş 19
Software and Piracy
� Software Copyrights
� Law protects the original work of authorship.
� Plagiarism = failure to acknowledge another author’s work when that work is included in one’s own work.
� Copyright law applies automatically to an original work from the day it is created until 50 years after the owner’s death
� Copyright protection © means that the owner of the copyright has the exclusive right to make, use, and/or sell the original work.
T.U. Cluj-Napoca - Computer Programming - lecture 1 - M. Joldoş 20
Software Patents
� Software Patents.� object program and user interface
can also be interpreted as a process or machine
� Need applications similar as the ones for inventions do need
� USA: a patent remains in force for 17 years
� Scope of a patent extends to the whole family of processes that have the same effect as the original product.
� Some algorithms may be patented if they are unique and original.
T.U. Cluj-Napoca - Computer Programming - lecture 1 - M. Joldoş 21
Software Trademarks
� Software Trademarks
� ”any word, name symbol, device, or any combination thereof adopted and used by a manufacturer or merchant to identify its goods...and to distinguish them from those manufactured or sold by others”.
� trademark symbol TM or ® (registered trademark) to acknowledge that
T.U. Cluj-Napoca - Computer Programming - lecture 1 - M. Joldoş 22
(Very) Brief Introduction to Computer Systems
� Computer system: a physical complex of equipment (hardware) + a program system (software) which ensures automatic processing of information as specified by algorithms derived from the user’s needs
� An algorithm shows the method used to solve a problem in a finite number of steps
T.U. Cluj-Napoca - Computer Programming - lecture 1 - M. Joldoş 23
(Very) Brief Introduction to Computer Systems
� Algorithm example: greatest common divisor (gcd)
� Problem statement (by the user): find the gcd of two natural numbers: a and b
� Problem algorithm (by the program developer): a sequence of steps1. Read the values of a and b2. Compute the remainder R of the division of a by b (i.e. R =a
mod b)3. If R=0 then goto step 64. Assign the value of b to a and the value of R to b (a:=b;
b:=R)5. Goto step 26. Write ‘gcd=‘, value of b7. Stop
T.U. Cluj-Napoca - Computer Programming - lecture 1 - M. Joldoş 24
(Very) Brief Introduction to Computer Systems
� A view of a computer system (von Neumann model)
T.U. Cluj-Napoca - Computer Programming - lecture 1 - M. Joldoş 25
Typical bus connections
T.U. Cluj-Napoca - Computer Programming - lecture 1 - M. Joldoş 26
(Very) Brief Introduction to Computer Systems
� Input unit(s): input conversions (to ASCII, EBCDIC, pure binary, numbers coded, etc.)
� Output unit(s): output conversions to human usable forms
� Bit, byte, word, memory capacities
� Bit: 0 or 1
� Byte: typically 8 bits
� Word: machine dependent, a number of bytes
� 1kB = 1024 bytes, 1MB = 1024 kB, 1GB = 1024 MB
T.U. Cluj-Napoca - Computer Programming - lecture 1 - M. Joldoş 27
Program Systems (software)
� Software classification:
� Basic software:
� Built-in: basic functionality, (hard)-coded by the manufacturer
� Application software
� Many different suppliers
� Solves specific problems
T.U. Cluj-Napoca - Computer Programming - lecture 1 - M. Joldoş 28
Program Systems. Basic software
� Operating system� Manages resources (memory, peripheral devices, CPU(s), I/O
processors) for optimal use
� Provides assistance to user, operator, maintenance personnel
� Examples: Unix, Ms-Dos, Microsoft Windows
� Programming system:� Text editors*
� Translators (assemblers, compilers, interpreters)*
� Link editor
� Loader
� Debugger*
� * - glued together in Interactive Development Environments
T.U. Cluj-Napoca - Computer Programming - lecture 1 - M. Joldoş 29
Stages from Source to Executable
� Compilation: source code ==> relocatable object code (binaries)
� Linking: many relocatable binaries (modules plus libraries) ==> one relocatable binary (with all external references satisfied)
� Loading: relocatable ==> absolute binary (with all code and data references bound to the addresses occupied in memory)
� Execution: control is transferred to the first instruction of the program
� At compile time (CT), absolute addresses of variables and statement labels are not known.
� In static languages (such as Fortran), absolute addresses are bound at load time (LT).
� In block-structured languages, bindings can change at run time (RT).
T.U. Cluj-Napoca - Computer Programming - lecture 1 - M. Joldoş 30
Phases of the Compilation Process
� Lexical analysis (scanning): the source text is broken into tokens.
� Syntactic analysis (parsing): tokens are combined to form syntactic structures, typically represented by a parse tree. The parser may be replaced by a syntax-directed editor, which directly generates a parse tree as a product of editing.
� Semantic analysis: intermediate code is generated for each syntactic structure. Type checking is performed in this phase. Complicatedfeatures such as generic declarations and operator overloading (as in Ada and C++) are also processed.
� Machine-independent optimization: intermediate code is optimized to improve efficiency.
� Code generation: intermediate code is translated to relocatable object code for the target machine.
� Machine-dependent optimization: the machine code is optimized.
� On some systems (e.g., C under Unix), the compiler produces assembly code, which is then translated by an assembler.
T.U. Cluj-Napoca - Computer Programming - lecture 1 - M. Joldoş 31
Program Systems
� From source code to executable
T.U. Cluj-Napoca - Computer Programming - lecture 1 - M. Joldoş 32
Programming Languages
� Programming language: notation for writing programs� Specific syntax
� Use keywords with well-defined semantics
� Program: data description + processing statements
� Programming: program development = algorithm development + coding in an appropriate programming language
T.U. Cluj-Napoca - Computer Programming - lecture 1 - M. Joldoş 33
Programming Languages
� Short history: http://www.princeton.edu/~ferguson/adw/programming_languages.shtml
� 1955, John W. Backus (IBM) – FORTRAN (1957)
� John McCarthy (MIT) - LISP
� 1960, Peter Naur – Algol 60
� 1959, a committee COBOL
� 1964, BASIC
� 1970 Alain Colmerauer and Philippe Roussel - Prolog
� 1971, Niklaus Wirth (ETHZ) – Pascal
� 1972, Dennis M. Ritchie and Brian W. Kernighan – C
� 1980, Bjarne Stroustrup (AT&T) – C++
� 1995, Sun Microsystems – Java
� 2000+, Microsoft C#
http://www.levenez.com/lang/history.html
T.U. Cluj-Napoca - Computer Programming - lecture 1 - M. Joldoş 34
Defining a problem
� Basic requirements for a well-posed problem:
� The known information is clearly specified.
� We can determine when the problem has been solved.
� The problem does not change during its attempted solution.
T.U. Cluj-Napoca - Computer Programming - lecture 1 - M. Joldoş 35
Problem analysis
� Results of the analysis stage:
� A clear and precise statement of the problem to
solve (the function of the program)
� Input data specification
� Output data specification
� I/O data organization the media
T.U. Cluj-Napoca - Computer Programming - lecture 1 - M. Joldoş 36
Program design
� Program design: elaboration of an algorithm to achieve the function of the program� Knowing the problem, establish the method to solve it
� For simple problems: a compact algorithm
� For complex problems: problem decomposition
� Complex problems: two stages:� Problem decomposition into subproblems. For each subproblem, find a clear specification and describe an algorithm for solving the problem in terms of subproblems
� Find the algorithm to solve each subproblem� Subproblems may be further decomposed
T.U. Cluj-Napoca - Computer Programming - lecture 1 - M. Joldoş 37
Program design
� Program resulted using problem decomposition: composed of a collection of program modules
� A program module results from the implementation of an algorithm in a programming language
� Program design: set the modules and the logic chaining (inside modules and between modules)
� A module has three features:� Function
� Logic
� Interface – input and output data specification
T.U. Cluj-Napoca - Computer Programming - lecture 1 - M. Joldoş 38
Benefits of modularity
� The capability of decomposing a complex system into simpler pieces
� The capability of composing a complex system from existing modules
� The capability of understanding a system in terms of its pieces
� The capability of modifying a system by modifying only a small number of its pieces
T.U. Cluj-Napoca - Computer Programming - lecture 1 - M. Joldoş 39
Program design
� Detailed logic for each module: described using flowcharts or pseudo code
� Design techniques for the modular structure of large programs:
� Top-down
� Bottom-up
T.U. Cluj-Napoca - Computer Programming - lecture 1 - M. Joldoş 40
� Given the program to design P, which will accomplish function F� Decompose F into a number of sub-functions, say F1, F2, F3, provided that F is completely fulfilled by functions F1, F2, F3.
� Three modules result, say P1, P2, P3, accomplishing F1, F2, F3, respectively
� Final result:
Program design. Top-down
PP
PP33PP22PP11
T.U. Cluj-Napoca - Computer Programming - lecture 1 - M. Joldoş 41
� Module P is called a main program
� Describes the relationships between P1, P2, and P3
� This way we start from the top (from the general problem) and proceed down (to details)
Program design. Top-down
PP
PP33PP22PP11
T.U. Cluj-Napoca - Computer Programming - lecture 1 - M. Joldoş 42
Program design. Top-down
� If function F is simple enough – represent it directly (pseudo code, flowchart)
� Otherwise – decompose it into modules. Result: a tree structure (example)
PP
PP33PP22PP11
PP1212PP1111 PP2222PP2121 PP3232PP3131
T.U. Cluj-Napoca - Computer Programming - lecture 1 - M. Joldoş 43
Program design. Top-down
� Benefits:
� Modules for each hierarchy level are fully tested and integrated with preceding program units, ahead of coding lower levels
� Ease of code changes: most changes are done at lower levels, the last ones to code
T.U. Cluj-Napoca - Computer Programming - lecture 1 - M. Joldoş 44
Program design. Bottom-up
� Start at the lowest level (highest detail), code modules with specific functions
� Combine modules to obtain overall functionality
� Structure: tree, built from the leaves
� For each module:� Determine its logic
� Code
� Test
T.U. Cluj-Napoca - Computer Programming - lecture 1 - M. Joldoş 45
Program design
� Bottom-up � Testing: design driver programs for testing each module
� After testing: integration and test of the complete application
� Most frequent errors: integration errors (communication among modules)
� A program design contains:� A representation of program structures, illustrating the modules of the program and the relationships among them
� A description of the logic for each module
T.U. Cluj-Napoca - Computer Programming - lecture 1 - M. Joldoş 46
Program implementation
� Implementation stages� Coding of each module in a programming language
� Editing the source code using a text editor
� Translation of the source code into object code and debugging syntax errors
� Translators can be assemblers or compilers
� Link editing to obtain a memory image (executable code) of the program and debugging link editing errors
� Link editor
� Loading into the core memory, running the program and debugging logic errors
� Logic errors are hard to find if no appropriate programming style is used
� Sometimes logic errors require the reconsideration of the whole logic
T.U. Cluj-Napoca - Computer Programming - lecture 1 - M. Joldoş 47
Program implementation
� Source code, object code and the executable code are stored in files
� Text editor, compiler, link editor and debugger can be integrated into development environments (IDEs)
� Program testing means experimenting
� Supply input data for which output is known
� All branches must be tested
� A program which satisfies testing requirements is considered validated or correctly implemented
T.U. Cluj-Napoca - Computer Programming - lecture 1 - M. Joldoş 48
Documenting a program
� Program documentation is used for maintenance and program usage
� Contents� Problem statement and I/O data specification� A presentation of the solution of the problem� Program design
� Modular structure and inter-module relationships� Description of the logic of each module
� Source code listing� Input and output data used in testing� Operation instructions concerning data package structure, I/O formats, error messages, media requirements (mounting of disks, tapes, etc.)
T.U. Cluj-Napoca - Computer Programming - lecture 1 - M. Joldoş 49
Program maintenance
� Maintenance
� Changes for error correction discovered during program usage
� Changes to adapt to new requirements
� Program documentation is especially important for this stage
� Documentation must be continually updated to reflect changes
T.U. Cluj-Napoca - Computer Programming - lecture 1 - M. Joldoş 50
Summary
� Who and What� Computing: discipline and profession� Software and Piracy� (Very) Brief Introduction to Computer Systems� Program Systems� Programming Languages� Stages for solving problems using computers� Stages for solving problems using computers
� Problem analysis� Program design� Program implementation� Program documentation� Program maintenance