SlideServe
Menu
Préférences
© SlideServe · v2026

CPSC 221 Basic Algorithms and Data Structures

wallaceg avatar
30 diapositives · 361 Views · Jan 05, 2020

Explore the concepts of call by value, pointer, and reference in C++ through code examples. Learn what gets printed on the screen after running specific functions.

CPSC 221 Basic Algorithms and Data Structures - slide 1
01 / 30CPSC 221 Basic Algorithms and Data Structures
1 / 17

Télécharger la présentation

CPSC 221 Basic Algorithms and Data Structures
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  1. CPSC 221Basic Algorithms and Data Structures Call by value, pointer, and reference in C++Stealing some of the examples from Alan

  2. Clicker Question • What would be printed to the screen after running the following code? void swap1(int x, int y){ intt = x; x=y; y=t; } intmain(){ int a=0; int b=1; swap1(a,b); std::cout << a << "," << b; } A: 0,1 B: 1,0 C: Does not compile D: I don’t know E: None of the above

  3. Call By Value • What would be printed to the screen after running the following code? swap1 t y=1 x=0 swap1 t=0 y=0 x=1 void swap1(int x, int y){ intt = x; x=y; y=t; } intmain(){ int a=0; int b=1; swap1(a,b); std::cout << a << "," << b; } 600 560 500 main b=1 a=0 main b=1 a=0 240 200 A: 0,1

  4. Call By Value swap1(int x, int y) main() a=0 t t=0 int t = x 600 240 b=1 x=0 x=1 200 560 x = y y=1 y=0 y = t 540 a = 0, b = 1

  5. Clicker Question • What would be printed to the screen after running the following code? void swap2(int* x, int* y) { int* t = x; x = y; y = t; } intmain(){ int a=0; int b=1; swap2(a,b); std::cout << a << "," << b; } A: 0,1 B: 1,0 C: Does not compile D: I don’t know E: None of the above

  6. Attempting to Use Pointers • What would be printed to the screen after running the following code? void swap2(int* x, int* y) { int* t = x; x = y; y = t; } intmain(){ int a=0; int b=1; swap2(a,b); std::cout << a << "," << b; } C: Does not compile

  7. Clicker Question • What would be printed to the screen after running the following code? void swap2(int* x, int* y) { int* t = x; x = y; y = t; } intmain(){ int a=0; int b=1; swap2(&a,&b); std::cout << a << "," << b; } A: 0,1 B: 1,0 C: Does not compile D: I don’t know E: None of the above

  8. Attempting to Use Pointers 2 • What would be printed to the screen after running the following code? swap2 *t *y=240 *x=200 swap2 *t=200 *y=200 *x=240 void swap2(int* x, int* y) { int* t = x; x = y; y = t; } intmain(){ int a=0; int b=1; swap2(&a,&b); std::cout << a << "," << b; } 600 560 500 main b=1 a=0 main b=1 a=0 240 200 A: 0,1

  9. Attempting to Use Pointers 2 swap2(int* x, int* y) main() a=0 t t=240 int* t = x 600 240 b=1 x=240 x=200 200 560 x = y swap2(&a, &b) y=240 y=200 y = t 540 a = 0, b = 1

  10. Clicker Question • What would be printed to the screen after running the following code? void swap3(int* x, int* y) { intt = *x; *x = *y; *y = t; } int main(){ int a=0; int b=1; swap3(&a,&b); std::cout << a << "," << b; } A: 0,1 B: 1,0 C: Does not compile D: I don’t know E: None of the above

  11. Using Pointers • What would be printed to the screen after running the following code? swap3 t *y=240 *x=200 swap3 t=0 *y=240 *x=200 void swap3(int* x, int* y) { intt = *x; *x = *y; *y = t; } int main(){ int a=0; int b=1; swap3(&a,&b); std::cout << a << "," << b; } 600 560 500 main b=1 a=0 main b=0 a=1 240 200 B: 1,0

  12. Using Pointers swap3(int* x, int* y) main() a=0 a=1 t t=0 int t = *x 600 240 b=0 b=1 x=240 200 560 *x = *y swap3(&a, &b) y=200 *y = t 540 a =1, b = 0

  13. Clicker Question • What would be printed to the screen after running the following code? void swap4(int &x, int &y){ intt = x; x=y; y=t; } intmain(){ int a=0; int b=1; swap4(&a,&b); std::cout << a << "," << b; } A: 0,1 B: 1,0 C: Does not compile D: I don’t know E: None of the above

  14. Attempting to Use References • What would be printed to the screen after running the following code? void swap4(int &x, int &y){ intt = x; x=y; y=t; } intmain(){ int a=0; int b=1; swap4(&a,&b); std::cout << a << "," << b; } C: Does not compile

  15. Clicker Question • What would be printed to the screen after running the following code? void swap4(int &x, int &y){ intt = x; x=y; y=t; } intmain(){ int a=0; int b=1; swap4(a,b); std::cout << a << "," << b; } A: 0,1 B: 1,0 C: Does not compile D: I don’t know E: None of the above

  16. Using References • What would be printed to the screen after running the following code? swap4 t &y=1 &x=0 swap4 t=0 &y=0 &x=1 void swap4(int &x, int &y){ intt = x; x=y; y=t; } intmain(){ int a=0; int b=1; swap4(a,b); std::cout << a << "," << b; } 600 560 500 main b=1 a=0 main b=0 a=1 240 200 & in a formal parameter makes the parameter another name for the argument that was passed in! B: 1,0

  17. Using References swap4(int &x, int &y) main() a=0 a=1 t t=0 int t = x 600 240 b=0 b=1 x=1 x=0 200 560 x = y swap4(a, b) y=0 y=1 y = t 540 a =1, b = 0

  18. Clicker Question • What would be printed to the screen after running the following code? void swap5(int* x, int* y){ int* t = x; x=y; y=t; } intmain(){ int* a=newint(0); int* b= newint(1); swap5(a,b); std::cout << *a << "," << *b; } A: 0,1 B: 1,0 C: Does not compile D: I don’t know E: None of the above

  19. Attempting to use pointers • What would be printed to the screen after running the following code? 950 950 1 1 void swap5(int *x, int* y){ int* t = x; x=y; y=t; } intmain(){ int* a=newint(0); int* b= newint(1); swap5(a,b); std::cout << *a << "," << *b; } 900 900 0 0 swap5 t *y=950 *x=900 swap5 t=900 *y=900 *x=950 600 600 560 560 500 500 main *b=950 *a=900 main *b=950 *a=900 240 240 200 200 A: 0,1

  20. Attempting to Use Pointers swap5(int* x, int* y) main() a=900 t t=900 int* t = x 600 240 b=950 x=900 x=950 200 560 x = y swap5(a, b) y=900 y=950 y = t 540 0 900 a = 0, b = 1 1 950

  21. Why use References? • Can we do everything without the use of references with just using pointers? • Yes • Then why use references? • Because using pointers might sometimes look more complicated. • Although you wouldn’t directly be tested on parameter passing, you might get questions on midterm and final that use either pointers or references so you should understand both.

  22. Would the following code successfully add a new node to the linked list? struct Node { int key; Node* next; }; void insert1( Node* head, int key) { Node * curr = newNode; curr->key = key; curr->next = head; head = curr; } int main(){ Node* list1 = NULL; insert1( list1, 1); std::cout << list1->key; return 0; } A: Yes B: No C: I don’t know

  23. B: No insert1 head=Null key=1 560 • Would the following code successfully add a new node to the linked list? struct Node { int key; Node* next; }; void insert1( Node* head, int key) { Node * curr = newNode; curr->key = key; curr->next = head; head = curr; } int main(){ Node* list1 = NULL; insert1( list1, 1); std::cout << list1->key; return 0; } 500 main list1=Null key =1 next = Null 200 950 insert1 curr=950 head=950 key=1 560 500 main list1=Null 200

  24. Attempting to Use Pointers insert1(Node* head, int key) main() head = 950 head = NULL 560 list1 = NULL 200 curr = new Node insert1(list1, 1) curr = 950 600 curr->key = key curr->next = head key next head = curr key = 1 next key = 1 next = NULL 950

  25. Would the following code successfully add a new node to the linked list? struct Node { int key; Node* next; }; void insert2( Node** head, int key) { Node * curr = newNode; curr->key = key; curr->next = *head; *head = curr; } int main(){ Node* list1 = NULL; insert2( &list1, 1); std::cout << list1->key; return 0; } A: Yes B: No C: I don’t know

  26. A: Yes insert2 head=200 key=1 560 • Would the following code successfully add a new node to the linked list? struct Node { int key; Node* next; }; void insert2( Node** head, int key) { Node * curr = newNode; curr->key = key; curr->next = *head; *head = curr; } int main(){ Node* list1 = NULL; insert2( &list1, 1); std::cout << list1->key; return 0; } 500 main list1=Null key =1 next = Null 200 950 insert2 curr=950 head=200 key=1 560 500 main list1=950 200

  27. Using Pointers insert1(Node** head, int key) main() head = 200 560 list1 = NULL list1 = 950 200 insert1(&list1, 1) curr = 950 600 *curr = new Node curr->key = key key next key = 1 next key = 1 next = NULL curr->next = *head 950 *head = curr

  28. Would the following code successfully add a new node to the linked list? struct Node { int key; Node* next; }; void insert3( Node*& head, int key) { Node* curr = newNode; curr->key = key; curr->next = head; head = curr; } int main(){ Node* list1 = NULL; insert3( list1, 1); std::cout << list1->key; return 0; } A: Yes B: No C: I don’t know

  29. A: Yes insert3 &head=Null key=1 560 • Would the following code successfully add a new node to the linked list? struct Node { int key; Node* next; }; void insert3( Node*& head, int key) { Node* curr = newNode; curr->key = key; curr->next = head; head = curr; } int main(){ Node* list1 = NULL; insert3( list1, 1); std::cout << list1->key; return 0; } 500 main list1=Null key =1 next = Null 200 950 insert3 curr=950 &head=950 key=1 560 500 main list1=950 200

  30. Using References insert3(Node*& head, int key) main() head = NULL head = 950 560 list1 = NULL list1 = 950 200 insert3(list1, 1) curr = 950 600 *curr = new Node curr->key = key key next key = 1 next key = 1 next = NULL curr->next = head 950 *head = curr