Top Banner
Recursion(strings+arrays) Oct. 27, Ch 7.4-8.2 + 14
18

Recursion(strings+arrays) - CSE Labs User Home Pages · C-Strings and strings Last lab, you basically worked with C-Strings (char arrays) You should end C-Strings with null character,

Dec 03, 2018

Download

Documents

dinhhanh
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Recursion(strings+arrays) - CSE Labs User Home Pages · C-Strings and strings Last lab, you basically worked with C-Strings (char arrays) You should end C-Strings with null character,

Recursion(strings+arrays)Oct. 27, Ch 7.4-8.2 + 14

Page 2: Recursion(strings+arrays) - CSE Labs User Home Pages · C-Strings and strings Last lab, you basically worked with C-Strings (char arrays) You should end C-Strings with null character,

Announcements

- midterm graded?

- HW 4 due tomorrow

- for each loops (new in C++ 11)

lab computers don't seem to like them(g++ -std=c++11 fileName.cpp)

Page 3: Recursion(strings+arrays) - CSE Labs User Home Pages · C-Strings and strings Last lab, you basically worked with C-Strings (char arrays) You should end C-Strings with null character,

Highlights

- multi-dimensional arrays

- recursion

Page 4: Recursion(strings+arrays) - CSE Labs User Home Pages · C-Strings and strings Last lab, you basically worked with C-Strings (char arrays) You should end C-Strings with null character,

Multidimensional Arrays

So far we have dealt with simple (onedimensional) arrays

We have represented this as all the databeing stored in a line

(See: lineWorld.cpp)

Page 5: Recursion(strings+arrays) - CSE Labs User Home Pages · C-Strings and strings Last lab, you basically worked with C-Strings (char arrays) You should end C-Strings with null character,

Multidimensional Arrays

foo's length = 3(number of rows)

foo[0]'s length=5(number of columnsin row 0)

Page 6: Recursion(strings+arrays) - CSE Labs User Home Pages · C-Strings and strings Last lab, you basically worked with C-Strings (char arrays) You should end C-Strings with null character,

Multidimensional Arrays

If we think of a couple simple (one dimensional) arrays on top of each other...

(See: gridWorld.cpp)

One array for“x . . . .”

One array for“x x x x x”

Page 7: Recursion(strings+arrays) - CSE Labs User Home Pages · C-Strings and strings Last lab, you basically worked with C-Strings (char arrays) You should end C-Strings with null character,

Multidimensional Arrays

Recreate:

(See: oneToAHundred.cpp)

Page 8: Recursion(strings+arrays) - CSE Labs User Home Pages · C-Strings and strings Last lab, you basically worked with C-Strings (char arrays) You should end C-Strings with null character,

C-Strings and strings

There are actually two types of “strings”(multiple characters) in C++

A C-String is a char array, and this is what you get when you put quotes around words

A string (the thing you #include) is a morecomplicated type called a class (next week)

C-String

Page 9: Recursion(strings+arrays) - CSE Labs User Home Pages · C-Strings and strings Last lab, you basically worked with C-Strings (char arrays) You should end C-Strings with null character,

C-Strings and strings

Last lab, you basically worked with C-Strings(char arrays)

You should end C-Strings with null character,as this tells cout when to stop displaying

This means you can initialize char arrayswith quotes (BUT NOT OTHER TYPES)(see: cstring.cpp)

Page 10: Recursion(strings+arrays) - CSE Labs User Home Pages · C-Strings and strings Last lab, you basically worked with C-Strings (char arrays) You should end C-Strings with null character,

C-Strings and strings

The string class is a char array packaged upwith some more functionality

strings are nice as they are more flexible:+: concatenates (mashes together) strings[]: can be used to index into them (as arrays)==: can be used to compare equality

(see: string.cpp)

Page 11: Recursion(strings+arrays) - CSE Labs User Home Pages · C-Strings and strings Last lab, you basically worked with C-Strings (char arrays) You should end C-Strings with null character,

C-Strings and strings

It is fairly easy to convert between C-Stringsand strings:

You can also convert between numbersand strings:

(see: stringConversion.cpp)

Page 12: Recursion(strings+arrays) - CSE Labs User Home Pages · C-Strings and strings Last lab, you basically worked with C-Strings (char arrays) You should end C-Strings with null character,

Recursion

Recursion is simply when a function callsitself (we did this for the maze in week 5)

This is quite powerful, but also confusing

(see: towerHanoi.cpp)

Page 13: Recursion(strings+arrays) - CSE Labs User Home Pages · C-Strings and strings Last lab, you basically worked with C-Strings (char arrays) You should end C-Strings with null character,

Recursion

There are two important parts of recursion:-A stopping case that ends the recursion-A base case that reduces the problem

What are the base and stopping cases forthe Fibonacci numbers?

(sum of the previous two numbers)(see: fibonacciRecursion.cpp)

Page 14: Recursion(strings+arrays) - CSE Labs User Home Pages · C-Strings and strings Last lab, you basically worked with C-Strings (char arrays) You should end C-Strings with null character,

Recursion

If you forget your stopping case, you will notget an infinite loop but crash the program

This is because every function calltakes up more memory, so youconstantly ask for more memory

Eventually the memory (stack)cannot store anymore(see: stackOverflow.cpp)

Page 15: Recursion(strings+arrays) - CSE Labs User Home Pages · C-Strings and strings Last lab, you basically worked with C-Strings (char arrays) You should end C-Strings with null character,

Recursion

How would you sum the numbers 1 to n usingrecursion (not a loop)?

For example sumToN(5) = 15,as 1+2+3+4+5 = 15

What is the stopping case?How do you reduce the problem?

(see: sumToN.cpp)

Page 16: Recursion(strings+arrays) - CSE Labs User Home Pages · C-Strings and strings Last lab, you basically worked with C-Strings (char arrays) You should end C-Strings with null character,

Recursion

What is the difference between:

(see: headTailRecursion.cpp)

Page 17: Recursion(strings+arrays) - CSE Labs User Home Pages · C-Strings and strings Last lab, you basically worked with C-Strings (char arrays) You should end C-Strings with null character,

Recursion

How would you solve a sudoku problem?Rules:1. Every row has numbers 1-92. Every column has numbers 1-93. The nine 3x3 boxes have numbers 1-9

Reduce problem?Stopping case?

(see: sudokuSolver.cpp)

Page 18: Recursion(strings+arrays) - CSE Labs User Home Pages · C-Strings and strings Last lab, you basically worked with C-Strings (char arrays) You should end C-Strings with null character,

Recursion

Do not try to solve chess in this manner!

You will segfault(you will also not finish computing beforethe sun burns the earth to a crisp)