Administrativia. CS107 Introduction to Computer Science. Readings. Algorithms. Expressing algorithms

Similar documents
REPRESENTING ALGORITHMS. REPRESENTING ALGORITHMS IB DP Computer science Standard Level ICS3U

Getting Started. Excerpted from Hello World! Computer Programming for Kids and Other Beginners

9/23/2013. Chapter 2. Objectives. Introduction. Objectives (continued) Representing Algorithms. Representing Algorithms

CS 142 Style Guide Grading and Details

Chapter 2: Algorithm Discovery and Design. Invitation to Computer Science, C++ Version, Fourth Edition

Programming with Python

A Quick Review of Chapter 1

Chapter 2. The Algorithmic Foundations of. Computer Science INVITATION TO. Computer Science. Tuesday, September 10, 13

Hello World! Computer Programming for Kids and Other Beginners. Chapter 1. by Warren Sande and Carter Sande. Copyright 2009 Manning Publications

Lecture Transcript While and Do While Statements in C++

Homework #2. If (your ID number s last two digits % 6) = 0: 6, 12, 18

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

Administration Computers Software Algorithms Programming Languages

Computer Science 210: Data Structures

Introduction to C++ General Rules, Conventions and Styles CS 16: Solving Problems with Computers I Lecture #2

Solving Problems Flow Control in C++ CS 16: Solving Problems with Computers I Lecture #3

SAMS Programming A/B. Lecture #1 Introductions July 3, Mark Stehlik

Programming Lecture 3

Decisions, Decisions. Testing, testing C H A P T E R 7

CS1 Lecture 9 Feb. 5, 2018

Pseudocode. ARITHMETIC OPERATORS: In pseudocode arithmetic operators are used to perform arithmetic operations. These operators are listed below:

LESSON 2 VARIABLES, OPERATORS, EXPRESSIONS, AND USER INPUT

Chapter 2. Designing a Program. Input, Processing, and Output Fall 2016, CSUS. Chapter 2.1

Question: Total Points: Score:

CMSC 150 LECTURE 1 INTRODUCTION TO COURSE COMPUTER SCIENCE HELLO WORLD

Pseudo-code vs. Assembly. Introduction to High-Level Language Programming. Disadvantages of Assembly. Pseudo-code vs. High-level Programs

Constructing Algorithms and Pseudocoding This document was originally developed by Professor John P. Russo

Introduction to Java Applications

Computer Programming, I. Laboratory Manual. Experiment #2. Elementary Programming

Basic Data Types and Operators CS 8: Introduction to Computer Science, Winter 2019 Lecture #2

Chapter 2: Data and Expressions

CS1 Lecture 3 Jan. 22, 2018

Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

If Statements, For Loops, Functions

Outline. Program development cycle. Algorithms development and representation. Examples.

Building Java Programs

Lecture 2. Programs and Pseudocode. Daily Puzzle

CS 150 Introduction to Computer Science 1

CSE 142, Summer 2015

HUDSONVILLE HIGH SCHOOL COURSE FRAMEWORK

CS1 Lecture 22 Mar. 6, 2019

JavaScript Basics. The Big Picture

Introduction and Functions Summer 2018 Discussion 0: June 19, 2018

Try typing the following in the Python shell and press return after each calculation. Write the answer the program displays next to the sums below.

Identify skills and personality traits of successful problem solving. Apply standard problem-solving techniques to aid in problem solving.

Practice Final Examination #2

(0) introduction to the course. how to learn a programming language. (0) course structure

Outline. Announcements. Homework 2. Boolean expressions 10/12/2007. Announcements Homework 2 questions. Boolean expression

WELCOME! (download slides and.py files and follow along!) LECTURE 1

WASHINGTON STATE MIDDLE SCHOOL COMPUTER SCIENCE COMPETITION 2017

CS1 Lecture 3 Jan. 18, 2019

CONTENTS: What Is Programming? How a Computer Works Programming Languages Java Basics. COMP-202 Unit 1: Introduction

#2 Madlibs. Print out a story using the five variables into a text box. Make a procedure called madlibs that works with a Main and a Start procedure.

(2 1) What does evaluate to? What does evaluate to? What does evaluate to?

AP Calculus AB Summer Assignment 2018

CpSc 1111 Lab 6 Conditional Statements, Loops, the Math Library, and Random Numbers What s the Point?

CIS 110: Introduction to Computer Programming

LOOPS. Repetition using the while statement

Tips from the experts: How to waste a lot of time on this assignment

MITOCW watch?v=0jljzrnhwoi

VISUAL GUIDE to. RX Scripting. for Roulette Xtreme - System Designer 2.0. L J Howell UX Software Ver. 1.0

CS 1110, LAB 1: PYTHON EXPRESSIONS.

