Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CS 100Lecture 11 Introduction to Programming n What is an algorithm? n Input and output n Sequential execution n Conditional execution Reading: Chapter."— Presentation transcript:

1 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

2 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

3 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

4 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.

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

6 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”.

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

8 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

9 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”.

10 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.

11 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");

12 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

13 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");}}

14 CS 100Lecture 114 Things to Do Now n Web: Browse CS100J web pages http://courses.cs.cornell.edu/cs100j/2002sp/ 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)


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

Similar presentations


Ads by Google