CS 101 Computer Programming and utilization Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal Rekhi Building IIT Bombay Session 6, Iterative Solutions Friday, August 12, 2011 [Some ideas: courtesy Prof Sohoni, and Prof Ranade] IIT BOMBAY Session 6, Iterative solutions
40
Embed
Dr Deepak B Phatakcs101/2011.2/Lectures/lecture_slides/201… · Dr. Deepak B Phatak 3. IIT BOMBAY. Finding sum of numbers • Find the sum of N numbers – Value of N is given as
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
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){
• 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
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?
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