computer programming- lecture 4

67
1 Computer Programming I Lecture 4 Procedural Abstraction and Predefined Functions

Upload: dr-md-shohel-sayeed

Post on 05-Dec-2014

5.036 views

Category:

Education


3 download

DESCRIPTION

Computer Programming- Lecture 4

TRANSCRIPT

Page 1: Computer Programming- Lecture 4

1Computer Programming I

Lecture 4Procedural Abstraction

andPredefined Functions

Page 2: Computer Programming- Lecture 4

2Computer Programming I

Outline

for loop switch casting predefined functions

Page 3: Computer Programming- Lecture 4

3Computer Programming I

for loop A for-loop is another loop mechanism in C++

Designed for common tasks such as adding numbers in a given range

Is sometimes more convenient to use than a while loop

Does not do anything a while loop cannot do

Page 4: Computer Programming- Lecture 4

4Computer Programming I

for/while Loop Comparison sum = 0;

n = 1;while(n <= 10) // add the numbers 1 - 10 { sum = sum + n; n++; }

sum = 0;for (n = 1; n <= 10; n++) //add the numbers 1 - 10 sum = sum + n;

Page 5: Computer Programming- Lecture 4

5Computer Programming I

For Loop Dissection The for loop uses the same components as the

while loop in a more compact form for (n = 1; n <= 10; n++)

Initialization Action

Boolean Expression

Update Action

Page 6: Computer Programming- Lecture 4

6Computer Programming I

for loop (tracing)#include <iostream>#include <conio.h>using namespace std;

int main(){ int i; for (i=1; i<=3; i++) { cout<<i<<“ “; } system(“pause”); return 0;}

i output

Page 7: Computer Programming- Lecture 4

7Computer Programming I

#include <iostream>#include <conio.h>using namespace std;

int main(){ int i; for (i=1; i<=3; i++) { cout<<i<<“ “; } system(“pause”); return 0;}

for loop (tracing)

i output1 1

Page 8: Computer Programming- Lecture 4

8Computer Programming I

#include <iostream>#include <conio.h>using namespace std;

int main(){ int i; for (i=1; i<=3; i++) { cout<<i<<“ “; } system(“pause”); return 0;}

for loop (tracing)

i output1 12

Page 9: Computer Programming- Lecture 4

9Computer Programming I

#include <iostream>#include <conio.h>using namespace std;

int main(){ int i; for (i=1; i<=3; i++) { cout<<i<<“ “; } system(“pause”); return 0;}

for loop (tracing)

i output1 12If true then execute body

Page 10: Computer Programming- Lecture 4

10Computer Programming I

#include <iostream>#include <conio.h>using namespace std;

int main(){ int i; for (i=1; i<=3; i++) { cout<<i<<“ “; } system(“pause”); return 0;}

for loop (tracing)

i output1 1 22

Page 11: Computer Programming- Lecture 4

11Computer Programming I

#include <iostream>#include <conio.h>using namespace std;

int main(){ int i; for (i=1; i<=3; i++) { cout<<i<<“ “; } system(“pause”); return 0;}

for loop (tracing)

i output1 1 223

Page 12: Computer Programming- Lecture 4

12Computer Programming I

#include <iostream>#include <conio.h>using namespace std;

int main(){ int i; for (i=1; i<=3; i++) { cout<<i<<“ “; } system(“pause”); return 0;}

for loop (tracing)

i output1 1 223

If true then execute body

Page 13: Computer Programming- Lecture 4

13Computer Programming I

#include <iostream>#include <conio.h>using namespace std;

int main(){ int i; for (i=1; i<=3; i++) { cout<<i<<“ “; } system(“pause”); return 0;}

for loop (tracing)

i output1 1 2 323

Page 14: Computer Programming- Lecture 4

14Computer Programming I

#include <iostream>#include <conio.h>using namespace std;

int main(){ int i; for (i=1; i<=3; i++) { cout<<i<<“ “; } system(“pause”); return 0;}

for loop (tracing)

i output1 1 2 3234

Page 15: Computer Programming- Lecture 4

15Computer Programming I

#include <iostream>#include <conio.h>using namespace std;

int main(){ int i; for (i=1; i<=3; i++) { cout<<i<<“ “; } system(“pause”); return 0;}

for loop (tracing)

i output1 1 2 3234

If true then execute body

Page 16: Computer Programming- Lecture 4

16Computer Programming I

#include <iostream>#include <conio.h>using namespace std;

int main(){ int i; for (i=1; i<=3; i++) { cout<<i<<“ “; } system(“pause”); return 0;}

for loop

i output1 1 2 3234

If true then execute body

Not True

Page 17: Computer Programming- Lecture 4

17Computer Programming I

for loop#include <iostream>#include <conio.h>

using namespace std;

int main(){ int i,j; for (i=1; i<=3; i++) { for (j=1; j<=3; j++) cout << "i" << "j" << "\t"; cout << endl; } system(“pause”); return 0;}

i j

output

Page 18: Computer Programming- Lecture 4

18Computer Programming I

for loop#include <iostream>#include <conio.h>

using namespace std;

int main(){ int i,j; for (i=1; i<=3; i++) { for (j=1; j<=3; j++) cout << "i" << "j" << "\t"; cout << endl; } system(“pause”); return 0;}

i j1

output

Page 19: Computer Programming- Lecture 4

19Computer Programming I

for loop#include <iostream>#include <conio.h>

using namespace std;

int main(){ int i,j; for (i=1; i<=3; i++) { for (j=1; j<=3; j++) cout << "i" << "j" << "\t"; cout << endl; } system(“pause”); return 0;}

i j1 1

output

Page 20: Computer Programming- Lecture 4

20Computer Programming I

for loop#include <iostream>#include <conio.h>

using namespace std;

int main(){ int i,j; for (i=1; i<=3; i++) { for (j=1; j<=3; j++) cout << "i" << "j" << "\t"; cout << endl; } system(“pause”); return 0;}

ij

i j1 1

output

Page 21: Computer Programming- Lecture 4

21Computer Programming I

for loop#include <iostream>#include <conio.h>

using namespace std;

int main(){ int i,j; for (i=1; i<=3; i++) { for (j=1; j<=3; j++) cout << "i" << "j" << "\t"; cout << endl; } system(“pause”); return 0;}

ij

i j1 1

2

output

Page 22: Computer Programming- Lecture 4

22Computer Programming I

for loop#include <iostream>#include <conio.h>

using namespace std;

int main(){ int i,j; for (i=1; i<=3; i++) { for (j=1; j<=3; j++) cout << "i" << "j" << "\t"; cout << endl; } system(“pause”); return 0;}

ij ij

i j1 1

2

output

Page 23: Computer Programming- Lecture 4

23Computer Programming I

for loop#include <iostream>#include <conio.h>

using namespace std;

int main(){ int i,j; for (i=1; i<=3; i++) { for (j=1; j<=3; j++) cout << "i" << "j" << "\t"; cout << endl; } system(“pause”); return 0;}

ij ij

i j1 1

23

output

Page 24: Computer Programming- Lecture 4

24Computer Programming I

for loop#include <iostream>#include <conio.h>

using namespace std;

int main(){ int i,j; for (i=1; i<=3; i++) { for (j=1; j<=3; j++) cout << "i" << "j" << "\t"; cout << endl; } system(“pause”); return 0;}

ij ij ij

i j1 1

23

output

Page 25: Computer Programming- Lecture 4

25Computer Programming I

for loop#include <iostream>#include <conio.h>

using namespace std;

int main(){ int i,j; for (i=1; i<=3; i++) { for (j=1; j<=3; j++) cout << "i" << "j" << "\t"; cout << endl; } system(“pause”); return 0;}

ij ij ij

i j1 1

23

2

output

Page 26: Computer Programming- Lecture 4

