• No results found

Department of CSE, Kanwal Rekhi Building IIT Bombay

N/A
N/A
Protected

Academic year: 2022

Share "Department of CSE, Kanwal Rekhi Building IIT Bombay"

Copied!
22
0
0

Loading.... (view fulltext now)

Full text

(1)

CS 101

Computer Programming and Utilization Dr Deepak B Phatak

Subrao Nilekani Chair Professor

Department of CSE, Kanwal Rekhi Building IIT Bombay

IIT BOMBAY

(2)

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

(3)

IIT BOMBAY

Iteration using While loop // previous line;

while (condition) { code block

};

// next line;

(4)

IIT BOMBAY

Control flow in While loop

// Previous line;

while (condition) {

code block };

// nextline;

Condition ? Previous line

Next line

Code Block

false true

(5)

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.

(6)

IIT BOMBAY

Finding a root by bisection method

(7)

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].

(8)

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.

(9)

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

(10)

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;

}

(11)

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

(12)

IIT BOMBAY

Another way of iterating: Do - While Loop

// previous line do {

code block

} while (condition);

// next line;

(13)

IIT BOMBAY

Do While loop

// previous line;

do {

code block

} while (condition) ;

// next line;

Condition ? Previous line

Next line

Code Block

(14)

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

(15)

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;

(16)

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

(17)

IIT BOMBAY

Control flow in the for loop

The for loop has four parts:

for (initial-expression; condition; end-expression) {

body }

(18)

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

(19)

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;}

(20)

IIT BOMBAY

Data file rollmarks.txt

1001 72 1002 45 1003 91 1004 86 1006 38 1008 53 1009 65

(21)

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

(22)

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);

}

References

Related documents

• If the condition is true originally, then the value of some variable used in condition must change in the execution of body, so that eventually condition becomes false.. •

Session 15,Software Engineering and Course Projects Friday, September 23, 2011..

Going backward from final winner sequence which ends in state S2 (indicated By the 2 nd tuple), we recover the sequence..2. The HMM,

Going backward from final winner sequence which ends in state S2 (indicated By the 2 nd tuple), we recover

• If condition is true originally, then value of some variable in condition must change in the execution of body, so that eventually condition becomes false...

• If the condition is true originally, then the value of some variable used in condition must change in the execution of body, so that eventually condition becomes false. •

• If the condition is true originally, then the value of some variable used in condition must change in the execution of body, so that eventually condition becomes false.. •

• Many problems will deal with a large number of values, performing similar operations on each value.. • In this case, we are required to write a separate instruction for