CPSC 217 Assignment 3

Lecture 1. Types, Expressions, & Variables

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

CS1 Lecture 5 Jan. 26, 2018

The Beauty and Joy of Computing

Topic 4 Expressions and variables

CSCI 121: Anatomy of a Python Script

How to approach a computational problem

COMP 202 Java in one week

CS1 Lecture 5 Jan. 25, 2019

C++ Programming Language Lecture 2 Problem Analysis and Solution Representation

CS 1803 Pair Homework 3 Calculator Pair Fun Due: Wednesday, September 15th, before 6 PM Out of 100 points

CSC209. Software Tools and Systems Programming.

MergeSort, Recurrences, Asymptotic Analysis Scribe: Michael P. Kim Date: April 1, 2015

Unit 7: Algorithms and Python CS 101, Fall 2018

Problem Solving and Program Design - Chapter 1. Cory L. Strope

Question: Total Points: Score:

COMP108 Algorithmic Foundations

CSCE 120: Learning To Code

Week One: Introduction A SHORT INTRODUCTION TO HARDWARE, SOFTWARE, AND ALGORITHM DEVELOPMENT

(I m not printing out these notes! Take your own.)

Data 8 Final Review #1

CS164: Midterm I. Fall 2003

CS 177 Week 15 Recitation Slides. Review

Lecture 2: Variables & Assignments

CS 1803 Individual Homework 1 Python Practice Due: Wednesday, January 26th, before 6 PM Out of 100 points

CSEN 102 Introduction to Computer Science

Name: Per: Date: Intermediate Computers - Standard 5 Programming Checklist. again" if they got it wrong. Pay attention to the IF/THEN

CIS220 In Class/Lab 1: Due Sunday night at midnight. Submit all files through Canvas (25 pts)

A453 Task 1: Analysis: Problem: Solution:

Lecture Notes for Chapter 2: Getting Started

CSCA08 Winter 2018 Week 3: Logical Operations, Design Recipe. Marzieh Ahmadzadeh, Brian Harrington University of Toronto Scarborough

Unit E Step-by-Step: Programming with Python

BUGCATCHER SITE REVIEW Version 1, Cassie Chin, 10/10/2014,

6.189 Project 1. Readings. What to hand in. Project 1: The Game of Hangman. Get caught up on all the readings from this week!

Algorithm Discovery and Design

Introduction to C Final Review Chapters 1-6 & 13

Transcription:

CS107 Introduction to Computer Science Lecture 2 An Introduction to Algorithms: and Conditionals Administrativia Lab access Searles 128: Mon-Friday 8-5pm (unless class in progress) and 6-10pm Sat, Sun noon-10pm Searles 117: 6-10pm, Sat-Sun 12-10pm Study group Leader: Richard Hoang 05 Time: TBD (Mondays??) Location: Searles 128 Puzzle: How do people think??????? Before A, B, C and D ran a race they made the following predictions: A predicted that B would win B predicted that D would be last C predicted that A would be third D predicted that A s prediction would be correct. Only one of these predictions was true, and this was the prediction made by the winner. In what order did A, B, C, D finish the race? Readings A techie, absolutely, and no more Faster supercomputer aiding weather forecasts Space station gets HAL-like computer Algorithms How to come up with an algorithm? Part of it its routine Part it s problem solving So far we ve talked about how to solve a problem (e.g. sorting) at a high level of abstraction If we are to write down in detail an algorithm, what is a good way to express it? In English?? In a programming language?? Expressing algorithms Is natural language good? For daily life, yes but for CS is lacks structure and would be hard to follow Too rich, ambiguous, depends on context How about a programming language? Good, but not when we try to solve a problem..we want to think at an abstract level It shifts the emphasis from how to solve the problem to tedious details of syntax and grammar.

Pseudocode Pseudocode = English but looks like programming Good compromise Simple, readable, no rules, don t worry about punctuation. Lets you think at an abstract level about the problem. Contains only instructions that have a well-defined structure and resemble programming languages Building algorithms Basic (primitive) instructions Read the input from user Print the output to the user Cary out basic arithmetical computations Conditional instructions Execute an instruction (or a block of instructions) if a condition is true Repeat instructions Execute a block of instructions (multiple) times until a certain condition is met and variables Variables Variable A memory location that can store a value; we address it by its name Think of it as a box into which you can store a value, and from which you can retrieve a value A model for visualizing an algorithm Algorithm Variables s: i M Instructions An algorithm consists of instructions Instructions involve variables 1. Assign values to variables using basic arithmetic operations Set the value of x to 3 Set the value of y to x/10 Set the value of z to x +25 2. Get input from user Get the value of x from user 3. Print to user (screen) Print the value of y, z to the user 1. Assign values to variables using basic arithmetic operations : Let s assume we have two variable, a and b. The current value of a is 10 and the current value of b is 31. We execute: Set the value of a to 3 This instruction changes the value of a to 3. Now we execute Set the value of b to a*3 + 12 This instruction changes the value of b to 3+3 + 12 = 21