26Computer Programming I

for loop#include <iostream>#include <conio.h>

using namespace std;

int main(){ int i,j; for (i=1; i<=3; i++) { for (j=1; j<=3; j++) cout << "i" << "j" << "\t"; cout << endl; } system(“pause”); return 0;}

ij ij ij

i j1 1

23

2 1

output

Page 27: Computer Programming- Lecture 4

27Computer Programming I

for loop#include <iostream>#include <conio.h>

using namespace std;

int main(){ int i,j; for (i=1; i<=3; i++) { for (j=1; j<=3; j++) cout << "i" << "j" << "\t"; cout << endl; } system(“pause”); return 0;}

ij ij ijij

i j1 1

23

2 1

output

Page 28: Computer Programming- Lecture 4

28Computer Programming I

for loop#include <iostream>#include <conio.h>

using namespace std;

int main(){ int i,j; for (i=1; i<=3; i++) { for (j=1; j<=3; j++) cout << "i" << "j" << "\t"; cout << endl; } system(“pause”); return 0;}

ij ij ijij

i j1 1

23

2 12

output

Page 29: Computer Programming- Lecture 4

29Computer Programming I

for loop#include <iostream>#include <conio.h>

using namespace std;

int main(){ int i,j; for (i=1; i<=3; i++) { for (j=1; j<=3; j++) cout << "i" << "j" << "\t"; cout << endl; } system(“pause”); return 0;}

ij ij ijij ij

i j1 1

23

2 12

output

Page 30: Computer Programming- Lecture 4

30Computer Programming I

for loop#include <iostream>#include <conio.h>

using namespace std;

int main(){ int i,j; for (i=1; i<=3; i++) { for (j=1; j<=3; j++) cout << "i" << "j" << "\t"; cout << endl; } system(“pause”); return 0;}

ij ij ijij ij ijij ij ij

i j1 1

23

2 123

3 123

output

Page 31: Computer Programming- Lecture 4

31Computer Programming I

for loop#include <iostream>#include <conio.h>

using namespace std;

int main(){ int i,j; for (i=1; i<=3; i++) { for (j=1; j<=3; j++) cout << i << j << "\t"; cout << endl; } system(“pause”); return 0;}

output

Page 32: Computer Programming- Lecture 4

32Computer Programming I

for loop#include <iostream>#include <conio.h>

using namespace std;

int main(){ int i,j; for (i=1; i<=3; i++) { for (j=1; j<=3; j++) cout << i << j << "\t"; cout << endl; } system(“pause”); return 0;}

i j

output

Page 33: Computer Programming- Lecture 4

33Computer Programming I

for loop#include <iostream>#include <conio.h>

using namespace std;

int main(){ int i,j; for (i=1; i<=3; i++) { for (j=1; j<=3; j++) cout << i << j << "\t"; cout << endl; } system(“pause”); return 0;}

i j1

output

Page 34: Computer Programming- Lecture 4

34Computer Programming I

for loop#include <iostream>#include <conio.h>

using namespace std;

int main(){ int i,j; for (i=1; i<=3; i++) { for (j=1; j<=3; j++) cout << i << j << "\t"; cout << endl; } system(“pause”); return 0;}

i j1 1

output

Page 35: Computer Programming- Lecture 4

35Computer Programming I

for loop#include <iostream>#include <conio.h>

using namespace std;

int main(){ int i,j; for (i=1; i<=3; i++) { for (j=1; j<=3; j++) cout << i << j << "\t"; cout << endl; } system(“pause”); return 0;}

11

i j1 1

output

Page 36: Computer Programming- Lecture 4

36Computer Programming I

for loop#include <iostream>#include <conio.h>

using namespace std;

int main(){ int i,j; for (i=1; i<=3; i++) { for (j=1; j<=3; j++) cout << i << j << "\t"; cout << endl; } system(“pause”); return 0;}

11

i j1 1

2

output

Page 37: Computer Programming- Lecture 4

