0% found this document useful (0 votes)
16 views125 pages

Principles of Computer Programming II Week - 0: Irakli Iremashvili

This document outlines the curriculum for a C++ programming course, focusing on fundamentals, the compilation process, debugging, recursion, and GitHub Classroom. It introduces C++ as a powerful language with features like static typing, object-oriented programming, and direct memory management. The course also emphasizes practical skills through the use of QT Creator as an IDE and covers the structure of C++ programs, including functions and recursion.

Uploaded by

zluka6930
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views125 pages

Principles of Computer Programming II Week - 0: Irakli Iremashvili

This document outlines the curriculum for a C++ programming course, focusing on fundamentals, the compilation process, debugging, recursion, and GitHub Classroom. It introduces C++ as a powerful language with features like static typing, object-oriented programming, and direct memory management. The course also emphasizes practical skills through the use of QT Creator as an IDE and covers the structure of C++ programs, including functions and recursion.

Uploaded by

zluka6930
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 125

Principles of Computer

Programming II
Week - 0
Irakli Iremashvili
What We'll Learn Today:
• 🚀 C++ Fundamentals &
Development Environment
• ⚙️The Compilation Process &
Debugging
CS106B: C++ • 🔄 Recursion & Recursive
Thinking
and Recursion 🚀 • 💻 Writing Your First Recursive
Functions
• 🔗 Introduction to GitHub
Classroom
Building on Your CS50
Knowledge! 🎓
What is C++?
• 💪 Powerful, high-performance compiled
language
• Created in 1979 by Bjarne Stroustrup as an
extension of C
• 🔧 Combines low-level memory manipulation
C++ with high-level abstractions

Fundamentals • 🎮 Powers game engines, operating systems,


browsers, and performance-critical
& Development applications

Environment 🚀 Key Differences from CS50:


• 🔍 Static typing (variables must have
declared types)
• 🔄 Object-oriented programming features
• ⚡ Direct memory management
• 🧩 Standard Template Library (STL)
QT Creator as Our IDE
• Integrated Development Environment
• 📝 Project management, code editing, and
debugging in one place

C++ • 🚦 One-click build and run functionality


• 🔬 Powerful debugging tools
Fundamentals Stanford Libraries
& Development • 🧰 CS106B comes with special Stanford
libraries
Environment 🚀 • 🎨 Simplified graphics, console interaction,
and collections
• 🧠 Lets you focus on concepts rather than
implementation details
Main Features:
• 📂 Project Explorer: Organize files and project
structure
• Code Editor: Syntax highlighting, auto-completion,
and error detection
• 🚦 Build & Run: One-click compilation and execution
• 🐞 Debugging Tools: Set breakpoints and inspect

QT Creator: variables in real-time


Setting Up Your First Project:

Your C++ • 📝 File → New Project → Application → Qt Widgets


Application

Workshop • Give your project a name and location


• ⚙️Configure build settings (we'll use default
configurations)
CS106B Project Structure:
• 📄 .pro file: Project configuration
• 📁 Source files (.cpp)
• 📁 Header files (.h)
• 📁 Stanford library includes
QT Creator: Your C++ Workshop
How C++ Programs Get Built:
• 🔄 C++ is a compiled language (unlike Python or
JavaScript)
• 📋 Your code must be converted to machine
instructions before execution
• Compilation takes time, but execution is faster

The Compilation Stages:


• Preprocessing 📄
Compilation • Handles #include and #define directives

Process &
• Expands macros and includes header files
• Compilation 🔄

Debugging ⚙️ • Translates C++ code into machine code


• Performs type checking and syntax validation
• Creates object files (.o or .obj)
• Linking 🔗
• Combines object files into an executable
• Resolves references between files
• Links external libraries (Stanford libraries,
STL)
Compilation
Preprocessi Modifies the original program according to the directives
ng that start with '#'

Compilatio Translates the program into an object file containing


n machine language code

Linking Handles merging and make executable file


Common Errors and Debugging:
• 📝 Syntax Errors: Typos, missing
semicolons (caught during compilation)
• 🔢 Semantic Errors: Type mismatches,
undefined variables

The • 🐞 Runtime Errors: Segmentation faults,


null pointer exceptions
Compilation • 🤔 Logic Errors: Program compiles but
produces incorrect results
Process & Using QT Creator's Debugger:
Debugging ⚙️ • 🛑 Set breakpoints by clicking in the left
margin
• 🔍 Step through code line by line
• Watch variables and track their values
• 🔄 Resume execution or stop debugging at
any point
Key Differences from C:
• ✨ Object-Oriented Programming:
• Classes and objects
• Encapsulation of data and methods
• Inheritance and polymorphism

C++ Basics: • 📝 Input/Output System:


• cout and cin instead of printf and scanf

Building on •

Stream-based I/O using << and >> operators
More type-safe than C's I/O functions

Your C • 🧵 Strings as Objects:


• string class replaces C-style character
arrays

Knowledge 🧱 •

Built-in methods for manipulation
No manual memory management for strings
• Standard Template Library (STL):
• Ready-to-use data structures (vectors,
maps, sets)
• Algorithms for common operations
• Iterators for traversing collections
Basic
CS106B
Program
Structure:
📚 simpio.h:

