📖 Scenario: Imagine you have a list of daily expenses saved in an array. You want to find the total amount spent using a simple program.
🎯 Goal: Build a C program that uses recursion to calculate the sum of all elements in an integer array.
📋 What You'll Learn
Create an integer array with exact values
Create a variable for the array size
Write a recursive function to sum the array elements
Print the final sum
💡 Why This Matters
🌍 Real World
Recursion helps solve problems like summing expenses, searching, or processing data step-by-step in many software applications.
💼 Career
Understanding recursion is important for software developers to write clean and efficient code for tasks like data processing, algorithms, and problem-solving.
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 curly braces to list the values inside the array.
2
Set the size of the array
Create an integer variable called size and set it to the number of elements in the expenses array, which is 5.
DSA C
Hint
Count the number of elements in the array and assign it to size.
3
Write the recursive function to sum the array
Write a recursive function called sumArray that takes two parameters: an integer array arr and an integer n representing the size. The function should return 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
Use the last element arr[n - 1] and call sumArray with n - 1.
4
Print the sum of the array
Call the sumArray function with expenses and size, store the result in an integer variable called total, and print total using printf.
DSA C
Hint
Use printf("%d\n", total); to print the sum.
Practice
(1/5)
1. What does recursion mean when applied to arrays or strings?
easy
A. A function calls itself to solve smaller parts of the problem
B. A loop runs through all elements once
C. The array or string is sorted automatically
D. The data is copied to a new array or string
Solution
Step 1: Understand recursion concept
Recursion means a function calls itself to break down a problem into smaller parts.
Step 2: Apply to arrays or strings
For arrays or strings, recursion processes one element and then calls itself on the rest.
Final Answer:
A function calls itself to solve smaller parts of the problem -> Option A
Quick Check:
Recursion = function calls itself [OK]
Hint: Recursion always involves a function calling itself [OK]
Common Mistakes:
Confusing recursion with loops
Thinking recursion sorts data automatically
Believing recursion copies data
2. Which of the following is the correct base case for a recursive function that processes a string?
easy
A. if (length < 0) return;
B. if (length == 0) return;
C. if (string == NULL) continue;
D. if (length > 0) return;
Solution
Step 1: Identify base case role
The base case stops recursion when no more characters remain, usually length 0.
Step 2: Check options for correct base case
if (length == 0) return; correctly returns when length is 0, stopping recursion safely.
Final Answer:
if (length == 0) return; -> Option B
Quick Check:
Base case stops recursion at empty string [OK]
Hint: Base case usually checks for empty string or zero length [OK]