1 of 104

UPE Tutoring:

CS 31 Midterm 2 Review

Sign-in http://bit.ly/2QCmwRH

Slides link available upon sign-in

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH

2 of 104

Arrays

  • Valid declarations:

int arr[10];

bool list[5];

const int MAX_SIZE = 10;

string words[MAX_SIZE];

int arr[] = {1, 2, 3};

2

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH

3 of 104

Arrays (cont.)

  • Rules for specifying size:
    • Must be included in the brackets
    • Cannot involve a variable unless it is a constant known at compile time
    • The only time size can be left out is when a list of its contents is included
  • Not allowed in C++:
    • int arr[]; // Size not included.
    • /****** Use of non-const variable. ******/

int x;

cin >> x;

char buffer[x];

3

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH

4 of 104

Passing Arrays to Functions

  • Parameter Syntax
    • (..., type name[], ...)
  • Arrays are default passed by reference
    • Any changes made to the array will be retained outside of the function scope

4

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH

5 of 104

Passing Arrays to Functions (cont.)

  • Size of array should be passed to the function
  • Call to the function just passes in array name

// arr is the array itself, n is the size.

int firstOdd(int arr[], int n) {

for (int i = 0; i < n; i++) {

if (arr[i] % 2 == 1)

return i;

}

return n; // If no odd number found.

}

5

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH

6 of 104

What does this print?

// arr is the array itself, n is the size.

int changeOdd(int arr[], int n) {

int count = 0;

for (int i = 0; i < n; i++) {

if (arr[i] % 2 == 1) {

arr[i]--;

count++;

}

}

n++;

return count;

}

int main() {

int n = 5;

int arr[5] = {2, 6, 3, 5, 10};

cout << changeOdd(arr, n) << endl;

}

6

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH

7 of 104

What does this print?

// arr is the array itself, n is the size.

int changeOdd(int arr[], int n) {

int count = 0;

for (int i = 0; i < n; i++) {

if (arr[i] % 2 == 1) {

arr[i]--;

count++;

}

}

n++;

return count;

}

int main() {

int n = 5;

int arr[5] = {2, 6, 3, 5, 10};

cout << changeOdd(arr, n) << endl;

}

7

5

n

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH

8 of 104

What does this print?

// arr is the array itself, n is the size.

int changeOdd(int arr[], int n) {

int count = 0;

for (int i = 0; i < n; i++) {

if (arr[i] % 2 == 1) {

arr[i]--;

count++;

}

}

n++;

return count;

}

int main() {

int n = 5;

int arr[5] = {2, 6, 3, 5, 10};

cout << changeOdd(arr, n) << endl;

}

8

5

n

[2, 6, 3, 5, 10]

arr

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH

9 of 104

What does this print?

// arr is the array itself, n is the size.

int changeOdd(int arr[], int n) {

int count = 0;

for (int i = 0; i < n; i++) {

if (arr[i] % 2 == 1) {

arr[i]--;

count++;

}

}

n++;

return count;

}

int main() {

int n = 5;

int arr[5] = {2, 6, 3, 5, 10};

cout << changeOdd(arr, n) << endl;

}

9

5

n

0

count

5

n

[2, 6, 3, 5, 10]

arr

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH

10 of 104

What does this print?

// arr is the array itself, n is the size.

int changeOdd(int arr[], int n) {

int count = 0;

for (int i = 0; i < n; i++) {

if (arr[i] % 2 == 1) {

arr[i]--;

count++;

}

}

n++;

return count;

}

int main() {

int n = 5;

int arr[5] = {2, 6, 3, 5, 10};

cout << changeOdd(arr, n) << endl;

}

10

5

n

0

count

5

n

[2, 6, 3, 5, 10]

arr

0

i

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH

11 of 104

What does this print?

// arr is the array itself, n is the size.

int changeOdd(int arr[], int n) {

int count = 0;

for (int i = 0; i < n; i++) {

if (arr[i] % 2 == 1) {

arr[i]--;

count++;

}

}

n++;

return count;

}

int main() {

int n = 5;

int arr[5] = {2, 6, 3, 5, 10};

cout << changeOdd(arr, n) << endl;

}

11

5

n

0

count

5

n

[2, 6, 3, 5, 10]

arr

0

i

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH

12 of 104

What does this print?

// arr is the array itself, n is the size.

int changeOdd(int arr[], int n) {

int count = 0;

for (int i = 0; i < n; i++) {

if (arr[i] % 2 == 1) {

arr[i]--;

count++;

}

}

n++;

return count;

}

int main() {

int n = 5;

int arr[5] = {2, 6, 3, 5, 10};

cout << changeOdd(arr, n) << endl;

}

12

5

n

0

count

5

n

[2, 6, 3, 5, 10]

arr

1

i

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH

13 of 104

What does this print?

// arr is the array itself, n is the size.

int changeOdd(int arr[], int n) {

int count = 0;

for (int i = 0; i < n; i++) {

if (arr[i] % 2 == 1) {

arr[i]--;

count++;

}

}

n++;

return count;

}

int main() {

int n = 5;

int arr[5] = {2, 6, 3, 5, 10};

cout << changeOdd(arr, n) << endl;

}

13

5

n

0

count

5

n

[2, 6, 3, 5, 10]

arr

1

i

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH

14 of 104

What does this print?

// arr is the array itself, n is the size.

int changeOdd(int arr[], int n) {

int count = 0;

for (int i = 0; i < n; i++) {

if (arr[i] % 2 == 1) {

arr[i]--;

count++;

}

}

n++;

return count;

}

int main() {

int n = 5;

int arr[5] = {2, 6, 3, 5, 10};

cout << changeOdd(arr, n) << endl;

}

14

5

n

0

count

5

n

[2, 6, 3, 5, 10]

arr

1

i

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH

15 of 104

What does this print?

// arr is the array itself, n is the size.

int changeOdd(int arr[], int n) {

int count = 0;

for (int i = 0; i < n; i++) {

if (arr[i] % 2 == 1) {

arr[i]--;

count++;

}

}

n++;

return count;

}

int main() {

int n = 5;

int arr[5] = {2, 6, 3, 5, 10};

cout << changeOdd(arr, n) << endl;

}

