ECE250: Algorithms and Data Structures C++ Language Tutorial Ladan Tahvildari, PEng Associate Professor Software Technologies Applied Research (STAR) Group Dept. of Elect. & Comp. Eng. University of Waterloo Tiuley Alguindigue Lab Instructor. M. Eng. University of Waterloo.
Tutorial Overview Part 1 v Vector Class Exercise in Quiz v Compiling and Executing in the ECEUNIX server Ø Accessing ECE UNIX server Ø UNIX some basic commands ECE250 2
Tutorial Part 2 v Pointers v Pointers and Arrays v Dynamic Allocation v ECE250 Projects Structure and Testing ECE250 3
Question 1a - on Pointers #include <iostream> using namespace std; int main() { int a_number = 10 ; // declaring a pointer int *address_of_a_number; 0x6ffe44 int *address_of_another_number; // Changing the value of number // & - address off or reference operator address_of_a_number = &a_number ; // * dereference operator *address_of_a_number = 20 ; 10 } cout << a_number << endl; cout << address_of_a_number << endl ECE250 4
Answer to Q1a - on Pointers 20 0x6ffe44 ECE250 5
Question 1b - Pointer Arithmethics #include <iostream> using namespace std; int main() { int a_number = 10 ; int *address_of_a_number; int *address_of_another_number; 0x6ffe44 10 address_of_a_number = &a_number ; // changing the address - adding 1 address_of_a_number = address_of_a_number + 1; cout << address_of_a_number << endl; } ECE250 6
Answer to Q1b - Pointer Arithmethics 0x6ffe48 0x6ffe44 + 4, 4 is the length of an integer ECE250 7
More on C++ Pointers http://www.cplusplus.com/doc/tutorial/pointers/ ECE250 8
Question 2a - Arrays and Pointers int numbers[5]= {0,10,20,30,40}; int *ptr_numbers = numbers ; for( int i = 0; i < 5; i++){ cout << *(ptr_numbers + i) << endl; } for( int i = 0; i < 5; i++){ cout << numbers[i] << endl; } What is the output of this program? ECE250 9
Answer Q2a - Arrays and Pointers What is the output of this program? 0 10 20 30 40 0 10 20 30 40 ECE250 10
Question 2b - Arrays and Pointers int numbers[5]= {0,10,20,30,40}; int *ptr_numbers = numbers ; // Assigning values to the elements of the array two ways numbers[2] = 2000; *(ptr_numbers + 4)= 4000; for( int i = 0; i < 5; i++){ cout << numbers[i] << endl; } What is the output of this program? ECE250 11
Answer Q2b - Arrays and Pointers What is the output of this program? 0 10 2000 30 4000 ECE250 12
Static vs Dynamic Memory // statically allocated (compilation time) int sa_number = 200; // dynamically allocated (run time) int *ptr_number; ptr_number = new int(200);.. do something with *ptr_number.. delete ptr_number; ECE250 13
Arrays - Static vs Dynamic Memory // statically allocated (compilation time) int sa_number = 200; // dynamically allocated (run time) int *ptr_numbers = new int[5]; delete [] ptr_number; ECE250 14
Q3 - Arrays and Dynamic Memory Allocation // dynamically allocated (run time) int *ptr_numbers = new int[5]; // Write the code to assign values to // elements in the array delete [] ptr_number; ECE250 15
Answer to Q3 - Arrays and Dynamic Memory Allocation ptr_numbers[0] = 50; ptr_numbers[1] = 10; ptr_numbers[2] = 20; ptr_numbers[3] = 30; ptr_numbers[4] = 40; ECE250 16
ECE250 Projects v Project 1 is the implementation of some variation of a stack or queue data structure. v You will be writing the implementation of the class, ie. For a Dequeue (Double ended Queue) you will write the class DequeAsArray.h ECE250 17
ECE250 Projects v For the Deque example, we will provide you with: Ø a driver (main() invoking test files) Ø tester files (Tester.h and DequeAsArrayTester.h), Ø exceptions (Exception.h) Ø definitions and some utilities (ece250.h) ECE250 18
ECE250 Projects v You will be able to enter commands at the prompt that will allow you to test each method in your program. You can save a series of commands in a text file and repeat your testing as needed (or share with others! this is allowed). ECE250 19
ECE250 Projects Example of test file: new enqueue_tail 10 enqueue_tail 20 enqueue_tail 30 size 3 head 10 tail 30 dequeue_head 10 size 2 empty 0 head 20 tail 30 delete summary ECE250 20
ECE250 Projects If everything is working, you will see OK for each line, otherwise you will see an error: Starting Test Run 1 % new Okay 2 % enqueue_tail 10 Okay 3 % enqueue_tail 20 Okay 4 % enqueue_tail 30 Okay 5 % size 3 Okay 6 % head 10 Failure in head(): expecting the value '10' but got '0' 7 % tail 30 Failure in tail(): expecting the value '30' but got '2' 8 % dequeue_head 10 Okay ECE250 21
ECE250 Projects v Your IDE Project must contain Ø Your solution file ie. DequeueAsArray.h Ø Driver file Ø Tester files (2) Ø Exeception.h Ø ece250.h v Use this list as a reference when defining all your projects in your IDE. ECE250 22
Tutorial 2 Summary v Pointers v Pointers and Arrays v Dynamic Allocation v ECE250 Projects Structure and Testing ECE250 23