Lecture II Basic computer and programming concepts Yi

Lecture II Basic computer and programming concepts Yi Lin Sept 7, 2006 COMP 208 Computers in Engr

What is computer? l Computers are clever idiots l l l Who gives the orders? l l Idiot: Do whatever it is ordered; Clever: But fast and never wrong. Programmers In what forms? l Programs in certain syntaxes (i. e. , in certain programming languages) 34253 * 98573 * 15462 = 5 ? 2206221022678 11/9/2020 COMP 208 Computers in Engr 2

Computer 11/9/2020 Software Windows XP, Linux, Microsoft Office, Internet Browser … Hardward Pentium 4 CPU, AMD, 512 MB DDR memory, 19 inch LCD, Keyboard, … COMP 208 Computers in Engr 3

Computer Hardware Command n …… Command 2 Command 1 storage l Von Neumann model l FETCH and EXECUTE Command n …… Command 2 n results processor Monitor 11/9/2020 COMP 208 Computers in Engr 4

Computer Hardware Keyboard Hard Disk Memory BUS Cache Register Monitor Printer CPU 11/9/2020 COMP 208 Computers in Engr 5

The 5 Classic Components Computer CPU 11/9/2020 Memory Control Input Devices Registers Output Devices COMP 208 Computers in Engr 6

Input / Ouput Output Input 11/9/2020 Those are only the ones I came up with when I wrote this slide… COMP 208 Computers in Engr 7

Computer Software (program) l A program is a set of step-by-step instructions that directs the computer to do the tasks you want it to do and produce the results you want. In what form? l E. g. , Windows XP, Red hat, Microsoft Office (Word, Power. Point, Excel), Computer Virus … l Showing a few example programs, Ball. Game, Typing. Game, … 11/9/2020 COMP 208 Computers in Engr 8

Different natural languages How are you? Wie geht es Ihnen? And more …… Comment allez-vous? 11/9/2020 你好� ? COMP 208 Computers in Engr 9

Different programming languages C++ C# C Assembly (Intel 8086, Motorola 68000) Machine language Fortran 11/9/2020 Java COMP 208 Computers in Engr And more …… 10

Levels of language Natural language Very high level language (e. g. , Java, C++, SQL) Easiness for human, i. e. , more flexibility, easier to be implemented, maintained High level language (e. g. , Fortran, C, Cobol) Assembly language (Intel: 8086, Motorola: 68000, Sun: Sparc) Speed 11/9/2020 Machine Language (e. g. , 01001110000) COMP 208 Computers in Engr 11

Choosing a language l l Manager choices a language for everyone in the group Suitable for your tasks l l l 11/9/2020 Satellite communication (speed) Assembly Education Basic, Pascal Business Cobol Web development Javascript, Java Scientific calculation Fortran Real programmer in general purpose C COMP 208 Computers in Engr 12

Major programming language: Fortran l l The First High-Level Language Developed by IBM and introduced in 1957 Primarily associated with engineering, mathematical, and scientific research tasks. Rich in math library 11/9/2020 COMP 208 Computers in Engr 13

Major programming language: COBOL l l The Language of Business Although Fortran in 1950 s, no high level language for business. . l The U. S. Department of Defense: CODASYL-COnference of DAta SYstem Languages. l 1959 COBOL-COmmon Business Oriented Language. l 1974, American National Standards Institute: ANSI-COBOL l 1985, COBOL 85 COBOL is very good for processing large files and performing relatively simple business calculations, such as payroll or interest. English-like-far more than FORTRAN or BASIC 11/9/2020 COMP 208 Computers in Engr 14

Major programming language: Basic l l For Beginners and Others BASIC-Beginners' All-purpose Symbolic Instruction Code Developed at Dartmouth College by John Kemeny and Thomas Kurtz in 1965 and originally intended for use by students. For many years, BASIC was looked down on by "real programmers, " who complained that it had too many limitations and was not suitable for complex tasks. Newer versions, such as Microsofts Quick. BASIC, include substantial improvements. 11/9/2020 COMP 208 Computers in Engr 15

Major programming language: C l l l A language invented by Dennis Ritchie at Bell Labs in 1972 Advantage: l C very fast ≈ assembly language while still offering high-level language features. Disadvantage: l Although C is simple and elegant, it is not simple to learn. C was originally designed to write systems software but is now considered a general-purpose language. The availability of C on personal computers has greatly enhanced the value of personal computers for budding software entrepreneurs. 11/9/2020 COMP 208 Computers in Engr 16

The Programming Process Developing a program involves steps similar to any problem-solving task: l 1. 2. 3. 4. 5. 11/9/2020 Defining the problem Planning the solution Coding the program Testing the program Documenting the program COMP 208 Computers in Engr 17