15

5

n

0

count

5

n

[2, 6, 3, 5, 10]

arr

2

i

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH

16 of 104

What does this print?

// arr is the array itself, n is the size.

int changeOdd(int arr[], int n) {

int count = 0;

for (int i = 0; i < n; i++) {

if (arr[i] % 2 == 1) {

arr[i]--;

count++;

}

}

n++;

return count;

}

int main() {

int n = 5;

int arr[5] = {2, 6, 3, 5, 10};

cout << changeOdd(arr, n) << endl;

}

16

5

n

0

count

5

n

[2, 6, 3, 5, 10]

arr

2

i

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH

17 of 104

What does this print?

// arr is the array itself, n is the size.

int changeOdd(int arr[], int n) {

int count = 0;

for (int i = 0; i < n; i++) {

if (arr[i] % 2 == 1) {

arr[i]--;

count++;

}

}

n++;

return count;

}

int main() {

int n = 5;

int arr[5] = {2, 6, 3, 5, 10};

cout << changeOdd(arr, n) << endl;

}

17

5

n

0

count

5

n

[2, 6, 3, 5, 10]

arr

2

i

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH

18 of 104

What does this print?

// arr is the array itself, n is the size.

int changeOdd(int arr[], int n) {

int count = 0;

for (int i = 0; i < n; i++) {

if (arr[i] % 2 == 1) {

arr[i]--;

count++;

}

}

n++;

return count;

}

int main() {

int n = 5;

int arr[5] = {2, 6, 3, 5, 10};

cout << changeOdd(arr, n) << endl;

}

18

5

n

0

count

5

n

[2, 6, 2, 5, 10]

arr

2

i

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH

19 of 104

What does this print?

// arr is the array itself, n is the size.

int changeOdd(int arr[], int n) {

int count = 0;

for (int i = 0; i < n; i++) {

if (arr[i] % 2 == 1) {

arr[i]--;

count++;

}

}

n++;

return count;

}

int main() {

int n = 5;

int arr[5] = {2, 6, 3, 5, 10};

cout << changeOdd(arr, n) << endl;

}

19

5

n

1

count

5

n

[2, 6, 2, 5, 10]

arr

2

i

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH

20 of 104

What does this print?

// arr is the array itself, n is the size.

int changeOdd(int arr[], int n) {

int count = 0;

for (int i = 0; i < n; i++) {

if (arr[i] % 2 == 1) {

arr[i]--;

count++;

}

}

n++;

return count;

}

int main() {

int n = 5;

int arr[5] = {2, 6, 3, 5, 10};

cout << changeOdd(arr, n) << endl;

}

20

5

n

1

count

5

n

[2, 6, 2, 5, 10]

arr

3

i

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH

21 of 104

What does this print?

// arr is the array itself, n is the size.

int changeOdd(int arr[], int n) {

int count = 0;

for (int i = 0; i < n; i++) {

if (arr[i] % 2 == 1) {

arr[i]--;

count++;

}

}

n++;

return count;

}

int main() {

int n = 5;

int arr[5] = {2, 6, 3, 5, 10};

cout << changeOdd(arr, n) << endl;

}

21

5

n

1

count

5

n

[2, 6, 2, 5, 10]

arr

3

i

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH

22 of 104

What does this print?

// arr is the array itself, n is the size.

int changeOdd(int arr[], int n) {

int count = 0;

for (int i = 0; i < n; i++) {

if (arr[i] % 2 == 1) {

arr[i]--;

count++;

}

}

n++;

return count;

}

int main() {

int n = 5;

int arr[5] = {2, 6, 3, 5, 10};

cout << changeOdd(arr, n) << endl;

}

22

5

n

1

count

5

n

[2, 6, 2, 5, 10]

arr

3

i

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH

23 of 104

What does this print?

// arr is the array itself, n is the size.

int changeOdd(int arr[], int n) {

int count = 0;

for (int i = 0; i < n; i++) {

if (arr[i] % 2 == 1) {

arr[i]--;

count++;

}

}

n++;

return count;

}

int main() {

int n = 5;

int arr[5] = {2, 6, 3, 5, 10};

cout << changeOdd(arr, n) << endl;

}

23

5

n

1

count

5

n

[2, 6, 2, 4, 10]

arr

3

i

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH

24 of 104

What does this print?

// arr is the array itself, n is the size.

int changeOdd(int arr[], int n) {

int count = 0;

for (int i = 0; i < n; i++) {

if (arr[i] % 2 == 1) {

arr[i]--;

count++;

}

}

n++;

return count;

}

int main() {

int n = 5;

int arr[5] = {2, 6, 3, 5, 10};

cout << changeOdd(arr, n) << endl;

}

24

5

n

2

count

5

n

[2, 6, 2, 4, 10]

arr

3

i

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH

25 of 104

What does this print?

// arr is the array itself, n is the size.

int changeOdd(int arr[], int n) {

int count = 0;

for (int i = 0; i < n; i++) {

if (arr[i] % 2 == 1) {

arr[i]--;

count++;

}

}

n++;

return count;

}

int main() {

int n = 5;

int arr[5] = {2, 6, 3, 5, 10};

cout << changeOdd(arr, n) << endl;

}

25

5

n

2

count

5

n

[2, 6, 2, 4, 10]

arr

4

i

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH

26 of 104

What does this print?

// arr is the array itself, n is the size.

int changeOdd(int arr[], int n) {

int count = 0;

for (int i = 0; i < n; i++) {

if (arr[i] % 2 == 1) {

arr[i]--;

count++;

}

}

n++;

return count;

}

int main() {

int n = 5;

int arr[5] = {2, 6, 3, 5, 10};

cout << changeOdd(arr, n) << endl;

}

26

5

n

2

count

5

n

[2, 6, 2, 4, 10]

arr

4

i

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH

27 of 104

What does this print?

// arr is the array itself, n is the size.

int changeOdd(int arr[], int n) {

int count = 0;

for (int i = 0; i < n; i++) {

if (arr[i] % 2 == 1) {

arr[i]--;

count++;

}

}

n++;

return count;

}

