Computer Science I Lecture Notes PDF
Document Details
Uploaded by FervidDune
ETH Zurich
2024
Carlos Cotrini, Ralf Sasse
Tags
Related
- ENSC 151 - Week 0(1) (Complete) PDF
- CE 101 Algorithms and Programming I PDF
- Data Structures and Algorithms with Python and C++ PDF
- CSE2305 Problem Solving using Competitive Programming PDF
- Data Structures and Algorithm Analysis in C++ 4th Edition PDF
- Chapter 1: Introduction to Computers and Programming PDF
Summary
This document is lecture notes for an undergraduate Computer Science course at ETH Zurich. The course discusses foundational concepts such as algorithms and programming in C++. These lecture notes are from Fall 2024 and are focused on introductory Computer Science concepts and the Euclidean algorithm.
Full Transcript
Carlos Cotrini, Ralf Sasse Computer Science I Course from D-INFK at ETH Zurich Fall 2024 1. Introduction Computer Science: Definition and History, Algorithms, Turing Machine, Higher Level Programming Languages, Tools 12 What...
Carlos Cotrini, Ralf Sasse Computer Science I Course from D-INFK at ETH Zurich Fall 2024 1. Introduction Computer Science: Definition and History, Algorithms, Turing Machine, Higher Level Programming Languages, Tools 12 What is Computer Science? 13 What is Computer Science? Computer Science = Automation of intellectual activities 13 Computer Science vs. Computers Computer science is not about machines, in the same way that astronomy is not about telescopes. Mike Fellows, US Computer Scientist (1991) 14 This course content Systematic problem solving with algorithms and the programming language C++. Hence: not only but also programming course. 15 Why Program? 16 Why Program? Well, because computers and software run the world! 16 Algorithm: Fundamental in Computer Science Algorithm: 17 Algorithm: Fundamental in Computer Science Algorithm: Instructions to solve a problem step by step “Dixit algorizmi... ” (Latin translation) 17 Algorithm: Fundamental in Computer Science Algorithm: Instructions to solve a problem step by step Execution does not require any intelligence, but precision (even computers can do it) “Dixit algorizmi... ” (Latin translation) 17 Algorithm: Fundamental in Computer Science Algorithm: Instructions to solve a problem step by step Execution does not require any intelligence, but precision (even computers can do it) according to Muhammed al-Chwarizmi, author of an arabic computation textbook (about 825) “Dixit algorizmi... ” (Latin translation) 17 Oldest Nontrivial Algorithm Euclidean algorithm (from the elements from Euklid, 3. century B.C.) Input: integers a > 0, b > 0 Output: gcd of a und b a b Oldest Nontrivial Algorithm Euclidean algorithm (from the elements from Euklid, 3. century B.C.) Input: integers a > 0, b > 0 Output: gcd of a und b a b a b Oldest Nontrivial Algorithm Euclidean algorithm (from the elements from Euklid, 3. century B.C.) Input: integers a > 0, b > 0 Output: gcd of a und b a b a b a b Oldest Nontrivial Algorithm Euclidean algorithm (from the elements from Euklid, 3. century B.C.) Input: integers a > 0, b > 0 Output: gcd of a und b a b a b a b a b Oldest Nontrivial Algorithm Euclidean algorithm (from the elements from Euklid, 3. century B.C.) Input: integers a > 0, b > 0 Output: gcd of a und b a b a b a b a b a b Oldest Nontrivial Algorithm Euclidean algorithm (from the elements from Euklid, 3. century B.C.) Input: integers a > 0, b > 0 Output: gcd of a und b a b a b a b a b a b a b 18 Oldest Nontrivial Algorithm Euclidean algorithm (from the elements from Euklid, 3. century B.C.) Input: integers a > 0, b > 0 Output: gcd of a und b While b ̸= a If a > b then a←a−b else: b←b−a Result: a. a b a b a b a b a b a b 18 Algorithms: 3 Levels of Abstractions 1. Core idea (abstract): the essence of any algorithm (“Eureka moment”) 19 Algorithms: 3 Levels of Abstractions 1. Core idea (abstract): the essence of any algorithm (“Eureka moment”) 2. Pseudo code (semi-detailed): made for humans (education, correctness and efficiency discussions, proofs 19 Algorithms: 3 Levels of Abstractions 1. Core idea (abstract): the essence of any algorithm (“Eureka moment”) 2. Pseudo code (semi-detailed): made for humans (education, correctness and efficiency discussions, proofs 3. Implementation (very detailed): made for humans & computers (read- & executable, specific programming language, various implementations possible) 19 Algorithms: 3 Levels of Abstractions 1. Core idea (abstract): the essence of any algorithm (“Eureka moment”) 2. Pseudo code (semi-detailed): made for humans (education, correctness and efficiency discussions, proofs 3. Implementation (very detailed): made for humans & computers (read- & executable, specific programming language, various implementations possible) Euclid: Core idea and pseudo code shown, implementation yet missing 19 Euklid in the Box Speicher 0 1 2 3 4 5 6 7 8 9 Links Rechts Register 20 Euklid in the Box Speicher 0 1 2 3 4 5 6 7 8 9 Programmcode Links Rechts Register 20 Euklid in the Box Speicher 0 1 2 3 4 5 6 7 8 9 R > L? L = R? L−R springe R−L springe springe →L →R stop → zu 0 → zu 0 zu 6 Programmcode Daten Links Rechts Register 20 Euklid in the Box Speicher 0 1 2 3 4 5 6 7 8 9 R > L? L = R? L−R springe R−L springe springe →L →R stop → zu 0 → zu 0 zu 6 Programmcode Daten Daten Links Rechts Register 20 Euklid in the Box Speicher 0 1 2 3 4 5 6 7 8 9 R > L? L = R? L−R springe R−L springe springe →L →R stop → zu 0 → zu 0 zu 6 Links Rechts Register 20 Euklid in the Box Speicher 0 1 2 3 4 5 6 7 8 9 R > L? L = R? L−R springe R−L springe →L →R stop springe → zu 0 → zu 0 b a zu 6 While b ̸= a Links Rechts If a > b then a←a−b else: b a b←b−a Ergebnis: a. Register 20 Euklid in the Box Speicher 0 1 2 3 4 5 6 7 8 9 R > L? L = R? L−R springe R−L springe →L →R stop springe → zu 0 → zu 0 b a zu 6 While b ̸= a Links Rechts If a > b then a←a−b else: b a b←b−a Ergebnis: a. Register 20 Euklid in the Box Speicher 0 1 2 3 4 5 6 7 8 9 R > L? L = R? L−R springe R−L springe →L →R stop springe → zu 0 → zu 0 b a zu 6 While b ̸= a Links Rechts If a > b then a←a−b else: b a b←b−a Ergebnis: a. Register 20 Euklid in the Box Speicher 0 1 2 3 4 5 6 7 8 9 R > L? L = R? L−R springe R−L springe →L →R stop springe → zu 0 → zu 0 b a zu 6 While b ̸= a Links Rechts If a > b then a←a−b else: b a b←b−a Ergebnis: a. Register 20 Euklid in the Box Speicher 0 1 2 3 4 5 6 7 8 9 R > L? L = R? L−R springe R−L springe →L →R stop springe → zu 0 → zu 0 b a zu 6 While b ̸= a Links Rechts If a > b then a←a−b else: b a b←b−a Ergebnis: a. Register 20 Euklid in the Box Speicher 0 1 2 3 4 5 6 7 8 9 R > L? L = R? L−R springe R−L springe →L →R stop springe → zu 0 → zu 0 b a zu 6 While b ̸= a Links Rechts If a > b then a←a−b else: b←b−a Ergebnis: a. Register 20 Computers – Concept A bright idea: universal Turing machine (Alan Turing, 1936) Sequence of symbols on a (conceptually unlimited) tape Instructions x Input Output RW head Fixed Program Computer "read symbol!" Control "write symbol!" "move left!" Unit "move right!" Internal State Alan Turing 21 Computer – Implementation Z1 – Konrad Zuse (1938) ENIAC – John Von Neumann (1945) Von Neumann Architecture Central Processing Unit Konrad Zuse Control Unit Arithmetic/Logic Unit Bus RAM I/O John von Neumann 22 Computing speed computers are not intelligent, despite reports to the contrary But they execute commands unbelievably fast! 23 Computing speed computers are not intelligent, despite reports to the contrary But they execute commands unbelievably fast! For example (instructions per second (IPS), coarsely rounded): Raspberry Pi 2: 5 × 109 Standard Intel/AMD: 200 − 800 × 109 AMD Ryzen Threadripper: 2 × 1012 (Source: https://en.wikipedia.org/wiki/Instructions_per_second) 23 Programming Languages Core “Dictionary idea search” The language that the computer can understand (machine Pseudo 245 chars/ language) is very primitive. code 46 words Simple operations have to be subdivided into (extremely) Impl. many single steps 282/55 (C++) The machine language varies between computers. Machine 1536/— code 24 Why C++? C++ as an introductory language? Why not Python or JavaScript? C++ makes many decisions explicit; or rather, forces programmers to make explicit choices This makes it easier to later on switch to a “simpler” language Analogy: getting driver’s license for manual transmission systems, but driving automatic transmission later on 25 Why C++? C++ as an introductory language? Why not Python or JavaScript? C++ makes many decisions explicit; or rather, forces programmers to make explicit choices This makes it easier to later on switch to a “simpler” language Analogy: getting driver’s license for manual transmission systems, but driving automatic transmission later on C++ well-suited for systems programming since it enables/requires careful resource management (memory,...) 25 Syntax and Semantics Like our language, programs have to be formed according to certain rules. Syntax: Connection rules for elementary symbols (characters) Semantics: interpretation rules for connected symbols. 26 Syntax and Semantics Like our language, programs have to be formed according to certain rules. Syntax: Connection rules for elementary symbols (characters) Semantics: interpretation rules for connected symbols. Corresponding rules for a computer program are simpler but also more strict because computers are relatively stupid. 26 Deutsch vs. C++ Deutsch Alleen sind nicht gefährlich, Rasen ist gefährlich! (Wikipedia: Mehrdeutigkeit) C++ // computation int b = a * a; // b = a2 b = b * b; // b = a4 27 Syntax and Semantics of C++ Syntax: When is a text a C++ program? I.e. is it grammatically correct? → Can be checked by a computer Semantics: What does a program mean? Which algorithm does a program implement? → Requires human understanding 28 2. A C++Program and its Components Programming Tools, Components 29 Programming Tools Editor: Program to modify, edit and store C++program texts Compiler: program to translate a program text into machine language 30 Programming Tools Editor: Program to modify, edit and store C++program texts Compiler: program to translate a program text into machine language Computer: machine to execute machine language programs Operating System: program to organize all procedures such as file handling, editor-, compiler- and program execution. 30 The first C++ program // Program: power8.cpp // Raise a number to the eighth power. #include int main() { // input std::cout > a; // computation int b = a * a; // b = a^2 b = b * b; // b = a^4 // output b * b, i.e., a^8 std::cout