2. Read input from user : get the value of [variable] a [from user] When this instruction is executed, the algorithm stops, waits for the user to type in an answer, and the value typed by the user is stored into variable a. Whatever the value of a was before, after this instruction it is whatever the user entered. 3. Print to user (screen) Print variables and/or text : print the value of variable x When a print instruction is executed, the algorithm writes to screen the value of variable x print Hello world! This instruction prints to screen the text Hello world! Why do we need quotation signs? Print hello Print hello Write an algorithm that asks the user for three numbers, computes their sum and average and outputs them. One possible algorithm for this problem: Variables: a,b,c, sum, avg Get the values of a, b, c from user Set avg to (a+b+c)/3 Set sum to (a+b+c) Print sum, avg Write an algorithm that reads the value of a circle radius from the user, and prints the circumference of a circle with that radius. One possible algorithm for this problem: variables: r, c 1. Get the value of r from user 2. Set c to 2 * pi * r 3. Print The circumference of your circle is c A model for visualizing an algorithm s behavior Input (keyboard) Computer Algorithm Output (screen) Write an algorithm that asks the user for his year of birth, then prints it out, says thank you and good bye. It should look like this: Hi. Please tell me your year of birth: 1920 You said 1920. Congratulations. Goodbye.

Specifies an instruction (or a block of instructions) that may or may not be done, depending whether the condition is true or false. The else part is optional. if <condition> then <instruction(s) to be done> [else <instruction(s) to be done otherwise>] Conditional Instructions if the value of x is 0 then set the value of a to 0, else set the vale of a to a+1 If the value of b is equal to the value of a than print equal numbers, else print bad luck.. Conditional instructions Variants: Both then and else cases Only then case (no else) One instruction or a block of instructions (use indentation) s: If the value of a is equal to the value of b then print The numbers are equal else print The numbers are not equal If the value of a is greater than10 then substract 10 from a If the value of x is >10 then set b to x - 10 set c to x+10 print b, Else set x to x + 10 print x Consider the following algorithm: print Enter two values get a, get b if the value of a is larger then the value of b then print a else print b What does the program print if the user enter 10, 3? Describe in English what the algorithm accomplishes. Write an algorithm that asks the user for two numbers that represent the scores to a soccer game Bowdoin vs. Colby, and prints out either You won, You tied or You lost. For example: Enter Bowdoin score: 4 Enter Colby score: 2 You won. Pseudocode This is correct Get the value of a from user Get the value of b from user If the value of a is equal to the value of b then print the value of a, else set the value of c to the value of a + the value of b and print c But, we ll slowly converge towards a more compact, math-like style Get a, get b If (a==b) then print a Else set c = a + b print c Note: use == to test for equality use = for assignment Pseudocode examples Equivalent: Set the value of a to 1 Set a to 1 a=1 Equivalent Add 1 to count Set count to count + 1 Increment the value of count by 1 count = count + 1 Writing in pseudocode gives you the freedom to choose any of these. In general people prefer the shorter form.

Incorrect Set 1 to a Add a + b (what do you do with the result?) Set a+b +3 NOT the same set a to b set b to a : what is the output of the following algorithms? set a to 2 set a to 2 set b to 4 set b to 4 set a to b set b to a print a, b print a, b Write an algorithm to compute the distance traveled and the average miles per gallon on a trip when given as input the number of gallons used and the starting and ending mileage readings on the odometer. Variables: gallons, start, end, distance, mpg get gallons, start, end distance = end - start mpg = distance / gallons print mpg if mpg > 25.0 then print You are getting good gas mileage else print You are NOT getting good gas mileage Conditions If (condition) then. else Condition must evaluate to true or false Building conditions Use >, >=, <, <=, ==, remainder s: If (a >= b) If (a + b <= c) If (x == y) If (the remainder of x divided by y == 0) Note: the operator for remainder is denoted % If (x%2 == 0) print even number Combining Conditions Use logical operators: and, or, not If (condition 1 and condition 2) True if BOTH are true If (condition 1 or condition 2) True if at least one is true If (not condition 1) True if condition 1 is not true s If (score > 85 and score < 95) then print grade is A- If (not (score < 2)) print you passed Write an algorithm that asks the user for an exam score, which is expected to be a number between 0 and 100. If the score does not fit in this range, display an error message. Otherwise assign a letter grade A, B, C, D or F. The breaks are 90, 80, 70, 60. For example: Please enter a score, between 0 and 100: 110. Sorry, that s impossible. Please enter a score between 0 and 100: 85 The grade is B.