37Computer Programming I

for loop#include <iostream>#include <conio.h>

using namespace std;

int main(){ int i,j; for (i=1; i<=3; i++) { for (j=1; j<=3; j++) cout << i << j << "\t"; cout << endl; } system(“pause”); return 0;}

11 12

i j1 1

2

output

Page 38: Computer Programming- Lecture 4

38Computer Programming I

for loop#include <iostream>#include <conio.h>

using namespace std;

int main(){ int i,j; for (i=1; i<=3; i++) { for (j=1; j<=3; j++) cout << i << j << "\t"; cout << endl; } system(“pause”); return 0;}

11 12

i j1 1

23

output

Page 39: Computer Programming- Lecture 4

39Computer Programming I

for loop#include <iostream>#include <conio.h>

using namespace std;

int main(){ int i,j; for (i=1; i<=3; i++) { for (j=1; j<=3; j++) cout << i << j << "\t"; cout << endl; } system(“pause”); return 0;}

11 12 13

i j1 1

23

output

Page 40: Computer Programming- Lecture 4

40Computer Programming I

for loop#include <iostream>#include <conio.h>

using namespace std;

int main(){ int i,j; for (i=1; i<=3; i++) { for (j=1; j<=3; j++) cout << i << j << "\t"; cout << endl; } system(“pause”); return 0;}

11 12 13

i j1 1

23

2

output

Page 41: Computer Programming- Lecture 4

41Computer Programming I

for loop#include <iostream>#include <conio.h>

using namespace std;

int main(){ int i,j; for (i=1; i<=3; i++) { for (j=1; j<=3; j++) cout << i << j << "\t"; cout << endl; } system(“pause”); return 0;}

11 12 13

i j1 1

23

2 1

output

Page 42: Computer Programming- Lecture 4

42Computer Programming I

for loop#include <iostream>#include <conio.h>

using namespace std;

int main(){ int i,j; for (i=1; i<=3; i++) { for (j=1; j<=3; j++) cout << i << j << "\t"; cout << endl; } system(“pause”); return 0;}

11 12 1321

i j1 1

23

2 1

output

Page 43: Computer Programming- Lecture 4

43Computer Programming I

for loop#include <iostream>#include <conio.h>

using namespace std;

int main(){ int i,j; for (i=1; i<=3; i++) { for (j=1; j<=3; j++) cout << i << j << "\t"; cout << endl; } system(“pause”); return 0;}

11 12 1321

i j1 1

23

2 12

output

Page 44: Computer Programming- Lecture 4

44Computer Programming I

for loop#include <iostream>#include <conio.h>

using namespace std;

int main(){ int i,j; for (i=1; i<=3; i++) { for (j=1; j<=3; j++) cout << i << j << "\t"; cout << endl; } system(“pause”); return 0;}

11 12 1321 22

i j1 1

23

2 12

output

Page 45: Computer Programming- Lecture 4

45Computer Programming I

for loop#include <iostream>#include <conio.h>

using namespace std;

int main(){ int i,j; for (i=1; i<=3; i++) { for (j=1; j<=3; j++) cout << i << j << "\t"; cout << endl; } system(“pause”); return 0;}

11 12 1321 22 2331 32 33

i j1 1

23

2 123

3 123

output

Page 46: Computer Programming- Lecture 4

46Computer Programming I

for loop#include <iostream>#include <conio.h>

using namespace std;

int main(){ int i,j; for (i=0; i<=4; i++) { for (j=0; j<=4; j++) cout << "(" << i << "," << j << ")\t"; cout << endl; } system(“pause”); return 0;}

(0,0) (0,1) (0,2) (0,3) (0,4)(1,0) (1,1) (1,2) (1,3) (1,4)(2,0) (2,1) (2,2) (2,3) (2,4)(3,0) (3,1) (3,2) (3,3) (3,4)(4,0) (4,1) (4,2) (4,3) (4,4)

output