int main() {

int n = 5;

int arr[5] = {2, 6, 3, 5, 10};

cout << changeOdd(arr, n) << endl;

}

27

5

n

2

count

5

n

[2, 6, 2, 4, 10]

arr

4

i

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH

28 of 104

What does this print?

// arr is the array itself, n is the size.

int changeOdd(int arr[], int n) {

int count = 0;

for (int i = 0; i < n; i++) {

if (arr[i] % 2 == 1) {

arr[i]--;

count++;

}

}

n++;

return count;

}

int main() {

int n = 5;

int arr[5] = {2, 6, 3, 5, 10};

cout << changeOdd(arr, n) << endl;

}

28

5

n

2

count

5

n

[2, 6, 2, 4, 10]

arr

5

i

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH

29 of 104

What does this print?

// arr is the array itself, n is the size.

int changeOdd(int arr[], int n) {

int count = 0;

for (int i = 0; i < n; i++) {

if (arr[i] % 2 == 1) {

arr[i]--;

count++;

}

}

n++;

return count;

}

int main() {

int n = 5;

int arr[5] = {2, 6, 3, 5, 10};

cout << changeOdd(arr, n) << endl;

}

29

5

n

2

count

5

n

[2, 6, 2, 4, 10]

arr

5

i

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH

30 of 104

What does this print?

// arr is the array itself, n is the size.

int changeOdd(int arr[], int n) {

int count = 0;

for (int i = 0; i < n; i++) {

if (arr[i] % 2 == 1) {

arr[i]--;

count++;

}

}

n++;

return count;

}

int main() {

int n = 5;

int arr[5] = {2, 6, 3, 5, 10};

cout << changeOdd(arr, n) << endl;

}

30

5

n

2

count

6

n

[2, 6, 2, 4, 10]

arr

5

i

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH

31 of 104

What does this print?

// arr is the array itself, n is the size.

int changeOdd(int arr[], int n) {

int count = 0;

for (int i = 0; i < n; i++) {

if (arr[i] % 2 == 1) {

arr[i]--;

count++;

}

}

n++;

return count;

}

int main() {

int n = 5;

int arr[5] = {2, 6, 3, 5, 10};

cout << changeOdd(arr, n) << endl;

}

31

5

n

2

count

6

n

[2, 6, 2, 4, 10]

arr

5

i

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH

32 of 104

What does this print?

// arr is the array itself, n is the size.

int changeOdd(int arr[], int n) {

int count = 0;

for (int i = 0; i < n; i++) {

if (arr[i] % 2 == 1) {

arr[i]--;

count++;

}

}

n++;

return count;

}

int main() {

int n = 5;

int arr[5] = {2, 6, 3, 5, 10};

cout << changeOdd(arr, n) << endl;

}

32

5

n

[2, 6, 2, 4, 10]

arr

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH

33 of 104

What does this print?

// arr is the array itself, n is the size.

int changeOdd(int arr[], int n) {

int count = 0;

for (int i = 0; i < n; i++) {

if (arr[i] % 2 == 1) {

arr[i]--;

count++;

}

}

n++;

return count;

}

int main() {

int n = 5;

int arr[5] = {2, 6, 3, 5, 10};

cout << changeOdd(arr, n) << endl;

}

> 2

33

5

n

[2, 6, 2, 4, 10]

arr

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH

34 of 104

Printing Arrays

  • To print an array, we need to use a loop to print each element.
  • Printing the name will just print the starting address of the array

string arr[] = {“Smallberg”, “CS31”, “Midterm”};

for (int i = 0; i < 3; ++i) {

cout << arr[i];

}

34

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH

35 of 104

Out of Bounds Errors

  • Occur anytime you can access memory past the end (or beginning) of an array
    • Only certain spaces in memory have useful data
    • Anything outside is essentially garbage
    • Hard to debug. C++ doesn’t do bounds checking on array access so out of bounds accesses can often go unnoticed.

string array[3] = {“CS31”, “Smallberg”, “Midterm”};

cout << array[3] << endl; // Out of bounds error!

35

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH

36 of 104

Out of Bounds Example

Do we have an out of bounds memory access here?

// Assume arr only contains n elements.

int countFives(int arr[], int n) {

int count = 0;

for (int i = 0; i <= n; ++i) {

if (arr[i] == 5) {

count++;

}

}

return count;

}

36

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH

37 of 104

Out of Bounds Example

Do we have an out of bounds memory access here?

// Assume arr only contains n elements

int countFives(int arr[], int n) {

int count = 0;

for (int i = 0; i <= n; ++i) {

if (arr[i] == 5) { Yes! The for loop will access the

count++; element at the nth index.

}

}

return count;

}

37

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH

38 of 104

Practice Question: Index of First Repeated

Given an array of integers and the size of the array, write a function firstRepeat that returns the index of the first repeat element. Return -1 if there are no duplicate elements.

Input: int arr[] = {1, 2, 3, 2, 4}; int size = 5;

Output: 3

Input: int arr[] = {1, 2, 3, 7, 0, 2, 7, 3, 1}; int size = 9;

Output: 5

(Contributed by Carter Wu)

38

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH

39 of 104

Solution: Index of First Repeated

We use nested for loops:

int firstRepeat(int a[], int n) {

for (int i = 1; i < n; i++)

for (int j = 0; j < i; j++)

if (a[i] == a[j])

return i;

return -1;

}

39

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH

40 of 104

Practice Question: What Makes CS Beautiful

int main() {

string oneD[] = {"Zayn", "Louis", "Harry", "Niall", "Liam"};

int size = 5;

for (int i = 0; i < size; i++) {

int min = i;

for (int j = i + 1; j < size; j++)

if (oneD[j] < oneD[min])

min = j;

string temp = oneD[i];

oneD[i] = oneD[min];

oneD[min] = temp;

}

oneD[4] = "RIP" + oneD[4];

}

40

What does the string array contain after this code is executed?

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH

41 of 104

Walkthrough

string oneD[] = {....};

int size = 5;

for (int i = 0; i < size; i++) {

int min = i;

for (int j = i + 1; j < size; j++)

if (oneD[j] < oneD[min])

min = j;

string temp = oneD[i];

oneD[i] = oneD[min];

oneD[min] = temp;

}