1. Defining the problem l identifying l l l Need to meet with l l l what it is you know (input-given data), and what it is you want to obtain (output-the result). users from the client organization to analyze the problem, a systems analyst who outlines the project. Finally a written agreement is produced. 11/9/2020 COMP 208 Computers in Engr 18

2. Planning the solution draw a flowchart 1. l l l a pictorial representation of a step-by-step solution to a problem arrows the direction the program takes boxes representing actions. write pseudocode, 2. l l l 3. 11/9/2020 an English-like nonstandard language Focus on programming logic, no syntax No executable possibly both. COMP 208 Computers in Engr 19

2. 1. Flow chart start l Example: Calculate the sum of N=1, sum=0 conditions positive integers less than 100 action l N < 100 No Yes sum = sum + N N = N +1 loop 11/9/2020 end actions COMP 208 Computers in Engr 20

2. 2. Pseudocodes Initiate N as smallest positive integer, sum=0 Loop until N equals to 100 1. 2. 1. 2. 11/9/2020 Sum = sum + N Increase N by 1 COMP 208 Computers in Engr 21

Essential things in a program l l l Variables, e. g. , N, sum Type: e. g. , integer Operator: e. g. , +, =, < Conditions: e. g. , if N < 100 Loop: e. g. , loop until N < 100 l l For reusing the codes Exit conditions: e. g. , N < 100 11/9/2020 COMP 208 Computers in Engr 22

3. Coding the program l l Translate the logic from the flowchart or pseudocode-or some other tool-to a programming language. Programming language is more precise than natural languages Have to follow exactly the rules (i. e. , the syntax) of the language you are using. Note: No syntax error does not guarantee the program will work correctly as you expect. 11/9/2020 COMP 208 Computers in Engr 23

4. Testing the program l l Desk-checking: similar to proofreading. Compiling: l use compilor, a program, to check if syntax error. l l l N=2 *(I+J)) // compilor will report an error. Then translates your program into a form the computer can understand, i. e. , machine language Debugging: l l l 11/9/2020 Run the translated program in machine language detecting, locating, and correcting bugs (mistakes) These bugs are logic errors (e. g. , n < 100 not n <=100) COMP 208 Computers in Engr 24

5. Documenting the program l Documentation is a written detailed description of the programming cycle and specific facts about the program. l l 11/9/2020 Separated documents Comments in the program itself l Many programmers omit these or put little comments on the codes. l Comments should not just rephrase the codes in plain English l Bad: n=n+1 // increase n by 1 l Good: n=n+1 // n needed to increased for terminating the loop COMP 208 Computers in Engr 25

Let’s start A simplest Fortran program 11/9/2020 COMP 208 Computers in Engr 26

Hello World! PROGRa. M hello IMPLIc. IT NONE Hello. World. f 90 WRITE(*, *) "Hello World!" END PROGRa. M l Edit: l l 11/9/2020 any text editor, e. g. , Sci. TE, Notepad, Or IDE (Integrated Development Environment), e. g. , Visual Fortran COMP 208 Computers in Engr 27

Hello World (cont. ) l l Edit compile l Repeat Until succeed l l l ~%>gfortran Hello. World. f 90 a. exe ~%>gfortran Hello. World. f 90 –o Hello. World -> Hello. World. exe Link (not necessary for simple programs) l Linking libraries used in the program Run l l 11/9/2020 Fortran Machine Language GFortran: ~%>Hello. World Output: “Hello World!” COMP 208 Computers in Engr 28

Hello World (cont. ) Program name block PROGRa. M hello IMPLIc. IT NONE WRITE(*, *) "Hello World!" END PROGRa. M Later “!” comment the whole line WRITE(*, *): Write something on the screen 11/9/2020 COMP 208 Computers in Engr 29

The Program Block PROGRAM hello IMPLICIT NONE !This is my first program WRITE (*, *) “Hello World!“ END PROGRAM hello The bold keywords tell the compiler where the program begins and ends. They bracket a section of code called a block Using uppercase is a convention to distinguish keywords. FORTRAN is case insensitive. PROGRAM, program, pro. GRAM, p. Ro. Gr. Am are all the same. 11/9/2020 COMP 208 Computers in Engr 30

The Program Block in General Syntax for the program block PROGRAM program-name IMPLICIT NONE {declarations} {statements} {subprograms} END PROGRAM program-name 11/9/2020 COMP 208 Computers in Engr 31

Some specific facts about codes l White space insensitive l l case insensitive l l l WRITE(*, *) “Hello world!” same WRITE (*, *) “Hello world!” W RITE(*, *) “Hello world!” compile error WRITE(*, *) “Hello world!” write(*, *) “Hello world!” same Note: These two facts only for codes, not for data l l WRITE(*, *) “Hello world!” WRITE(*, *) “HELLO W O RLD !” Different 11/9/2020 COMP 208 Computers in Engr 32
- Slides: 32