Algorithms CSC1310 Fall 2009. What Is Programming? Programming Programming means writing down a series of instructions that tell a computer what to do.

Slides:



Advertisements
Similar presentations
Pudding recipes Using egg as a thickener What is tempering? What are some tips when cooking with milk? If you are cooking in a teflon pan what kind of.
Advertisements

CS101: Introduction to Computer programming
A number of MATLAB statements that allow us to control the order in which statements are executed in a program. There are two broad categories of control.
CS107 Introduction to Computer Science Lecture 2.
CS107: Introduction to Computer Science Lecture 2 Jan 29th.
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Making Decisions in Python Sec 9-10 Web Design. Objectives The student will: Understand how to make a decision in Python Understand the structure of an.
Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 1: Getting Started Java Programming FROM THE BEGINNING Chapter 1 Getting Started.
CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
Chapter 2: Algorithm Discovery and Design
10 November JavaScript. Presentation Hints What do YOU think makes a good presentation Some of my suggestions Don’t write full sentences on slides Talk,
Program Design and Development
Slides prepared by Rose Williams, Binghamton University Chapter 3 Flow of Control Loops in Java.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Objectives You should be able to describe:
Programming Fundamentals (750113) Ch1. Problem Solving
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements II.
Chapter 3 Planning Your Solution
C++ Basics CSci 107. A C++ program //include headers; these are modules that include functions that you may use in your //program; we will almost always.
Fundamentals of Python: From First Programs Through Data Structures
Chapter 9 IF Statement Bernard Chen. If Statement The main statement used for selecting from alternative actions based on test results It’s the primary.
Chapter 9 IF Statement Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2012.
Chapter 1: Getting Started Java Programming FROM THE BEGINNING 1 Chapter 1 Getting Started.
The University of Texas – Pan American
Fundamentals of Python: First Programs
General Programming Introduction to Computing Science and Programming I.
Goals of Course Introduction to the programming language C Learn how to program Learn ‘good’ programming practices.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Lecture 2 Fundamental Data Types. Variable Declaration.
Input, Output, and Processing
Flow of Control Part 1: Selection
Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Branches and Program Design
Chapter 1: Getting Started Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 1 Getting Started.
Chapter 3: Branching and Program Flow CSCI-UA 0002 – Introduction to Computer Programming Mr. Joel Kemp.
Python Conditionals chapter 5
CSCI-100 Introduction to Computing
Python 101 Dr. Bernard Chen University of Central Arkansas PyArkansas.
Software Development. Software Development Loop Design  Programmers need a solid foundation before they start coding anything  Understand the task.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
COIT29222 Structured Programming 1 COIT29222-Structured Programming Lecture Week 02  Reading: Textbook(4 th Ed.), Chapter 2 Textbook (6 th Ed.), Chapters.
Chapter 1: Getting Started Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 1 Getting Started.
Algorithms and Pseudocode
1 Control Structures (Chapter 3) 3 constructs are essential building blocks for programs Sequences  compound statement Decisions  if, switch, conditional.
Pseudocode Skill Area Materials Prepared by Dhimas Ruswanto, BMm.
Pseudocode. Algorithm A procedure for solving a problem in terms of the actions to be executed and the order in which those actions are to be executed.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand how decisions are made in a computer ❏ To understand the logical.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
Control Structures I Chapter 3
Basic concepts of C++ Presented by Prof. Satyajit De
Topics Designing a Program Input, Processing, and Output
Getting Started Chapter 1 Copyright © 2000 W. W. Norton & Company.
CSC113: Computer Programming (Theory = 03, Lab = 01)
Introduction To Flowcharting
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Getting Started Chapter 1 Copyright © 2000 W. W. Norton & Company.
Chapter 8 JavaScript: Control Statements, Part 2
Programming Fundamentals (750113) Ch1. Problem Solving
T. Jumana Abu Shmais – AOU - Riyadh
Chapter 3: Selection Structures: Making Decisions
Boolean Expressions to Make Comparisons
Topics discussed in this section:
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
CHAPTER 5: Control Flow Tools (if statement)
Chapter 3: Selection Structures: Making Decisions
Module 3 Selection Structures 6/25/2019 CSE 1321 Module 3.
Presentation transcript:

Algorithms CSC1310 Fall 2009

What Is Programming? Programming Programming means writing down a series of instructions that tell a computer what to do. Properties of these instructions:  Computation proceeds in discrete steps.  Each step is precisely defined.  The order in which steps are performed may be important.

Algorithms algorithm A set of instructions with these properties is said to be an algorithm. The steps in an algorithm are not always short and simple.  Some steps may involve a series of smaller steps.  Some steps may involve making decisions.  Some steps may repeat. Algorithms are common in the real world.

