Computer Programming- Lecture 4

Post on 05-Dec-2014

5036 Views

Category:

Education

3 Downloads

Preview:

Click to see full reader

DESCRIPTION

Computer Programming- Lecture 4

Transcript

1Computer Programming I

Lecture 4Procedural Abstraction

andPredefined Functions

2Computer Programming I

Outline

for loop switch casting predefined functions

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

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;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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?

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

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

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

52Computer Programming I

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

constructing multi-way branches

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}

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'

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

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

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

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

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

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

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.

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>

63Computer Programming I

Predefined functions

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

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

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

67Computer Programming I

End of slides

top related