Introduction%20to%20Programming - PowerPoint PPT Presentation

About This Presentation
Title:

Introduction%20to%20Programming

Description:

precise and unambiguous. methodical and effective. abstract. CS 100. Lecture 1. 3. Sample Problems ... more precisely ... An integer is written on a card. ... – PowerPoint PPT presentation

Number of Views:23
Avg rating:3.0/5.0
Slides: 15
Provided by: Mill1
Category:

less

Transcript and Presenter's Notes

Title: Introduction%20to%20Programming


1
Introduction to Programming
  • What is an algorithm?
  • Input and output
  • Sequential execution
  • Conditional execution
  • Reading
  • Chapter 1 in either text
  • Lewis Loftus, or Savitch

2
Problems and Algorithms
  • problem A task to be performed
  • algorithm A procedure for solving a problem
  • Algorithms are
  • precise and unambiguous
  • methodical and effective
  • abstract

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

4
Programs and Computers
  • program An algorithm written down in some
    language, e.g., English, Russian, Java, MatLab.
  • computer A device that can execute programs
    written in programming languages, e.g., Java,
    MatLab, C, C, Fortran, Basic.

5
Algorithms and Programs
the thing with the property threeness
6
Problem 1
  • Is a given integer even or odd?
  • more precisely
  • An integer is written on a card. If the integer
    is even, say even, otherwise say odd.

7
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
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
Input and Output
  • input Bring data into the program, e.g., the
    integer from the card.
  • output Send values out from the program, e.g.,
    the text even or odd.

10
Sequential and Conditional Execution
  • sequential execution Perform sequence of
    computation steps in order, e.g., step 1, step 2,
    step 3.
  • 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
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()
  • rem num 2
  • if ( rem 0 )
  • System.out.println("even")
  • else
  • System.out.println("odd")

12
Initial Observations
  • A segment is a part of a program
  • Java is like stylized English
  • Sequence of imperatives, known as statements
  • Statements are not numbered
  • Layout is essential for readability, but
    irrelevant for meaning, e.g., this code segment
    has the same effect
  • numin.readInt()remnum2if(rem0)
    System.out.println("even")else
    System.out.println("odd")
  • English version
  • names num and rem
  • Java version
  • memory locations num and rem
  • Memory locations are known as variables

13
Complete Program
  • / Input an integer and output "even" if it
  • is even, otherwise output "odd. /
  • import java.io.
  • public class OddEven
  • public static void main(String args)
  • int num // Input integer.
  • int rem // Remainder of num / 2.
  • // Initialize Text object in to read
  • // from standard input.
  • TokenReader in new TokenReader(System.in)
  • // Set num to be the input integer.
  • num in.readInt()
  • // Set rem to be the remainder of num/2.
  • rem num 2

14
Things to Do Now
  • Web Browse CS100J web pages
  • http//courses.cs.cornell.edu/cs100j/2002sp/
  • Program 1 get description from web (Homework),
    and start
  • Read relevant textbook chapter
  • Lecture (Thursday) attend
  • Sections pick one and attend. (Start next
    Tuesday or Wednesday)
Write a Comment
User Comments (0)
About PowerShow.com