Page 47: Computer Programming- Lecture 4

47Computer Programming I

#include <iostream>#include <conio.h>

using namespace std;

int main(){ int i,j; for (i=0; i<=4; i++) { for (j=0; j<=4; j++) cout << "(" << i << "," << j << ")\t"; cout << endl; } system(“pause”); return 0;}

(0,0) (0,1) (0,2) (0,3) (0,4)(1,0) (1,1) (1,2) (1,3) (1,4)(2,0) (2,1) (2,2) (2,3) (2,4)(3,0) (3,1) (3,2) (3,3) (3,4)(4,0) (4,1) (4,2) (4,3) (4,4)

for loop

i=j i<j

i>j

output

Page 48: Computer Programming- Lecture 4

48Computer Programming I

#include <iostream>#include <conio.h>

using namespace std;

int main(){ int i,j; for (i=0; i<=4; i++) { for (j=0; j<=4; j++) cout << "(" << i << "," << j << ")\t"; cout << endl; } system(“pause”); return 0;}

(1,0)(2,0) (2,1)(3,0) (3,1) (3,2)(4,0) (4,1) (4,2) (4,3)

?

for loop

How to producethis output?

Page 49: Computer Programming- Lecture 4

49Computer Programming I

#include <iostream>#include <conio.h>

using namespace std;

int main(){ int i,j; for (i=0; i<=4; i++) { for (j=0; j<=4; j++)

if (i>j) cout << "(" << i << "," << j << ")\t"; cout << endl; } system(“pause”); return 0;}

for loop

(1,0)(2,0) (2,1)(3,0) (3,1) (3,2)(4,0) (4,1) (4,2) (4,3)

output

Page 50: Computer Programming- Lecture 4

50Computer Programming I

Multi-way if-else-statements An if-else-statement is a two-way branch Three or four (or more) way branches can be

designed using nested if-else-statements

Page 51: Computer Programming- Lecture 4

51Computer Programming I

#include <iostream>#include <conio.h>

using namespace std;

int main(){ int no; double price; double total; cout << “Enter no. of students "; cin >> no;

if (no==1) price=10; else if (no==2) price=9; else if (no==3) price=7.5; else if (no==4) price=6.5; else if (no==5) price=5.5; else price=4.5; total=no*price; cout << "the total price is " << total; system(“pause”); return 0;}

Multi-way if-else

Page 52: Computer Programming- Lecture 4

52Computer Programming I

The switch-statement The switch-statement is an alternative for

constructing multi-way branches

Page 53: Computer Programming- Lecture 4

53Computer Programming I

switch-statement Syntax switch (controlling expression)

{ case Constant_1: statement_Sequence_1

break; case Constant_2: Statement_Sequence_2 break; . . . case Constant_n: Statement_Sequence_n break; default: Default_Statement_Sequence}

Page 54: Computer Programming- Lecture 4

54Computer Programming I

The break Statement The break statement ends the switch-statement

Omitting the break statement will cause the code for the next case to be executed!

Omitting a break statement allows the use of multiple case labels for a section of code case 'A':

case 'a': cout << "Excellent."; break;

Runs the same code for either 'A' or 'a'

Page 55: Computer Programming- Lecture 4

55Computer Programming I

The default Statement If no case label has a constant that matches the

controlling expression, the statements followingthe default label are executed If there is no default label, nothing happens

when the switch statement is executed It is a good idea to include a default section

Page 56: Computer Programming- Lecture 4

56Computer Programming I

switch (if-else to switch) //break will exit switch() switch (no) { case 1: price=10.0; break; case 2: price=9.0; break; case 3: price=7.5; break; case 4: price=6.5; break; case 5: price=5.5; break; default: price=4.5; } total=no*price; cout << "the total price is " << total; return 0;}

#include <iostream>#include <conio.h>

using namespace std;