oneD[4] = "RIP" + oneD[4];

41

["Zayn", "Louis", "Harry", "Niall", "Liam"]

oneD

5

size

0

i

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH

42 of 104

Walkthrough

string oneD[] = {....};

int size = 5;

for (int i = 0; i < size; i++) {

int min = i;

for (int j = i + 1; j < size; j++)

if (oneD[j] < oneD[min])

min = j;

string temp = oneD[i];

oneD[i] = oneD[min];

oneD[min] = temp;

}

oneD[4] = "RIP" + oneD[4];

42

["Zayn", "Louis", "Harry", "Niall", "Liam"]

oneD

5

size

0

i

0

min

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH

43 of 104

Walkthrough

string oneD[] = {....};

int size = 5;

for (int i = 0; i < size; i++) {

int min = i;

for (int j = i + 1; j < size; j++)

if (oneD[j] < oneD[min])

min = j;

string temp = oneD[i];

oneD[i] = oneD[min];

oneD[min] = temp;

}

oneD[4] = "RIP" + oneD[4];

43

["Zayn", "Louis", "Harry", "Niall", "Liam"]

oneD

5

size

0

i

1

j

0

min

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH

44 of 104

Walkthrough

string oneD[] = {....};

int size = 5;

for (int i = 0; i < size; i++) {

int min = i;

for (int j = i + 1; j < size; j++)

if (oneD[j] < oneD[min])

min = j;

string temp = oneD[i];

oneD[i] = oneD[min];

oneD[min] = temp;

}

oneD[4] = "RIP" + oneD[4];

44

["Zayn", "Louis", "Harry", "Niall", "Liam"]

oneD

5

size

0

i

1

j

0

min

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH

45 of 104

Walkthrough

string oneD[] = {....};

int size = 5;

for (int i = 0; i < size; i++) {

int min = i;

for (int j = i + 1; j < size; j++)

if (oneD[j] < oneD[min])

min = j;

string temp = oneD[i];

oneD[i] = oneD[min];

oneD[min] = temp;

}

oneD[4] = "RIP" + oneD[4];

45

["Zayn", "Louis", "Harry", "Niall", "Liam"]

oneD

5

size

0

i

1

j

1

min

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH

46 of 104

Walkthrough

string oneD[] = {....};

int size = 5;

for (int i = 0; i < size; i++) {

int min = i;

for (int j = i + 1; j < size; j++)

if (oneD[j] < oneD[min])

min = j;

string temp = oneD[i];

oneD[i] = oneD[min];

oneD[min] = temp;

}

oneD[4] = "RIP" + oneD[4];

46

["Zayn", "Louis", "Harry", "Niall", "Liam"]

oneD

5

size

0

i

2

j

1

min

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH

47 of 104

Walkthrough

string oneD[] = {....};

int size = 5;

for (int i = 0; i < size; i++) {

int min = i;

for (int j = i + 1; j < size; j++)

if (oneD[j] < oneD[min])

min = j;

string temp = oneD[i];

oneD[i] = oneD[min];

oneD[min] = temp;

}

oneD[4] = "RIP" + oneD[4];

47

["Zayn", "Louis", "Harry", "Niall", "Liam"]

oneD

5

size

0

i

2

j

1

min

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH

48 of 104

Walkthrough

string oneD[] = {....};

int size = 5;

for (int i = 0; i < size; i++) {

int min = i;

for (int j = i + 1; j < size; j++)

if (oneD[j] < oneD[min])

min = j;

string temp = oneD[i];

oneD[i] = oneD[min];

oneD[min] = temp;

}

oneD[4] = "RIP" + oneD[4];

48

["Zayn", "Louis", "Harry", "Niall", "Liam"]

oneD

5

size

0

i

2

j

2

min

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH

49 of 104

Walkthrough

string oneD[] = {....};

int size = 5;

for (int i = 0; i < size; i++) {

int min = i;

for (int j = i + 1; j < size; j++)

if (oneD[j] < oneD[min])

min = j;

string temp = oneD[i];

oneD[i] = oneD[min];

oneD[min] = temp;

}

oneD[4] = "RIP" + oneD[4];

49

["Zayn", "Louis", "Harry", "Niall", "Liam"]

oneD

5

size

0

i

3

j

2

min

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH

50 of 104

Walkthrough

string oneD[] = {....};

int size = 5;

for (int i = 0; i < size; i++) {

int min = i;

for (int j = i + 1; j < size; j++)

if (oneD[j] < oneD[min])

min = j;

string temp = oneD[i];

oneD[i] = oneD[min];

oneD[min] = temp;

}

oneD[4] = "RIP" + oneD[4];

50

["Zayn", "Louis", "Harry", "Niall", "Liam"]

oneD

5

size

0

i

3

j

2

min

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH

51 of 104

Walkthrough

string oneD[] = {....};

int size = 5;

for (int i = 0; i < size; i++) {

int min = i;

for (int j = i + 1; j < size; j++)

if (oneD[j] < oneD[min])

min = j;

string temp = oneD[i];

oneD[i] = oneD[min];

oneD[min] = temp;

}

oneD[4] = "RIP" + oneD[4];

51

["Zayn", "Louis", "Harry", "Niall", "Liam"]

oneD

5

size

0

i

4

j

2

min

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH

52 of 104

Walkthrough

string oneD[] = {....};

int size = 5;

for (int i = 0; i < size; i++) {

int min = i;

for (int j = i + 1; j < size; j++)

if (oneD[j] < oneD[min])

min = j;

string temp = oneD[i];

oneD[i] = oneD[min];

oneD[min] = temp;

}

oneD[4] = "RIP" + oneD[4];

52

["Zayn", "Louis", "Harry", "Niall", "Liam"]

oneD

5

size

0

i

4

j

2

min

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH

53 of 104

Walkthrough

string oneD[] = {....};

int size = 5;

for (int i = 0; i < size; i++) {

int min = i;

for (int j = i + 1; j < size; j++)

if (oneD[j] < oneD[min])

min = j;

string temp = oneD[i];

oneD[i] = oneD[min];

oneD[min] = temp;

}

oneD[4] = "RIP" + oneD[4];

