计算机科学概述 Introduction to Computer Science 陆嘉恒 中国人民大学 信息学院 www.jiahenglu.net.

Slides:



Advertisements
Similar presentations
CS107 Introduction to Computer Science Lecture 2.
Advertisements

CS107: Introduction to Computer Science Lecture 2 Jan 29th.
CPSC 171 Introduction to Computer Science Efficiency of Algorithms.
COMPUTER PROGRAMMING I Understand Problem Solving Tools to Design Programming Solutions.
Computer Science 101 Overview of Algorithms. Example: Make Pancakes Prepare batter Beat 2 eggs Add 1 tablespoon of brown sugar Add 1 cup of milk Add 2.
Designing Algorithms Csci 107 Lecture 4. Outline Last time Computing 1+2+…+n Adding 2 n-digit numbers Today: More algorithms Sequential search Variations.
ITEC113 Algorithms and Programming Techniques
Chapter 1: An Introduction to Computer Science Invitation to Computer Science, C++ Version, Third Edition.
CS107 Introduction to Computer Science
What is an algorithm? Informally: An Algorithm is a step by step method for solving a problem. It’s purpose is to break a larger task down so that each.
Efficiency of Algorithms
Chapter 2: Algorithm Discovery and Design
Chapter 2: Algorithm Discovery and Design
Designing Algorithms February 2nd. Administrativia Lab assignments will be due every Monday Lab access –Searles 128: daily until 4pm unless class in progress.
CS107 Introduction to Computer Science Lecture 2.
Designing Algorithms Csci 107 Lecture 3. Designing algorithms Last time –Pseudocode –Algorithm: computing the sum 1+2+…+n –Gauss formula for 1+2+…+n Today.
Chapter 2 The Algorithmic Foundations of Computer Science
CHAPTER 2 CS
Designing Algorithms Csci 107 Lecture 3. Administrativia Lab access –Searles 128: daily until 4pm unless class in progress –Searles 117: 6-10pm, Sat-Sun.
Chapter 2: Design of Algorithms
Chapter 8: Introduction to High-level Language Programming Invitation to Computer Science, C++ Version, Third Edition.
Designing Algorithms Csci 107 Lecture 4.
Chapter 2: Algorithm Discovery and Design
Chapter 2: Algorithm Discovery and Design
Chapter 2: Algorithm Discovery and Design
New Mexico Computer Science For All More Looping in NetLogo Maureen Psaila-Dombrowski.
Adapted from slides by Marie desJardins
CSC103: Introduction to Computer and Programming
CPSC 171 Introduction to Computer Science 3 Levels of Understanding Algorithms More Algorithm Discovery and Design.
Algorithmic Problem Solving CMSC 201 Adapted from slides by Marie desJardins (Spring 2015 Prof Chang version)
Chapter 2: Algorithm Discovery and Design Invitation to Computer Science, C++ Version, Third Edition.
Invitation to Computer Science 6th Edition
Invitation to Computer Science, Java Version, Second Edition.
计算机科学概述 Introduction to Computer Science 陆嘉恒 中国人民大学 信息学院
CSE 102 Introduction to Computer Engineering What is an Algorithm?
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 5: Software Design & Testing; Revision Session.
Introduction to Algorithms. What is Computer Science? Computer Science is the study of computers (??) This leaves aside the theoretical work in CS, which.
CPSC 171 Introduction to Computer Science Algorithm Discovery and Design.
CPSC 171 Introduction to Computer Science More Algorithm Discovery and Design.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
Pseudocode Simple Program Design Third Edition A Step-by-Step Approach 2.
ITEC113 Algorithms and Programming Techniques
Dale Roberts 1 Program Control - Algorithms Department of Computer and Information Science, School of Science, IUPUI CSCI N305.
CSCI-100 Introduction to Computing
More Python!. Lists, Variables with more than one value Variables can point to more than one value at a time. The simplest way to do this is with a List.
Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure /
Algorithm Discovery and Design Objectives: Interpret pseudocode Write pseudocode, using the three types of operations: * sequential (steps in order written)
8.1 8 Algorithms Foundations of Computer Science  Cengage Learning.
September 7, 2004ICP: Chapter 3: Control Structures1 Introduction to Computer Programming Chapter 3: Control Structures Michael Scherger Department of.
Invitation to Computer Science 5 th Edition Chapter 2 The Algorithmic Foundations of Computer Science.
INVITATION TO Computer Science 1 11 Chapter 2 The Algorithmic Foundations of Computer Science.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 4: Introduction to C: Control Flow.
Algorithms and Pseudocode
ALGORITHMS AND FLOWCHARTS. Why Algorithm is needed? 2 Computer Program ? Set of instructions to perform some specific task Is Program itself a Software.
Flowcharts C++ Lab. Algorithm An informal definition of an algorithm is: a step-by-step method for solving a problem or doing a task. Input data A step-by-step.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
Chapter 2: Algorithm Discovery and Design Invitation to Computer Science.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
Program design Program Design Process has 2 phases:
Understand Problem Solving Tools to Design Programming Solutions
Understand Problem Solving Tools to Design Programming Solutions
Lecture 2: Introduction to Algorithms
COMS W1004 Introduction to Computer Science and Programming in Java
Chapter 6 Repetition Objectives ❏ To understand basic loop concepts:
Algorithm Discovery and Design
Algorithm Discovery and Design
Introduction to Algorithms and Programming
Computer Science Core Concepts
Discovery and Design of Algorithms
Presentation transcript:

