Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 144 Advanced C++ Programming February 12 Class Meeting

Similar presentations


Presentation on theme: "CS 144 Advanced C++ Programming February 12 Class Meeting"— Presentation transcript:

1 CS 144 Advanced C++ Programming February 12 Class Meeting
Department of Computer Engineering San Jose State University Spring 2019 Instructor: Ron Mak

2 Assignment #2: Sample Solution
#include <cassert> using namespace std; typedef int Door; const int SIMULATION_COUNT = 100; /**  * Run a simulation. sequence the sequence number. win1 number of first choice wins. win2 number of second choice wins.  */ void simulate(int sequence, int& win1, int& win2);

3 Assignment #2: Sample Solution
/**  * Hide the car behind a door. the door that the car is hidden behind.  */ Door hide_car(); the player's first door choice, which is either 1, 2, or 3. Door make_first_choice(); a random door 1, 2, or 3. Door random_door();

4 Assignment #2: Sample Solution, cont’d
/**  * Monty opens a door that is not: first_choice_door the player's first door choice. car_behind_door the door that the car is hidden behind. the door to open.  */ Door open_door(Door first_choice_door, Door car_behind_door);  * Return a random door 1, 2, or 3 that is not: a_door a door. another_door another door, which can be equal to a_door. the random door. Door random_door_not(Door a_door, Door another_door);

5 Assignment #2: Sample Solution, cont’d
/**  * Return the player's second door choice, which cannot be: first_door the player's first choice door. opened_door the opened door. the second door choice.  */ Door make_second_choice(Door first_door, Door opened_door);  * Choose door 1, 2, or 3 that is not: first_door the player's first door choice. the remaining door. Door choose_remaining_door(Door first_door, Door opened_door);

6 Assignment #2: Sample Solution, cont’d
int main() {     int win1 = 0, win2 = 0;     cout << "   #     Car   First  Opened  Second    Win    Win" << endl;     cout << "        here  choice    door  choice  first second" << endl;     cout << endl;     srand(time(NULL));  // seed the random number generator     // Run the simulations.     for (int i = 1; i <= SIMULATION_COUNT; i++) simulate(i, win1, win2);     cout << setw(4) << win1 << " wins if stay with the first choice" << endl;     cout << setw(4) << win2 << " wins if switch to the second choice" << endl;     cout.setf(ios::fixed);     cout.setf(ios::showpoint);     cout.precision(1);     cout << "Win ratio of switch over stay: ";     cout << static_cast<double>(win2)/win1 << endl; }

7 Assignment #2: Sample Solution, cont’d
void simulate(int sequence, int& win1, int& win2) {     // Perform a simulation.     Door car_behind_door    = hide_car();     Door first_choice_door  = make_first_choice();     Door opened_door        = open_door(first_choice_door,                                         car_behind_door);     Door second_choice_door = make_second_choice(first_choice_door,                                                  opened_door);     // Print the results.     cout << setw(4) << sequence << setw(8) << car_behind_door;     cout << setw(8) << first_choice_door << setw(8) << opened_door;     cout << setw(8) << second_choice_door;     if (first_choice_door == car_behind_door)     {         cout << "    yes";  // the car was behind the first door choice         win1++;     }     else         cout << "           yes";  // it was behind the second door choice         win2++;     cout << endl; }

8 Assignment #2: Sample Solution, cont’d
Door hide_car() {     // The simulation randomly chooses the door to hide the car.     return random_door(); } Door make_first_choice()     // The simulation randomly makes the player's first door choice. Door random_door()     return rand()%3 + 1;

9 Assignment #2: Sample Solution, cont’d
Door open_door(Door first_choice_door, Door car_behind_door) {     // Monty Hall knows which door the car is behind     // and so he opens a door that has a goat behind it.     Door opened_door = random_door_not(first_choice_door, car_behind_door);     assert(   (opened_door != first_choice_door)            && (opened_door != car_behind_door));     return opened_door; } Door random_door_not(Door a_door, Door another_door)     Door door;     // If doors a_door and another_door are the same, then randomly     // choose between the other two doors to return.     // Otherwise, return the third door.     do {         door = random_door();     } while ((door == a_door) || (door == another_door));     return door;

10 Assignment #2: Sample Solution, cont’d
Door make_second_choice(Door first_door, Door opened_door) {     // The player's second door choice can't be     // the first door choice or the opened door.     Door second_choice = choose_remaining_door(first_door, opened_door);     assert(   (second_choice != first_door)            && (second_choice != opened_door));     return second_choice; } Door choose_remaining_door(Door first_door, Door opened_door)     // Check door 1 and door 2.     for (Door door = 1; door <= 2; door++)     {         if ((door != first_door) && (door != opened_door)) return door;     }     return 3;  // if not door 1 or door 2

11 Pointers Pointers are an extremely powerful feature of C and C++ programs. You cannot be a competent C or C++ programmer if you do not know how to use pointers effectively. Pointers can also be extremely dangerous. Many runtime errors and program crashes are due to misbehaving pointers. Pointers are a prime cause of memory errors.

12 An int vs. Pointer to an int
A graphical representation of an int variable named num and its value: A graphical representation of a pointer variable named ptr that points to an int value of a variable named num: 5 num 5 ptr num

13 Declaring and Assigning Pointers
After the following statements are executed: We have this situation: int num = 5; int *ptr = &num; 5 ptr num

14 Pointers are Addresses
To declare that a variable is a pointer, use a * before the variable name: ptr can point to an int value ptr2 can point to a double value The statement assigns the address of variable num to pointer variable ptr Make ptr point to the address of variable num. int *ptr; double *ptr2; & is the address-of operator ptr = &num;

15 The Dereferencing Operator
5 ptr num int num = 5; int *ptr = &num; To get the value that pointer ptr is pointing to: Now the * is the dereferencing operator. “Follow the pointer to get what it’s pointing to.” We can use *ptr in an expression. Example: *ptr + 2 gives the value 7. *ptr

16 The Dereferencing Operator, cont’d
5 ptr num int num = 5; int *ptr = &num; In the above example, both *ptr and num refer to the same value 5. What happens if we execute the statement? Now both num and *ptr are 9. *ptr = 9; 9 ptr num

17 A Pointer Declaration Warning
You can declare several pointer variables in one line: How many pointer variables do we have? Only ptr1 is a pointer to a double value. ptr2 and ptr3 are simple double variables. double *ptr1, *ptr2, *ptr3; double* ptr1, ptr2, ptr3;

18 The new Operator So far, all our variables have names and are created automatically when we declare them: We can also create nameless variables. The new operator returns a pointer to the variable it just created. This is ideal for pointer variables. int num; int *ptr = new int(42); 42 ptr a nameless variable

19 The delete Operator If your program creates nameless variables, then it must remove them from memory when the program no longer needs them. Delete from memory the nameless variable that ptr points to. If your program doesn’t get rid of all the nameless variables it created, those variables clutter up memory, and therefore you are said to have a memory leak. delete ptr;

20 Pointer Parameters We can pass a pointer by value to a function:
We can change the value of the variable that ptr1 points to. We can also pass a pointer by reference: We can change what variable ptr1 points to. Ugly syntax! void foo(int *ptr1, double *ptr2); void bar(int* &ptr1, double* &ptr2);

21 typedef Use typedefs to simplify pointer notation:
Now you can use IntPtr in place of int * and DoublePtr in place of double * typedef int *IntPtr; typedef double *DoublePtr; void foo(IntPtr ptr1, DoublePtr ptr2); void bar(IntPtr& ptr1, DoublePtr& ptr2);

22 Using Pointers to Pass-by-Reference
C programmers used pointers to pass parameters by reference. Example: A call to the function needed the address of the corresponding argument: Because parm points back to the actual argument, the function can use *parm to change the value of the actual argument. function baz(int *parm); int arg; baz(&arg);

23 Assignment #3. War and Peace
This assignment uses strings and vectors. Review last week’s practice problems. Write a program to read the text of the long Russian novel War and Peace. Search for several names of characters: Makar Alexeevich Joseph Bazdeev Boris Drubetskoy Tricky: A name can be split across two consecutive lines.

24 Assignment #3. War and Peace, cont’d
Learn to use the online C++ reference: For vectors: For strings: Use vector and string functions. Do not use arrays or C-strings.


Download ppt "CS 144 Advanced C++ Programming February 12 Class Meeting"

Similar presentations


Ads by Google