Upcoming SlideShare
×

Like this presentation? Why not share!

# Computer Programming- Lecture 4

## on Mar 09, 2013

• 4,460 views

Computer Programming- Lecture 4

Computer Programming- Lecture 4

### Views

Total Views
4,460
Views on SlideShare
242
Embed Views
4,218

Likes
0
20
0

### 2 Embeds4,218

 http://drshohel.net 4214 http://edit.optimizely.com 4

### Categories

Uploaded via as Microsoft PowerPoint

### Report content

• Comment goes here.
Are you sure you want to
Your message goes here

## Computer Programming- Lecture 4Presentation Transcript

• Lecture 4 Procedural Abstraction and Predefined FunctionsComputer Programming I 1
• Outline for loop switch casting predefined functions Computer Programming I 2
• 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 Computer Programming I 3
• 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; Computer Programming I 4
• 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 Update Action Boolean Expression Computer Programming I 5
• for loop (tracing) #include <iostream> #include <conio.h> using namespace std; i output int main() { int i; for (i=1; i<=3; i++) { cout<<i<<“ “; } system(“pause”); return 0; } Computer Programming I 6
• for loop (tracing) #include <iostream> #include <conio.h> using namespace std; i output int main() { 1 1 int i; for (i=1; i<=3; i++) { cout<<i<<“ “; } system(“pause”); return 0; } Computer Programming I 7
• for loop (tracing) #include <iostream> #include <conio.h> using namespace std; i output int main() { 1 1 int i; 2 for (i=1; i<=3; i++) { cout<<i<<“ “; } system(“pause”); return 0; } Computer Programming I 8
• for loop (tracing) #include <iostream> #include <conio.h> using namespace std; i output int main() { 1 1 If true then execute body int i; 2 for (i=1; i<=3; i++) { cout<<i<<“ “; } system(“pause”); return 0; } Computer Programming I 9
• for loop (tracing) #include <iostream> #include <conio.h> using namespace std; i output int main() { 1 12 int i; 2 for (i=1; i<=3; i++) { cout<<i<<“ “; } system(“pause”); return 0; } Computer Programming I 10
• for loop (tracing) #include <iostream> #include <conio.h> using namespace std; i output int main() { 1 12 int i; 2 3 for (i=1; i<=3; i++) { cout<<i<<“ “; } system(“pause”); return 0; } Computer Programming I 11
• for loop (tracing) #include <iostream> #include <conio.h> using namespace std; i output int main() { 1 12 If true then execute body int i; 2 3 for (i=1; i<=3; i++) { cout<<i<<“ “; } system(“pause”); return 0; } Computer Programming I 12
• for loop (tracing) #include <iostream> #include <conio.h> using namespace std; i output int main() { 1 123 int i; 2 3 for (i=1; i<=3; i++) { cout<<i<<“ “; } system(“pause”); return 0; } Computer Programming I 13
• for loop (tracing) #include <iostream> #include <conio.h> using namespace std; i output int main() { 1 123 int i; 2 3 for (i=1; i<=3; i++) 4 { cout<<i<<“ “; } system(“pause”); return 0; } Computer Programming I 14
• for loop (tracing) #include <iostream> #include <conio.h> using namespace std; i output int main() { 1 123 If true then execute body int i; 2 3 for (i=1; i<=3; i++) 4 { cout<<i<<“ “; } system(“pause”); return 0; } Computer Programming I 15
• for loop #include <iostream> #include <conio.h> using namespace std; i output int main() { 1 123 If true then execute body int i; 2 3 for (i=1; i<=3; i++) 4 { cout<<i<<“ “; } system(“pause”); Not True return 0; } Computer Programming I 16
• for loop i j #include <iostream> #include <conio.h> using namespace std; int main() { int i,j; for (i=1; i<=3; i++) { output for (j=1; j<=3; j++) cout << "i" << "j" << "t"; cout << endl; } system(“pause”); return 0; } Computer Programming I 17
• for loop i j 1 #include <iostream> #include <conio.h> using namespace std; int main() { int i,j; for (i=1; i<=3; i++) { output for (j=1; j<=3; j++) cout << "i" << "j" << "t"; cout << endl; } system(“pause”); return 0; } Computer Programming I 18
• for loop i j 1 1 #include <iostream> #include <conio.h> using namespace std; int main() { int i,j; for (i=1; i<=3; i++) { output for (j=1; j<=3; j++) cout << "i" << "j" << "t"; cout << endl; } system(“pause”); return 0; } Computer Programming I 19
• for loop i j 1 1 #include <iostream> #include <conio.h> using namespace std; int main() { int i,j; for (i=1; i<=3; i++) { output for (j=1; j<=3; j++) cout << "i" << "j" << "t"; ij cout << endl; } system(“pause”); return 0; } Computer Programming I 20
• for loop i j 1 1 #include <iostream> #include <conio.h> 2 using namespace std; int main() { int i,j; for (i=1; i<=3; i++) { output for (j=1; j<=3; j++) cout << "i" << "j" << "t"; ij cout << endl; } system(“pause”); return 0; } Computer Programming I 21
• for loop i j 1 1 #include <iostream> #include <conio.h> 2 using namespace std; int main() { int i,j; for (i=1; i<=3; i++) { output for (j=1; j<=3; j++) cout << "i" << "j" << "t"; ij ij cout << endl; } system(“pause”); return 0; } Computer Programming I 22
• for loop i j 1 1 #include <iostream> #include <conio.h> 2 3 using namespace std; int main() { int i,j; for (i=1; i<=3; i++) { output for (j=1; j<=3; j++) cout << "i" << "j" << "t"; ij ij cout << endl; } system(“pause”); return 0; } Computer Programming I 23
• for loop i j 1 1 #include <iostream> #include <conio.h> 2 3 using namespace std; int main() { int i,j; for (i=1; i<=3; i++) { output for (j=1; j<=3; j++) cout << "i" << "j" << "t"; ij ij ij cout << endl; } system(“pause”); return 0; } Computer Programming I 24
• for loop i j 1 1 #include <iostream> #include <conio.h> 2 3 using namespace std; 2 int main() { int i,j; for (i=1; i<=3; i++) { output for (j=1; j<=3; j++) cout << "i" << "j" << "t"; ij ij ij cout << endl; } system(“pause”); return 0; } Computer Programming I 25
• for loop i j 1 1 #include <iostream> #include <conio.h> 2 3 using namespace std; 2 1 int main() { int i,j; for (i=1; i<=3; i++) { output for (j=1; j<=3; j++) cout << "i" << "j" << "t"; ij ij ij cout << endl; } system(“pause”); return 0; } Computer Programming I 26
• for loop i j 1 1 #include <iostream> #include <conio.h> 2 3 using namespace std; 2 1 int main() { int i,j; for (i=1; i<=3; i++) { output for (j=1; j<=3; j++) cout << "i" << "j" << "t"; ij ij ij cout << endl; ij } system(“pause”); return 0; } Computer Programming I 27
• for loop i j 1 1 #include <iostream> #include <conio.h> 2 3 using namespace std; 2 1 2 int main() { int i,j; for (i=1; i<=3; i++) { output for (j=1; j<=3; j++) cout << "i" << "j" << "t"; ij ij ij cout << endl; ij } system(“pause”); return 0; } Computer Programming I 28
• for loop i j 1 1 #include <iostream> #include <conio.h> 2 3 using namespace std; 2 1 2 int main() { int i,j; for (i=1; i<=3; i++) { output for (j=1; j<=3; j++) cout << "i" << "j" << "t"; ij ij ij cout << endl; ij ij } system(“pause”); return 0; } Computer Programming I 29
• for loop i j 1 1 #include <iostream> #include <conio.h> 2 3 using namespace std; 2 1 2 int main() 3 { int i,j; 3 1 2 for (i=1; i<=3; i++) 3 { output for (j=1; j<=3; j++) cout << "i" << "j" << "t"; ij ij ij cout << endl; ij ij ij } system(“pause”); ij ij ij return 0; } Computer Programming I 30
• for loop #include <iostream> #include <conio.h> using namespace std; int main() { int i,j; for (i=1; i<=3; i++) { output for (j=1; j<=3; j++) cout << i << j << "t"; cout << endl; } system(“pause”); return 0; } Computer Programming I 31
• for loop i j #include <iostream> #include <conio.h> using namespace std; int main() { int i,j; for (i=1; i<=3; i++) { output for (j=1; j<=3; j++) cout << i << j << "t"; cout << endl; } system(“pause”); return 0; } Computer Programming I 32
• for loop i j 1 #include <iostream> #include <conio.h> using namespace std; int main() { int i,j; for (i=1; i<=3; i++) { output for (j=1; j<=3; j++) cout << i << j << "t"; cout << endl; } system(“pause”); return 0; } Computer Programming I 33
• for loop i j 1 1 #include <iostream> #include <conio.h> using namespace std; int main() { int i,j; for (i=1; i<=3; i++) { output for (j=1; j<=3; j++) cout << i << j << "t"; cout << endl; } system(“pause”); return 0; } Computer Programming I 34
• for loop i j 1 1 #include <iostream> #include <conio.h> using namespace std; int main() { int i,j; for (i=1; i<=3; i++) { output for (j=1; j<=3; j++) cout << i << j << "t"; 11 cout << endl; } system(“pause”); return 0; } Computer Programming I 35
• for loop i j 1 1 #include <iostream> #include <conio.h> 2 using namespace std; int main() { int i,j; for (i=1; i<=3; i++) { output for (j=1; j<=3; j++) cout << i << j << "t"; 11 cout << endl; } system(“pause”); return 0; } Computer Programming I 36
• for loop i j 1 1 #include <iostream> #include <conio.h> 2 using namespace std; int main() { int i,j; for (i=1; i<=3; i++) { output for (j=1; j<=3; j++) cout << i << j << "t"; 11 12 cout << endl; } system(“pause”); return 0; } Computer Programming I 37
• for loop i j 1 1 #include <iostream> #include <conio.h> 2 3 using namespace std; int main() { int i,j; for (i=1; i<=3; i++) { output for (j=1; j<=3; j++) cout << i << j << "t"; 11 12 cout << endl; } system(“pause”); return 0; } Computer Programming I 38
• for loop i j 1 1 #include <iostream> #include <conio.h> 2 3 using namespace std; int main() { int i,j; for (i=1; i<=3; i++) { output for (j=1; j<=3; j++) cout << i << j << "t"; 11 12 13 cout << endl; } system(“pause”); return 0; } Computer Programming I 39
• for loop i j 1 1 #include <iostream> #include <conio.h> 2 3 using namespace std; 2 int main() { int i,j; for (i=1; i<=3; i++) { output for (j=1; j<=3; j++) cout << i << j << "t"; 11 12 13 cout << endl; } system(“pause”); return 0; } Computer Programming I 40
• for loop i j 1 1 #include <iostream> #include <conio.h> 2 3 using namespace std; 2 1 int main() { int i,j; for (i=1; i<=3; i++) { output for (j=1; j<=3; j++) cout << i << j << "t"; 11 12 13 cout << endl; } system(“pause”); return 0; } Computer Programming I 41
• for loop i j 1 1 #include <iostream> #include <conio.h> 2 3 using namespace std; 2 1 int main() { int i,j; for (i=1; i<=3; i++) { output for (j=1; j<=3; j++) cout << i << j << "t"; 11 12 13 cout << endl; 21 } system(“pause”); return 0; } Computer Programming I 42
• for loop i j 1 1 #include <iostream> #include <conio.h> 2 3 using namespace std; 2 1 2 int main() { int i,j; for (i=1; i<=3; i++) { output for (j=1; j<=3; j++) cout << i << j << "t"; 11 12 13 cout << endl; 21 } system(“pause”); return 0; } Computer Programming I 43
• for loop i j 1 1 #include <iostream> #include <conio.h> 2 3 using namespace std; 2 1 2 int main() { int i,j; for (i=1; i<=3; i++) { output for (j=1; j<=3; j++) cout << i << j << "t"; 11 12 13 cout << endl; 21 22 } system(“pause”); return 0; } Computer Programming I 44
• for loop i j 1 1 #include <iostream> #include <conio.h> 2 3 using namespace std; 2 1 2 int main() 3 { int i,j; 3 1 2 for (i=1; i<=3; i++) 3 { output for (j=1; j<=3; j++) cout << i << j << "t"; 11 12 13 cout << endl; 21 22 23 } system(“pause”); 31 32 33 return 0; } Computer Programming I 45
• 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++) output cout << "(" << i << "," << j << ")t"; cout << endl; (0,0) (0,1) (0,2) (0,3) (0,4) } (1,0) (1,1) (1,2) (1,3) (1,4) system(“pause”); (2,0) (2,1) (2,2) (2,3) (2,4) return 0; (3,0) (3,1) (3,2) (3,3) (3,4) } (4,0) (4,1) (4,2) (4,3) (4,4) Computer Programming I 46
• 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++) i=j output i<j cout << "(" << i << "," << j << ")t"; cout << endl; (0,0) (0,1) (0,2) (0,3) (0,4) } (1,0) (1,1) (1,2) (1,3) (1,4) system(“pause”); (2,0) (2,1) (2,2) (2,3) (2,4) return 0; (3,0) (3,1) (3,2) (3,3) (3,4) } i>j (4,0) (4,1) (4,2) (4,3) (4,4) Computer Programming I 47
• for loop #include <iostream> #include <conio.h> using namespace std; int main() { How to produce ? int i,j; for (i=0; i<=4; i++) { this output? for (j=0; j<=4; j++) cout << "(" << i << "," << j << ")t"; cout << endl; } (1,0) system(“pause”); (2,0) (2,1) return 0; (3,0) (3,1) (3,2) } (4,0) (4,1) (4,2) (4,3) Computer Programming I 48
• 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++) if (i>j)  output  cout << "(" << i << "," << j << ")t"; cout << endl; } (1,0) system(“pause”); (2,0) (2,1) return 0; (3,0) (3,1) (3,2) } (4,0) (4,1) (4,2) (4,3) Computer Programming I 49
• 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 Computer Programming I 50
• Multi-way if-else #include <iostream> if (no==1) #include <conio.h> price=10; else if (no==2) using namespace std; price=9; else if (no==3) int main() price=7.5; { else if (no==4) int no; price=6.5; double price; else if (no==5) double total; price=5.5; else cout << “Enter no. of students "; price=4.5; cin >> no; total=no*price; cout << "the total price is " << total; system(“pause”); return 0; } Computer Programming I 51
• The switch-statement The switch-statement is an alternative for constructing multi-way branches Computer Programming I 52
• 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 } Computer Programming I 53
• 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 Computer Programming I 54
• The default Statement If no case label has a constant that matches the controlling expression, the statements following the 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 Computer Programming I 55
• Output:switch (if-else to4switch)Enter no. of studentsthe total<iostream> 26 #include price is //break will exit switch() #include <conio.h> switch (no)Note: 26 = 4 * 6.5 { using namespace std; case 1: price=10.0; break; case 2: price=9.0; break; int main() case 3: price=7.5; break; { case 4: price=6.5; break; int no; case 5: price=5.5; break; double price; default: price=4.5; double total; } total=no*price; cout << “Enter no. of students "; cout << "the total price is " << total; cin >> no; return 0; } Computer Programming I 56
• Output:switch (if-else to 4Enter no. of students switch)the total price is 18 #include <iostream> //break will exit switch() #include <conio.h> switch (no) {Note: 18 = 4 * 4.5std;using namespace case 1: price=10.0; case 2: price=9.0; int main() case 3: price=7.5; { case 4: price=6.5; int no; case 5: price=5.5; double price; default: price=4.5; double total; } total=no*price; cout << “Enter no. of students "; cout << "the total price is " << total; cin >> no; return 0; } Computer Programming I 57
• #include <iostream>switch #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; } Computer Programming I 58
• #include <iostream>switch #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; } Computer Programming I 59
• Static cast  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: int a = 5; double b; b = static_cast <double> (a);  b is now have a value 5.0 (note the decimal to indicate floating point value) Computer Programming I 60
• Static cast A problem with integer division: int sugarInKg = 10, noOfPeople = 4; double kgPerPerson; kgPerPerson = sugarInKg / noOfPeople; (2.0) (10) (4) kgPerPerson should be 2.5, not 2.0! Solutions: 1) declare sugarInKg as double OR 2) static_cast sugarInKg to double. int sugarInKg = 10, noOfPeople = 4; double kgPerPerson; kgPerPerson = static_cast <double> (sugarInKg) / noOfPeople; (2.5) (10.0) (4) Computer Programming I 61
• Predefined functions  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: #include <cmath> Computer Programming I 62
• Predefined functions Computer Programming I 63
• Predefined functions Function call syntax: functionName (argument1, argument2, …); Example: sqrt function returns, or computes, the square root of a number answer = sqrt (9.0); return value function name argument answer now have a value of 3.0 Computer Programming I 64
• Predefined functions Function arguments can be 1) Constants: sqrt ( 4.0 ); from double 2) Variables: sqrt ( x ); data type 3) Expressions: sqrt ( sqrt ( x ) ); sqrt ( 3 – 6*x ); Computer Programming I 65
• Predefined functions  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); return value function name argument 1 argument 2 Computer Programming I 66
• End of slides Computer Programming I 67