2621008 - C++ 3

375 views

Published on

Published in: Health & Medicine, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
375
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
27
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

2621008 - C++ 3

  1. 1. Slide 1 Lecture 03
  2. 2. Slide 2 Loop Basics
  3. 3. Slide 3 Loop Types  Sentinel  while  do-while  Counting  for
  4. 4. Slide 4 Sentinel Loops
  5. 5. Slide 5 while Syntax while(test condition) { statement }
  6. 6. Slide 6 When a program encounters a while loop, the test condition is evaluated first. If the condition is TRUE, the program executes the body of the loop. The program then returns to the test condition and reevaluates. If the condition is still TRUE, the body executes again. while Syntax
  7. 7. Slide 7 Example #1 while (7) cout << “hello” << endl;
  8. 8. Slide 8 Example #2 #include<iostream> using namespace std; int main() { int input; cout <<"enter number between 5 and 89, inclusive: "; cin >> input; while (input < 5 || input > 89) { cout << "that value is unacceptable... try again: "; cin >> input; } cout << "the value"<<input<< "is in the interval[5, 89]"<< endl; cin.get(); //system("PAUSE"); return 0; }
  9. 9. Slide 9 Example #3 #include<iostream> using namespace std; int main() { long sum = 0; int counter = 1; while (counter<=100) { sum += counter; counter++ ; } cout << "sum of first 100 integers is "<< sum ; cin.get(); system("PAUSE"); return 0; }
  10. 10. do-while Syntax Do { statement } while (test condition);
  11. 11. Copyright © 2003 Pearson Education, Inc. Slide 11 The do-while loop is similar to the while loop, except that the test condition occurs at the end of the loop. The do-while loop is an exit-condition loop. This means that the body of the loop is always executed first. Then, the test condition is evaluated. If the test condition is TRUE, the program executes the body of the loop again. If the test condition is FALSE, the loop terminates and program execution continues with the statement following the while. do-while Syntax
  12. 12. Slide 12 Example #1 do{ cout << "Enter an integer between 2 and 174 (inclusive)" << "that is a multiple of 3: "; cin >> input; } while (!(input >= 2 && input <= 174 && input%3 == 0));
  13. 13. Slide 13 Counting Loops
  14. 14. Slide 14 for Syntax for (startExpression; testExpression; countExpression) } statement }
  15. 15. Slide 15 • The startExpression is evaluated before the loop begins. It is acceptable to declare and assign in the startExpression(such as int x = 1;). • This startExpression is evaluated only once at the beginning of the loop. • The testExpression will evaluate to TRUE (nonzero) or FALSE (zero). While TRUE, the body of the loop repeats. When the testExpression becomes FALSE, the looping stops and the program continues with the statement immediately following the for loop body in the program code. • The countExpression executes after each trip through the loop. The count may increase/decrease by an increment of 1 or of some other value. • Braces are not required if the body of the for loop consists of only ONE statement. Please indent the body of the loop for readability. for Syntax
  16. 16. Slide 16 Example #1 int max; float average; long sum = 0; short i = 0; cout << "enter positive integer: "; cin >> max; for(i=0; i <= max; i++) sum += i; average = sum/max; cout << "average is " << average << endl;
  17. 17. Slide 17 Goal Output * * * * * * * * * * * * * * * Example #2
  18. 18. Slide 18 Outputting 5 Lines for (int i = 1; i <= 5; i++) { cout << endl; }
  19. 19. Slide 19 Outputting 5 Stars for (int i = 1; i <= 5; i++) { cout << “* ”; }
  20. 20. Slide 20 Combining for (int i = 1; i <= 5; i++) { for (int j = 1; j <= 5; j++) cout << "* "; cout << endl; } output i=1 j=? variable values
  21. 21. Slide 21 Combining for (int i = 1; i <= 5; i++) { for (int j = 1; j <= 5; j++) cout << "* "; cout << endl; } output i=1 j=? variable values
  22. 22. Slide 22 Combining for (int i = 1; i <= 5; i++) { for (int j = 1; j <= 5; j++) cout << "* "; cout << endl; } output i=1 j=1 variable values
  23. 23. Slide 23 Combining for (int i = 1; i <= 5; i++) { for (int j = 1; j <= 5; j++) cout << "* "; cout << endl; } * output i=1 j=1 variable values
  24. 24. Slide 24 Combining for (int i = 1; i <= 5; i++) { for (int j = 1; j <= 5; j++) cout << "* "; cout << endl; } * output i=1 j=2 variable values
  25. 25. Slide 25 Combining for (int i = 1; i <= 5; i++) { for (int j = 1; j <= 5; j++) cout << "* "; cout << endl; } * * output i=1 j=2 variable values
  26. 26. Slide 26 Combining for (int i = 1; i <= 5; i++) { for (int j = 1; j <= 5; j++) cout << "* "; cout << endl; } * * output i=1 j=3 variable values
  27. 27. Slide 27 Combining for (int i = 1; i <= 5; i++) { for (int j = 1; j <= 5; j++) cout << "* "; cout << endl; } * * * output i=1 j=3 variable values
  28. 28. Slide 28 Combining for (int i = 1; i <= 5; i++) { for (int j = 1; j <= 5; j++) cout << "* "; cout << endl; } * * * output i=1 j=4 variable values
  29. 29. Slide 29 Combining for (int i = 1; i <= 5; i++) { for (int j = 1; j <= 5; j++) cout << "* "; cout << endl; } * * * * output i=1 j=4 variable values
  30. 30. Slide 30 Combining for (int i = 1; i <= 5; i++) { for (int j = 1; j <= 5; j++) cout << "* "; cout << endl; } * * * * output i=1 j=5 variable values
  31. 31. Slide 31 Combining for (int i = 1; i <= 5; i++) { for (int j = 1; j <= 5; j++) cout << "* "; cout << endl; } * * * * * output i=1 j=5 variable values
  32. 32. Slide 32 Combining for (int i = 1; i <= 5; i++) { for (int j = 1; j <= 5; j++) cout << "* "; cout << endl; } * * * * * output i=1 j=6 variable values
  33. 33. Slide 33 Combining for (int i = 1; i <= 5; i++) { for (int j = 1; j <= 5; j++) cout << "* "; cout << endl; } * * * * * output i=1 j=6 variable values
  34. 34. Slide 34 Combining for (int i = 1; i <= 5; i++) { for (int j = 1; j <= 5; j++) cout << "* "; cout << endl; } * * * * * output i=2 j=? variable values
  35. 35. Slide 35 Combining for (int i = 1; i <= 5; i++) { for (int j = 1; j <= 5; j++) cout << "* "; cout << endl; } * * * * * output i=2 j=1 variable values
  36. 36. Slide 36 Combining for (int i = 1; i <= 5; i++) { for (int j = 1; j <= 5; j++) cout << "* "; cout << endl; } * * * * * * output i=2 j=1 variable values
  37. 37. Slide 37 Combining for (int i = 1; i <= 5; i++) { for (int j = 1; j <= 5; j++) cout << "* "; cout << endl; } * * * * * * output i=2 j=2 variable values
  38. 38. Slide 38 Combining for (int i = 1; i <= 5; i++) { for (int j = 1; j <= 5; j++) cout << "* "; cout << endl; } * * * * * * * output i=2 j=2 variable values
  39. 39. Slide 39 Combining for (int i = 1; i <= 5; i++) { for (int j = 1; j <= 5; j++) cout << "* "; cout << endl; } * * * * * * * output i=2 j=3 variable values
  40. 40. Slide 40 Combining for (int i = 1; i <= 5; i++) { for (int j = 1; j <= 5; j++) cout << "* "; cout << endl; } * * * * * * * * output i=2 j=3 variable values
  41. 41. Slide 41 Combining for (int i = 1; i <= 5; i++) { for (int j = 1; j <= 5; j++) cout << "* "; cout << endl; } * * * * * * * * output i=2 j=4 variable values
  42. 42. Slide 42 Combining for (int i = 1; i <= 5; i++) { for (int j = 1; j <= 5; j++) cout << "* "; cout << endl; } * * * * * * * * * output i=2 j=4 variable values
  43. 43. Combining for (int i = 1; i <= 5; i++) { for (int j = 1; j <= 5; j++) cout << "* "; cout << endl; } * * * * * * * * * output i=2 j=5 variable values
  44. 44. Slide 44 Combining for (int i = 1; i <= 5; i++) { for (int j = 1; j <= 5; j++) cout << "* "; cout << endl; } * * * * * * * * * * output i=2 j=5 variable values oops!!!
  45. 45. Slide 45 Goal vs Actual Goal * * * * * * * * * * * * * * * Actual * * * * * * * * * * * * * * * * * * * * * * * * * j=1 j=2 j=3 j=4 j=5 i=1 i=2 i=3 i=4 i=5
  46. 46. Slide 46 Fixed for (int i=1; i <= 5; i++) { for (int j=1; j <= 5 – i + 1; j++) cout << "* "; cout << endl; } output i=1 j=? variable values
  47. 47. Slide 47 Fixed for (int i=1; i <= 5; i++) { for (int j=1; j <= 5 – i + 1; j++) cout << "* "; cout << endl; } output i=1 j=1 variable values
  48. 48. Slide 48 Fixed for (int i=1; i <= 5; i++) { for (int j=1; j <= 5 – i + 1; j++) cout << "* "; cout << endl; } * output i=1 j=1 variable values
  49. 49. Slide 49 Fixed for (int i=1; i <= 5; i++) { for (int j=1; j <= 5 – i + 1; j++) cout << "* "; cout << endl; } * output i=1 j=2 variable values
  50. 50. Slide 50 Fixed for (int i=1; i <= 5; i++) { for (int j=1; j <= 5 – i + 1; j++) cout << "* "; cout << endl; } * * output i=1 j=2 variable values
  51. 51. Slide 51 Fixed for (int i=1; i <= 5; i++) { for (int j=1; j <= 5 – i + 1; j++) cout << "* "; cout << endl; } * * output i=1 j=3 variable values
  52. 52. Slide 52 Fixed for (int i=1; i <= 5; i++) { for (int j=1; j <= 5 – i + 1; j++) cout << "* "; cout << endl; } * * * output i=1 j=3 variable values
  53. 53. Slide 53 Fixed for (int i=1; i <= 5; i++) { for (int j=1; j <= 5 – i + 1; j++) cout << "* "; cout << endl; } * * * output i=1 j=4 variable values
  54. 54. Slide 54 Fixed for (int i=1; i <= 5; i++) { for (int j=1; j <= 5 – i + 1; j++) cout << "* "; cout << endl; } * * * * output i=1 j=4 variable values
  55. 55. Slide 55 Fixed for (int i=1; i <= 5; i++) { for (int j=1; j <= 5 – i + 1; j++) cout << "* "; cout << endl; } * * * * * output i=1 j=5 variable values
  56. 56. Slide 56 Fixed for (int i=1; i <= 5; i++) { for (int j=1; j <= 5 – i + 1; j++) cout << "* "; cout << endl; } * * * * * output i=1 j=6 variable values
  57. 57. Slide 57 Fixed for (int i=1; i <= 5; i++) { for (int j=1; j <= 5 – i + 1; j++) cout << "* "; cout << endl; } * * * * * output i=1 j=6 variable values
  58. 58. Slide 58 Fixed for (int i=1; i <= 5; i++) { for (int j=1; j <= 5 – i + 1; j++) cout << "* "; cout << endl; } * * * * * output i=2 j=? variable values
  59. 59. Slide 59 Fixed for (int i=1; i <= 5; i++) { for (int j=1; j <= 5 – i + 1; j++) cout << "* "; cout << endl; } * * * * * output i=2 j=1 variable values
  60. 60. Slide 60 Fixed for (int i=1; i <= 5; i++) { for (int j=1; j <= 5 – i + 1; j++) cout << "* "; cout << endl; } * * * * * * output i=2 j=1 variable values
  61. 61. Slide 61 Fixed for (int i=1; i <= 5; i++) { for (int j=1; j <= 5 – i + 1; j++) cout << "* "; cout << endl; } * * * * * * output i=2 j=2 variable values
  62. 62. Slide 62 Fixed for (int i=1; i <= 5; i++) { for (int j=1; j <= 5 – i + 1; j++) cout << "* "; cout << endl; } * * * * * * * output i=2 j=2 variable values
  63. 63. Slide 63 Fixed for (int i=1; i <= 5; i++) { for (int j=1; j <= 5 – i + 1; j++) cout << "* "; cout << endl; } * * * * * * * output i=2 j=3 variable values
  64. 64. Slide 64 Fixed for (int i=1; i <= 5; i++) { for (int j=1; j <= 5 – i + 1; j++) cout << "* "; cout << endl; } * * * * * * * * output i=2 j=3 variable values
  65. 65. Slide 65 Fixed for (int i=1; i <= 5; i++) { for (int j=1; j <= 5 – i + 1; j++) cout << "* "; cout << endl; } * * * * * * * * output i=2 j=4 variable values
  66. 66. Slide 66 Fixed for (int i=1; i <= 5; i++) { for (int j=1; j <= 5 – i + 1; j++) cout << "* "; cout << endl; } * * * * * * * * * output i=2 j=4 variable values
  67. 67. Slide 67 Fixed for (int i=1; i <= 5; i++) { for (int j=1; j <= 5 – i + 1; j++) cout << "* "; cout << endl; } * * * * * * * * * output i=2 j=5 variable values
  68. 68. Slide 68 Fixed for (int i=1; i <= 5; i++) { for (int j=1; j <= 5 – i + 1; j++) cout << "* "; cout << endl; } * * * * * * * * * output i=2 j=5 variable values
  69. 69. Slide 69 Fixed for (int i=1; i <= 5; i++) { for (int j=1; j <= 5 – i + 1; j++) cout << "* "; cout << endl; } * * * * * * * * * output i=3 j=? variable values
  70. 70. Slide 70 Fixed for (int i=1; i <= 5; i++) { for (int j=1; j <= 5 – i + 1; j++) cout << "* "; cout << endl; } * * * * * * * * * output i=3 j=1 variable values
  71. 71. Slide 71 Fixed for (int i=1; i <= 5; i++) { for (int j=1; j <= 5 – i + 1; j++) cout << "* "; cout << endl; } * * * * * * * * * * output i=3 j=1 variable values
  72. 72. Slide 72 Fixed for (int i=1; i <= 5; i++) { for (int j=1; j <= 5 – i + 1; j++) cout << "* "; cout << endl; } * * * * * * * * * * output i=3 j=2 variable values
  73. 73. Fixed for (int i=1; i <= 5; i++) { for (int j=1; j <= 5 – i + 1; j++) cout << "* "; cout << endl; } * * * * * * * * * * * output i=3 j=2 variable values
  74. 74. Slide 74 Fixed for (int i=1; i <= 5; i++) { for (int j=1; j <= 5 – i + 1; j++) cout << "* "; cout << endl; } * * * * * * * * * * * output i=3 j=3 variable values
  75. 75. Slide 75 Fixed for (int i=1; i <= 5; i++) { for (int j=1; j <= 5 – i + 1; j++) cout << "* "; cout << endl; } * * * * * * * * * * * * output i=3 j=3 variable values
  76. 76. Slide 76 Fixed for (int i=1; i <= 5; i++) { for (int j=1; j <= 5 – i + 1; j++) cout << "* "; cout << endl; } * * * * * * * * * * * * output i=3 j=4 variable values
  77. 77. Slide 77 Fixed for (int i=1; i <= 5; i++) { for (int j=1; j <= 5 – i + 1; j++) cout << "* "; cout << endl; } * * * * * * * * * * * * output i=3 j=4 variable values
  78. 78. Slide 78 Fixed for (int i=1; i <= 5; i++) { for (int j=1; j <= 5 – i + 1; j++) cout << "* "; cout << endl; } * * * * * * * * * * * * output i=4 j=? variable values
  79. 79. Slide 79 Fixed for (int i=1; i <= 5; i++) { for (int j=1; j <= 5 – i + 1; j++) cout << "* "; cout << endl; } * * * * * * * * * * * * output i=4 j=1 variable values
  80. 80. Fixed for (int i=1; i <= 5; i++) { for (int j=1; j <= 5 – i + 1; j++) cout << "* "; cout << endl; } * * * * * * * * * * * * * output i=4 j=1 variable values
  81. 81. Slide 81 Fixed for (int i=1; i <= 5; i++) { for (int j=1; j <= 5 – i + 1; j++) cout << "* "; cout << endl; } * * * * * * * * * * * * * output i=4 j=2 variable values
  82. 82. Slide 82 Fixed for (int i=1; i <= 5; i++) { for (int j=1; j <= 5 – i + 1; j++) cout << "* "; cout << endl; } * * * * * * * * * * * * * * output i=4 j=2 variable values
  83. 83. Slide 83 Fixed for (int i=1; i <= 5; i++) { for (int j=1; j <= 5 – i + 1; j++) cout << "* "; cout << endl; } * * * * * * * * * * * * * * output i=4 j=3 variable values
  84. 84. Slide 84 Fixed for (int i=1; i <= 5; i++) { for (int j=1; j <= 5 – i + 1; j++) cout << "* "; cout << endl; } * * * * * * * * * * * * * * output i=4 j=3 variable values
  85. 85. Slide 85 Fixed for (int i=1; i <= 5; i++) { for (int j=1; j <= 5 – i + 1; j++) cout << "* "; cout << endl; } * * * * * * * * * * * * * * output i=5 j=? variable values
  86. 86. Slide 86 Fixed for (int i=1; i <= 5; i++) { for (int j=1; j <= 5 – i + 1; j++) cout << "* "; cout << endl; } * * * * * * * * * * * * * * output i=5 j=1 variable values
  87. 87. Slide 87 Fixed for (int i=1; i <= 5; i++) { for (int j=1; j <= 5 – i + 1; j++) cout << "* "; cout << endl; } * * * * * * * * * * * * * * * output i=5 j=1 variable values
  88. 88. Slide 88 Fixed for (int i=1; i <= 5; i++) { for (int j=1; j <= 5 – i + 1; j++) cout << "* "; cout << endl; } * * * * * * * * * * * * * * * output i=5 j=2 variable values
  89. 89. Slide 89 Fixed for (int i=1; i <= 5; i++) { for (int j=1; j <= 5 – i + 1; j++) cout << "* "; cout << endl; } * * * * * * * * * * * * * * * output i=5 j=2 variable values
  90. 90. Slide 90 Fixed for (int i=1; i <= 5; i++) { for (int j=1; j <= 5 – i + 1; j++) cout << "* "; cout << endl; } * * * * * * * * * * * * * * * output i=6 j=? variable values
  91. 91. Slide 91 Fixed for (int i=1; i <= 5; i++) { for (int j=1; j <= 5 – i + 1; j++) cout << "* "; cout << endl; } * * * * * * * * * * * * * * * output i=6 j=? variable values
  92. 92. Slide 92 * * * * * * * * * * * * * * *
  93. 93. Slide 93 Solution Rows for (short i = 0; i < 5; i++) { for (short j = 1; j <= i; j++) cout << " "; for (short j = i; j < 5; j++) cout << "* "; cout << endl; } * * * * * * * * * * * * * * *
  94. 94. Slide 94 Solution Spaces for (short i = 0; i < 5; i++) { for (short j = 1; j <= i; j++) cout << " "; for (short j = i; j < 5; j++) cout << "* "; cout << endl; } * * * * * * * * * * * * * * *
  95. 95. Slide 95 Solution Stars for (short i = 0; i < 5; i++) { for (short j = 1; j <= i; j++) cout << " "; for (short j = i; j < 5; j++) cout << "* "; cout << endl; } * * * * * * * * * * * * * * *
  96. 96. Slide 96 Random Number Generation
  97. 97. Slide 97 rand() #include <iostream> using namespace std; int main() { for(int i = 1; i <= 10; i++) cout << rand() << endl; return 0; }
  98. 98. Slide 98 rand() #include <iostream> using namespace std; int main() { for(int i = 1; i <= 10; i++) cout << rand() << endl; return 0; } 38457 733887 7384 94877 243 3994452 374008 23146 11833432 237844 First Execution
  99. 99. Slide 99 rand() #include <iostream> using namespace std; int main() { for(int i = 1; i <= 10; i++) cout << rand() << endl; return 0; } 38457 733887 7384 94877 243 3994452 374008 23146 11833432 237844 First Execution 38457 733887 7384 94877 243 3994452 374008 23146 11833432 237844 Second Executio
  100. 100. Slide 100 Pseudo-Random Generation The numbers aren't actually random -- they're generated by a formula, so they're just pseudorandom.
  101. 101. Slide 101 The pseudo-random number generator is initialized using the argument passed as seed. For every different seed value used in a call to srandsrand, the pseudo-randompseudo-random number generatornumber generator can be expected to generate a different succession of results in the subsequent calls to rand. Two different initializations with the same seed will generate the same succession of results in subsequent calls to rand. In order to generate random-like numbers, srandsrand is usually initialized to some distinctive runtime value, like the value returned by function time (declared in header <ctime><ctime>). This is distinctive enough for most trivial randomization needs.
  102. 102. Slide 102 srand() #include <iostream> #include <ctime> #include <cstdlib> using namespace std; int main() { srand(time(NULL)); for(int i = 1; i <= 10; i++) cout << rand() << endl; return 0; }
  103. 103. Slide 103 srand() #include <iostream> #include <ctime> #include <cstdlib> using namespace std; int main() { srand(time(NULL)); for(int i = 1; i <= 10; i++) cout << rand() << endl; return 0; } 464 53735 342 23 6578 889 93723 7165 7422457 78614 First Execution
  104. 104. Slide 104 srand() #include <iostream> #include <ctime> #include <cstdlib> using namespace std; int main() { srand(time(NULL)); for(int i = 1; i <= 10; i++) cout << rand() << endl; return 0; } 464 53735 342 23 6578 889 93723 7165 7422457 78614 First Execution 6877 245768 215 57618 78511 79738 3461 175117 35 257868 Second Execution
  105. 105. Slide 105 limiting range: [0, MAX_RAND] #include <iostream> #include <ctime> #include <cstdlib> using namespace std; int main() { long next_value; srand(time(NULL)); for (int i = 1; i <= 100000; i++) if (rand()%100 <= 34) cout << "hello" << endl; // ~35,000 times else cout << "goodbye" << endl; // ~65,000 times return 0; }
  106. 106. Slide 106 #include <iostream> using namespace std; void main () { int x, y; double z; if(typeid(x) == typeid(y)) cout << "x and y are of same type."<<endl; else cout<< "x and y are of different type. " <<endl; cout << "The type of z is "<< typeid(z).name()<<endl; cout<< "The type of y is "<< typeid(y).name() << endl; } typeid () The expected output of the program is given below. x and y are of same type. The type of z is double The type of y is int The operator typeid () identifies the type of variable. Here we only show the use of the operator. It returns reference to type of its argument. It is coded as: typeid(object);
  107. 107. Slide 107

×