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.

Slides:



Advertisements
Similar presentations
Introduction to Programming
Advertisements

CS107 Introduction to Computer Science Lecture 3, 4 An Introduction to Algorithms: Loops.
CS107 Introduction to Computer Science Lecture 2.
CS107: Introduction to Computer Science Lecture 2 Jan 29th.
Computer Science 101 Survey of Computer Science What is computer science?
CSci 107 Introduction to Computer Science Lecture 1.
 Control structures  Algorithm & flowchart  If statements  While statements.
© 2011 wheresjenny.com PREPARING PANCAKES PANCAKES SERVINGS =8 PANCAKES PREPARATION TIME : 25 MINS.
Chapter 2: Algorithm Discovery and Design
CSci 107 Introduction to Computer Science Lecture 1.
Programming Concepts (Part A) Ping Hsu. What is a program? WHITE CAKE RECIPE 1.Preheat oven to 350 degrees F (175 degrees C). 2.Grease and flour a 9x9.
Slides prepared by Rose Williams, Binghamton University Chapter 3 Flow of Control Loops in Java.
Chapter 2: Design of Algorithms
Chapter 3 Planning Your Solution
Make butter milk pancake batter.  Did you know that there is a pancake day?  These breakfast treats can be called hot cakes, and flapjacks  Pancake.
Fundamentals of Python: From First Programs Through Data Structures
Adapted from slides by Marie desJardins
How to make a pancake By Sophie Draper. Ingredients: Frying pan Large Mixing Bowl Long handled spatula Mixing Spoon 2 Eggs 2 cups of flour 2 ¾ cups of.
Fundamentals of Python: First Programs
An ordered sequence of unambiguous and well-defined instructions that performs some task and halts in finite time Let's examine the four parts of this.
Algorithmic Problem Solving CMSC 201 Adapted from slides by Marie desJardins (Spring 2015 Prof Chang version)
计算机科学概述 Introduction to Computer Science 陆嘉恒 中国人民大学 信息学院
Invitation to Computer Science, Java Version, Second Edition.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 4: Control Structures I (Selection)
Algorithms A sequence of instructions 1. Preheat oven to 350 degrees F (175 degrees C). Grease and flour a 9x9 inch pan or line a muffin pan with paper.
Computer Science 111 Fundamentals of Programming I The while Loop and Indefinite Loops.
Chapter 8 High-Level Programming Languages. 8-2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,
CSE 102 Introduction to Computer Engineering What is an Algorithm?
Introduction to Algorithms. What is Computer Science? Computer Science is the study of computers (??) This leaves aside the theoretical work in CS, which.
Problem Solving Techniques. Compiler n Is a computer program whose purpose is to take a description of a desired program coded in a programming language.
Computer Science 111 Fundamentals of Programming I Making Choices with if Statements.
Practical Programming COMP153-08S Week 4: Recap, Accessing Files, Menus.
CPSC 171 Introduction to Computer Science Algorithm Discovery and Design.
ITEC113 Algorithms and Programming Techniques
Pancakes. Person 1 In a medium size bowl measure 1 c flour 2 T sugar 2 tsp baking powder 3/4 tsp salt - mix thoroughly.
Computer Science 101 Introduction.
CSCI-100 Introduction to Computing
Q How to make pancakes /. 4 cups flour 2 tablespoons of sugar 1 teaspoon of salt two eggs beaten 4 cups of milk oil.
CS 240 – Computer Programming I Lab Kalpa Gunaratna –
ALGORITHMS.
Algorithm Discovery and Design Objectives: Interpret pseudocode Write pseudocode, using the three types of operations: * sequential (steps in order written)
Introduction to design and analysis algorithm
Topic: Control Statements. Recap of Sequence Control Structure Write a program that accepts the basic salary and allowance amount for an employee and.
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.
STEP 3- DEVELOP AN ALGORITHM At this stage we break down the problem into simple manageable steps so that they can be handled easily.
Algorithms JPC and JWD © 2002 McGraw-Hill, Inc. 2 Algorithms 2 An Algorithm is a finite set of precise instructions for performing a computation or for.
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
PROGRAMMING: What’s It All About?
Program design Program Design Process has 2 phases:
Computer Science Department
Applied Discrete Mathematics Week 2: Functions and Sequences
Making Choices with if Statements
ALGORITHMS AND FLOWCHARTS
Algorithms and Flowcharts
Cooking a traditional dish (Yeast pancakes)
Algorithm and Ambiguity
CS 240 – Lecture 11 Pseudocode.
What Is Computer Science?
Introduction to pseudocode
CSci 107 Introduction to Computer Science
MSIS 655 Advanced Business Applications Programming
CSC128 FUNDAMENTALS OF COMPUTER PROBLEM SOLVING
Algorithm and Ambiguity
Computer Science Core Concepts
Flowcharts and Pseudo Code
PANCAKES & WAFFLES.
Computer Science 101 Survey of Computer Science
REPETITION Why Repetition?
Presentation transcript:

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 tablespoons of melted butter Add 2 cups of flour and 1 tablespoon of baking powder Mix well Cook batter Pour three spoons full of batter into greased pan Heat at medium until bubbles appear Turn pancakes over Repeat last 3 steps until batter is gone