• getLine() - Gets a line of text from user


• getInteger() - Gets an integer with error
checking
• getReal() - Gets a floating-point number
Stanford • getYesOrNo() - Gets a yes/no response
console.h:
CS106B • Sets up the interactive console window

Libraries
• Handles text display formatting

📊 vector.h, map.h, set.h:

• Stanford's custom implementations of STL


containers
• More beginner-friendly error messages
• Enhanced debugging capabilities
What is a Function?
• 📦 A reusable block of code
C++ that performs a specific
task
Functions: • 🔄 Can be called multiple
Building times from different parts
Blocks of of your program
• 🧱 Promotes code
Your organization, reusability,
Programs 🧩 and maintainability
Function Components:
• Return Type 📤
• Specifies what kind of data the function will give

C++
back
• Examples: int, double, string, void (nothing
returned)

Functions:
• Must match the type of data in your return
statement
• Function Name

Building
• Identifies the function (should be descriptive of
what it does)
• Examples: calculateArea, printMessage, isEven

Blocks of
• Follows camelCase convention in CS106B
• Parameters 📥
• Input values the function needs to perform its task

Your •

Each parameter has a type and a name
Can have multiple parameters, separated by
commas

Programs 🧩 • Function Body 🧠




Code between curly braces { }
Contains the actual instructions the function
executes
• May include a return statement to send back a
value
Function Prototype vs. Definition:
• Prototype (Declaration):
o Tells the compiler that a function
exists
o Usually placed at the top of your file
C++ Functions: or in header files
o Ends with a semicolon instead of a
Building Blocks function body

of Your
o Example: double
calculateArea(double radius);
Programs 🧩 • Definition (Implementation):
o Contains the actual code that runs
when the function is called
o Must match the prototype's return
type and parameters
Understanding Recursive Thinking:
• 🧠 A different approach to problem-solving
• 🔍 Identifies how a problem contains
smaller versions of itself
• 🧩 Solves a problem by combining
solutions to smaller instances
• 🌱 Starts with the simplest case and builds
Introduction to up

Recursion 🔄 Visualizing Recursion:


• 📚 Stack of books - each recursive call is
like adding a book
• 🪄 Russian nesting dolls - each contains a
smaller version
• 🪞 Two mirrors facing each other - creating
infinite reflections
The Two Essential Components:
1. Base Case(s) ⛔
• The simplest scenario that can be
solved directly
• Provides the stopping condition
• Prevents infinite recursion
• Example: Factorial of 0 is 1 (defined
Introduction to as the base case)
2.Recursive Case 🔁
Recursion 🔄 • Breaks down problem into smaller
subproblems
• Function calls itself with modified
parameters
• Each call must move toward the base
case
• "Solves" the current problem using
solutions to smaller problem
Mathematical Definition:
A Classic • Factorial of n (written as n!):

Example: o
o
n × (n-1) × (n-2) × ... × 2 × 1
0! is defined as 1

Factorial Recursive Definition:

Function 🧮 • Base Case: 0! = 1


• Recursive Case: n! = n × (n-1)!
Alexes Compute Factorials
Alexes Compute Factorials

Me
!
Alexes Compute Factorials
I
wonder
what 3!
is?

Me
!
Alexes Compute Factorials
I’ll ask
my
friend
Alex!

Me
!
Alexes Compute Factorials
Alex #3,
what’s 3! ?

Me
!
Alexes Compute Factorials
Alex #3,
what’s 3! ?

Me
! Ale
x
#3
Alexes Compute Factorials
3! = 3 × 2!.
I wonder
what 2 !
is?

Me
! Ale
x
#3
Alexes Compute Factorials
Let me
ask
my
friend
Alex!

Me
! Ale
x
#3
Alexes Compute Factorials
Alex #2,
what’s 2 ! ?

Me
! Ale
x
#3
Alexes Compute Factorials
Alex #2,
what’s 2 ! ?

Me
! Ale
x
#3 Ale
x
#2
Alexes Compute Factorials

2! = 2 × 1!.
I wonder
what 1!
is?

Me
! Ale
x
#3 Ale
x
#2
Alexes Compute Factorials

Let me
ask
my
friend
Alex!

Me
! Ale
x
#3 Ale
x
#2
Alexes Compute Factorials

Alex #1,
what’s 1! ?

Me
! Ale
x
#3 Ale
x
#2
Alexes Compute Factorials

Alex #1,
what’s 1! ?

Me
! Ale
x
#3 Ale
x
#2 Ale
x
#1
Alexes Compute Factorials

1! = 1 × 0!.
I
wonder
what 0!
is?

Me
! Ale
x
#3 Ale
x
#2 Ale
x
#1
Alexes Compute Factorials

Let me ask
my friend
Alex!

Me
! Ale
x
#3 Ale
x
#2 Ale
x
#1
Alexes Compute Factorials

Alex #0,
what’s 0! ?

Me
! Ale
x
#3 Ale
x
#2 Ale
x
#1
Alexes Compute Factorials

Alex #0,
what’s 0! ?