53

["Zayn", "Louis", "Harry", "Niall", "Liam"]

oneD

5

size

0

i

5

j

2

min

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH

54 of 104

Walkthrough

string oneD[] = {....};

int size = 5;

for (int i = 0; i < size; i++) {

int min = i;

for (int j = i + 1; j < size; j++)

if (oneD[j] < oneD[min])

min = j;

string temp = oneD[i];

oneD[i] = oneD[min];

oneD[min] = temp;

}

oneD[4] = "RIP" + oneD[4];

54

["Zayn", "Louis", "Harry", "Niall", "Liam"]

oneD

5

size

0

i

2

min

“Zayn”

temp

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH

55 of 104

Walkthrough

string oneD[] = {....};

int size = 5;

for (int i = 0; i < size; i++) {

int min = i;

for (int j = i + 1; j < size; j++)

if (oneD[j] < oneD[min])

min = j;

string temp = oneD[i];

oneD[i] = oneD[min];

oneD[min] = temp;

}

oneD[4] = "RIP" + oneD[4];

55

["Harry", "Louis", "Harry", "Niall", "Liam"]

oneD

5

size

0

i

2

min

“Zayn”

temp

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH

56 of 104

Walkthrough

string oneD[] = {....};

int size = 5;

for (int i = 0; i < size; i++) {

int min = i;

for (int j = i + 1; j < size; j++)

if (oneD[j] < oneD[min])

min = j;

string temp = oneD[i];

oneD[i] = oneD[min];

oneD[min] = temp;

}

oneD[4] = "RIP" + oneD[4];

56

["Harry", "Louis", "Zayn", "Niall", "Liam"]

oneD

5

size

0

i

2

min

“Zayn”

temp

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH

57 of 104

Walkthrough

string oneD[] = {....};

int size = 5;

for (int i = 0; i < size; i++) {

int min = i;

for (int j = i + 1; j < size; j++)

if (oneD[j] < oneD[min])

min = j;

string temp = oneD[i];

oneD[i] = oneD[min];

oneD[min] = temp;

}

oneD[4] = "RIP" + oneD[4];

57

["Harry", "Louis", "Zayn", "Niall", "Liam"]

oneD

5

size

1

i

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH

58 of 104

Walkthrough

string oneD[] = {....};

int size = 5;

for (int i = 0; i < size; i++) {

int min = i;

for (int j = i + 1; j < size; j++)

if (oneD[j] < oneD[min])

min = j;

string temp = oneD[i];

oneD[i] = oneD[min];

oneD[min] = temp;

}

oneD[4] = "RIP" + oneD[4];

58

["Harry", "Louis", "Zayn", "Niall", "Liam"]

oneD

5

size

1

i

1

min

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH

59 of 104

Walkthrough

string oneD[] = {....};

int size = 5;

for (int i = 0; i < size; i++) {

int min = i;

for (int j = i + 1; j < size; j++)

if (oneD[j] < oneD[min])

min = j;

string temp = oneD[i];

oneD[i] = oneD[min];

oneD[min] = temp;

}

oneD[4] = "RIP" + oneD[4];

59

["Harry", "Louis", "Zayn", "Niall", "Liam"]

oneD

5

size

1

i

2

j

1

min

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH

60 of 104

Walkthrough

string oneD[] = {....};

int size = 5;

for (int i = 0; i < size; i++) {

int min = i;

for (int j = i + 1; j < size; j++)

if (oneD[j] < oneD[min])

min = j;

string temp = oneD[i];

oneD[i] = oneD[min];

oneD[min] = temp;

}

oneD[4] = "RIP" + oneD[4];

60

["Harry", "Louis", "Zayn", "Niall", "Liam"]

oneD

5

size

1

i

2

j

1

min

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH

61 of 104

Walkthrough

string oneD[] = {....};

int size = 5;

for (int i = 0; i < size; i++) {

int min = i;

for (int j = i + 1; j < size; j++)

if (oneD[j] < oneD[min])

min = j;

string temp = oneD[i];

oneD[i] = oneD[min];

oneD[min] = temp;

}

oneD[4] = "RIP" + oneD[4];

61

["Harry", "Louis", "Zayn", "Niall", "Liam"]

oneD

5

size

1

i

3

j

1

min

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH

62 of 104

Walkthrough

string oneD[] = {....};

int size = 5;

for (int i = 0; i < size; i++) {

int min = i;

for (int j = i + 1; j < size; j++)

if (oneD[j] < oneD[min])

min = j;

string temp = oneD[i];

oneD[i] = oneD[min];

oneD[min] = temp;

}

oneD[4] = "RIP" + oneD[4];

62

["Harry", "Louis", "Zayn", "Niall", "Liam"]

oneD

5

size

1

i

3

j

1

min

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH

63 of 104

Walkthrough

string oneD[] = {....};

int size = 5;

for (int i = 0; i < size; i++) {

int min = i;

for (int j = i + 1; j < size; j++)

if (oneD[j] < oneD[min])

min = j;

string temp = oneD[i];

oneD[i] = oneD[min];

oneD[min] = temp;

}

oneD[4] = "RIP" + oneD[4];

63

["Harry", "Louis", "Zayn", "Niall", "Liam"]

oneD

5

size

1

i

4

j

1

min

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH

64 of 104

Walkthrough

string oneD[] = {....};

int size = 5;

for (int i = 0; i < size; i++) {

int min = i;

for (int j = i + 1; j < size; j++)

if (oneD[j] < oneD[min])

min = j;

string temp = oneD[i];

oneD[i] = oneD[min];

oneD[min] = temp;

}

oneD[4] = "RIP" + oneD[4];

64

["Harry", "Louis", "Zayn", "Niall", "Liam"]

oneD

5

size

1

i

4

j

1

min

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH

65 of 104

Walkthrough

string oneD[] = {....};

int size = 5;

for (int i = 0; i < size; i++) {

int min = i;

for (int j = i + 1; j < size; j++)

if (oneD[j] < oneD[min])

min = j;

string temp = oneD[i];

oneD[i] = oneD[min];

oneD[min] = temp;

}

oneD[4] = "RIP" + oneD[4];

65

