gentle introduction to programming
DESCRIPTION
Gentle Introduction to Programming. Assaf Zaritsky Summer 2010. Administration. August 31 - September 19 9:00 – 13:30 Mostly at דן דויד 002 No grading Web: http://www.cs.tau.ac.il/gip10/ Check what’s new! My email: [email protected]. Course Description. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Gentle Introduction to Programming](https://reader036.vdocuments.us/reader036/viewer/2022062408/568135ef550346895d9d60e6/html5/thumbnails/1.jpg)
1
Gentle Introduction to Programming
Assaf ZaritskySummer 2010
![Page 2: Gentle Introduction to Programming](https://reader036.vdocuments.us/reader036/viewer/2022062408/568135ef550346895d9d60e6/html5/thumbnails/2.jpg)
2
Administration
• August 31 - September 19• 9:00 – 13:30• Mostly at 002דן דויד
• No grading• Web: http://www.cs.tau.ac.il/gip10/
Check what’s new!• My email: [email protected]
![Page 3: Gentle Introduction to Programming](https://reader036.vdocuments.us/reader036/viewer/2022062408/568135ef550346895d9d60e6/html5/thumbnails/3.jpg)
3
Course Description
This course will provide a gentle introduction to programming
using Scala for highly motivated students with little or no prior experience in programming
![Page 4: Gentle Introduction to Programming](https://reader036.vdocuments.us/reader036/viewer/2022062408/568135ef550346895d9d60e6/html5/thumbnails/4.jpg)
4
Objective
Bridge the gap for students without prior programming knowledge
![Page 5: Gentle Introduction to Programming](https://reader036.vdocuments.us/reader036/viewer/2022062408/568135ef550346895d9d60e6/html5/thumbnails/5.jpg)
5
Course Description
Lectures will be interactive featuring in-class exercises with lots of support
You are expected to work hard!
Practical sessions in lab/class
Faculty guests lectures
![Page 6: Gentle Introduction to Programming](https://reader036.vdocuments.us/reader036/viewer/2022062408/568135ef550346895d9d60e6/html5/thumbnails/6.jpg)
6
Course PlanSession Material
1 Basic concepts in CS and programming, basic Scala
2 Basic Scala (cont.), Functions
3 Recursion
4 Arrays, sorting algorithms
5 Lists
6 Object oriented programming
![Page 7: Gentle Introduction to Programming](https://reader036.vdocuments.us/reader036/viewer/2022062408/568135ef550346895d9d60e6/html5/thumbnails/7.jpg)
7
Questions?
![Page 8: Gentle Introduction to Programming](https://reader036.vdocuments.us/reader036/viewer/2022062408/568135ef550346895d9d60e6/html5/thumbnails/8.jpg)
8
Agenda for Today
• Administration• General introduction• The Scala programming language:
• Variables and Types• Compiler, Interpreter• Working environment• Home work• Guest lecture by Ohad Barzilay
![Page 9: Gentle Introduction to Programming](https://reader036.vdocuments.us/reader036/viewer/2022062408/568135ef550346895d9d60e6/html5/thumbnails/9.jpg)
9
Basic Terms
• Computer Science• Computer• Hardware / Software• Algorithm• Input / Output• Pseudo-Code• Programming Language• Computer Program
![Page 10: Gentle Introduction to Programming](https://reader036.vdocuments.us/reader036/viewer/2022062408/568135ef550346895d9d60e6/html5/thumbnails/10.jpg)
10
Computer Science
ענף מדעי העוסק בלימוד הבסיס התיאורטי למידע ולחישוביות, והחלתם על מערכות
מחשב
מדעי המחשב אינם עוסקים במחשב יותר משאסטרונומיה עוסקת בטלסקופ", דייקסטרה"
![Page 11: Gentle Introduction to Programming](https://reader036.vdocuments.us/reader036/viewer/2022062408/568135ef550346895d9d60e6/html5/thumbnails/11.jpg)
11
Computer
מכונה המעבדת נתונים על פי רצף פקודות נתון מראש
מחשב = חומרה + תכנה
מעבד אמצעי פלט
אמצעי קלט
זכרון
עכבר, מקלדת, )(דיסק קשיח
מדפסת, מסך, )(דיסק קשיח
![Page 12: Gentle Introduction to Programming](https://reader036.vdocuments.us/reader036/viewer/2022062408/568135ef550346895d9d60e6/html5/thumbnails/12.jpg)
12
Algorithm
סדרת פעולות סופית לביצוע משימה מסויימת
Algorithm
Input Output
![Page 13: Gentle Introduction to Programming](https://reader036.vdocuments.us/reader036/viewer/2022062408/568135ef550346895d9d60e6/html5/thumbnails/13.jpg)
13
לביצוע משימה סופיתסופיתסדרת פעולות אלגוריתם: מסוימת
תרשים זרימה
![Page 14: Gentle Introduction to Programming](https://reader036.vdocuments.us/reader036/viewer/2022062408/568135ef550346895d9d60e6/html5/thumbnails/14.jpg)
14
Pseudo Code
תיאור מצומצם ולא רשמי לאלגוריתם. מיועד לקריאה של בני אדם
![Page 15: Gentle Introduction to Programming](https://reader036.vdocuments.us/reader036/viewer/2022062408/568135ef550346895d9d60e6/html5/thumbnails/15.jpg)
15
Example
![Page 16: Gentle Introduction to Programming](https://reader036.vdocuments.us/reader036/viewer/2022062408/568135ef550346895d9d60e6/html5/thumbnails/16.jpg)
16
Programming Language
Machine-readable artificial language designed to express computations that
can be performed by a computer
![Page 17: Gentle Introduction to Programming](https://reader036.vdocuments.us/reader036/viewer/2022062408/568135ef550346895d9d60e6/html5/thumbnails/17.jpg)
17
Machine Code (Language)
• Computers understand only machine language• Basically looks like a sequence of 1’s and 0’s• Very inconvenient to work with and non intuitive
• All other computer languages were created for human convenience• The computer does not understand C/C#/Java/Scala• Must be “translated” into machine language
![Page 18: Gentle Introduction to Programming](https://reader036.vdocuments.us/reader036/viewer/2022062408/568135ef550346895d9d60e6/html5/thumbnails/18.jpg)
18
There are Many Programming Languages
Over 500 different computer languages are listed by Wikipedia
![Page 19: Gentle Introduction to Programming](https://reader036.vdocuments.us/reader036/viewer/2022062408/568135ef550346895d9d60e6/html5/thumbnails/19.jpg)
19
![Page 20: Gentle Introduction to Programming](https://reader036.vdocuments.us/reader036/viewer/2022062408/568135ef550346895d9d60e6/html5/thumbnails/20.jpg)
20
Language Selection
• Goal• Runtime vs. Development time• Operating systems• Platforms
![Page 21: Gentle Introduction to Programming](https://reader036.vdocuments.us/reader036/viewer/2022062408/568135ef550346895d9d60e6/html5/thumbnails/21.jpg)
21
Computer Program
מימוש של אלגוריתם באמצעות שפת תכנות
![Page 22: Gentle Introduction to Programming](https://reader036.vdocuments.us/reader036/viewer/2022062408/568135ef550346895d9d60e6/html5/thumbnails/22.jpg)
22
Computer Program(more technically)
• A sequence of processor instructions designed to achieve a specific purpose
• The instructions are executed sequentially. No instruction is executed before the previous has been completed
![Page 23: Gentle Introduction to Programming](https://reader036.vdocuments.us/reader036/viewer/2022062408/568135ef550346895d9d60e6/html5/thumbnails/23.jpg)
23
Agenda for Today
• Administration• General introduction• The Scala programming language:
• Variables and Types• Operators• Branching and Repetition
• Compiler, Interpreter• Working environment• Home work
![Page 24: Gentle Introduction to Programming](https://reader036.vdocuments.us/reader036/viewer/2022062408/568135ef550346895d9d60e6/html5/thumbnails/24.jpg)
24
Scala History
• Created by Martin Odersky at EPFL• Released Jan 2004
• Twitter switched large portions of their code to Scala (and intend to convert the rest)• Linkedin use it• Other companies
• Over 4000 downloads per month
![Page 25: Gentle Introduction to Programming](https://reader036.vdocuments.us/reader036/viewer/2022062408/568135ef550346895d9d60e6/html5/thumbnails/25.jpg)
25
Why Scala?
• Semester A: Scheme• Semester B: Java• Scala language has some features similar to
Scheme and some to Java• Scala is cool!
![Page 26: Gentle Introduction to Programming](https://reader036.vdocuments.us/reader036/viewer/2022062408/568135ef550346895d9d60e6/html5/thumbnails/26.jpg)
26
Hello World!
![Page 27: Gentle Introduction to Programming](https://reader036.vdocuments.us/reader036/viewer/2022062408/568135ef550346895d9d60e6/html5/thumbnails/27.jpg)
27
My First Scala Program: Hello World!
Hello.scala
![Page 28: Gentle Introduction to Programming](https://reader036.vdocuments.us/reader036/viewer/2022062408/568135ef550346895d9d60e6/html5/thumbnails/28.jpg)
28
Memory
![Page 29: Gentle Introduction to Programming](https://reader036.vdocuments.us/reader036/viewer/2022062408/568135ef550346895d9d60e6/html5/thumbnails/29.jpg)
29
Memory (Cont.)
• The computer memory is composed of a long list of bits
• Bits are grouped into bytes and words
• Every byte is numbered sequentially
• This number is called an address
![Page 30: Gentle Introduction to Programming](https://reader036.vdocuments.us/reader036/viewer/2022062408/568135ef550346895d9d60e6/html5/thumbnails/30.jpg)
30
Variables and Types
): יחידת מידע המאכסנת ערך variable (משתנה•במהלך ריצת התכנית
ניתן ל"גשת" למשתנה (לשנות או לקבל את ערכו)•ניתן לבצע פעולות חשבוניות (ואחרות) בעזרת •
משתניםשקובע אילו סוגי )type (טיפוסלכל משתנה מוגדר •
ערכים המשתנה יכול להכיל (דוגמאות: מספר שלם, מספר ממשי, תו)
![Page 31: Gentle Introduction to Programming](https://reader036.vdocuments.us/reader036/viewer/2022062408/568135ef550346895d9d60e6/html5/thumbnails/31.jpg)
31
Define Variables • Use var to declare variables:
var x : Int = 3
var y : Int = 5
var z : Int = x + y
var s1 : String = “Hello World”
define variable
variable name
type valueassignment operator
Variable x “is of type” Int and is assigned the value 3
![Page 32: Gentle Introduction to Programming](https://reader036.vdocuments.us/reader036/viewer/2022062408/568135ef550346895d9d60e6/html5/thumbnails/32.jpg)
32
Why do We Need Variables?
• Computer programs manipulate data• Data is given as input or calculated throughout
the program• To be later accessed, variables must be
remembered• Thus, variables are stored in the memory• Variable name memory address
![Page 33: Gentle Introduction to Programming](https://reader036.vdocuments.us/reader036/viewer/2022062408/568135ef550346895d9d60e6/html5/thumbnails/33.jpg)
33
Types
טיפוס של משתנה קובע:• אילו ערכים יכול המשתנה להכיל• מהן הפעולות שניתן לבצע על המשתנה•
שדורש (יחידה בסיסית של זכרון המחשב)מספר הבתים •אחסון של משתנה בזכרון תלוי בטיפוסו
![Page 34: Gentle Introduction to Programming](https://reader036.vdocuments.us/reader036/viewer/2022062408/568135ef550346895d9d60e6/html5/thumbnails/34.jpg)
34
Some Basic Types
![Page 35: Gentle Introduction to Programming](https://reader036.vdocuments.us/reader036/viewer/2022062408/568135ef550346895d9d60e6/html5/thumbnails/35.jpg)
35
Why Do We Need Different Types?
• Saving memory
• Execution speed
• Makes compiler “life” easier
![Page 36: Gentle Introduction to Programming](https://reader036.vdocuments.us/reader036/viewer/2022062408/568135ef550346895d9d60e6/html5/thumbnails/36.jpg)
36
Define Variables • Int:
var x : Int = 5
• Double:var pi : Double = 3.14
• Boolean:var term : Boolean = true
• Char:var c : Char = ‘A’
• String:var st : String = “I want a break!”
![Page 37: Gentle Introduction to Programming](https://reader036.vdocuments.us/reader036/viewer/2022062408/568135ef550346895d9d60e6/html5/thumbnails/37.jpg)
37
Arithmetic Operators
![Page 38: Gentle Introduction to Programming](https://reader036.vdocuments.us/reader036/viewer/2022062408/568135ef550346895d9d60e6/html5/thumbnails/38.jpg)
38
Example 1 var a : Int = 3var b: Int = 5var c : Int = a + bprintln(c)c = c * 2println(c)var first : Int = (a + b) * 2var second : Int = a + b * 2println(first)println(second)
![Page 39: Gentle Introduction to Programming](https://reader036.vdocuments.us/reader036/viewer/2022062408/568135ef550346895d9d60e6/html5/thumbnails/39.jpg)
39
Example 2 // Demonstrate strings additionvar s1 : String = “He”var s2 : String = “llo”var s3 : String = s1 + s2println(s3)var s4 : String = s3 + “ World ”var c : Char = ‘!’println(s4 + 2009 + c)
![Page 40: Gentle Introduction to Programming](https://reader036.vdocuments.us/reader036/viewer/2022062408/568135ef550346895d9d60e6/html5/thumbnails/40.jpg)
40
Example 3 // Reverse a 3-digits numberprintln(“Enter a 3-digit number”)var num : Int = Console.readInt() // read a number from the user
var ones : Int = num % 10var tens : Int = (num % 100) / 10var hundreds : Int = num / 100
var reverseNum : Int = (ones * 100) + (tens * 10) + hundreds println(“the reverse number is “ + reverseNum)
![Page 41: Gentle Introduction to Programming](https://reader036.vdocuments.us/reader036/viewer/2022062408/568135ef550346895d9d60e6/html5/thumbnails/41.jpg)
41
Rational Operators
Operator
Name Description
x < y Less than true if x is less than y, otherwise false.
x > y Greater than true if x is greater than y, otherwise false.
x <= y Less than or equal to
true if x is less than or equal to y, otherwise false.
x >= y Greater than or equal to
true if x is greater than or equal to y, otherwise false.
x == y Equal true if x equals y, otherwise false.
x != y Not Equal true if x is not equal to y, otherwise false.
Compares two numbers and returns a Boolean
![Page 42: Gentle Introduction to Programming](https://reader036.vdocuments.us/reader036/viewer/2022062408/568135ef550346895d9d60e6/html5/thumbnails/42.jpg)
42
Example // Demonstrate rational operatorsprintln(“Enter the first number”)var x : Int = Console.readInt()println(“Enter the second number”)var y : Int = Console.readInt()
println(“x < y is “ + (x < y))println(“x > y is “ + (x > y))println(“x <= y is “ + (x <= y))println(“x >= y is “ + (x >= y))println(“x == y is “ + (x == y))println(“x != y is “ + (x != y))
![Page 43: Gentle Introduction to Programming](https://reader036.vdocuments.us/reader036/viewer/2022062408/568135ef550346895d9d60e6/html5/thumbnails/43.jpg)
43
Logical Operators Operates on two Booleans and returns a Boolean
Operator
Name Description
x && y And True if both x and y are true, otherwise false.
x || y OrTrue if at least one of x or y are true, otherwise false.
! X Not True if x is false, otherwise false.
![Page 44: Gentle Introduction to Programming](https://reader036.vdocuments.us/reader036/viewer/2022062408/568135ef550346895d9d60e6/html5/thumbnails/44.jpg)
44
&&, ||, !=
0 1
0 0 0
1 0 1
0 1
0 0 1
1 1 1
0 1
1 0
&&
||!
![Page 45: Gentle Introduction to Programming](https://reader036.vdocuments.us/reader036/viewer/2022062408/568135ef550346895d9d60e6/html5/thumbnails/45.jpg)
45
Example
// Demonstrate logical operators println(“Enter the first number”)var x : Int = Console.readInt()println(“Enter the second number”)var y : Int = Console.readInt()
println(“(x < 10) && (y < 10) is “ + ((x < 10) && (y < 10)))println(“(x < 10) || (y < 10) is “ + ((x < 10) || (y < 10)))var state : Boolean = ((x < 10) || (y < 10) )println(“state is “ + state)
![Page 46: Gentle Introduction to Programming](https://reader036.vdocuments.us/reader036/viewer/2022062408/568135ef550346895d9d60e6/html5/thumbnails/46.jpg)
46
All Examples at BasicExamples.scala
![Page 47: Gentle Introduction to Programming](https://reader036.vdocuments.us/reader036/viewer/2022062408/568135ef550346895d9d60e6/html5/thumbnails/47.jpg)
48
Flow Control
• Usually a program is executed line after line in order
• It is reasonable to expect different execution orders on different inputs• Computer games• Illegal input
• Control structures• if-else• for loop• while loop
![Page 48: Gentle Introduction to Programming](https://reader036.vdocuments.us/reader036/viewer/2022062408/568135ef550346895d9d60e6/html5/thumbnails/48.jpg)
49
Conditional Statement: if
• Used to execute conditionally a statement (or block of code)
• Syntax: if (expression) statement
• If expression is true, statement is executed
• statement can be replaced by a block of statements, enclosed in curly braces
![Page 49: Gentle Introduction to Programming](https://reader036.vdocuments.us/reader036/viewer/2022062408/568135ef550346895d9d60e6/html5/thumbnails/49.jpg)
50
if-else Statement
if (expression) statement1
else statement2
• if expression is true, statement1 is executed. • if expression is false, statement2 is executed • both statements can be (and very often are) replaced
by blocks of statements (“compound statements”)
![Page 50: Gentle Introduction to Programming](https://reader036.vdocuments.us/reader036/viewer/2022062408/568135ef550346895d9d60e6/html5/thumbnails/50.jpg)
61
Agenda for Today
• Administration• General introduction• The Scala programming language:
• Variables and Types• Operators
• Compiler, Interpreter• Working environment• Home work
![Page 51: Gentle Introduction to Programming](https://reader036.vdocuments.us/reader036/viewer/2022062408/568135ef550346895d9d60e6/html5/thumbnails/51.jpg)
62
Compiler
![Page 52: Gentle Introduction to Programming](https://reader036.vdocuments.us/reader036/viewer/2022062408/568135ef550346895d9d60e6/html5/thumbnails/52.jpg)
63
Interpreter
![Page 53: Gentle Introduction to Programming](https://reader036.vdocuments.us/reader036/viewer/2022062408/568135ef550346895d9d60e6/html5/thumbnails/53.jpg)
64
Interpreter
• The Read/Evaluate/Print Loop• Read an expression• Compute its value• Print the result• Repeat the above
• The Environment
![Page 54: Gentle Introduction to Programming](https://reader036.vdocuments.us/reader036/viewer/2022062408/568135ef550346895d9d60e6/html5/thumbnails/54.jpg)
65
Compiler = Clerk"ראש קטן"
![Page 55: Gentle Introduction to Programming](https://reader036.vdocuments.us/reader036/viewer/2022062408/568135ef550346895d9d60e6/html5/thumbnails/55.jpg)
66
Runtime
![Page 56: Gentle Introduction to Programming](https://reader036.vdocuments.us/reader036/viewer/2022062408/568135ef550346895d9d60e6/html5/thumbnails/56.jpg)
68
Hello World ;-)
![Page 57: Gentle Introduction to Programming](https://reader036.vdocuments.us/reader036/viewer/2022062408/568135ef550346895d9d60e6/html5/thumbnails/57.jpg)
69
Agenda for Today
• Administration• General introduction• The Scala programming language:
• Variables and Types• Operators• Branching and Repetition
• Compiler, Interpreter• Working environment• Home work
![Page 58: Gentle Introduction to Programming](https://reader036.vdocuments.us/reader036/viewer/2022062408/568135ef550346895d9d60e6/html5/thumbnails/58.jpg)
70
Working Environment
• Interpreter vs. scripts vs. Eclipse
• Home vs. labs
VS.
![Page 59: Gentle Introduction to Programming](https://reader036.vdocuments.us/reader036/viewer/2022062408/568135ef550346895d9d60e6/html5/thumbnails/59.jpg)
71
Setting Environment @ Home • Download Java (www.java.com/getjava/ ) • Download Scala (http://www.scala-lang.org/downloads )
• Download Eclipse (http://www.eclipse.org/downloads/ )• Set Scala plugin to Eclipse (https://www.assembla.com/wiki/show/scala-ide/Requirements_and_Installation )
• Start programming
![Page 60: Gentle Introduction to Programming](https://reader036.vdocuments.us/reader036/viewer/2022062408/568135ef550346895d9d60e6/html5/thumbnails/60.jpg)
72
Download Java http://www.java.com/en/download/index.jsp
![Page 61: Gentle Introduction to Programming](https://reader036.vdocuments.us/reader036/viewer/2022062408/568135ef550346895d9d60e6/html5/thumbnails/61.jpg)
73
Download Scala http://www.scala-lang.org/downloads
![Page 62: Gentle Introduction to Programming](https://reader036.vdocuments.us/reader036/viewer/2022062408/568135ef550346895d9d60e6/html5/thumbnails/62.jpg)
74
Download Scala
![Page 63: Gentle Introduction to Programming](https://reader036.vdocuments.us/reader036/viewer/2022062408/568135ef550346895d9d60e6/html5/thumbnails/63.jpg)
75
Use the Interpreter Type Scala from the command prompt (linux) or using “run” (windows)
![Page 64: Gentle Introduction to Programming](https://reader036.vdocuments.us/reader036/viewer/2022062408/568135ef550346895d9d60e6/html5/thumbnails/64.jpg)
76
Download Eclipse
http://www.eclipse.org/downloads/
![Page 65: Gentle Introduction to Programming](https://reader036.vdocuments.us/reader036/viewer/2022062408/568135ef550346895d9d60e6/html5/thumbnails/65.jpg)
77
Set Scala Plugin to Eclipse https://www.assembla.com/wiki/show/scala-ide/Requirements_and_Installation
![Page 66: Gentle Introduction to Programming](https://reader036.vdocuments.us/reader036/viewer/2022062408/568135ef550346895d9d60e6/html5/thumbnails/66.jpg)
78
Set Scala Plugin to Eclipse (new)
![Page 67: Gentle Introduction to Programming](https://reader036.vdocuments.us/reader036/viewer/2022062408/568135ef550346895d9d60e6/html5/thumbnails/67.jpg)
79
Set Scala Plugin to Eclipse (new)
![Page 68: Gentle Introduction to Programming](https://reader036.vdocuments.us/reader036/viewer/2022062408/568135ef550346895d9d60e6/html5/thumbnails/68.jpg)
80
Open Eclipse – Set Workspace
![Page 69: Gentle Introduction to Programming](https://reader036.vdocuments.us/reader036/viewer/2022062408/568135ef550346895d9d60e6/html5/thumbnails/69.jpg)
81
Open Eclipse
![Page 70: Gentle Introduction to Programming](https://reader036.vdocuments.us/reader036/viewer/2022062408/568135ef550346895d9d60e6/html5/thumbnails/70.jpg)
82
Define New Project
![Page 71: Gentle Introduction to Programming](https://reader036.vdocuments.us/reader036/viewer/2022062408/568135ef550346895d9d60e6/html5/thumbnails/71.jpg)
83
If you don’t find the Scala new project…
File New Project
![Page 72: Gentle Introduction to Programming](https://reader036.vdocuments.us/reader036/viewer/2022062408/568135ef550346895d9d60e6/html5/thumbnails/72.jpg)
84
New Object
![Page 73: Gentle Introduction to Programming](https://reader036.vdocuments.us/reader036/viewer/2022062408/568135ef550346895d9d60e6/html5/thumbnails/73.jpg)
85
Write Some Code
![Page 74: Gentle Introduction to Programming](https://reader036.vdocuments.us/reader036/viewer/2022062408/568135ef550346895d9d60e6/html5/thumbnails/74.jpg)
86
Run
output
![Page 75: Gentle Introduction to Programming](https://reader036.vdocuments.us/reader036/viewer/2022062408/568135ef550346895d9d60e6/html5/thumbnails/75.jpg)
87
Agenda for Today
• Administration• General introduction• The Scala programming language:
• Variables and Types• Operators• Branching and Repetition
• Compiler, Interpreter• Working environment• Home work
![Page 76: Gentle Introduction to Programming](https://reader036.vdocuments.us/reader036/viewer/2022062408/568135ef550346895d9d60e6/html5/thumbnails/76.jpg)
88
Exercise 0
• Write your first “Hello World!” program in Scala:• Use the interpreter• In Eclipse
• Make sure it compiles and executes properly
![Page 77: Gentle Introduction to Programming](https://reader036.vdocuments.us/reader036/viewer/2022062408/568135ef550346895d9d60e6/html5/thumbnails/77.jpg)
89
Fibonacci
• Fibonacci series0, 1, 1, 2, 3, 5, 8, 13, 21, 34• Definition:
• fib(0) = 1• fib(1) = 1• fib(n) = fib(n-1) + fib(n-2)
en.wikipedia.org/wiki/Fibonacci_number
![Page 78: Gentle Introduction to Programming](https://reader036.vdocuments.us/reader036/viewer/2022062408/568135ef550346895d9d60e6/html5/thumbnails/78.jpg)
90
סלט פיבונאצ'י
![Page 79: Gentle Introduction to Programming](https://reader036.vdocuments.us/reader036/viewer/2022062408/568135ef550346895d9d60e6/html5/thumbnails/79.jpg)
91
Exercise 1
• Write a program that receives from the user an integer n > 0 and prints to the screen the n th Fibonacci number
a. Use a “for” loop
b. Use a “while” loop
![Page 80: Gentle Introduction to Programming](https://reader036.vdocuments.us/reader036/viewer/2022062408/568135ef550346895d9d60e6/html5/thumbnails/80.jpg)
92
Exercise 2
• Write a program that receives from the user two integers 0 < n1 < n2 and prints to the screen all prime numbers between n1 and n2
• Divide the problem to smaller sub-problems:• How would you traverse over all the numbers
between n1 and n2?• How would you be sure that a given number is a
prime?
![Page 81: Gentle Introduction to Programming](https://reader036.vdocuments.us/reader036/viewer/2022062408/568135ef550346895d9d60e6/html5/thumbnails/81.jpg)
93
Want More Exercises?
![Page 82: Gentle Introduction to Programming](https://reader036.vdocuments.us/reader036/viewer/2022062408/568135ef550346895d9d60e6/html5/thumbnails/82.jpg)
94
Exercise – Integer Division
• Input:• Two integers – A and B
• Output:• How many times A contains B (it is the result of
the integer division A/B)
• Do not use the operators ‘/’, ‘*’
• Solution:
![Page 83: Gentle Introduction to Programming](https://reader036.vdocuments.us/reader036/viewer/2022062408/568135ef550346895d9d60e6/html5/thumbnails/83.jpg)
95
Exercise – Power of Two
• Input: integer A
• Output: is there an integer N such that A == 2^N?
• Solution:
![Page 84: Gentle Introduction to Programming](https://reader036.vdocuments.us/reader036/viewer/2022062408/568135ef550346895d9d60e6/html5/thumbnails/84.jpg)
96
Write a program that prints an upside-down half triangle of *.
The height of the pyramid is the input.
*****
*****
****
*
Exercise – Triangle Printout