Me
! Ale
x
#3 Ale
x
#2 Ale
x
#1 Ale
x
#0
Alexes Compute Factorials

Ooh, I know!
0! is 1.

Me
! Ale
x
#3 Ale
x
#2 Ale
x
#1 Ale
x
#0
Alexes Compute Factorials

Thanks,
Alex
#0.

Me
! Ale
x
#3 Ale
x
#2 Ale
x
#1 Ale
x
#0
Alexes Compute Factorials

Thanks,
Alex
#0.

Me
! Ale
x
#3 Ale
x
#2 Ale
x
#1
Alexes Compute Factorials
Because 0! = 1 and
1! = 1 × 0!, the
answer is 1! = 1.

Me
! Ale
x
#3 Ale
x
#2 Ale
x
#1
Alexes Compute Factorials
Thanks,
Alex
#1.

Me
! Ale
x
#3 Ale
x
#2 Ale
x
#1
Alexes Compute Factorials
Thanks,
Alex
#1.

Me
! Ale
x
#3 Ale
x
#2
Alexes Compute Factorials
Because 1! = 1 and
2! = 2 × 1!, the
answer is 2 ! = 2 .

Me
! Ale
x
#3 Ale
x
#2
Alexes Compute Factorials
Thanks,
Alex
#2.

Me
! Ale
x
#3 Ale
x
#2
Alexes Compute Factorials
Thanks,
Alex
#2.

Me
! Ale
x
#3
Alexes Compute Factorials
Because 2 ! = 2 and
3! = 3 × 2 ! , the
answer is 3! = 6.

Me
! Ale
x
#3
Alexes Compute Factorials
Thanks,
Alex
#3.

Me
! Ale
x
#3
Alexes Compute Factorials
Thanks,
Alex
#3.

Me
!
Alexes Compute Factorials

There are multiple


people, each named Alex,
but they’re not the same
person.

Each Alex is tasked with


computing a diff erent
number factorial.

Me Each Alex gives their


! answer back to the
previous person.

Eventually I get the answer!


Recursion in Action
int main() {
int nFact = factorial(3);
cout << "3! = " << nFact << endl;
return 0;
}
Recursion in Action
int main() {
int nFact = factorial(3);
cout << "3! = " << nFact << endl;
return 0;
}
Recursion in Action
int main()
int n = factorial(5);
{ int factorial(int n) {
cout <<=="5!0)={" << n <<
if (n
endl; return 1;
3
} else { int n
} returnreturn
0; n * factorial(n - 1);
}
}
Recursion in Action
int main()
int n = factorial(5);
{ int factorial(int n) {
cout <<== "5!0)={" << n <<
if (n
endl; return
3
} else
int n
1;0;
} returnreturn
{ n * factorial(n - 1);
}
}
Recursion in Action
int main()
int n = factorial(5);
{ int factorial(int n) {
cout <<== "5!0)={" << n <<
if (n
endl; return
3
} else
int n
1;0;
} returnreturn
{ n * factorial(n - 1);
}
}
Recursion in Action
int main()
int n = factorial(5);
{ int factorial(int n) {
cout <<== "5!0)={" << n <<
if (n
endl; return
3
1;
int n
} returnreturn
0; n * factorial(n - 1);
} else {
}
}
Recursion in Action
int main()
int n = factorial(5);
{ int factorial(int n) {
cout <<=="5!0)={" << n <<
if (n
endl; return 1;
3
} else { int n
} returnreturn
0; n * factorial(n - 1);
}
}
Recursion in Action
int main()
int n = factorial(5);
{ int factorial(int n) {
cout <<=="5!0)={" << n <<
if (n
endl; return 1;
3
} else { int n
} returnreturn
0; n * factorial(n - 1);
}
} 3
Recursion in Action
int main()
int n = factorial(5);
{ int factorial(int n) {
cout <<== "5!0)={" << n <<
if (n
endl; return
3
1;
int n
} returnreturn
0; n * factorial(n - 1);
} else {
}
} 3
Recursion in Action
int main() {
int n = factorial(5);
int factorial(int n) {
cout <<==
if (n "5!0)={" << n << 3
int factorial(int
endl; return 1; n) {
if (n
int==n 0) { 2
} return return 1;
0;
} }else
else{{ int n
return n * factorial(n - 1);
} return n * factorial(n - 1);
} 5
}
}
Recursion in Action
int main() {
int n = factorial(5);
int factorial(int n) {
cout <<==
if (n "5!0)={" << n << 3
int factorial(int
endl; return 1; n) {
if (n
int==n 0) { 2
} return return 1;
0;
} }else
else{{ int n
return n * factorial(n - 1);
} return n * factorial(n - 1);
} 5
}
}

Every time w e call


