📖 Scenario: Imagine you have a list of daily expenses and you want to find the total amount spent. Instead of adding them one by one using a loop, you will use a method that calls itself to find the total.
🎯 Goal: Build a recursive method to calculate the sum of all elements in an integer array.
📋 What You'll Learn
Create an integer array called expenses with the exact values: 100, 200, 300, 400, 500
Create an integer variable called length that stores the length of the expenses array
Write a recursive method called SumArray that takes the array and its length as parameters and returns the sum of the elements
Call the SumArray method with expenses and length and print the result
💡 Why This Matters
🌍 Real World
Recursion helps solve problems like summing expenses, searching, or processing data step-by-step, similar to peeling layers of an onion.
💼 Career
Understanding recursion is important for software developers to solve problems efficiently and write clean code for tasks like searching, sorting, and processing data.
Progress0 / 4 steps
1
Create the array of expenses
Create an integer array called expenses with these exact values: 100, 200, 300, 400, 500
DSA C#
Hint
Use int[] expenses = {100, 200, 300, 400, 500}; to create the array.
2
Store the length of the array
Create an integer variable called length that stores the length of the expenses array
DSA C#
Hint
Use expenses.Length to get the array length.
3
Write the recursive method to sum the array
Write a recursive method called SumArray that takes an integer array arr and an integer n as parameters and returns the sum of the first n elements. Use the base case when n is 0 to return 0, otherwise return the last element plus the sum of the rest.
DSA C#
Hint
Check if n is 0 to stop recursion. Otherwise add the last element arr[n - 1] to the sum of the rest.
4
Call the recursive method and print the result
Call the SumArray method with expenses and length as arguments and print the returned sum using Console.WriteLine
DSA C#
Hint
Call SumArray(expenses, length) and print the result with Console.WriteLine.
Practice
(1/5)
1. What is the main idea behind using recursion on arrays or strings?
easy
A. Sorting the array or string before processing
B. Using loops to iterate over each element
C. Breaking the problem into smaller similar problems until a base case is reached
D. Storing all elements in a new data structure
Solution
Step 1: Understand recursion concept
Recursion solves problems by calling itself with smaller inputs until a simple base case is reached.
Step 2: Apply to arrays or strings
For arrays or strings, recursion processes one element and then calls itself on the rest.
Final Answer:
Breaking the problem into smaller similar problems until a base case is reached -> Option C
Quick Check:
Recursion = divide and conquer [OK]
Hint: Think: solve small part, then repeat on smaller input [OK]
Common Mistakes:
Confusing recursion with loops
Ignoring the base case
Trying to process all elements at once
2. Which of the following is the correct base case for a recursive function that processes a string character by character?
easy
A. if (str[0] == '\0') return;
B. if (str == null) return;
C. if (str.Length > 0) return;
D. if (str.Length == 0) return;
Solution
Step 1: Identify base case for string recursion
The base case should stop recursion when the string is empty (length zero).
Step 2: Check options
if (str.Length == 0) return; correctly checks if string length is zero to stop recursion.
Final Answer:
if (str.Length == 0) return; -> Option D
Quick Check:
Base case = empty string check [OK]
Hint: Base case usually checks for empty string or array length zero [OK]
Common Mistakes:
Checking for null instead of empty string
Using wrong condition like length > 0
Checking for null character '\0' in C# strings
3. What will be the output of this C# recursive function call?
static int SumArray(int[] arr, int index) {
if (index == arr.Length) return 0;
return arr[index] + SumArray(arr, index + 1);
}
int[] nums = {1, 2, 3};
Console.WriteLine(SumArray(nums, 0));
medium
A. 6
B. 0
C. 3
D. StackOverflowException
Solution
Step 1: Trace recursive calls
SumArray called with index 0 returns arr[0] + SumArray(1) = 1 + ...
SumArray(1) returns 2 + SumArray(2)
SumArray(2) returns 3 + SumArray(3)
SumArray(3) returns 0 (base case)
Step 2: Calculate total sum
Sum = 1 + 2 + 3 + 0 = 6
Final Answer:
6 -> Option A
Quick Check:
Sum of [1,2,3] = 6 [OK]
Hint: Add current element + recursive sum of rest [OK]
Common Mistakes:
Forgetting base case leads to infinite recursion
Returning wrong index or value
Confusing index with length
4. Identify the error in this recursive function that reverses a string: