Principles of Computer Programming II Week - 0: Irakli Iremashvili
Principles of Computer Programming II Week - 0: Irakli Iremashvili
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
Process &
• Expands macros and includes header files
• Compilation 🔄
Building on •
•
Stream-based I/O using << and >> operators
More type-safe than C's I/O functions
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:
Libraries
• Handles text display formatting
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
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
Example: o
o
n × (n-1) × (n-2) × ... × 2 × 1
0! is defined as 1
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
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…
1 2 5 8
Summing Up Digits
1 2 5 8
sumOfDigitsOf(n)
is equal to…
1 2 5 8
Summing Up Digits
1 2 5 8
sumOfDigitsOf(n)
is equal to…
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
Learning
• 🧩 LeetCode - Recursion problems with solutions
• 🎮 Recursion Visualization - Interactive visualizations
• 📝 HackerRank - Practice problems