int main(){ int no; double price; double total;

cout << “Enter no. of students "; cin >> no;

Output:Enter no. of students 4the total price is 26

Note: 26 = 4 * 6.5

Page 57: Computer Programming- Lecture 4

57Computer Programming I

switch (if-else to switch) //break will exit switch() switch (no) { case 1: price=10.0; case 2: price=9.0; case 3: price=7.5; case 4: price=6.5; case 5: price=5.5; default: price=4.5; } total=no*price; cout << "the total price is " << total; return 0;}

#include <iostream>#include <conio.h>

using namespace std;

int main(){ int no; double price; double total;

cout << “Enter no. of students "; cin >> no;

Output:Enter no. of students 4the total price is 18

Note: 18 = 4 * 4.5

Page 58: Computer Programming- Lecture 4

58Computer Programming I

switch #include <iostream>#include <conio.h>using namespace std;

int main() {

int no; cout << "Please enter a number "; cin >> no;

switch (no) //no break is used { case 1: cout << "1" << " your number is " << no << endl; case 2: cout << "2" << " your number is " << no << endl; case 3: cout << "3" << " your number is " << no << endl; case 4: cout << "4" << " your number is " << no << endl; case 5: cout << "5" << " your number is " << no << endl; default: cout << "No such number" << endl; }

return 0;}

Page 59: Computer Programming- Lecture 4

59Computer Programming I

switch #include <iostream>#include <conio.h>using namespace std;

int main() {

int no; cout << "Please enter a number "; cin >> no; switch (no) //break will cause program to exit switch() { case 10: cout << "your number is 10" ; break; case 20: case 30: cout << "your number is 20 or 30" ; break; case 40: cout << "your number is 40" ; break; default: cout << "No such number" << endl; } return 0;}

Page 60: Computer Programming- Lecture 4

60Computer Programming I

Type casts are data conversions specified by the programmer. A Type Cast produces a value of one type from another type

var1 = static_cast <var1_type> (var2);

Example:

b is now have a value 5.0 (note the decimal to indicate floating point value)

int a = 5;

double b;

b = static_cast <double> (a);

Static cast

Page 61: Computer Programming- Lecture 4

61Computer Programming I

A problem with integer division:

kgPerPerson should be 2.5, not 2.0!

int sugarInKg = 10, noOfPeople = 4;double kgPerPerson;kgPerPerson = sugarInKg / noOfPeople;

(2.0) (10) (4)

Static cast

int sugarInKg = 10, noOfPeople = 4;double kgPerPerson;kgPerPerson = static_cast <double> (sugarInKg) / noOfPeople;

(2.5) (10.0) (4)

Solutions: 1) declare sugarInKg as double OR2) static_cast sugarInKg to double.

Page 62: Computer Programming- Lecture 4

62Computer Programming I

C++ comes with libraries of predefined functions.

To make the functions available, the library must be “included” in a program using include directive.

An include directive tells the compiler which library header file to include.

Example: To use the sqrt function from the math library, cmath, we need to define:

Predefined functions

#include <cmath>

Page 63: Computer Programming- Lecture 4

63Computer Programming I

Predefined functions

Page 64: Computer Programming- Lecture 4

64Computer Programming I

Function call syntax:functionName (argument1, argument2, …);

Example: sqrt function returns, or computes, the square root of a number

answer now have a value of 3.0

answer = sqrt (9.0);

function name argumentreturn value

Predefined functions

Page 65: Computer Programming- Lecture 4

65Computer Programming I

Function arguments can be

1) Constants: sqrt ( 4.0 );

2) Variables: sqrt ( x );

3) Expressions: sqrt ( sqrt ( x ) );

sqrt ( 3 – 6*x );

Predefined functions

from double data type

Page 66: Computer Programming- Lecture 4

66Computer Programming I

pow (x, y) Returns value of x to the power of y Return value is of type double Both arguments are of type double Found in the library cmath

value = pow (2.0, 3.0);

function name argument 1return value argument 2

Predefined functions

Page 67: Computer Programming- Lecture 4

67Computer Programming I

End of slides