cs149 elements of computer science programming
play

CS149: Elements of Computer Science Programming 1. The need for - PowerPoint PPT Presentation

CS149: Elements of Computer Science Programming 1. The need for programming languages (a) CPU executes machine code i. Commands CPU can understand and execute ii. Numeric (binary) format: binary storage! iii. Writing program was not an easy


  1. CS149: Elements of Computer Science Programming 1. The need for programming languages (a) CPU executes machine code i. Commands CPU can understand and execute ii. Numeric (binary) format: binary storage! iii. Writing program was not an easy task (b) Quest for something more “humane” 2. Solution: higher level programming languages (a) Level of abstraction above machine code (b) Allows humans to specify program in something humans can read and understand (c) Must of course be converted to machine code for computer (d) Conversion is performed by compiler or interpreter software Johan Bollen - http://www.cs.odu.edu/˜jbollen January 18, 2004 Page 1

  2. CS149: Elements of Computer Science Programming Languages Johan Bollen - http://www.cs.odu.edu/˜jbollen January 18, 2004 Page 2

  3. CS149: Elements of Computer Science Compiler vs. Interpreter Compiler: Interpreter: 1. Transform entire program to 1. Transform program to machine one piece of CPU specific ma- code one instruction at a time chine code 2. Error? Stop program and 2. Error? Change program and change. compile again. 3. Languages: Basic, Perl 3. Difference computer: compile again 4. Languages: C++, Pascal, Java, Fortran Johan Bollen - http://www.cs.odu.edu/˜jbollen January 18, 2004 Page 3

  4. CS149: Elements of Computer Science Johan Bollen - http://www.cs.odu.edu/˜jbollen January 18, 2004 Page 4

  5. CS149: Elements of Computer Science Johan Bollen - http://www.cs.odu.edu/˜jbollen January 18, 2004 Page 5

  6. CS149: Elements of Computer Science Development of Programming Languages Where do compilers come from? 1. If compiler = program, it can 1. First step: design language x be written in any language, as 2. Write Compiler or Interpreter long as compiler is available... to transform language x pro- 2. This means you bootstrap the grams to machine language development of new languages 3. Use Compiler to transform all and compilers from existing programs written in language x ones, starting with machine to machine language code 3. Low-Level and High-Level languages: closer or far- ther from machine code or hardware Johan Bollen - http://www.cs.odu.edu/˜jbollen January 18, 2004 Page 6

  7. CS149: Elements of Computer Science Low-level vs. High Level Programming Languages 1. Low Level: close to actual hardware/CPU (a) Instructions match what machine and CPU can do (b) Extremely tedious (c) Machine code: binary, or hexadecimal (d) Assembly Language 2. High Level: far remove from actual hardware/CPU (a) Language reflects philosophy of data storage and manipulation (b) Instructions do not match capabilities of specific CPU, but rather general high-level data processing features (c) C, C++, Java, Perl, Python, Delphi, etc. Johan Bollen - http://www.cs.odu.edu/˜jbollen January 18, 2004 Page 7

  8. CS149: Elements of Computer Science Programming Languages Johan Bollen - http://www.cs.odu.edu/˜jbollen January 18, 2004 Page 8

  9. CS149: Elements of Computer Science Some examples Assembly Language: C++: int main (void) { 0 LD R0, 0A 2 LD R1, 0C int a, b, c; 4 ADD R0, R0, R1 a = 3; 6 STO R0, 0E b = 5; 8 HALT c= a+b; 0A 03 cout << c; 0C 05 return 0; } 0E 08 Johan Bollen - http://www.cs.odu.edu/˜jbollen January 18, 2004 Page 9

  10. CS149: Elements of Computer Science C and C++ Programming 1. C’s history: (a) Developed at Bell Labs, 1972 (b) Based on existing BCPL and B (c) Idea: hardware independent programs i. Exchange of hardware independent source code ii. Compiled to fit local hardware iii. Requires local compiler (d) Standardized in 1989: ANSI C 2. C++: expansion of existing C standard (a) Object-oriented approach (b) Additional support for data abstraction Johan Bollen - http://www.cs.odu.edu/˜jbollen January 18, 2004 Page 10

  11. CS149: Elements of Computer Science C++ Compilation Source code to executable: Linking and Loading: 1. Source code is compiled to Object code 1. Linking and Loading are performed auto- 2. Object code: machine code matically by g++ com- (a) Incomplete: needs to be linked to libraries piler (b) Libraries: sets of predefined constant vari- 2. Rationale: re-use and ables and functions maintenance of exist- (c) Are inserted (loaded) or linked into code ing, pre-defined mod- 3. Final executable ules Johan Bollen - http://www.cs.odu.edu/˜jbollen January 18, 2004 Page 11

  12. CS149: Elements of Computer Science C++ Compilation: in practical terms 1. Write source (a) C++ syntax (b) Use any editor (c) save to source code file 2. Compile source code (a) Use compiler: g++ (b) Errors: back to 1 (c) No errors: executable has been generated 3. Run executable (a) Do not confuse with source code! (b) Machine-specific! (c) No compilation errors != correct program Johan Bollen - http://www.cs.odu.edu/˜jbollen January 18, 2004 Page 12

  13. CS149: Elements of Computer Science Murphy’s law Believe me, it applies to software development! Compile Errors: Run-Time Errors: 1. Error in source code: 1. Errors in actual execution of program 2. Syntax: error in actual spelling 2. Example: 3. Structure: error in program (a) Values out of range structure (b) Memory allocation 4. Picked out by compiler (c) Validity: is output really what it is supposed to be? Johan Bollen - http://www.cs.odu.edu/˜jbollen January 18, 2004 Page 13

  14. CS149: Elements of Computer Science How to write a program? It’s all about PROBLEM SOLVING 1. State the problem clearly 2. Describe the input and desired output information 3. Work the problem by hand, check for exceptions 4. Develop a solution and convert it to a computer program 5. Test the solution with a variety of data The purpose is to decompose a problem into little steps and commands a computer can understand, i.e. rephrase the problem in a sequence of instructions and transformations that correspond to the syntax and semantics of the language you are programming in. Johan Bollen - http://www.cs.odu.edu/˜jbollen January 18, 2004 Page 14

  15. CS149: Elements of Computer Science An example 1. Problem: compute straight line distance between two points in a plane 2. Input - Output description: blackbox model of program Johan Bollen - http://www.cs.odu.edu/˜jbollen January 18, 2004 Page 15

  16. CS149: Elements of Computer Science Hand Example Point 1: p 1 = ( 1 , 3 ) Point 2: p 2 = ( 4 , 4 ) Distance: hypothenusa of right triangle � ∆ x 2 + ∆ y 2 d ( p 1 , p 2 ) = √ 3 2 + 1 2 = √ 9 + 1 d ( p 1 , p 2 ) = d ( p 1 , p 2 ) = 3 . 16 Johan Bollen - http://www.cs.odu.edu/˜jbollen January 18, 2004 Page 16

  17. CS149: Elements of Computer Science Algorithm Development Decompose problems in small steps that correspond to computer language 1. Ask for x and y coordinates for point 1, x 1 and y 1 2. Ask for x and y coordinats for point 2, x 2 and y 2 3. Compute ∆ x = x 2 − x 1 and ∆ y = y 2 − y 1 � ∆ x 2 + ∆ y 2 4. Compute distance, d = 5. Print d Johan Bollen - http://www.cs.odu.edu/˜jbollen January 18, 2004 Page 17

  18. CS149: Elements of Computer Science Write and Compile C++ Program: demo in prog Johan Bollen - http://www.cs.odu.edu/˜jbollen January 18, 2004 Page 18

  19. CS149: Elements of Computer Science Compilation Process (c) Executable runs only on specific 1. Source code: your C++ program CPU/machine (a) Text file 3. UNIX commands: (b) Any editor (Let’s use pico) (a) We use g++ (c) Adheres to C++ language (b) Assume your source code file: specifications test.cpp 2. Compilation: (c) You want executable in file: test (a) Production of Executable (d) Type: g++ -o test test.cpp (b) Transformation of source code to machine code (executable) Johan Bollen - http://www.cs.odu.edu/˜jbollen January 18, 2004 Page 19

  20. CS149: Elements of Computer Science Johan Bollen - http://www.cs.odu.edu/˜jbollen January 18, 2004 Page 20

  21. CS149: Elements of Computer Science C++ Programming 2. General Structure: 1. Your C++ program must conform to (a) Preprocessor directives: specific syntax: i. Specifies libraries ( include (a) Every line terminates with statement) semi-colon ’;’ (except include !) ii. Definition of constants (b) Blocks of commands are grouped (b) main function: mother of all with { and } command groups and functions (c) Comments are preceded by //, i. Variable declaration preprocessor directive by # ii. Commands Johan Bollen - http://www.cs.odu.edu/˜jbollen January 18, 2004 Page 21

  22. CS149: Elements of Computer Science Syntax and structure of C++ programs Johan Bollen - http://www.cs.odu.edu/˜jbollen January 18, 2004 Page 22

  23. CS149: Elements of Computer Science Syntax and structure of C++ programs Elements in example: / / p r i n t Hello World message 1. Comments, preceeded by // # include < i o s t r e a m > 2. Preprocessor Directives: ( include ) main ( void ) { i n t 3. Main function block: main {···} cout << ” Hello \ ” World \ ” \ t !” ; (a) Object/Variable declaration return 0 ; (b) Commands } Johan Bollen - http://www.cs.odu.edu/˜jbollen January 18, 2004 Page 23

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend