Welcome to CISC220H Data Structures in C++ sakai.udel.edu Office Hours: Mon/Wed 3:30PM - 4:30PM TA:...
-
Upload
shannon-peters -
Category
Documents
-
view
212 -
download
0
Transcript of Welcome to CISC220H Data Structures in C++ sakai.udel.edu Office Hours: Mon/Wed 3:30PM - 4:30PM TA:...
![Page 1: Welcome to CISC220H Data Structures in C++ sakai.udel.edu Office Hours: Mon/Wed 3:30PM - 4:30PM TA: Adnan Ozsoy.](https://reader035.fdocuments.in/reader035/viewer/2022081603/5697bfe31a28abf838cb4e54/html5/thumbnails/1.jpg)
Welcome to CISC220H
• Data Structures in C++
• sakai.udel.edu
• Office Hours: Mon/Wed 3:30PM - 4:30PM
• http://www.udel.edu/CIS/220/jatlas/09FH/
• TA: Adnan Ozsoy– ozsoy at cis.udel.edu
![Page 2: Welcome to CISC220H Data Structures in C++ sakai.udel.edu Office Hours: Mon/Wed 3:30PM - 4:30PM TA: Adnan Ozsoy.](https://reader035.fdocuments.in/reader035/viewer/2022081603/5697bfe31a28abf838cb4e54/html5/thumbnails/2.jpg)
Who am I?
![Page 3: Welcome to CISC220H Data Structures in C++ sakai.udel.edu Office Hours: Mon/Wed 3:30PM - 4:30PM TA: Adnan Ozsoy.](https://reader035.fdocuments.in/reader035/viewer/2022081603/5697bfe31a28abf838cb4e54/html5/thumbnails/3.jpg)
Who are you?
![Page 4: Welcome to CISC220H Data Structures in C++ sakai.udel.edu Office Hours: Mon/Wed 3:30PM - 4:30PM TA: Adnan Ozsoy.](https://reader035.fdocuments.in/reader035/viewer/2022081603/5697bfe31a28abf838cb4e54/html5/thumbnails/4.jpg)
What will we learn?
• Pointers• Lists• Analysis/
Big O notation• Stacks• Queues• Trees
• Sorting• Hashing• Heaps• Graphs• NP-completeness
![Page 5: Welcome to CISC220H Data Structures in C++ sakai.udel.edu Office Hours: Mon/Wed 3:30PM - 4:30PM TA: Adnan Ozsoy.](https://reader035.fdocuments.in/reader035/viewer/2022081603/5697bfe31a28abf838cb4e54/html5/thumbnails/5.jpg)
How will we learn?
• Class exercises (5%)
• Homework problems/programming (5*5%)
• Three programming projects (30%)
• Three tests (two midterm * 10%, final 20%)
![Page 6: Welcome to CISC220H Data Structures in C++ sakai.udel.edu Office Hours: Mon/Wed 3:30PM - 4:30PM TA: Adnan Ozsoy.](https://reader035.fdocuments.in/reader035/viewer/2022081603/5697bfe31a28abf838cb4e54/html5/thumbnails/6.jpg)
What is “Honors”
• Labs– Normal lab part is optional (but required
knowledge)
• Projects– Pick a “fun” area at the beginning
• Class– Required discussion and participation– Cover same topics but in more depth
![Page 7: Welcome to CISC220H Data Structures in C++ sakai.udel.edu Office Hours: Mon/Wed 3:30PM - 4:30PM TA: Adnan Ozsoy.](https://reader035.fdocuments.in/reader035/viewer/2022081603/5697bfe31a28abf838cb4e54/html5/thumbnails/7.jpg)
What is a Data Structure?
![Page 8: Welcome to CISC220H Data Structures in C++ sakai.udel.edu Office Hours: Mon/Wed 3:30PM - 4:30PM TA: Adnan Ozsoy.](https://reader035.fdocuments.in/reader035/viewer/2022081603/5697bfe31a28abf838cb4e54/html5/thumbnails/8.jpg)
![Page 9: Welcome to CISC220H Data Structures in C++ sakai.udel.edu Office Hours: Mon/Wed 3:30PM - 4:30PM TA: Adnan Ozsoy.](https://reader035.fdocuments.in/reader035/viewer/2022081603/5697bfe31a28abf838cb4e54/html5/thumbnails/9.jpg)
![Page 10: Welcome to CISC220H Data Structures in C++ sakai.udel.edu Office Hours: Mon/Wed 3:30PM - 4:30PM TA: Adnan Ozsoy.](https://reader035.fdocuments.in/reader035/viewer/2022081603/5697bfe31a28abf838cb4e54/html5/thumbnails/10.jpg)
![Page 11: Welcome to CISC220H Data Structures in C++ sakai.udel.edu Office Hours: Mon/Wed 3:30PM - 4:30PM TA: Adnan Ozsoy.](https://reader035.fdocuments.in/reader035/viewer/2022081603/5697bfe31a28abf838cb4e54/html5/thumbnails/11.jpg)
Objectives for Today
• Manipulate pointers– read/write– create– dereference– assign value
• Call functions– by value, by reference
• Reading - K+W P.1-P.6
![Page 12: Welcome to CISC220H Data Structures in C++ sakai.udel.edu Office Hours: Mon/Wed 3:30PM - 4:30PM TA: Adnan Ozsoy.](https://reader035.fdocuments.in/reader035/viewer/2022081603/5697bfe31a28abf838cb4e54/html5/thumbnails/12.jpg)
Review of pointers & memory
cf. K&W, Chapter P
A C++ Primer
section P.5
![Page 13: Welcome to CISC220H Data Structures in C++ sakai.udel.edu Office Hours: Mon/Wed 3:30PM - 4:30PM TA: Adnan Ozsoy.](https://reader035.fdocuments.in/reader035/viewer/2022081603/5697bfe31a28abf838cb4e54/html5/thumbnails/13.jpg)
Smith Hall • Newark, DE 19716 • USA
![Page 14: Welcome to CISC220H Data Structures in C++ sakai.udel.edu Office Hours: Mon/Wed 3:30PM - 4:30PM TA: Adnan Ozsoy.](https://reader035.fdocuments.in/reader035/viewer/2022081603/5697bfe31a28abf838cb4e54/html5/thumbnails/14.jpg)
![Page 15: Welcome to CISC220H Data Structures in C++ sakai.udel.edu Office Hours: Mon/Wed 3:30PM - 4:30PM TA: Adnan Ozsoy.](https://reader035.fdocuments.in/reader035/viewer/2022081603/5697bfe31a28abf838cb4e54/html5/thumbnails/15.jpg)
• How could we move the Department of Computer and Information Sciences?
![Page 16: Welcome to CISC220H Data Structures in C++ sakai.udel.edu Office Hours: Mon/Wed 3:30PM - 4:30PM TA: Adnan Ozsoy.](https://reader035.fdocuments.in/reader035/viewer/2022081603/5697bfe31a28abf838cb4e54/html5/thumbnails/16.jpg)
Memory Address vs. Value Stored• Consider memory to be a single huge array:
– Each cell of the array has an address associated with it.
– Each cell also stores some value.
• Don’t confuse the address referring to a memory location with the value stored in that location.
23 42 ... ...101 102 103 104 105 ...
![Page 17: Welcome to CISC220H Data Structures in C++ sakai.udel.edu Office Hours: Mon/Wed 3:30PM - 4:30PM TA: Adnan Ozsoy.](https://reader035.fdocuments.in/reader035/viewer/2022081603/5697bfe31a28abf838cb4e54/html5/thumbnails/17.jpg)
Pointers
• An address refers to a particular memory location. In other words, it points to a memory location.
• Pointer: A variable that contains the address of a variable.
z23 42 ... ...
101 102 103 104 105 ...
x y
Location (address)
name
104
![Page 18: Welcome to CISC220H Data Structures in C++ sakai.udel.edu Office Hours: Mon/Wed 3:30PM - 4:30PM TA: Adnan Ozsoy.](https://reader035.fdocuments.in/reader035/viewer/2022081603/5697bfe31a28abf838cb4e54/html5/thumbnails/18.jpg)
Pointers• How to initialize a pointer:
– can initialize to NULL (i.e. not currently pointing to anything)
– & operator: get address of a variable
int *x;
x ? y ?
int y = 3; x ? y 3
x = &y; x y 3
![Page 19: Welcome to CISC220H Data Structures in C++ sakai.udel.edu Office Hours: Mon/Wed 3:30PM - 4:30PM TA: Adnan Ozsoy.](https://reader035.fdocuments.in/reader035/viewer/2022081603/5697bfe31a28abf838cb4e54/html5/thumbnails/19.jpg)
Pointers• How to get the value that is pointed to?
– * “dereference operator”: get value pointed to• * x returns 3
• How to change the variable pointed to?– Use dereference * operator to left of =
x y 5
*x = 5;
x y 3
![Page 20: Welcome to CISC220H Data Structures in C++ sakai.udel.edu Office Hours: Mon/Wed 3:30PM - 4:30PM TA: Adnan Ozsoy.](https://reader035.fdocuments.in/reader035/viewer/2022081603/5697bfe31a28abf838cb4e54/html5/thumbnails/20.jpg)
Functions
fx f(x)
![Page 21: Welcome to CISC220H Data Structures in C++ sakai.udel.edu Office Hours: Mon/Wed 3:30PM - 4:30PM TA: Adnan Ozsoy.](https://reader035.fdocuments.in/reader035/viewer/2022081603/5697bfe31a28abf838cb4e54/html5/thumbnails/21.jpg)
Call-by-value
int sum(int x, int y) {
return x + y;
}
int main(void) {
cout << sum(5,6) << endl;
}
![Page 22: Welcome to CISC220H Data Structures in C++ sakai.udel.edu Office Hours: Mon/Wed 3:30PM - 4:30PM TA: Adnan Ozsoy.](https://reader035.fdocuments.in/reader035/viewer/2022081603/5697bfe31a28abf838cb4e54/html5/thumbnails/22.jpg)
Call-by-reference
int sum(const int& x, const int& y) {
return x + y;
}
int main(void) {
cout << sum(5,6) << endl;
}
![Page 23: Welcome to CISC220H Data Structures in C++ sakai.udel.edu Office Hours: Mon/Wed 3:30PM - 4:30PM TA: Adnan Ozsoy.](https://reader035.fdocuments.in/reader035/viewer/2022081603/5697bfe31a28abf838cb4e54/html5/thumbnails/23.jpg)
Group Review
• Break into teams of 4-5 students
• One person write an official answer
• Swap answers with a neighboring group and keep scores!
![Page 24: Welcome to CISC220H Data Structures in C++ sakai.udel.edu Office Hours: Mon/Wed 3:30PM - 4:30PM TA: Adnan Ozsoy.](https://reader035.fdocuments.in/reader035/viewer/2022081603/5697bfe31a28abf838cb4e54/html5/thumbnails/24.jpg)
Question 1
• What is output from the following code:
double x = 5.5;
double *px = &x;
cout << *px << ‘\n’;
*px = 10.0;
cout << x << ‘\n’;
![Page 25: Welcome to CISC220H Data Structures in C++ sakai.udel.edu Office Hours: Mon/Wed 3:30PM - 4:30PM TA: Adnan Ozsoy.](https://reader035.fdocuments.in/reader035/viewer/2022081603/5697bfe31a28abf838cb4e54/html5/thumbnails/25.jpg)
Question 2
• What is output from the following code:
double x = 5.5;
double y = 10.0;
double* px, py;
px = &x;
py = &y;
cout << *px << ‘\n’ << *py << ‘\n’;
![Page 26: Welcome to CISC220H Data Structures in C++ sakai.udel.edu Office Hours: Mon/Wed 3:30PM - 4:30PM TA: Adnan Ozsoy.](https://reader035.fdocuments.in/reader035/viewer/2022081603/5697bfe31a28abf838cb4e54/html5/thumbnails/26.jpg)
Question 3
• What is output from the following code:
double x = 5.5;
double *px = &x;
*px = 3.14;
double& r = *px;
r = 99.44;
cout << x << ‘\n’;
![Page 27: Welcome to CISC220H Data Structures in C++ sakai.udel.edu Office Hours: Mon/Wed 3:30PM - 4:30PM TA: Adnan Ozsoy.](https://reader035.fdocuments.in/reader035/viewer/2022081603/5697bfe31a28abf838cb4e54/html5/thumbnails/27.jpg)
Question 4
• What is output from the following code:void swap(int x, int y) {
int temp = x;
x = y;
y = temp;
}
int main(void) {
int a = 0;
int b = 5;
swap(a,b);
cout << a << endl;
}
![Page 28: Welcome to CISC220H Data Structures in C++ sakai.udel.edu Office Hours: Mon/Wed 3:30PM - 4:30PM TA: Adnan Ozsoy.](https://reader035.fdocuments.in/reader035/viewer/2022081603/5697bfe31a28abf838cb4e54/html5/thumbnails/28.jpg)
Question 5
• Change the code to work correctly using pointers:void swap(int x, int y) {
int temp = x;
x = y;
y = temp;
}
int main(void) {
int a = 0;
int b = 5;
swap(a,b);
cout << a << endl;
}
![Page 29: Welcome to CISC220H Data Structures in C++ sakai.udel.edu Office Hours: Mon/Wed 3:30PM - 4:30PM TA: Adnan Ozsoy.](https://reader035.fdocuments.in/reader035/viewer/2022081603/5697bfe31a28abf838cb4e54/html5/thumbnails/29.jpg)
Question 6
• What is the value of temp after each assignment?char blocks[3] = {'A','B','C'};
char *ptr = &blocks[0];
char temp;
/*1*/ temp = blocks[0];
/*2*/ temp = *(blocks + 2);
/*3*/ temp = *(ptr + 1);
ptr = blocks + 1;
/*4*/ temp = *ptr;
/*5*/ temp = *(ptr + 1);
![Page 30: Welcome to CISC220H Data Structures in C++ sakai.udel.edu Office Hours: Mon/Wed 3:30PM - 4:30PM TA: Adnan Ozsoy.](https://reader035.fdocuments.in/reader035/viewer/2022081603/5697bfe31a28abf838cb4e54/html5/thumbnails/30.jpg)
Question 7
• What is the value of temp after each assignment?char blocks[3] = {'A','B','C'};
char *ptr = blocks;
char temp;
/*1*/ temp = *++ptr;
/*2*/ temp = ++*ptr;
/*3*/ temp = *ptr++;
/*4*/ temp = *ptr;
![Page 31: Welcome to CISC220H Data Structures in C++ sakai.udel.edu Office Hours: Mon/Wed 3:30PM - 4:30PM TA: Adnan Ozsoy.](https://reader035.fdocuments.in/reader035/viewer/2022081603/5697bfe31a28abf838cb4e54/html5/thumbnails/31.jpg)
Question 8• Write code to reverse a string using only pointer arithmetic
(no array accessors):int main(void) {
char s[10] = "abcde";
for (int i = 0; s[i] != NULL; i++) {
cout << s[i];
}
revtString(s);
for (int i = 0; s[i] != NULL; i++) {
cout << s[i];
}
cout << ‘\n’;
return 0;
}