Programming a VCR Step 1. If the clock and calendar are not correctly set, then go to page 9 of the owner's manual and follow instructions there. Step 2. Place a blank tape in the VCR tape slot. Step 3. Repeat steps 4-7 for each program, up to 10 times Step 4. Enter the channel number and press CHAN Step 5. Enter time to start and press TIME-START Step 6. Enter time to stop and press TIME-FINISH Step 7. If no more programs, press END-PROG Step 8. Press TIMER

Properties of an Algorithm A finite sequence of steps Describes a process that halts with a solution to a problem Each step must be effectively computable

Pseudocode Informal language used to present algorithms Pretty close to English but precise enough for a computing agent to carry out

Pseudocode Algorithms Algorithms consist of a set of steps Steps are also called instructions, statements, or actions

Instructions and Data Instructions describe processes Data are things or entities that can be processed Example: (the + names the addition instruction and the 2 is an integer datum)

Variables and Constants Variables are names for data values The value of a variable can change during the course of a process Constants also are data values but cannot change

Example: Compute the Area of a Circle input radius set area to 3.14 * radius * radius output area Uses mathematical knowledge, that is area =  r 2 Pseudocode algorithm:

Arithmetic Expressions input radius set area to 3.14 * radius * radius output area Use the standard operators for addition and multiplication Use variables and constants for operands

Variable Assignment input radius set area to 3.14 * radius * radius output area Make a variable take on a new value set to

Input input radius set area to 3.14 * radius * radius output area Send data from the external world to the computing agent input get or

Output input radius set area to 3.14 * radius * radius output area Send data from the computing agent to the external world output print or

Flow of Control: Sequencing input radius set area to 3.14 * radius * radius output area Run a sequence of statements one after the other … A sequence

Flow Diagram for Sequencing statement Sequencing statement

Compute the Radius Uses input area set radius to square root of (area / 3.14) output radius  /arearadius 

Functions Uses input area set radius to sqrt(area / 3.14) output radius  /arearadius  A function names an action that transforms one or more data values (its arguments) into another data value sqrt(4) is 2, sqrt(25) is 5, etc.

Restricting the Inputs Well-defined for input values that are greater than 0 We should output an error message if the input is less than or equal to 0 input area set radius to sqrt(area / 3.14) output radius

Check the Input First input area if area <= 0 output "Error: area must be positive" else set radius to sqrt(area / 3.14) output radius Well-defined for input values that are greater than 0 We should output an error message if the input is less than or equal to 0

Flow of Control: Conditional input area if area <= 0 output "Error: area must be positive" else set radius to sqrt(area / 3.14) output radius Take one action if a true/false test is true Take an alternative action if that test is false

Flow of Control: Conditional if else Take one action if a true/false test is true Take an alternative action if that test is false Each statement can be a sequence of statements

Boolean Expressions if else A true/false test is also called a Boolean expression Most Boolean expressions compare two values using standard operators ( =,, etc)

Compute the Absolute Value input number if number < 0 set number to -number output number Uses arithmetic negation (-)

One-Way Conditional input number if number < 0 set number to -number output number There is one course of action if the test is true but no alternative course of action if the test is false

Flow Diagrams for Conditionals ? statement ? false statement One-way conditional Two-way conditional

Syntax and Semantics Syntax: the form of a statement Semantics: the meaning of a statement or what the statement does

One-Way Conditional ? statement false Semantics Syntax if Example if number < 0 set number to -number

Example: Summation Write an algorithm that computes the sum of the numbers between 1 and 10. set counter to 1 set sum to 0 while counter <= 10 set sum to sum + counter increment counter output sum

Flow of Control: Iteration Iteration allows a sequence of statements to be repeated zero or more times A loop keeps going while a continuation condition is true set counter to 1 set sum to 0 while counter <= 10 set sum to sum + counter increment counter output sum

Syntax and Semantics of while Statements while while. ? statement true false

Count-controlled Loops // General form while

Today Print Homework Exercise 1 and get started