The Recipe 1.Melt slowly and keep warm: 1/2 cup butter. 2.Barely heat: 1 1/2 tablespoons lemon juice, dry sherry, or tarragon vinegar. 3.Place in the top of a double boiler over—not in—hot water: 3 egg yolks. 4. Beat the yolks with a wire whisk until they begin to thicken. Add: 1 tablespoon boiling water. 5. Beat again until the eggs begin to thicken. Repeat this process until you have added: 3 more tablespoons water. 6.Then beat in the warm lemon juice. 7.Remove double boiler from heat. 8.Beat the sauce well with a wire whisk. Continue to beat while slowly adding the melted butter and: 1/4 teaspoon salt; a few grains cayenne. 9.Beat until the sauce is thick. 10.Serve at once.

The Recipe as an Algorithm This recipe satisfies most of the requirements for an algorithm:  It involves discrete steps.  Each step is more or less precisely defined.  The order of the steps matters, at least to some extent. Some steps require making decisions and repeating actions. This recipe is detailed enough for an experienced cook, but it might pose problems for a novice. Computer algorithms need to be much more precise.

Computer Algorithms Computer algorithms often involve obtaining input, performing a calculation, and producing output. An algorithm for converting from Fahrenheit to Celsius: 1. Ask the user to enter a Fahrenheit temperature. 2. Obtain the input entered by the user. 3. Convert the user’s input into numerical form. 4. Calculate the equivalent Celsius temperature, using the formula C = (F – 32)  (5 / 9) 5. Convert the Celsius temperature into character form and display the result.

Boil Water for a Tea Algorithm Input: empty kettle, stove, faucet Output: boiled water in the kettle. Algorithm? Reuse of algorithm.

Ways to Express Algorithms Natural languages Natural languages. Allows anyone who understands that language to read the algorithm, but lacks precision. Programming languages Programming languages. Precise, yet simple enough for computers to understand. Pseudocode. Pseudocode. A mixture of natural language and a programming language. More precise than natural language but less precise than a programming language. Often easier to read (and to write) than a programming language.

Fahrenheit to Celsius Conversion in Python 1. Ask the user to enter a Fahrenheit temperature. 2. Obtain the input entered by the user. 3. Convert the user’s input into numerical form. >>>x=float(raw_input(“Please, enter Fahrenheit temperature:\n”)) 4. Calculate the equivalent Celsius temperature, using the formula C = (F – 32)  (5 / 9) 5. Convert the Celsius temperature into character form and display the result. >>>print “The Celsius temperature is ” +str((x-32)*(5.0/9))

Chapter 9 IF Statement CSC1310 Fall 2009

If Statement The main statement used for selecting from alternative actions based on test results It’s the primary selection tool in Python and represents the Logic process ( “==”, “!=”, “>”, “>=”, “<”, “<=”)

Simple If statement if It takes the form of an if test if :

Simple If statement >>>if 4>3: print “it is true that 4>3” >>>if 1: print “true” >>>a=10 >>>if a==10: print “a=10”

If... else statement if else It takes the form of an if test, and ends with an optional else block if : else:

If... else statement >>>if 3>4: print “3 is larger than 4” else: print “3 is NOT larger than 4” >>>if {}: print “true” else: print “false”

If… else if … else statement if elif else It takes the form of an if test, followed by one or more optional elif tests, and ends with an optional else block if : elif : else:

If… else if … else statement >>>a=10 >>>if a>10: print “a > 10” elif a==10: print “a = 10” else: print “a < 10”

If… else if … else statement example >>>number = 23 >>>guess = int(raw_input('Enter an integer : ')) >>>if guess == number: print 'Congratulations, you guessed it.' # New block starts here print "(but you do not win any prizes!)" # New block ends here elif guess < number: print 'No, it is a little higher than that' # Another block else: print 'No, it is a little lower than that'

Syntax Rules Statements execute one after another, unless you say otherwise. automatically Block and statement boundaries are detected automatically (no “end/begin” delimiters). Header “:” indented statements Header “:” indented statements (pattern of all compound statements). Statement delimiters Statement delimiters :  Statement may span lines if you’re continuing an open syntactic pair((),{},[]);  Statement may span lines if they end with backslash.  Block literals. non-compound Several non-compound statements could be written at the same line being separated by semicolons. You could move body of compound statement up to header line.

Truth Test X and Y bothtrue X and Y is true if both X and Y are true. >>>a=5;b=3;c=2 >>> a>b and a>c >>> b>c and b>a >>>2 and 3, 3 and 2, {} and [], 3 and [] X or Y eithertrue X or Y is true if either X or Y are true. >>> b>c or b>a >>> b>a or b>c >>>2 or 3, 3 or 2, {} or [], 3 or [] not X false not X is true if X is false. >>> not 2, not {}, not [1,2,3,4,5]

Non empty set Assume user may input some data to A, B and C. Program does not know if data was provided. >>>X = A or B or C or None