Download presentation
Presentation is loading. Please wait.
1
CSC 237 - Data Structures, Fall, 2008
Monday, September 8, end of week 2 C++ pointers and tail recursion
2
C and C++ pointers whole_array_size is the overall array size
~parson/DataStructures/recursion_assignment1/pointer_parson_example_code whole_array_size is the overall array size int *whole_array = new int [ whole_array_size ] ; whole_array can now be subscripted as an array for (int i = 0 ; i < whole_array_size ; i++) { whole_array[i] = i ; } “whole_array[i]” means the same as *(wholearray+i) Adding an integer N to a pointer to an array element moves the pointer “to the right” N elements.
3
C++ Pointer Arithmetic
int *end_whole_array = whole_array + whole_array_size ; Suppose whole_array_size is set to 19, and sub_array_size is set to 3. whole_array end_whole_array for (sub_array = whole_array ; sub_array < end_whole_array ; sub_array += sub_array_size) { At each step sub_array defines a conceptual “sub-array” with 3 elements: sub_array[0], sub_array[1] and sub_array[2]
4
Pointer Arithmetic continued
whole_array end_whole_array sub_array_size = for (sub_array = whole_array ; sub_array < end_whole_array ; sub_array += sub_array_size) { int current_sub_array_size ; if ((sub_array + sub_array_size) <= end_whole_array) { current_sub_array_size = sub_array_size ; } else { current_sub_array_size = end_whole_array - sub_array ; // pointer “-” } visit(current_sub_array_size, sub_array); }
5
More Pointer Arithmetic
“pointer_a – pointer_b” gives the number of elements between the two pointers. They must be the same type, for example int *. If pointer_a == pointer_b, their difference is 0. If pointer_a < pointer_b (precedes it in memory), the difference is a negative integer. Pointer arithmetic always uses the number of elements in storage, not the number of bytes in storage. “pointer_a + 3” points to higher memory, “pointer_a – 3” to lower memory. You cannot add pointers!
6
Printing a sub-array visit(current_sub_array_size, sub_array);
static void visit(int count, int iarray[]) { for (int i = 0 ; i < count ; i++) { cout << "[" << i << "] == " << iarray[i] << "\t" ; } cout << endl ; sub_array appears to be an entire, small array, starting at subscript 0.
7
Tail recursive sum() of an array
Directory recursive_parson_barebones_code/ int result = sum(argc-1, intarray); cout << "sum = " << result << endl ; static int sum(int count, int iarray[]) { if (count == 1) { return iarray[0] ; } else { return iarray[0] + sum(count-1, iarray+1); // iarray+1 !!! }
8
Recursive bisection Do something to the left half of an array, and to the right half, then combine the 2 results. Apply this strategy, recursively, to ever smaller arrays, until you reach a base case or cases. sum sum sum sum sum sum sum sum sum sum sum sum sum sum
9
Recursive Bisection Requirements
An even number of elements divides evenly in half. An odd number of elements divides into an odd and even half that differ in count by 1. Bisected portions must never overlap. Never step off the bottom or top of the big array or the subarrays. Make sure to handle all possible base cases.
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.