factorial(), w e get a new
copy of the local variable n
that’s independent of all
the previous copies.
Recursion in Action
int main() {
int n = factorial(5);
int factorial(int n) {
cout <<==
if (n "5!0)={" << n << 3
int factorial(int
endl; return 1; n) {
if (n
int==n 0) { 2
} return
} else return
0;{ int n
} else
1;
return n * factorial(n - 1);
} { return n * factorial(n - 1);
} 5
}
}
Recursion in Action
int main() {
int n = factorial(5);
int factorial(int n) {
cout <<==
if (n "5!0)={" << n << 3
int factorial(int
endl; return 1; n) {
if (n
int==n 0) { 2
} return
} else return
0;{ int n
} else
1;
return n * factorial(n - 1);
} { return n * factorial(n - 1);
} 5
}
}
Recursion in Action
int main() {
int n = factorial(5);
int factorial(int n) {
cout <<==
if (n "5!0)={" << n << 3
int factorial(int
endl; return 1; n) {
if (n
int==n 0) { 2
} return return
0;
} else
1;
{ int n
return n * factorial(n - 1);
} } else
return
{ 5 n * factorial(n - 1);
}
}
}
Recursion in Action
int main() {
int n = factorial(5);
int factorial(int n) {
cout <<==
if (n "5!0)={" << n << 3
int factorial(int
endl; return 1; n) {
if (n
int==n 0) { 2
} return return 1;
0;
} }else
else{{ int n
return n * factorial(n - 1);
} return n * factorial(n - 1);
} 5
}
}
Recursion in Action
int main() {
int n = factorial(5);
int factorial(int n) {
cout <<==
if (n "5!0)={" << n << 3
int factorial(int
endl; return 1; n) {
if (n
int==n 0) { 2
} return return 1;
0;
} }else
else{{ int n
return n * factorial(n - 1);
} return n * factorial(n - 1);
} 5
}
} 2
Recursion in Action
int main() {
int n = factorial(5);
int factorial(int n) {
cout <<==
if (n "5!0)={" << n << 3
int factorial(int
endl; return 1; n) {
if (n
int==n 0) { 2
} return return
0;
} else
1;
{ int n
return n * factorial(n - 1);
} } else
return
{ 5 n * factorial(n - 1);
}
}
} 2
Recursion in Action
int main() {
int n = factorial(5);
int factorial(int n) {
cout <<==
if (n "5!0)={" << n << 3
int
endl;factorial(int
return 1; n) {
if (n == 0) { 2
int factorial(int
int n n) {
} return return
0; 1;
if (n{== 0) {
} else
int n n * factorial(n - 1); 1
return
return 1;
}
} } }else
else { {5 n * factorial(n - 1); int n
return
} return n * factorial(n - 1);
} 4
}
}
Recursion in Action
int main() {
int n = factorial(5);
int factorial(int n) {
cout <<==
if (n "5!0)={" << n << 3
int
endl;factorial(int
return 1; n) {
if (n == 0) { 2
int n
int factorial(int
return 1; n) {
} return 0;
if (n{== 0) {
} else
int n n * factorial(n - 1); 1
return
return 1;
}
} } }else
else { {5 n * factorial(n - 1); int n
return
} return n * factorial(n - 1);
} 4
}
}
Recursion in Action
int main() {
int n = factorial(5);
int factorial(int n) {
cout <<==
if (n "5!0)={" << n << 3
int
endl;factorial(int
return 1; n) {
if (n == 0) { 2
int factorial(int
int n n) {
} return return
0; 1;
} else
if (n{== 0) {
int n n * factorial(n - 1); 1
return
return
} } else {
} else
int n
} 1; {5 n * factorial(n - 1);
return
} return n * factorial(n - 1);
} 4
}
}
Recursion in Action
int main() {
int n = factorial(5);
int factorial(int n) {
cout <<==
if (n "5!0)={" << n << 3
int
endl;factorial(int
return 1; n) {
if (n == 0) { 2
int factorial(int
int n n) {
} return return
0; 1;
if (n{== 0) {
} else
int n n * factorial(n - 1); 1
return
return 1;
}
} } else
} else
{ int n
return5 n * factorial(n - 1);
} { return n * factorial(n - 1);
} 4
}
}
Recursion in Action
int main() {
int n = factorial(5);
int factorial(int n) {
cout <<==
if (n "5!0)={" << n << 3
int
endl;factorial(int
return 1; n) {
if (n == 0) { 2
int factorial(int
int n n) {
} return return
0; 1;
if (n{== 0) {
} else
int n n * factorial(n - 1); 1
return
return 1;
}
} } }else
else { {5 n * factorial(n - 1); int n
return
} return n * factorial(n - 1);
} 4
}
}
Recursion in Action
int main() {
int n = factorial(5);
int factorial(int n) {
cout <<==
if (n "5!0)={" << n << 3
int
endl;factorial(int
return 1; n) {
if (n == 0) { 2
int factorial(int
int n n) {
} return return
0; 1;
if (n{== 0) {
} else
int n n * factorial(n - 1); 1
return
return 1;
}
} } }else
else { {5 n * factorial(n - 1); int n
return
} return n * factorial(n - 1);
} 4
}
} 1
Recursion in Action
int main() {
int n = factorial(5);
int factorial(int n) {
cout <<==
if (n "5!0)={" << n << 3
int
endl;factorial(int
return 1; n) {
if (n == 0) { 2
int factorial(int
int n n) {
} return return
0; 1;
if (n{== 0) {
} else
int n n * factorial(n - 1); 1
return
return 1;
}
} } else
} else
{ int n
return5 n * factorial(n - 1);
} { return n * factorial(n - 1);
} 4
}
} 1
Recursion in Action
int main() {
int n = factorial(5);
int factorial(int n) {
cout <<==
if (n "5!0)={" << n << 3
int
endl;factorial(int
return 1; n) {
if (n == 0) { 2
int n
int factorial(int
return 1; n) {
} return 0;
if (n{== 0) {
} else 1
int
int return n n * 1;
factorial(int
return n) {
factorial(n - 1);
}
} } else
if (n {
int==
n
5
0) { 0
return n * factorial(n - 1);
return
}
} } else {
} else
int n
1; 4 n * factorial(n - 1);
return
return n * factorial(n - 1);
} }{ 3
}
}
Recursion in Action
int main() {
int n = factorial(5);
int factorial(int n) {
cout <<==
if (n "5!0)={" << n << 3
int
endl;factorial(int
return 1; n) {
if (n == 0) { 2
int n
int factorial(int
return 1; n) {
} return 0;
if (n{== 0) {
} else 1
int
int return n n * 1;
factorial(int
return n) {
factorial(n - 1);
}
} } else
if (n {
int==
n
5
0) { 0
return n * factorial(n - 1);
return
}
} } else {
} else
int n
1; 4 n * factorial(n - 1);
return
return n * factorial(n - 1);
} }{ 3
}
}
Recursion in Action
int main() {
int n = factorial(5);
int factorial(int n) {
cout <<==
if (n "5!0)={" << n << 3
int
endl;factorial(int
return 1; n) {
if (n == 0) { 2
int n
int factorial(int
return 1; n) {
} return 0;
if (n{== 0) {
} else 1
int
int return n n * 1;
factorial(int
return n) {
factorial(n - 1);
}
} } else
if (n {
int==
n
5
0) { 0
return n * factorial(n - 1);
return
}
} } else {
} else
int n
1; 4 n * factorial(n - 1);
return
return n * factorial(n - 1);
} }{ 3
}
}
Recursion in Action
int main() {
int n = factorial(5);
int factorial(int n) {
cout <<==
if (n "5!0)={" << n << 3
int
endl;factorial(int
return 1; n) {
if (n == 0) { 2
int factorial(int
int n n) {
} return return
0; 1;
if (n{== 0) {
} else
int n n * factorial(n - 1); 1
return
return 1;
}
} } else
} else
{ int n
return5 n * factorial(n - 1);
} { return n * factorial(n - 1);
} 4
}
} 1
Recursion in Action
int main() {
int n = factorial(5);
int factorial(int n) {
cout <<==
if (n "5!0)={" << n << 3
int
endl;factorial(int
return 1; n) {
if (n == 0) { 2
int factorial(int
int n n) {
} return return
0; 1;
if (n{== 0) {
} else
int n n * factorial(n - 1); 1
return
return 1;
}
} } else
} else
{ int n
return5 n * factorial(n - 1);
} { return n * factorial(n - 1);
} 4
}
} 1 1
Recursion in Action
int main() {
int n = factorial(5);
int factorial(int n) {
cout <<==
if (n "5!0)={" << n << 3
int
endl;factorial(int
return 1; n) {
if (n == 0) { 2
int factorial(int
int n n) {
} return return
0; 1;
if (n{== 0) {
} else
int n n * factorial(n - 1); 1
return
return 1;
}
} } else
} else
{ int n
return5 n * factorial(n - 1);
} { return n * factorial(n - 1);
} 4
}
} 1 1
Recursion in Action
int main() {
int n = factorial(5);
int factorial(int n) {
cout <<==
if (n "5!0)={" << n << 3
int
endl;factorial(int
return 1; n) {
if (n == 0) { 2
int factorial(int
int n n) {
} return return
0; 1;
if (n{== 0) {
} else
int n n * factorial(n - 1); 1
return
return 1;
}
} } else
} else
{ int n
return5 n * factorial(n - 1);
} { return n * factorial(n - 1);
} 4
}
} ×
1 1
Recursion in Action
int main() {
int n = factorial(5);
int factorial(int n) {
cout <<==
if (n "5!0)={" << n << 3
int
endl;factorial(int
return 1; n) {
if (n == 0) { 2
int factorial(int
int n n) {
} return return
0; 1;
if (n{== 0) {
} else
int n n * factorial(n - 1); 1
return
return 1;
}
} } else
} else
{ int n
return5 n * factorial(n - 1);
} { return n * factorial(n - 1);
} 4
}
} 1
Recursion in Action
int main() {
int n = factorial(5);
int factorial(int n) {
cout <<==
if (n "5!0)={" << n << 3
int factorial(int
endl; return 1; n) {
if (n
int==n 0) { 2
} return return
0;
} else
1;
{ int n
return n * factorial(n - 1);
} } else
return
{ 5 n * factorial(n - 1);
}
}
} 2
Recursion in Action
int main() {
int n = factorial(5);
int factorial(int n) {
cout <<==
if (n "5!0)={" << n << 3
int factorial(int
endl; return 1; n) {
if (n
int==n 0) { 2
} return return
0;
} else
1;
{ int n
return n * factorial(n - 1);
} } else
return
{ 5 n * factorial(n - 1);
}
}
} 2 1
Recursion in Action
int main() {
int n = factorial(5);
int factorial(int n) {
cout <<==
if (n "5!0)={" << n << 3
int factorial(int
endl; return 1; n) {
if (n
int==n 0) { 2
} return return
0;
} else
1;
{ int n
return n * factorial(n - 1);
} } else
return
{ 5 n * factorial(n - 1);
}
}
} 2 1
Recursion in Action
int main() {
int n = factorial(5);
int factorial(int n) {
cout <<==
if (n "5!0)={" << n << 3
int factorial(int
endl; return 1; n) {
if (n
int==n 0) { 2
} return return
0;
} else
1;
{ int n
return n * factorial(n - 1);
} } else
return
{ 5 n * factorial(n - 1);
}
}
} 2 × 1
Recursion in Action
int main() {
int n = factorial(5);
int factorial(int n) {
cout <<==
if (n "5!0)={" << n << 3
int factorial(int
endl; return 1; n) {
if (n
int==n 0) { 2
} return return
0;
} else
1;
{ int n
return n * factorial(n - 1);
} } else
return
{ 5 n * factorial(n - 1);
}
}
} 2
Recursion in Action
int main()
int n = factorial(5);
{ int factorial(int n) {
cout <<== "5!0)={" << n <<
if (n
endl; return
3
1;
int n
} returnreturn
0; n * factorial(n - 1);
} else {
}
} 3
Recursion in Action
int main()
int n = factorial(5);
{ int factorial(int n) {
cout <<== "5!0)={" << n <<
if (n
endl; return
3
1;
int n
} returnreturn
0; n * factorial(n - 1);
} else {
}
} 3 2
Recursion in Action
int main()
int n = factorial(5);
{ int factorial(int n) {
cout <<== "5!0)={" << n <<
if (n
endl; return
3
1;
int n
} returnreturn
0; n * factorial(n - 1);
} else {
}
} 3 2
Recursion in Action
int main()
int n = factorial(5);
{ int factorial(int n) {
cout <<== "5!0)={" << n <<
if (n
endl; return
3
1;
int n
} returnreturn
0; n * factorial(n - 1);
} else {
}
} 3 × 2
Recursion in Action
int main()
int n = factorial(5);
{ int factorial(int n) {
cout <<== "5!0)={" << n <<
if (n
endl; return
3
1;
int n
} returnreturn
0; n * factorial(n - 1);
} else {
}
} 6
Recursion in Action
int main() {
int nFact = factorial(3);
cout << "3! = " << nFact << endl;
return 0;
}
Recursion in Action
int main() {
int nFact = factorial(3); 6
cout << "3! = " << nFact << int nFact
endl;
return
0;
}
When we call factorial(4):
• Is 4 == 0? No, so use recursive case
• Return 4 * factorial(3)
• Is 3 == 0? No, so use recursive case
• Return 3 * factorial(2)
• Is 2 == 0? No, so use recursive
case

