CS 101
Computer Programming and Utilization Dr Deepak B Phatak
Subrao Nilekani Chair Professor
Department of CSE, Kanwal Rekhi Building IIT Bombay
IIT BOMBAY
IIT BOMBAY
Overview
• Review of flow of program control in c++
• while and for loops
• Example programs
• Array manipulations
• Finding a given element in the array
IIT BOMBAY
Iteration using While loop // previous line;
while (condition) { code block
};
// next line;
IIT BOMBAY
Control flow in While loop
// Previous line;
while (condition) {
code block };
// nextline;
Condition ? Previous line
Next line
Code Block
false true
IIT BOMBAY
Iteration using While loop // Previous line;
while (condition) { code block
};
// Next line;
• condition is evaluated at the beginning.
• code block executed only if condition is true
and then you go back to evaluate the condition.
IIT BOMBAY
Finding a root by bisection method
IIT BOMBAY
Finding a root by bisection method
Start with a lo and hi values such that f (lo) ∗ f (hi) < 0.
Compute mid and f (mid).
while |f (mid)| > 0 (some small threshold value)., Locate the next interval to be either
[low,mid] or [mid,hi].
IIT BOMBAY
Finding root by bisection
We write a program to solve cubics Ax3 + Bx2 + Cx + D.
Takes in inputs A,B,C,D.
Takes in inputs lo,hi, tol.
Returns if f (lo) * f (hi) > 0.
while |f (mid)| > tol,
locate the next interval to be either [low,mid] or [mid,hi].
returns mid so that |f (mid)| < tol.
IIT BOMBAY
#include <iostream>
using namespace std;
int main() {
float A,B,C,D,lo,hi,mid,flo,fhi,fmid;
float tol;
cout << "A B C D ?" << "\n";
cin >> A >> B >> C >> D;
cout << "low high tolerance" << "\n";
cin >> lo >> hi >> tol;
Bisection Program
IIT BOMBAY
Bisection method …
flo=A*lo*lo*lo +B*lo*lo +C*lo +D;
fhi=A*hi*hi*hi +B*hi*hi +C*hi +D;
if (flo*fhi>0) {
cout << "error in hi-lo" << "\n";
return 1;
};
//
//Main part of our code will come here //
cout << mid << "\n";
return 0;
}
IIT BOMBAY
// Initial values for mid and fmid mid=(lo+hi)/2;
fmid=A*mid*mid*mid +B*mid*mid +C*mid +D;
while (fabs(fmid)>tol) { if (flo*fmid >0) {
lo=mid; flo=fmid; } else {
hi=mid; fhi=fmid; };
mid=(lo+hi)/2;
fmid=A*mid*mid*mid +B*mid*mid +C*mid +D;
Main part of the code
IIT BOMBAY
Another way of iterating: Do - While Loop
// previous line do {
code block
} while (condition);
// next line;
IIT BOMBAY
Do While loop
// previous line;
do {
code block
} while (condition) ;
// next line;
Condition ? Previous line
Next line
Code Block
IIT BOMBAY
Features of do - while Loop
// previous line do {
Code block
} while (condition);
// next line;
• code block is executed at least once.
• Condition is evaluated only at the end
• If true, then go back to again execute code block
IIT BOMBAY
Iteration using for loop
#include <iostream>
using namespace std;
// countdown using a for loop
// (based on www.cplusplus.com) int main () {
int count;
for (count=10; count>0; count=count-1) { cout << count << ", ";
}
cout << endl << “value of count ” << count;
return 0;
IIT BOMBAY
Quiz
When we execute the code lines
for (count=10; count>0; count=count-1) { cout << count << ", ";
}
cout << endl << “value of count ” << count;
The value of count printed is:
(A) 11
(B) 1
(C) 0
(D) -1
(E) None of these
IIT BOMBAY
Control flow in the for loop
The for loop has four parts:
for (initial-expression; condition; end-expression) {
body }
IIT BOMBAY
Control flow in for loop
// Previous line
initial-expression;
Condition;
code block;
end-expression;
// next line;
Condition ? initial Expression
Next line
Code Block
false true
End-expression Previous line
IIT BOMBAY
Find maximum of N numbers int main(){
int a[100], max, N, I;
cin >> N;
for (i=0; i < N; i++) cin >> a[i];
max = a[0];
for (i=1; i < N; i++) { if (a[i] > max){
max = a[i];
} }
cout << “maximum Value is ” << max << endl;
return 0;}
IIT BOMBAY
Data file rollmarks.txt
1001 72 1002 45 1003 91 1004 86 1006 38 1008 53 1009 65
IIT BOMBAY
program readrollmarks.cpp - execution
$ c++ readrollmarks.cpp
$ ./a.out 0 1001 72 1 1002 45 2 1003 91 3 1004 86 4 1006 38 5 1008 53 6 1009 65
Number of students in these arrays is 7
IIT BOMBAY
Findmarks.cpp
// given a roll number find the marks int roll[100], marks[100], nstudents;
int givenroll,foundmarks, position, i;
nstudents = getdata(roll, marks);
cin >> givenroll;
for (i=0; i<nstudents; i++){
if (roll[i] == givenroll){
foundmarks = marks[i];
}
cout << “Marks for ” << givenroll << “are ” <<
foundmarks;
return (0);
}