["Harry", "Louis", "Zayn", "Niall", "Liam"]

oneD

5

size

1

i

4

j

4

min

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH

66 of 104

Walkthrough

string oneD[] = {....};

int size = 5;

for (int i = 0; i < size; i++) {

int min = i;

for (int j = i + 1; j < size; j++)

if (oneD[j] < oneD[min])

min = j;

string temp = oneD[i];

oneD[i] = oneD[min];

oneD[min] = temp;

}

oneD[4] = "RIP" + oneD[4];

66

["Harry", "Louis", "Zayn", "Niall", "Liam"]

oneD

5

size

1

i

5

j

4

min

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH

67 of 104

Walkthrough

string oneD[] = {....};

int size = 5;

for (int i = 0; i < size; i++) {

int min = i;

for (int j = i + 1; j < size; j++)

if (oneD[j] < oneD[min])

min = j;

string temp = oneD[i];

oneD[i] = oneD[min];

oneD[min] = temp;

}

oneD[4] = "RIP" + oneD[4];

67

["Harry", "Louis", "Zayn", "Niall", "Liam"]

oneD

5

size

1

i

4

min

“Louis”

temp

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH

68 of 104

Walkthrough

string oneD[] = {....};

int size = 5;

for (int i = 0; i < size; i++) {

int min = i;

for (int j = i + 1; j < size; j++)

if (oneD[j] < oneD[min])

min = j;

string temp = oneD[i];

oneD[i] = oneD[min];

oneD[min] = temp;

}

oneD[4] = "RIP" + oneD[4];

68

["Harry", "Liam", "Zayn", "Niall", "Liam"]

oneD

5

size

1

i

4

min

“Louis”

temp

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH

69 of 104

Walkthrough

string oneD[] = {....};

int size = 5;

for (int i = 0; i < size; i++) {

int min = i;

for (int j = i + 1; j < size; j++)

if (oneD[j] < oneD[min])

min = j;

string temp = oneD[i];

oneD[i] = oneD[min];

oneD[min] = temp;

}

oneD[4] = "RIP" + oneD[4];

69

["Harry", "Liam", "Zayn", "Niall", "Louis"]

oneD

5

size

1

i

4

min

“Louis”

temp

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH

70 of 104

Solution: What Makes CS Beautiful

After walking through two iterations of the outer for loop, we notice that the loops are sorting the array into alphabetical order!

(this is called Selection Sort, but don’t worry about it for now) https://en.wikipedia.org/wiki/Selection_sort

Initial: ["Zayn", "Louis", "Harry", "Niall", "Liam"]

i = 0: ["Harry", "Louis", "Zayn", "Niall", "Liam"]

i = 1: ["Harry", "Liam", "Zayn", "Niall", "Louis"]

i = 2: ["Harry", "Liam", "Louis", "Niall", "Zayn"]

i = 3: ["Harry", "Liam", "Louis", "Niall", "Zayn"]

i = 4: ["Harry", "Liam", "Louis", "Niall", "Zayn"]

Final Answer: ["Harry", "Liam", "Louis", "Niall", "RIPZayn"]

70

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH

71 of 104

Practice Question: Transpose

71

Implement a function that takes in a pointer to an nxn 2d array of ints and transposes it. That is, the rows should become the columns and vice versa.

void transpose(int** matrix, int n);��Example: 1 2 3 (n = 3) → 1 4 7� 4 5 6 2 5 8� 7 8 9 3 6 9

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH

72 of 104

Solution: Transpose

72

void transpose(int** matrix, int n) {� for (int i = 0; i < n; i++) {

for (int j = 0; j < i; j++) {

int temp = matrix[i][j];� matrix[i][j] = matrix[j][i];

matrix[j][i] = temp;

}

}�}

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH

73 of 104

Practice Question: Resolve Merge Issues

73

// Assume arr1 and arr2 are ordered from least to

// greatest and have size n1 and n2, respectively.�// Also assume arr3 has size n1 + n2.

void merge(int arr1[], int n1, int arr2[], int n2,

int arr3[]) {

int i1 = 0, i2 = 0, i3 = 0;

while (i3 < n1 + n2) {� if (arr1[i1] < arr2[i2]) {

arr3[i3] = arr1[i1];

i1++;

} else if (arr2[i2] < arr1[i1]) {

arr3[i3] = arr2[i2];

i2++;

}

i3++;

}

}

This function attempts to merge two arrays arr1 and arr2 that are ordered from least to greatest into a third array arr3, so that arr3 contains the contents of both arr1 and arr2 ordered from least to greatest.

�Example: arr1 = {1, 2, 5}, arr2 = {2, 4, 6}� → arr3 = {1, 2, 2, 4, 5, 6}��Can you find and fix the bugs in this function so that it performs correctly?

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Spring ‘19) https://tinyurl.com/y62uz9sc

74 of 104

Practice Question: Resolve Merge Issues

74

// Assume arr1 and arr2 are ordered from least to

// greatest and have size n1 and n2, respectively.�// Also assume arr3 has size n1 + n2.

void merge(int arr1[], int n1, int arr2[], int n2,

int arr3[]) {

int i1 = 0, i2 = 0, i3 = 0;

while (i3 < n1 + n2) {� if (arr1[i1] < arr2[i2]) { // what if i1>=n1

arr3[i3] = arr1[i1]; // or i2 >= n2??

i1++;

} else if (arr2[i2] < arr1[i1]) { // same!

arr3[i3] = arr2[i2];

i2++;

} // what do we do if arr1[i1] == arr2[i2]?

i3++;

}

}

This function attempts to merge two arrays arr1 and arr2 that are ordered from least to greatest into a third array arr3, so that arr3 contains the contents of both arr1 and arr2 ordered from least to greatest.

�Example: arr1 = {1, 2, 5}, arr2 = {2, 4, 6}� → arr3 = {1, 2, 2, 4, 5, 6}��Can you find and fix the bugs in this function so that it performs correctly?

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Spring ‘19) https://tinyurl.com/y62uz9sc

75 of 104

Solution: Resolve Merge Issues

75