计算机科学概述 Introduction to Computer Science 陆嘉恒 中国人民大学 信息学院

Computer Algorithm (计算机算法简介)

An Algorithm is A well-ordered collection of Unambiguous and Effectively computable operations that, when executed Produces a result and Halts in a finite amount of time Textbook definition

Representing Algorithms How do we represent algorithms? English Programming Language We will start with pseudocode and later will look at various programming languages.

Pseudocode Set of English Constructs designed to resemble statements in programming languages Steps presented in structured manner Simple, Highly readable

Constructs We Will Use Sequential operations to carry out –computation, –input, and –output. Conditional operations. Iterative operations.

Sequential Operations computation Set the value of X to 3. Assign X a value of A + B. X=2 - C. Set the value of Name to the first person's name. input Get a value for X, Y, and Z. Input values for A1, A2,..., Am. Read X, Y, and Carry. output Output the value of X. Print values for X, Y, and Carry. Print the message, "Error".

Example Algorithm (Sequential) Figure 2.3 Algorithm for Computing Average Miles per Gallon

Practice Write an algorithm in pseudocode to: Get the radius of a circle as input and output the circumference and area of the circle

Conditional Operation If “ a true/false condition ” is true then first set of algorithmic operations Else second set of algorithm operations Notice the indentation

Example Algorithm (conditionals) Figure 2.5 Second Version of the Average Miles per Gallon Algorithm

Practice Modify your algorithm to print “ large circle ” if the circumference is greater than 100 and “ small circle ” if it is equal to or less than 100.

Conditional Operation Picture true-false statement truefalse operation false branch true branch Note: either branch can be missing

true-false statement truefalse operation false branch true branch Note: either branch can be missing Conditional Operation Picture

true-false statement truefalse operation false branch true branch Note: either branch can be missing Conditional Operation Picture

Iterative Operations Set count to 5 While count < 4 Add 1 to count Print count stop Set count to 5 Do Add 1 to count Print count While count < 4 stop Notice when the continuation condition is checked Pretest loopPosttest loop

Infinite Loops Set count to 0 While count >= 0 Add 1 to count Print count stop

Iteration Operation Picture true-false statement true-false statement false true operation false operation while loop Do/while loop

Figure 2.7 Third Version of the Average Miles per Gallon Algorithm Example Algorithm (iteration)

Practice Write an algorithm that gets as input a single data value, x, and outputs the three values x 2, sin x, 1/x. This process should repeat until the input value for x is equal to 99.

Summary of Operation Types for Algorithms Sequential operations to carry out –computation, –input, and –output. Conditional operations. Iterative operations.

There is a theorem in theoretical computer science that proves that these operations are sufficient to represent ANY algorithm! Algorithms are used to do everything you see on a computer! Do word processing. Fly NASA probes to the planets. Run the international telephone switching system. Create CAT scan images. Process your pay checks. Run computer games. Etc. Powerful Algorithms!!

EXAMPLE OF AN ALGORITHM PROBLEM: Start with a collection of names N 1, N 2,..., N 10000, and corresponding telephone numbers T 1, T 2,..., T Given a name, Name, find and print the telephone number for that name if a match on an N i occurs; otherwise, print "Not Found". Given a problem, there are often many ways to provide an algorithm for solving the problem. Note: You must understand the methodology for solving the problem in order to write an algorithm for the solution!!!

First Try 1. Get values for N 1, N 2,..., N 10000, T 1, T 2,,,,, T 10000, and Name. 2. If Name is N 1, then print T 1 Stop 3. If Name is N 2, then print T 2. Stop. {a lot of tedious writing here that is being skipped} If Name is N 10000, then print T Stop Print "Not found" Stop.

Second Try 1. Get values for N 1, N 2,..., N 10000, T 1, T 2,,,,, T 10000, and Name. 2. Set the value of i to 1 and the value of Found to NO. 3. While Found is NO 4.If Name is equal to N i, then 5.Print the telephone number T i 6. Set the value of Found to Yes Else 7.Add 1 to the value of i 8. Stop.

Third Try 1. Get values for N 1, N 2,..., N 10000, T 1, T 2,,,,, T 10000, and Name. 2. Set the value of i to 1 and the value of Found to NO. 3. While Found is NO and i <= If Name is equal to N i, then 5.Print the telephone number T i 6. Set the value of Found to Yes Else 7.Add 1 to the value of i 8. If (Found is No) then 9.Print "Not found" 10. Stop.

Game Time Who can beat me in the game 21? Objective: Get your opponent to say the number 21. Rules: One player goes first starting counting from 1. On each turn a player may say one or two consecutive numbers. You must begin where your opponent left off. The first person to say the number 21 loses. There is a way to always beat me!