CS 100Lecture 11 Introduction to Programming n What is an algorithm? n Input and output n Sequential execution n Conditional execution Reading: Chapter.

Slides:



Advertisements
Similar presentations
MATH 224 – Discrete Mathematics
Advertisements

CS107: Introduction to Computer Science Lecture 2 Jan 29th.
CS110 Programming Language I
P ROGRAMMING L ANGUAGES : C ONTROL 1. S LIDES R EFERENCES Kenneth C. Louden, Control I: Expressions and Statements: Principles and Practice. 2.
CS 100Lecture 61 CS100J Lecture 6 n Previous Lecture –Programming Concepts n Programming by stepwise refinement –a pattern –sequential refinement –case.
Chapter 2: Algorithm Discovery and Design
Introduction to Computers and Programming Lecture 5 New York University.
Lecturer: Fintan Costello Welcome to Hdip 001 Introduction to Programming.
B-1 Lecture 2: Problems, Algorithms, and Programs © 2000 UW CSE University of Washington Computer Programming I.
Introduction to Computers and Programming Lecture 5 Boolean type; if statement Professor: Evan Korth New York University.
CS 100Lecture 41 CS100J Lecture 4 n Previous Lecture –Programming Concepts n iteration n programming patterns (templates) –Java Constructs n while-statements.
Lecture Notes 1/21/04 Program Design & Intro to Algorithms.
Variables, Data Types, & Arithmetic Expressions CSC 1401: Introduction to Programming with Java Lecture 3 Wanda M. Kunkle.
Relational Operators Control structures Decisions using “if” statements  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
1 Algorithms and Problem Solving. 2 Outline  Problem Solving  Problem Solving Strategy  Algorithms  Sequential Statements  Examples.
Algorithms. Introduction Before writing a program: –Have a thorough understanding of the problem –Carefully plan an approach for solving it While writing.
Lecture Notes 8/30/05 Program Design & Intro to Algorithms.
Writing algorithms using the for-statement. Programming example 1: find all divisors of a number We have seen a program using a while-statement to solve.
Introduction to Programming (in C++) Introduction Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
CS1101: Programming Methodology Aaron Tan.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
Introduction CSE 1310 – Introduction to Computers and Programming
Introduction CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
1 CSC 201: Computer Programming I B. S. Afolabi. Introduction  3 unit course  2 hours of lecture/week Thursdays 4.00pm – 6.00pm Mondays 4.00pm – 6.00pm.
Introduction CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Recursion Textbook chapter Recursive Function Call a recursive call is a function call in which the called function is the same as the one making.
Chapter 1 Introduction to Computers and C++ Programming Goals: To introduce the fundamental hardware and software components of a computer system To introduce.
Java means Coffee Java Coffee Beans The name “JAVA” was taken from a cup of coffee.
CS 100Lecture 281 CS100J Lecture 27 n Previous Lecture –Interfaces –Comparable –Reflection –super –Re-Reading: n Lewis & Loftus, Section 5.5 n Savitch,
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 5: Software Design & Testing; Revision Session.
CS1101: Programming Methodology Aaron Tan.
C++ Programming Language Lecture 2 Problem Analysis and Solution Representation By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
CPSC 171 Introduction to Computer Science More Algorithm Discovery and Design.
CS 100Lecture 11 Welcome to CS 100 n Goal: Learn to write intelligible, correct computer programs n Language: Java (but the language should not be the.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
CS 100Lecture 171 CS100J Lecture 17 n Previous Lecture –Programming concepts n Binary search n Application of the “rules of thumb” n Asymptotic complexity.
CS100A, Fall 1997, Lecture 91 CS100A, Fall 1997 Lecture 9, Tuesday, 30 September Input/Output & Program Schema System.in, class Text, Some basic data processing,
CS 100Lecture 21 CS100J: Lecture 2 n Previous Lecture –Programming Concepts n problem, algorithm, program, computer, input, output, sequential execution,
The while-statement. Syntax and meaning of the while-statement The LOOP-CONTINUATION-CONDITION is a Boolean expression (exactly the same as in the condition.
CS100A, Fall Lec. 1, Tue. 1 Sept. 1 CS100A, Fall 1998 After discussing the notion of an algorithm and (computer) program briefly, the first lecture.
Review :chapters What is an algorithm? A step by step description of how to accomplish a task. For example, Adding 3 numbers N1, N2 and N3 Add.
The Hashemite University Computer Engineering Department
Introduction CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
CS 100Lecture 111 CS100J Lecture 11 n Previous Lecture –Scope of names and the lifetime of variables n blocks and local variables n methods and parameters.
Flowchart. a diagram of the sequence of movements or actions of people or things involved in a complex system or activity. a graphical representation.
CS 100Lecture71 CS100J Lecture 7 n Previous Lecture –Computation and computational power –Abstraction –Classes, Objects, and Methods –References and aliases.
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
Lecture #1: Introduction to Algorithms and Problem Solving Dr. Hmood Al-Dossari King Saud University Department of Computer Science 6 February 2012.
Review A program is… a set of instructions that tell a computer what to do. Programs can also be called… software. Hardware refers to… the physical components.
CSC 241: Introduction to Computer Science I
Introduction to Algorithms
Introduction to Arrays
Chapter 2 Elementary Programming
Topic: Python’s building blocks -> Variables, Values, and Types
GC211Data Structure Lecture2 Sara Alhajjam.
TK1114 Computer Programming
Lecture 2: Introduction to Algorithms
COMS W1004 Introduction to Computer Science and Programming in Java
1) C program development 2) Selection structure
Algorithm Discovery and Design
CS100J Lecture 11 Previous Lecture This Lecture
CS100J Lecture 7 Previous Lecture This Lecture Java Constructs
CS100J Lecture 3 Previous Lecture This Lecture Programming Concepts
Introduction to Algorithms
The structure of programming
Introduction to Arrays
CS105 Introduction to Computer Concepts Intro to programming
CSC 241: Introduction to Computer Science I
CS100A Lect. 10, 1 Oct Input/Output & Program Schema
Presentation transcript:

