dr deepak b phatakcs101/2011.2/lectures/lecture_slides/201… · dr. deepak b phatak 3. iit bombay....
Post on 19-Oct-2020
11 Views
Preview:
TRANSCRIPT
CS 101Computer Programming and utilization
Dr Deepak B PhatakSubrao Nilekani Chair Professor
Department of CSE, Kanwal Rekhi BuildingIIT Bombay
Session 6, Iterative SolutionsFriday, August 12, 2011
[Some ideas: courtesy Prof Sohoni, and Prof Ranade]
IIT BOMBAY
Session 6, Iterative solutions
Dr. Deepak B Phatak 2
IIT BOMBAY
Overview
• Iterative solutions using a count– The ‘for’ statement in C++
• Estimating the value of loge a
• Hemachandra numbers
• Announcements– Labs next week
Session 6, Iterative solutions
Dr. Deepak B Phatak 3
IIT BOMBAY
Finding sum of numbers
• Find the sum of N numbers– Value of N is given as input
• Values of each individual number given as input
Session 6, Iterative solutions
Dr. Deepak B Phatak 4
IIT BOMBAY
Repeating the iteration-block a given number of times
• Execute a block of statements a fixed number of times, say N
• We need a ‘counting mechanism’ , which will
• start with a prescribed initial value, say, count = 1
• If count is <= N, continue iterative execution,
else jump out of the iterative block
• add 1 to the count every time, after we execute the block
Session 6, Iterative solutions
Dr. Deepak B Phatak 5
IIT BOMBAY
Finding sum of N numbers
cin >> N;count = 1;while( count < = N ){
count = count + 1;}
return 0;}
Session 6, Iterative solutions
Block of statements to be repeated
Dr. Deepak B Phatak 6
IIT BOMBAY
Finding sum of N numbers#include<iostream>using namespace std;int main(){
int a, sum = 0, N; int count;cout<< “Give number of values to be added ”; cin >> N;count = 1;while( count < = N){
cout<<"enter the next number:"; cin>>a;sum = sum +a;count = count + 1;
} cout << “Sum is “ << sum << endl;return 0;
}
Session 6, Iterative solutions
Dr. Deepak B Phatak 7
IIT BOMBAY
Solutions which use such counting mechanism
• Several computational problems need such mechanism– Adding a fixed number of terms of any sequence– Calculating average marks for a given number of students– Calculating N!
• N! = 1 x 2 x 3 x 4 x 5 ….. x N• No input value needed, we can generate each ‘term’
Session 6, Iterative solutions
Dr. Deepak B Phatak 8
IIT BOMBAY
Finding value of N!#include<iostream>using namespace std;int main(){
int a, FactN = 1, N; int count;cout<< “Give number whose factorial is needed:”; cin >> N;count = 1;while(count < = N){
FactN = FactN * count;count = count + 1;
} cout << “Factorial is ” << FactN << endl;return 0;
}
Session 6, Iterative solutions
Dr. Deepak B Phatak 9
IIT BOMBAY
Repetitive execution by counting
• C++ provides an instruction to implement such iteration control– ‘for’ instruction
• This instruction permits us to define these three actions at a single place in our program– Initial value of a count, – condition to continue execution of the iterative block
(to check if count is within the prescribed limit)– Increment the count after each iteration
• These actions are defined at the beginning of the iterative block
Session 6, Iterative solutions
Dr. Deepak B Phatak 10
IIT BOMBAY
Repetitive execution using for
for ( count = 1; count <=N; count ++){
// statements forming the iterative-block
}• It initializes the count before beginning the first iteration• At the beginning of each iteration, it checks condition
– continue execution if the condition is true, – else jump out, to exit the ‘for’ block
• It increments the count immediately after each iteration
Session 6, Iterative solutions
Dr. Deepak B Phatak 11
IIT BOMBAY
Program for adding N numbers using for statement
int main(){int number, N, sum, count;sum = 0;cout << “How many numbers do you wish to add ” ; cin >> N;for (count =1; count <=N; count ++){
cout<<“\nenter next number to be added: ”; cin >> number;sum = sum + number;
} cout << endl << “sum is ” << sum << endl;return 0;
}
Session 6, Iterative solutions
Dr. Deepak B Phatak 12
IIT BOMBAY
Finding maximum of N given numbers
int main() { int count, number, N, max; cout << “How many numbers?: ”; cin >> N;cout << “give first number: ”; cin >> number; max = number;for (count =2; count <=N; count ++){
cout << endl << “give next number: ”; cin >> number;if (number > max) { max = number;}
}cout << endl << “Maximum of all given numbers is: ” << max;return 0;
}
Session 6, Iterative solutions
Dr. Deepak B Phatak 13
IIT BOMBAY
Sum of N natural numbers
int main(){int i, N, Sum = 0;cin >> Nfor(i=1; i <= N; i=i+1){
sum = sum + i;}cout << “Sum of” << N << “natural numbers is ”;cout << sum << “endl”;return 0;
}
Session 6, Iterative solutions
Dr. Deepak B Phatak 14
IIT BOMBAYFactorial of a given number n
n! = 1 * 2 * 3 * 4 * … * n
int main() {int nfactorial, n, i;cout << “Give value of n “; cin >> n;nfactorial = 1;for (i =2; i <= n; i++){
nfactorial = nfactorial * i;};cout << “Factorial of “ << n << “ is” << nfactorial << endl;return 0;
}
Session 6, Iterative solutions
Dr. Deepak B Phatak 15
IIT BOMBAYGeneral format and semantics of the ‘for’ statement
for( init-group; condition; increment-group ){iterative-block
}1. Execute “init-group”. An assignment statement. “loop initialization”2. Evaluate “condition” . “loop test”.
if true, continue with step 3;
if false, execution of ‘for’ statement ends, control jumps to step 6
3. Execute “iterative-block”. “loop body”
4. Execute “increment-group”. “loop increment”
5. Go back to step 2.
6. Execution continues with the next statement following the ‘for’ blockSession 6, Iterative solutions
Dr. Deepak B Phatak 16
IIT BOMBAY
Variations in specifications of ‘for’ loop
for (i = 7; i < N ; i = i + 2){ …}
for (x = 1.5; x < = 2.8; x = x + 0.0001){…}
for (x= 1.5, count =1; count <=N; x +=0.0001, count++){…}
for (i = 0, j=N; i<j; i++, j--){…}
for (Number = 0; Number <= 20;){…}
for (;;){…}
Session 6, Iterative solutions
Dr. Deepak B Phatak 17
IIT BOMBAY
Computing Natural Logarithm
• Natural Logarithm of a number a is defined as
• A computer cannot integrate– We must use arithmetic operations to estimate integral value– Estimate area under the curve f(x) = 1/x from 1 to a.
• We will approximate the area by many small rectangles.– Each having a fixed width w– Sum of the areas = approximate value of the integral
1/ x dx1
a
Session 6, Iterative solutions
Dr. Deepak B Phatak 18
IIT BOMBAY
Integral
1 a
calculate the area under the curvebetween 1 and a.
x
y
y = 1/x,
Session 6, Iterative solutions
Dr. Deepak B Phatak 19
IIT BOMBAY
Riemann Integral
1 a
calculate Sum of areas of allrectangles between 1 and a.
Session 6, Iterative solutions
Dr. Deepak B Phatak 20
IIT BOMBAY
Riemann Integral
1 a
Each rectangle has a fixed width wWhat is the height?
Session 6, Iterative solutions
Dr. Deepak B Phatak 21
IIT BOMBAYRiemann Integral using n rectangles
If there are n rectangles of equal width ww = (a-1) / n
1 a
w
Session 6, Iterative solutions
Dr. Deepak B Phatak 22
IIT BOMBAYRiemann Integral using n rectangles
1 ax coordinate of bottom left corner of i th rectangle
ith rectangle: x = 1 + (i-1)w
x coordinate of bottom left corner of these rectangles:1st rectangle: x= 1; 2nd rectangle: x = 1 + w;
3rd rectangle: x = 1 + 2w;. . .
Session 6, Iterative solutions
Dr. Deepak B Phatak 23
IIT BOMBAYRiemann Integral using n rectangles
1 a
Height h of i th Rectangle
h = 1 / x
= 1/(1 + (i-1)*w)
Session 6, Iterative solutions
Dr. Deepak B Phatak 24
IIT BOMBAYRiemann Integral using n rectangles
1 a
Area of i th Rectangle = w * h
= w * (1 / x )
= w * (1/(1 + (i-1)*w)
Session 6, Iterative solutions
Dr. Deepak B Phatak 25
IIT BOMBAY
How many rectangles?
• More the merrier! Say 1000.• Width of all rectangles taken together = a - 1.• Width w of each rectangle= (a - 1)/1000• x coordinate of bottom left corner of ith rectangle
x = 1 + (i-1) * w.• Height h of ith rectangle
1/x = 1/(1+(i-1)*w) • Area of this rectangle is
width x height= w * (1/(1+ (i-1) *w))
Session 6, Iterative solutions
Dr. Deepak B Phatak 26
IIT BOMBAY
Program to compute logarithm of a given value
int main(){int i;float a, area=0, w;cout << “Give number whose logarithm is to be found ”; cin >> a;w = (a-1)/1000.0;for(i=1; i <= 1000; i=i+1){
area = area + w*(1/(1+(i-1)*w));}cout << “Log of ” << a << “is ” << area;return 0;
}
Session 6, Iterative solutions
Dr. Deepak B Phatak 27
IIT BOMBAY
Introduction to Hemachandra’s Problem
• Suppose I have to build a wall of length 4 feet. I have bricks which are 2 feet long, and also 1 foot long. In how many ways I can lay the bricks, so that I fill the 4 feet?
• Possibilities: 1, 1, 1, 12, 1, 11, 2, 11, 1, 22, 2
• In how many ways can this be done, if the length of wall is– 3 feet?– 5 feet?
Session 6, Iterative solutions
Dr. Deepak B Phatak 28
IIT BOMBAY
Hemachandra’s Problem …
• Possibilities, if length of wall is 3, 4, or 5 feet:
3 feet wall 4 feet wall 5 Feet wall1, 1, 1 1, 1, 1, 1 1, 1, 1, 1, 12, 1 2, 1, 1 2, 1, 1, 11, 2 1, 2, 1 1, 2, 1, 1
1, 1, 2 1, 1, 2, 12, 2 2, 2, 1
1, 1, 1, 22, 1, 21, 2, 2
Session 6, Iterative solutions
Dr. Deepak B Phatak 29
IIT BOMBAY
Hemachandra’s Problem …
• Possibilities, if length of wall is 3, 4, or 5 feet: 3 feet wall 4 feet wall 5 Feet wall1, 1, 1 1, 1, 1, 1 1, 1, 1, 1, 12, 1 2, 1, 1 2, 1, 1, 11, 2 1, 2, 1 1, 2, 1, 1
1, 1, 2 1, 1, 2, 12, 2 2, 2, 1
1, 1, 1, 22, 1, 21, 2, 2
• Possibilities for 5 feet wall =– Possibilities for 4 feet wall + possibilities for 3 feet wall
Session 6, Iterative solutions
Dr. Deepak B Phatak 30
IIT BOMBAY
Hemachandra’s Problem …
• Possibilities, if length of wall is 3, 4, or 5 feet: 3 feet wall 4 feet wall 5 Feet wall1, 1, 1 1, 1, 1, 1 1, 1, 1, 1, 12, 1 2, 1, 1 2, 1, 1, 11, 2 1, 2, 1 1, 2, 1, 1
1, 1, 2 1, 1, 2, 12, 2 2, 2, 1
1, 1, 1, 22, 1, 21, 2, 2
• Possibilities for 5 feet wall =– Possibilities for 4 feet wall + possibilities for 3 feet wall
Session 6, Iterative solutions
Dr. Deepak B Phatak 31
IIT BOMBAY
Hemachandra’s Actual Problem
• Poetic meter is made of short syllables and long syllables. : Long syllable (l) = 2 beats. Short syllable (s) = 1 beat,
• Example of a poetic meter
ya kun den du tu sha r ha r dha va laL L L S S L S L S S S L
ya shubh r vas tra vru taL L S L L S L
• Suppose I am designing a poetic meter with 8 beats. – How many ways are there of filling 8 beats?
Session 6, Iterative solutions
Dr. Deepak B Phatak 32
IIT BOMBAY
Hemachandra’s Solution
• “By the method of Pingala, it is enough to observe that the last beat is long or short”
• Pingala: mathematician/poet from 200 B.C.
• Hemachandra is giving credit to someone who lived more than 1000 years before him!!
• Copy if necessary, and if permitted,
• But always give credit
Session 6, Iterative solutions
Dr. Deepak B Phatak 33
IIT BOMBAY
Hemachandra’s solution contd.
• An 8 beat pattern can be made by– Take any 7 beat pattern and add a Short beatOr – Take any 6 Beat pattern and add a Long beat
• Thus the total number of possible 8 beat patterns is simply= Number of 7 bit patterns + number of 6 beat patterns
|8 beat patterns| = |7 beat patterns| + |6 beat patterns|
Session 6, Iterative solutions
Dr. Deepak B Phatak 34
IIT BOMBAY
Algebraically..
• Hn = number of patterns with n beats
H8 = H7 + H6
In general Hn = Hn-1 + Hn-2
• Does this help us to compute H8?
We need to know H7, H6, for which we need H5, ...
Session 6, Iterative solutions
Dr. Deepak B Phatak 35
IIT BOMBAY
Algorithm Idea
• H1 = number of patterns with 1 beat = 1 {S}• H2 = Number with 2 beats = 2 {SS, L}• H3 = H2 + H1 = 2 + 1 = 3 {SSS, SL, LS}• H4 = H3 + H2 = 3 + 2 = 5 • H5 = H4 + H3 = 5 + 3 = 8• H6 = H5 + H4 = 8 + 5 = 13• H7 = H6 + H5 = 13 + 8 = 21 • H8 = H7 + H6 = 21 + 13 = 34 ...
Session 6, Iterative solutions
Dr. Deepak B Phatak 36
IIT BOMBAY
Program to compute Hn
int n; cin >> n; // which Hemachandra number to computeint hprev = 1, hcurrent = 2, hnext;for (int i=3; i <= n; i++){
hnext = hprev + hcurrent;// prepare for next iteration, by shifting values of objectshprev = hcurrent; hcurrent = hnext;
}cout << hnext;
Session 6, Iterative solutions
Dr. Deepak B Phatak 37
IIT BOMBAY
On Hemachandra Numbers
• Mathematics from poetry!
• This interesting series models many real-life things– Number of petals in many flowers.
• Ratio of consecutive terms tends to a limit.– Golden Ratio
• These are more commonly known as Fibonacci numbers!!
• Hemachandra lived before Fibonacci.– Pingal lived several centuries earlier
Session 6, Iterative solutions
Dr. Deepak B Phatak 38
IIT BOMBAY
Lab Announcements
• From next week, students attending a lab will be organized into groups of 12 to 16 students. Each group will have two teams of 6 to 8 students each, and will be mentored by 2 Teaching Assistants. (One TA per team)
• Students are required to seat as per the earmarked locations to facilitate this arrangement
• Make up Lab will be held on Saturday in OSL– From 14:00 to 16:00 Hrs
• If there are more students, they can come to another make-up lab to be held on Tuesday from 18:00 to 20:00– Only about 40 students can be accommodated, since the lab
is primarily meant for a special group of students
Session 6, Iterative solutions
Dr. Deepak B Phatak 39
IIT BOMBAY
Announcement for special Lectures
• Special Lectures will be held on Saturday, 13 August 2011 in Room o SIC 301, Kanwal Rekhi Building, 3rd floor
• 14:00 to 16:00. Hindi lecture – by Deepak B Phatak
• 16:00 to 18:00 Discussion of programming problems– By Nagesh Rau Karmali
Session 6, Iterative solutions
Dr. Deepak B Phatak 40
IIT BOMBAY
Lecture schedule, Quizzes and Exams
Session 6, Iterative solutions
• Complete course schedule be available on moodle and on the course home page on 15th
• The first quiz will not be held the scheduled date, 2nd Sept 11– It will be held on Friday, 26 August 11, during regular class
hours in this lecture hall itself
top related