void merge(int arr1[], int n1, int arr2[], int n2,

int arr3[]) {

int i1 = 0, i2 = 0, i3 = 0;

while (i1 < n1 && i2 < n2) {� if (arr1[i1] < arr2[i2]) {

arr3[i3] = arr1[i1];

i1++;

} else if (arr2[i2] <= arr1[i1]) {

arr3[i3] = arr2[i2];

i2++;

}

i3++;

}

// continued...

while (i1 < n1) { // only one of these will run

arr3[i3] = arr1[i1];

i1++;

i3++;

}

while (i2 < n2) {

arr3[i3] = arr2[i2];

i2++;

i3++;

}

}

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH

76 of 104

C Strings

  • C does not have the string class (or classes at all!)
  • In C, we cannot declare strings or use class methods:
    • string x = “hello”;
      • x.size() // This is okay in C++, but not in C.
  • Instead, we represent strings using char arrays:
    • char y[] = “hello”;
    • Cannot use C++ string functions with it
      • y.size(), y.substr(...), etc. // Syntax errors.
    • #include <cstring> provides functions like strlen
      • strlen(x) returns 5

76

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH

77 of 104

Ascii: Characters are actually integers

77

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH

78 of 104

Ascii (cont.)

int x = ‘G’; // x is now 71.

x -= 1; // x is now 70.

char y = x; // y is now F.

int z = ‘5’; // z is now 53.

78

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH

79 of 104

C Strings (cont.)

  • The end of a C string is marked by a null byte (‘\0’)
    • Null byte has ASCII value 0
    • strlen simply looks for the null byte for you

char arr[] = “hello”;

for (int i = 0; arr[i] != ‘\0’; i++) // Standard for loop to iterate

// through c-strings.

Note: arr[i] != ‘\0’ and arr[i] != 0 are the same, as ascii value of ‘\0’ is 0.

79

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH

80 of 104

C Strings (cont.)

// A null character is automatically

// put in index 5.

char x[50] = “hello”;

// Because we have more space in the array

// (50 total), we can add more characters.

x[5] = ‘s’;

x[6] = ‘\0’;

80

[‘h’, ‘e’, ‘l’, ‘l’, ‘o’, ‘\0’, ...]

x

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH

81 of 104

C Strings (cont.)

// A null character is automatically

// put in index 5.

char x[50] = “hello”;

// Because we have more space in the array

// (50 total), we can add more characters.

x[5] = ‘s’;

x[6] = ‘\0’;

81

[‘h’, ‘e’, ‘l’, ‘l’, ‘o’, ‘s’, ...]

x

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH

82 of 104

C Strings (cont.)

// A null character is automatically

// put in index 5.

char x[50] = “hello”;

// Because we have more space in the array

// (50 total), we can add more characters.

x[5] = ‘s’;

x[6] = ‘\0’;

82

[‘h’, ‘e’, ‘l’, ‘l’, ‘o’, ‘s’, ‘\0’, ...]

x

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH

83 of 104

Practice Question: C Strings - removeNonAlpha

Given a C String, write a function removeNonAlpha that removes all non-alphabet chars in the C String. When removing a non-alphabet char, you should shift all following chars one position to the left. Don’t forget to shift the null byte as well!

char cstr[] = "S5mal.lb-erg! Is+ C$s Senpai$$$";

removeNonAlpha(cstr);

for (int i = 0; cstr[i] != '\0'; i++)

cout << cstr[i];

// OUTPUT: SmallbergIsCsSenpai

(Contributed by Matt Wong)

83

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH

84 of 104

Solution: C Strings - removeNonAlpha

The outer for loop iterates through every character position in the C String. The inner while loop and for loop shifts the characters to the left to remove all non-alphabet characters.

#include <cctype>

void removeNonAlpha(char str[]) {

for(int i = 0; str[i] != '\0'; i++)

while ( !isalpha(str[i]) && str[i] != '\0' )

for(int j = i; str[j] != '\0'; j++)

str[j] = str[j+1];

}

84

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH

85 of 104

Pointers

  • A pointer is the memory address of a variable.

  • The & operator can be used to determine the address of a variable to be stored in the pointer.
  • The * operator can be used to dereference a pointer and get the value stored in the variable that is being pointed to.

double d = 10.0;

double *dp; // pointer that holds the address of a double

dp = &d; // stores the address of d into dp

*dp = 20.0; // changes the value of d from 10.0 to 20.0

85

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH

86 of 104

Pointers

int var = 20; // actual variable declaration�int *ip; // pointer variable declaration

// store address of var in ip�ip = &var;��cout << "Value of var variable: ";�cout << var << endl;��// print the address stored in ip pointer�cout << "Address stored in ip variable: ";�cout << ip << endl;��// access the value at address stored in pointer�cout << "Value of *ip variable: ";�cout << *ip << endl;�

> Value of var variable: 20

> Address stored in ip variable: 0xBFC601AC

> Value of *ip variable: 20

86

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH

87 of 104

Pointer Arithmetic

int arr[] = {10, 20, 30, 40};

int *ptr = arr; // arr == &arr[0]

cout << *ptr << endl;

ptr++;

cout << *ptr << endl;

ptr = ptr + 1;

cout << *ptr << endl;

ptr--;

cout << *(ptr + 2) << endl;

> 10

> 20

> 30

> 40

87

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH

88 of 104

Pointers – new and delete

  • The new operator can be used to create dynamic variables. These variables can be accessed using pointers.

string *p;

p = new string;

p = new string(“hello”);

  • The delete operator eliminates dynamic variables.

delete p;

  • Note: Pointer p is now a dangling pointer! Dereferencing it is dangerous and leads to undefined behavior. One way to avoid this is to set p to NULL after using delete.

88

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH

89 of 104

Pointers – Dynamic Arrays

  • A pointer can also be used when creating a dynamic array. Dynamic arrays are useful because their size can be determined while the program is running!

int *ptr;

int arraySize;

cin >> arraySize;

ptr = new int[arraySize];

  • To destroy the dynamically allocated array, use the delete[] operator.

delete[] ptr;

89

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH

90 of 104

Pointers – the Heap and the Stack

  • As it turns out, there are two places where your variables live.
  • The first is the stack, which is the place you're most familiar with. With local variables, the compiler is like a city planner who decides where each variable should live.

