CPC Lec: Lesson 1 - Computational Thinking PDF

Summary

This document provides an introduction to computational thinking, algorithms, and programming concepts. It covers topics like decomposition, pattern identification, and abstraction. The document is formatted like lesson notes with sections on algorithms, selecting and creating algorithms, and functional decomposition.

Full Transcript

CPC Lec: Lesson 1 - Computational Thinking The principle of decompositions are: Algorithm - steps for solving a problem or task. 1.​ Minimize dependencies - input and output may flow between nodes, c...

CPC Lec: Lesson 1 - Computational Thinking The principle of decompositions are: Algorithm - steps for solving a problem or task. 1.​ Minimize dependencies - input and output may flow between nodes, changing the instructions in Programming Algorithm - set of steps that specify the one module or object should not require underlying logic and structure for the statements in a changes to others. computer program. 2.​ Maximize Cohesion - object or module contains attributes, methods, or instructions that perform Computer Program - sets of instructions, written in a a single logical task or represent a single entity. programming language, that performs a specific task when executed by a digital device. Pattern Identification - technique for finding patterns in procedures and tasks. ​ Good Algorithm - produce a computer program 1.​ Pattern Identification Basics that operates efficiently, quickly, reliably. 2.​ Repetitive Pattern Characteristics: 3.​ Classification Pattern - pattern of attributes and ​ Input - applies to a set of methods that apply to a collection of objects. specified inputs. ​ Classes: blueprint containing attributes ​ Output - produce one or more and methods that describe a collection outputs. of objects. ​ Finite - terminates after a finite number of steps. Abstraction Basics - process of hiding details or ​ Precise - algorithm is clear and substituting a generalization for something specific. unambiguous. 1.​ Classes and Objects ​ Effective - algorithm 2.​ Black Boxes - accepts some type of input and successfully produces the performs a specific process to produce output correct output. without requiring an understanding of its internal workings. Selecting and Creating Algorithm 1.​ Use a standard algorithm Levels of Abstraction - amount of details an objects or 2.​ Perform the task manually concepts are hidden. 3.​ Apply Computational Thinking techniques -​ Computational Thinking: set of techniques designed to formulate Summary problems and their solutions. ​ Computation Thinking - helps programmers define problems, find solutions, delineate Decomposition - technique for breaking a task into tasks, and devise algorithms. smaller parts. ​ Algorithm - series of steps for solving a Structural Decomposition - process that identifies a problem or carrying out a task. (Bluprint) hierarchy of structural units. ​ Standard Algorithms - exist for many computing tasks. When an algorithm does not exist, you can step through a process Creating a Structural Decomposition Diagram manually and record the steps, or apply 1.​ Use a top-down approach. computational thinking techniques. 2.​ Label nodes with nouns and adjectives rather ​ Decomposition Divides - complex problem than verbs. or task into manageable units. 3.​ Don’t worry about sequencing. ​ Pattern Identification - reveals sequence and repetitive tasks that can lead to algorithm efficiencies. Functional Decomposition - technique for dividing ​ Abstraction - suppresses details, substitutes modules into smaller action, processes, or steps. a generalization for something specific, and allows an algorithm to work for multiple inputs. Creating a Functional Decomposition Diagram 1.​ Label nodes with verb phrases. Lesson 2: Programming Tools 2.​ Sequence from left to right. ​ Programming Language - formal language with Object-Oriented Decomposition - technique for semantics and syntax for instructing a computer to dividing a problem or task into logical and physical perform a specific task. objects that a computer program will manipulate. -​ Objects: specific instances of a class. High-Level Programming Languages - Systems for Tips for Object-Oriented Decomposition issuing instructions to a CPU that abstract away the 1.​ Node titles are nouns. detail of low-level machine language to provide 2.​ Attributes are nouns. programmers with natural language commands. -​ Attributes: characteristics that describe an object. Semantics - In the context of programming, the 3.​ Methods are verb phrases. meaning and validity of program statements. -​ Methods: action that an object can perform. Keywords - Words in programming language that have 4.​ Sketch in connection arrows. special meaning, such as a command to perform an action. Reversed Word - Words that programming language Preprocessors and Linkers reserves for its use to specify commands and other ​ Preprocessor - software utility that performs actions. housekeeping tasks before the compiler gets to work. Syntax - programming language is equivalent to the ​ Preprocessor Directives - statements that grammar rules of a written language, the grammar rules provide instructions to the preprocessor, but are for word order and punctuation.​ not executed by the compiler. ​ ​ Linker - Utility software that converts one or _____________Core Elements___________________ more object files into a binary executable file. 1.​ Variables that can be assigned values and Virtual Machines - structures ​ Bytecode - 2.​ Arithmetic Operators - can be used to perform ​ Java Virtual Machine (JVM) - software that calculations. compiles and executes bytecode produced by 3.​ Keywords - perform operations such as prints the Java Programming Language. or append. 4.​ Data Types - define values and text. Interpreters - It is utility software that preprocesses and 5.​ Branching controls - that change the sequence executes program statements one by one. in which statements are executed. 6.​ Repetition Controls - repeat a series of —----------------------Debugging Tools—----------------------- statements. 7.​ Syntax Rules - constructing valid statements Debugging - programming jargon for finding and 8.​ Terminology - describing the components of a correcting errors—or “bugs”—that prevent software from language and their functions. performing correctly. Toolbox Essentials: Syntax error - A flaw in a program statement that 1.​ Coding Tools - provide a way for you to codify results from incorrectly using punctuation or word an algorithm. sequencing. 2.​ Build Tools - transform your code into a binary format that a computer can execute. Causes of Syntax Error 3.​ Debugging Tools - help you test programs and ​ A missing keyword track down errors. ​ A misspelled keyword ​ A keyword in the wrong place Program Editors ​ A capitalized keyword 1.​ Visual Platform ​ Incorrect type of punctuation 2.​ Word Processor ​ Unpaired quotes or brackets 3.​ Text Editors ​ Incorrect indentation 4.​ Code Editors ​ An empty block ​ Embedded characters Basic Structure ​ Incorrect method parameters 1.​ Comments - explanatory notation within ​ Missing return type program code that is not executed by a computer. Runtime error - A flaw in a program that causes a 2.​ Directive - statement that tells the computer program to fail during execution. how to handle the program rather than how to perform an algorithm. Look for these errors if you experience runtime error: 3.​ main() function - entry point for program ​ Division by Zero execution. ​ Expressions that use incompatible data types 4.​ Statements - Just as human-readable text is ​ Forgetting to declare the variable before using it divided into sentences and paragraphs. ​ Accessing a file that does not exist 5.​ Code Block - Statements that perform a specific ​ Running out of memory function, and that may be visually set off from other statements or blocks using blank lines. Semantic Errors - flaw in the logic of one or more statements that causes a program to produce incorrect Build Tools - tools such as a preprocessor, compiler, results. and linker, that convert source code into a program that a computer can execute. Look for these errors if you experience semantic error: ​ Using the wrong expression for a decision Before program run it converts it into Machine Code ​ Using the wrong name for a variable Executable code is stored executable files. ​ Forgetting the order of operations ​ Setting the wrong loop counter Compilers - Translates code written in one ​ Indenting a block to the wrong level or incorrect programming language into another language. placement of braces Debugging Utilities - Software that can be used to find Source Code - Set of statements usually written in a and correct errors in program code. high-level programming language. -​ Breakpoint: A location with program code that triggers the debugging utility to halt while a Object Code - binary program code that is produced by programmer identifies an error. a compiler. -​ Program Animation: debugging utilities that step through code one statement at a time. Integrated development environment (IDE) - all of the Memory Addresses - represent memory locations using tools needed to code, compile, link, and debug a unique address that’s either binary or hexadecimal programs. [Ex. 101011001011, 0xE2452440] ​ Online IDE (Web IDE) - runs in a browser and -​ You can store data in a memory location without requires no installation. directly handling these addresses. Software Development Kits (SKD) - set of tools for developing platform-specific software. Lesson 3: Literals, Variables, and Constant—----- Literal - fixed element of data used by computer programs. ​ Numerical Literal – one or more digits that can represent integers or floating-point numbers. Undefined Variable – variable that has not been ​ Character Literal – a single letter or symbol. specified by the programmer. ​ String Literal – one or more characters and is typically denoted with double quotes. Declaring a Variable – process of specifying a name ​ Tricky Literal – when numerical numbers can that is assigned, or bound, to a memory location. be set as string literals once in double quotes. [Ex. Int num_1; Char name_added;] ​ Boolean Literal – literal has a value of True or False. Initialize a Variable – process of specifying a name and value for a variable. [Ex. Int num_1 = 20;] Variable - named memory location that temporarily holds text or numeric value. Type Inference – a feature that deduces the data type based on the presence or absence of a decimal point. 3 Important Characteristics: ​ Has a name. Dynamically Typed – allows a variable to take on any ​ Corresponds to a memory location where data datatype during program execution. can be stored. total_rings = 19 ​ The data can be changed. total_rings = 20 Common Conversions to Combine Words total_rings = “Unknown” ​ Camel case – begins with a lowercase letter, then uses an uppercase letter for subsequent Statically Typed – requires a variable to hold only the words. [Ex. elvenRing] type of data that was declared or initialized. ​ Snake case – uses all lowercase and separates int total_rings = 19 words with an underscore. [Ex. elven_ring] total_rings = 20 Constant – named memory location that holds data but is not changed by the statements in a program. total_rings = “Unknown” has to be declared as int -​ named using all caps and separated by “Ways to Set Starting Values” underscore. [Ex. DISTANCE_MOON_EARTH] ​ Literal – can be set if you know its starting value Three important characteristics of constants are: when the program begins. ​ Initial value of counter – integer variables that ​ Has a name. you plan to use to count repetitions can be ​ Corresponds to a memory location where data initialized with 0 or 1, depending on the control can be stored. structure that operates the loop. ​ The data is not meant to be changed, unless n– ​ Null variable – null means no value; different makes it more convenient when a programmer from initializing 0, which is a value. Null is best needs to adjust the constant’s value. avoided. Initialization – the first time a variable is given a value. Assigning Variables – any process that sets or changes the value of a variable. ​ (Data can be changed once)​ ​ Assignment Statement – sets or changes the value that is stored in a variable. [Ex. variable_name = expression] Assigning a number dwarf_rings = 7 - 1 Subtracts 1 from 7 and assigns the result (6) to the variable (Data needs to be changed multiple times) dwarf_rings. Updating a variable dwarf_rings = Takes the current dwarf_rings - 1 value of dwarf_rings (6), subtracts 1, and updates dwarf_rings to 5. ​ The number 5 appears because it was entered by the user and stored in dwarf_rings. Assigning the result of total_rings = 3 + 7 + Adds all the a calculation 9+1 numbers and ​ When outputting text, the program retrieves assigns the result the stored value and includes it in the (20) to total_rings. message. Assigning a string ring_location = Initially assigns "Anduin River" "Anduin River" to Lesson 4: Numeric Data Types and Expression ring_location = ring_location, then "Bilbo's Pocket" changes it to "Bilbo's Pocket." Data Type – a category of data such as an integer, floating point, or character. ​ Primitive Data Type - built into a programming “Key Roles of Variables in Programs” language. [Ex. integer, floating point, double floating ​ Store data – hold literals (numbers, text, etc.) point]​ used for input, manipulation, and output. ​ Characteristics: (1) allocated memory is ​ Intermediate calculations – store temporary predetermined; (2) data type can be manipulated. results during complex calculations. ​ Function - procedure that performs a ​ Counters – control the number of repetitions specific task. [Ex. abs() – absolute, (loops) in a program. unsigned value of int.] ​ Decision-making – value of a variable can determine the next steps in an algorithm.​ ​ Composite Data Types – programmer defined; available as functions or via add-on library. [Ex. time] “Algorithm for Collecting User Input” Integer Data Types – whole numbers that are signed/unsigned and expressed in 1.​ Initialize a variable to store user input. deci/binary/hexadecimal notation. 2.​ Prompt the user to enter data. 3.​ Store the input in the initialized variable. ​ Unsigned Integer – whole number that has no + or − sign; considered positive. Prompt - specifies what a user should enter when a ​ Signed Integer – whole number that has a + or – program is running. sign. Ex. Integer Data Signedness Description ​ Prompt: "In Middle Earth, how many rings were 23 unsigned Number of songs given to the dwarf lords?" –40 signed Temperature Pseudocode:​ 0b00000101 unsigned Binary 5 initialize dwarf_rings = 0 display "In Middle Earth, how many rings +363 signed Number of were given to the dwarf lords?" viewers input dwarf_rings Binary Representation of Integers – integers are Program Output (Example in Figure 3-10):​ stored as binary numbers in memory. [Ex. memory In Middle Earth, how many rings were given storage: 1 byte = 8 bits] to the dwarf lords? 7 (User input) ​ Unsigned Integers (1 Byte) – Range: 0 to 255 (binary: 00000000 to 11111111). “Using Stored Data in Output” ​ Signed Integers (1 Byte) – Range: −127 to +127. 1.​ The user enters data, which is stored in a -​ The leftmost bit (MSB) is used as a sign variable. bit (0 = positive, 1 = negative). 2.​ The stored value can later be displayed as part of a message. Integer Storage in Programming Languages 3.​ ​ C++ and Java: 4 bytes per integer, values from: Pseudocode: −2,147,483,648 to +2,147,483,647. initialize dwarf_rings = 0 ​ Python: uses more memory for integers, display "In Middle Earth, how many rings allowing larger values. were given to the dwarf lords?" Floating-point Data Types – numbers with decimal and input dwarf_rings fractional parts; expressed in decimal or E notation. display "Is", dwarf_rings, "your final answer?" E Notation – expressing floating-point numbers as powers of 10. (displays one digit to the left of the User Interaction (Figure 3-11 Example):​ decimal point.) In Middle Earth, how many rings were given to the dwarf lords? Ex: 5 (User input) Is 5 your final answer? 459.67 → 4.5967E2 (decimal moves left, exponent is positive). 4996 → 4.996E−1 (decimal moves right, exponent is Formatting Outputs negative). ​ Formatting Parameters - Elements that can alter the appearance of numeric and text output. Storage (Single Precision - 4 Bytes) – Range: ±3.4E−38 -​ Width: Number of spaces allocated to to ±3.4E38; Stored in 32 bits, consisting of: the output -​ Fill: Pad with spaces or zero ​ 1 bit → Sign bit (positive or negative). -​ Alignment: Align left or right within the ​ 8 bits → Exponent bits (determines power of 10 specified width ​ 23 bits → Data bits (stores the significant digits). -​ Decimal point display: Presence or Double Precision Floating-Point Numbers (8 Bytes) – absence of a decimal point Range: ±1.7E−308 to ±1.7E308; Stored in 64 bits: -​ Pression: Number of places displayed after the decimal points ​ More exponents and data bits allow for higher -​ Type: Integer, floating point, decimal, precision. binary, hexadecimal, character, or E ​ Declared using the keyword double in C++ and notation. Java. -​ Sign:The inclusion of a + or − sign. Mathematical Expression Original Value Formatting Parameters ​ Arithmetic Operation (+, -, *, /, and %) -​ Expressions: statement that has a 4.43050 value and usually arithmetic operators 4.431 Precision = 3, Displays three and operands. decimals places -​ Important Takeaways: 1.​ Integer Division: produces _ _ _ _4.431 Width = 9. Fixes the width at 9 fractional parts. leaving four blank spaces in front 2.​ Modulo Division: Produces the of the numbers remainder of a division >>>>4.431 Fill =>. Fill spaces with specified operation. character ​ Order Operations – sequence where other Lesson 5: Character and String mathematical operations are performed. -​ Here’s the Sequence Character data type - variables that hold one character 1.​ Do the math in parentheses of data. first. 2.​ Carry out exponentiation and ASCII (American Standard Code for Information roots. Interchange) - method for encoding letters, symbols, 3.​ Perform multiplication, division, and numerals using seven bits or eight bits. and modulo division as they appear from left to right. String - sequence of characters. 4.​ Execute addition and _____________________________________________ subtraction as they appear from String data type - assigned to variables that hold a left to right. sequence of characters.​ ​ ​ Compound Operators (+=, -=, *=, /=) – Escape sequence - Embedded characters beginning shortcuts for basic assignment operations. with a backslash ( \ ) to insert special symbols into a -​ Increment string.​ -​ Decrement ​ Index - position of a character in a string, beginning with Numeric Type Conversion position. ​ Convert Integers and Floating-Point _____________________________________________ Numbers String Manipulations -​ Coercion – literals automatically ​ Find the length of a string converted into a different data type. ​ Change the case of a string -​ Type Casting (casting) – converting a ​ Check if a string contains a specific variable of one type into another type. character. ​ Retrieve a substring from a longer string. String Length () Case Sensitivity - differentiation that makes a lowercase letter different from its uppercase version. Concatenation - data using the + concatenation operator.​ —------------------------------------------------------------------------ Rounding Quirks - unusual or unexpected behaviors that can occur when rounding numbers.

Use Quizgecko on...
Browser
Browser