Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Computer Programming- Lecture 4

26,193 views

Published on

Computer Programming- Lecture 4

Published in: Education
  • Be the first to comment

Computer Programming- Lecture 4

  1. 1. Lecture 4 Procedural Abstraction and Predefined FunctionsComputer Programming I 1
  2. 2. Outline for loop switch casting predefined functions Computer Programming I 2
  3. 3. 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
  4. 4. 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
  5. 5. 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
  6. 6. 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
  7. 7. 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
  8. 8. 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
  9. 9. 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
  10. 10. 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
  11. 11. 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
  12. 12. 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
  13. 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++) { cout<<i<<“ “; } system(“pause”); return 0; } Computer Programming I 13
  14. 14. 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
  15. 15. 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
  16. 16. 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
  17. 17. 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
  18. 18. 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
  19. 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"; cout << endl; } system(“pause”); return 0; } Computer Programming I 19
  20. 20. 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
  21. 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 cout << endl; } system(“pause”); return 0; } Computer Programming I 21
  22. 22. 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
  23. 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 cout << endl; } system(“pause”); return 0; } Computer Programming I 23
  24. 24. 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
  25. 25. 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
  26. 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; } system(“pause”); return 0; } Computer Programming I 26
  27. 27. 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
  28. 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 } system(“pause”); return 0; } Computer Programming I 28
  29. 29. 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
  30. 30. 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
  31. 31. 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
  32. 32. 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
  33. 33. 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
  34. 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"; cout << endl; } system(“pause”); return 0; } Computer Programming I 34
  35. 35. 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
  36. 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 cout << endl; } system(“pause”); return 0; } Computer Programming I 36
  37. 37. 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
  38. 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 cout << endl; } system(“pause”); return 0; } Computer Programming I 38
  39. 39. 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
  40. 40. 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
  41. 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; } system(“pause”); return 0; } Computer Programming I 41
  42. 42. 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
  43. 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 } system(“pause”); return 0; } Computer Programming I 43
  44. 44. 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
  45. 45. 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
  46. 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++) 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
  47. 47. 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
  48. 48. 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
  49. 49. 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
  50. 50. 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
  51. 51. 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
  52. 52. The switch-statement The switch-statement is an alternative for constructing multi-way branches Computer Programming I 52
  53. 53. 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
  54. 54. 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
  55. 55. 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
  56. 56. 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
  57. 57. 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
  58. 58. #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
  59. 59. #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
  60. 60. 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
  61. 61. 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
  62. 62. 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
  63. 63. Predefined functions Computer Programming I 63
  64. 64. 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
  65. 65. 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
  66. 66. 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
  67. 67. End of slides Computer Programming I 67

×