90

int a[4]

int k

string s

100

116

120

void foo() {

int a[4]; // Stored at 100

int k; // Stored at 116

string s; // Stored at 120

}

Variables in the calling function.

0-100

If the size isn't specified at compile time, how would the compiler know where to put k or s?

When foo called

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH

91 of 104

Pointers – the Heap and the Stack (cont.)

  • As it turns out, there are two places where your variables live.
  • The first is the stack, which is the place you're most familiar with. With local variables, the compiler is like a city planner who decides where each variable should live.

91

Vacant

Vacant

Vacant

100

116

120

void foo() {

int a[4]; // Stored at 100

int k; // Stored at 116

string s; // Stored at 120

}

Variables in the calling function.

0-100

When the function returns, the variables are evicted from their addresses.

When foo returns

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH

92 of 104

Pointers – the Heap and the Stack (cont.)

  • As it turns out, there are two places where your variables live.
  • The second is the heap, which is the place where dynamic variables live. Dynamic variables essentially lease some part of the heap to live in.

92

int[5]

2000

void bar() {

int *p = new int[5];

string *q = new string("Cat");

}

"cat"

2248

HEAP

string *q

int *p

136

140

When bar called

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH

93 of 104

Pointers – the Heap and the Stack (cont.)

  • As it turns out, there are two places where your variables live.
  • The second is the heap, which is the place where dynamic variables live. Dynamic variables essentially lease some part of the heap to live in.

93

int[5]

2000

"cat"

2248

HEAP

Contains a memory leak!

void bar() {

int *p = new int[5];

string *q = new string("Cat");

}

When bar returns

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH

94 of 104

Pointers – the Heap and the Stack (cont.)

  • As it turns out, there are two places where your variables live.
  • The second is the heap, which is the place where dynamic variables live. Dynamic variables essentially lease some part of the heap to live in.

94

void bar() {

int *p = new int[5];

string *q = new string("Cat");

delete[] p;

delete q;

}

HEAP

Don't forget to clean up after yourself!

When bar returns

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH

95 of 104

Practice Question: Array Traversal w/ Pointers

Write a function that sums the items of an array of n integers using only pointers to traverse the array.

95

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH

96 of 104

Solution: Array Traversal w/ Pointers

Simple array traversal, but with pointers. To get to item i, add i to your head pointer then dereference. This works because the compiler knows the size of an item in your array in C++. Sum values as you go by adding them to a total value created outside of the for loop.

int sum(int *head, int n) {

int total = 0;

for (int i = 0; i < n; i++) {

total += *(head + i);

}

return total;

}

sum(arr, 5);

96

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH

97 of 104

Practice Question: C String Reversal with Pointers

97

Implement the function reverse, which takes a C String as an argument and prints out the characters in reverse order. You are not allowed to use the strlen function, and you must use pointers in any traversal of the C String.

void reverse(const char s[]);

int main() {

char str[] = “stressed”

reverse(str);

// OUTPUT: desserts

}

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH

98 of 104

Solution: C String Reversal with Pointers

98

void reverse(const char s[]) {

const char *p = s; // create a new pointer for our traversal

while (*p != '\0') { // move the pointer to the end of the C String

p++;

}

p--; // set p to point at the last char in the C String

while (p >= s) { // print out chars as we traverse back to the beginning

cout << *p;

p--;

}

cout << endl;

}

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH

99 of 104

Practice Question: strcat

99

Implement the C string concatenation function. The function takes two C strings and copies the chars from the source C string to the end of the destination C string. The original null byte of the destination is overwritten when copying the source. Return the destination pointer at the end of the function. You do not know the size of the destination and source C strings (so you can’t create a temporary C string to store all of the characters!)

char* strcat(char* destination, const char* source);

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH

100 of 104

Solution: strcat

100

char* strcat(char* destination, const char* source) {

char* d = destination;

while (*d) // this loop sets d to point at the null byte of destination

d++;

const char* s = source;

while (*s) { // this loop copies the source C string to where d is pointing

*d = *s;

d++;

s++;

}

*d = '\0'

return destination; }

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH

101 of 104

Structs

  • A struct is a collection of data that is treated as its own special data type. We use them to organize data that belongs together.

struct Person {

int age;

string name;

}; // Must end with semicolon!

  • Structs can store any number of any other data type, accessed using . (the dot operator). These stored values are called member variables.

101

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH

102 of 104

Structs

You can declare a struct outside of any functions and treat it as a normal variable, for the most part. A struct can even contain another struct!

struct Date {

int day, month, year;

};

struct Person {

Date birthday;

string name;

double money;

};

void doubleMoney(Person& guy) {

guy.money *= 2;

}

int main() {

Person p1;

p1.name = “Smelborp”;

p1.money = 3.50;

p1.birthday.day = p1.birthday.month = 1;

Person p2 = p1; // Perfectly legal

doubleMoney(p2);

cout << p2.money; // 7

cout << p1.money; // 3.5

Person p3 = { p1.birthday, “Jimbo”, 3.5 };

p2 += p1; // ERROR! How do you add people?!

}

102

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH

103 of 104

Structs – Tips

  • Structs are a good way to keep code looking organized and readable
  • When declaring a struct, member variables with primitive types will be left uninitialized. Classes will be constructed with their default constructor.
    • Long story short: you must assign them yourself!
  • Always remember the semicolon! It’s there so that you can declare struct variables at the same time that you define the struct.

struct Circle {

int radius;

} ring, hoop; // This creates two Circle structs named ring and hoop

103

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH

104 of 104

Good luck!

Sign-in http://bit.ly/2QCmwRH

Slides http://bit.ly/2NXAYls

Practice https://github.com/uclaupe-tutoring/practice-problems/wiki

Questions? Need more help?

  • Come up and ask us! We'll try our best.
  • UPE offers daily computer science tutoring:
    • Location: ACM/UPE Clubhouse (Boelter 2763)
    • Schedule: https://upe.seas.ucla.edu/tutoring/
  • You can also post on the Facebook event page.

104

UPSILON PI EPSILON

CS 31 Midterm 2 Review (Fall ‘19) http://bit.ly/2QCmwRH