CS 100Lecture 11 Introduction to Programming n What is an algorithm? n Input and output n Sequential execution n Conditional execution Reading: Chapter 1 in either text: Lewis & Loftus, or Savitch

CS 100Lecture 12 Problems and Algorithms n problem A task to be performed n algorithm A procedure for solving a problem: n Algorithms are: –precise and unambiguous –methodical and effective –abstract

CS 100Lecture 13 Sample Problems n Is a given integer even or is it odd? n For each integer in a list, is it even or odd? n Play tic-tac-toe so you never lose n Play tic-tac-toe so you always win n Play chess so you always win

CS 100Lecture 14 Programs and Computers n program An algorithm written down in some language, e.g., English, Russian, Java, MatLab. n computer A device that can execute programs written in programming languages, e.g., Java, MatLab, C, C++, Fortran, Basic.

CS 100Lecture 15 Algorithms and Programs * the thing with the property “threeness”

CS 100Lecture 16 Problem 1 n Is a given integer even or odd? more precisely … n An integer is written on a card. If the integer is even, say “even”, otherwise say “odd”.

CS 100Lecture 17 Algorithm / Program A 1. Get the integer from the card. 2. Divide the integer by If the remainder is 0, say “even”, otherwise say “odd”. Test:

CS 100Lecture 18 Algorithm / Program B 1. Get the integer from the card. 2. If the rightmost digit is 0, 2, 4, 6, or 8 say “even”, otherwise say “odd”. Test:MCMXCIX

CS 100Lecture 19 Input and Output n input Bring data into the program, e.g., the integer from the card. n output Send values out from the program, e.g., the text “even” or “odd”.

CS 100Lecture 110 Sequential and Conditional Execution n sequential execution Perform sequence of computation steps in order, e.g., step 1, step 2, step 3. n conditional execution Perform one of several steps based on a test, e.g., either print “even” or print “odd” based on whether the remainder is 0 or not.

CS 100Lecture 111 Algorithm / Program A Problem statement An integer is given as input data. If it is even, output “even”, otherwise output “odd”. Program in English 1. Get the integer and call it num 2. Divide num by 2 and call the remainder rem 3. If rem is 0, output “even”, otherwise output “odd” Program Segment in Java num = in.readInt(); num = in.readInt(); rem = num % 2; rem = num % 2; if ( rem == 0 ) if ( rem == 0 ) System.out.println("even"); System.out.println("even"); else else System.out.println("odd"); System.out.println("odd");

CS 100Lecture 112 Initial Observations n A “segment” is a part of a program n Java is like stylized English n Sequence of imperatives, known as statements n Statements are not numbered n Layout is essential for readability, but irrelevant for meaning, e.g., this code segment has the same effect: num=in.readInt();rem=num%2;if(rem==0) System.out.println("even");else System.out.println("odd"); n English version: –names num and rem Java version: Java version: –memory locations num and rem n Memory locations are known as variables

CS 100Lecture 113 Complete Program /* Input an integer and output "even" if it is even, otherwise output "odd. */ is even, otherwise output "odd. */ import java.io.*; public class OddEven { public static void main(String args[]) { int num; // Input integer. int num; // Input integer. int rem; // Remainder of num / 2. int rem; // Remainder of num / 2. // Initialize Text object in to read // Initialize Text object in to read // from standard input. // from standard input. TokenReader in = new TokenReader(System.in); TokenReader in = new TokenReader(System.in); // Set num to be the input integer. // Set num to be the input integer. num = in.readInt(); num = in.readInt(); // Set rem to be the remainder of num/2. // Set rem to be the remainder of num/2. rem = num % 2; rem = num % 2; if ( rem == 0 ) if ( rem == 0 ) System.out.println("even"); System.out.println("even"); else else System.out.println("odd"); System.out.println("odd");}}

CS 100Lecture 114 Things to Do Now n Web: Browse CS100J web pages n Program 1: get description from web (Homework), and start n Read relevant textbook chapter n Lecture (Thursday): attend n Sections: pick one and attend. (Start next Tuesday or Wednesday)