Tracing a • Return 2 * factorial(1)


• Is 1 == 0? No, so use
Recursive Call: recursive case
• Return 1 * factorial(0)
• Is 0 == 0? Yes! Return 1
(base case)
• Now we can compute: 1 * 1 =
1
• Now we can compute: 2 * 1 = 2
• Now we can compute: 3 * 2 = 6
• Now we can compute: 4 * 6 = 24
Problem:
• Calculate the sum of all digits in a
positive integer
• For example:
• sum_of_digits(123) = 1 + 2 + 3 = 6
• sum_of_digits(5) = 5
Recursive • sum_of_digits(9876) = 9 + 8 + 7 + 6
= 30
Example: Sum
of Digits 🔢 Recursive Approach:
• Base Case: If n < 10, return n (single
digit)
• Recursive Case:
• Extract last digit with n % 10
• Sum it with the sum of remaining
digits (n / 10)
Summing Up Digits

1 2 5 8
The sum of the
digits of
this number
is equal to…
the sum of the
plus this number.
digits of
this number…

1 2 5 8
Summing Up Digits

1 2 5 8
sumOfDigitsOf(n)
is equal to…

the sum of the


plus this number.
digits of
this number…

1 2 5 8
Summing Up Digits

1 2 5 8
sumOfDigitsOf(n)
is equal to…

sumOfDigitsOf(n / 10) plus this number.

1 2 5 8
Summing Up Digits

1 2 5 8
sumOfDigitsOf(n)
is equal to…

sumOfDigitsOf(n / 10) + (n % 10)

1 2 5 8
Tracing the Recursion
int main() {
int sum = sumOfDigitsOf(137);
cout << "Sum is " << sum <<
endl;
}
Tracing the Recursion
int main() {
int sum = sumOfDigitsOf(137
cout << );
"Sum is
" <<
sum <<
endl;
}
Tracing the Recursion
int main()
int
{int sum = sumOfDigitsOf(137);
sumOfDigitsOf(int n)
{ cout << "Sum is " << sum <<int n 137
} endl;
if (n < 10) {
return n;
return
} else { sumOfDigitsOf(n / 10) + (n %
10);
}
}
Tracing the Recursion
int main()
int
{int sum = sumOfDigitsOf(137);
sumOfDigitsOf(int n)
cout << "Sum is " << sum <<int n
{ if (n < 10) { 137
} endl; return
} else
n; {
return sumOfDigitsOf(n / 10) + (n %
10);
}
}
Tracing the Recursion
int main()
int
{int sum = sumOfDigitsOf(137);
sumOfDigitsOf(int n)
{ cout << "Sum is " << sum <<int n 137
} endl;
if (nreturn
< 10) {
} else
n; {
return sumOfDigitsOf(n / 10) + (n %
10);
}
}
Tracing the Recursion
int main()
int
{int sum = sumOfDigitsOf(137);
sumOfDigitsOf(int n)
{ cout << "Sum is " << sum <<int n 137
} endl;
if (n < 10) {
return n;
return
} else { sumOfDigitsOf(n / 10) + (n %
10);
}
}
Tracing the Recursion
int main()
int
{int sum = sumOfDigitsOf(137);
sumOfDigitsOf(int n)
{ cout << "Sum is " << sum <<int n 137
} endl;
if (n < 10) {
} else { n;
return
retur sumOfDigitsOf(n / + (n %
n 10) 10);
} }
Tracing the Recursion
int main() {
int
int sum = sumOfDigitsOf(137);
sumOfDigitsOf(int n)
cout <<
if (n
{ int
"Sum
< 10) { is " <<n)
sum <<int n 137
sumOfDigitsOf(int
} endl; return n; int n 13
{
} else
if (n{< 10) {
return
returnsumOfDigitsOf(n
n; / 10) + (n %
10);return sumOfDigitsOf(n / 10) + (n %
} else {
} } }
10);
}
Tracing the Recursion
int main() {
int
int sum = sumOfDigitsOf(137);
sumOfDigitsOf(int n)
cout <<
if (n
{ int
"Sum
< 10) { is " << sum <<int n 137
} endl;sumOfDigitsOf(int
return n;
n)
int n 13
{ if (n < 10) {
} else return
{
}return
else
n;
sumOfDigitsOf(n / 10) + (n %
10);
return sumOfDigitsOf(n / 10) + (n %
} } { } 10);
}
Tracing the Recursion
int main() {
int
int sum = sumOfDigitsOf(137);
sumOfDigitsOf(int n)
cout <<
if (n
{ int
"Sum
< 10) { is " <<n)
sum <<int n 137
sumOfDigitsOf(int
} endl; return n; int n 13
{
} else return
{
ifreturn
(n < 10) {
sumOfDigitsOf(n / 10) + (n %
} else
n; {
10);
return sumOfDigitsOf(n / 10) + (n %
} } }
10);
}
Tracing the Recursion
int main() {
int
int sum = sumOfDigitsOf(137);
sumOfDigitsOf(int n)
cout <<
if (n
{ int
"Sum
< 10) { is " <<n)
sum <<int n 137
sumOfDigitsOf(int
} endl; return n; int n 13
{
} else
if (n{< 10) {
return
returnsumOfDigitsOf(n
n; / 10) + (n %
10);return sumOfDigitsOf(n / 10) + (n %
} else {
} } }
10);
}
Tracing the Recursion
int main() {
int
int sum = sumOfDigitsOf(137);
sumOfDigitsOf(int n)
cout <<
if (n
{ int
"Sum
< 10) { is " <<n)
sum <<int n 137
sumOfDigitsOf(int
} endl; return n; int n 13
{
} else
if (n{< 10) {
}return
else sumOfDigitsOf(n / 10) + (n %
return n;
10);return sumOfDigitsOf(n / 10) + (n %
} } { } 10);
}
Tracing the Recursion
int main() {
int
int sum = sumOfDigitsOf(137);
sumOfDigitsOf(int n)
cout
if (n
{ int
<< "Sum
< 10) { is " << sum <<int n 137
} endl;sumOfDigitsOf(int
return n;
if (n < 10) {
n)
int n 13
{} int sumOfDigitsOf(int
else { n)
return n;
{ return sumOfDigitsOf(n / 10) + int n 1
} else {< 10) { (n %
if
10); (n
return
returnsumOfDigitsOf(n
n; / 10) + (n %
} } 10);
} return
else { sumOfDigitsOf(n / 10) + (n %
} } }
10);
}
Tracing the Recursion
int main() {
int
int sum = sumOfDigitsOf(137);
sumOfDigitsOf(int n)
cout
if (n
{ int
<< "Sum
< 10) { is " << sum <<int n 137
} endl;sumOfDigitsOf(int
return n;
if (n < 10) {
n)
int n 13
{} int sumOfDigitsOf(int
else { n)
return n;
if (n <sumOfDigitsOf(n
10) { int n 1
{}return
else return
{ / 10) + (n %
10);return sumOfDigitsOf(n / 10) + (n %
} } } else
n;
10);
return sumOfDigitsOf(n / 10) + (n %
} } { } 10);
}
Tracing the Recursion
int main() {
int
int sum = sumOfDigitsOf(137);
sumOfDigitsOf(int n)
cout
if (n
{ int
<< "Sum
< 10) { is " << sum <<int n 137
} endl;sumOfDigitsOf(int
return n;
if (n < 10) {
n)
int n 13
{} int sumOfDigitsOf(int
else { n)
return n;
if (n <sumOfDigitsOf(n
10) { int n 1
{}return
else return
{ / 10) + (n %
10);return sumOfDigitsOf(n / 10) + (n %
} } } else
n;
10);
return sumOfDigitsOf(n / 10) + (n %
} } { } 10);
}
Tracing the Recursion
int main() {
int
int sum = sumOfDigitsOf(137);
sumOfDigitsOf(int n)
cout <<
if (n
{ int
"Sum
< 10) { is " <<n)
sum <<int n 137
sumOfDigitsOf(int
} endl; return n; int n 13
{
} else
if (n{< 10) {
}return
else
return
sumOfDigitsOf(n / 10) + (n %
n;
10);
} } } { return sumOfDigitsOf(n / 10) + (n %
10);
} 1
Tracing the Recursion
int main() {
int
int sum = sumOfDigitsOf(137);
sumOfDigitsOf(int n)
cout <<
if (n
{ int
"Sum
< 10) { is " <<n)
sum <<int n 137
sumOfDigitsOf(int
} endl; return n; int n 13
{
} else
if (n{< 10) {
return
returnsumOfDigitsOf(n
n; / 10) + (n %
10);return sumOfDigitsOf(n / 10) + (n %
} else {
} } }
10);
} 1
Tracing the Recursion
int main() {
int
int sum = sumOfDigitsOf(137);
sumOfDigitsOf(int n)
cout <<
if (n
{ int
"Sum
< 10) { is " <<n)
sum <<int n 137
sumOfDigitsOf(int
} endl; return n; int n 13
{
} else
if (n{< 10) {
return
returnsumOfDigitsOf(n
n; / 10) + (n %
10);return sumOfDigitsOf(n / 10) + (n %
} else {
} } }
10);
} 1 + 3
Tracing the Recursion
int main() {
int
int sum = sumOfDigitsOf(137);
sumOfDigitsOf(int n)
cout <<
if (n
{ int
"Sum
< 10) { is " <<n)
sum <<int n 137
sumOfDigitsOf(int
} endl; return n; int n 13
{
} else
if (n{< 10) {
return
returnsumOfDigitsOf(n
n; / 10) + (n %
10);return sumOfDigitsOf(n / 10) + (n %
} else {
} } }
10);
} 4
Tracing the Recursion
int main()
int
{int sum = sumOfDigitsOf(137);
sumOfDigitsOf(int n)
{ cout << "Sum is " << sum <<int n 137
} endl;
if (n < 10) {
} else { n;
return
retur sumOfDigitsOf(n / + (n %
n 10) 10);
} } 4
Tracing the Recursion
int main()
int
{int sum = sumOfDigitsOf(137);
sumOfDigitsOf(int n)
{ cout << "Sum is " << sum <<int n 137
} endl;
if (n < 10) {
return n;
return
} else { sumOfDigitsOf(n / 10) (n % 10);
} +
} 4
Tracing the Recursion
int main()
int
{int sum = sumOfDigitsOf(137);
sumOfDigitsOf(int n)
{ cout << "Sum is " << sum <<int n 137
} endl;
if (n < 10) {
return n;
return
} else { sumOfDigitsOf(n / 10) (n % 10);
} +
} 4 + 7
Tracing the Recursion
int main()
int
{int sum = sumOfDigitsOf(137);
sumOfDigitsOf(int n)
{ cout << "Sum is " << sum <<int n 137
} endl;
if (n < 10) {
return n;
return
} else { sumOfDigitsOf(n / 10) (n % 10);
} +
} 11
Tracing the Recursion
int main() {
int sum = sumOfDigitsOf(137
cout << );
"Sum is
" <<
sum <<
endl;
}
1
1
Thinking Recursively
if (The problem is very simple) { These simple cases
are called
Directly solve the
base cases.
problem. Return the
solution.
} else {
Split the problem into one or
more smaller problems with
the same structure as the
original.
Solve each of those smaller
problems. These are the
Combine the results to get the recursive cases.
overall solution.
Return the overall solution.
S106B Official Resources:
• 🔗 CS106B Course Website
• 📖 Programming Abstractions in C++ - The course textbook

Resource • 🎬 Stanford Engineering Everywhere - Video lectures


C++ Language Resources:

s for • 📘 cppreference.com - Comprehensive C++ reference


• 🌐 cplusplus.com - Tutorials and reference

Further • 📱 C++ Core Guidelines - Best practices


Recursion Practice:

Learning
• 🧩 LeetCode - Recursion problems with solutions
• 🎮 Recursion Visualization - Interactive visualizations
• 📝 HackerRank - Practice problems

📚 Git and GitHub:


• 🐙 GitHub Learning Lab - Interactive tutorials
• 📑 Git Cheat Sheet - Quick reference
• 🎓 GitHub Classroom Guide - Help documentation

You might also like