R14 Count occurrences of an element (Recursion) Dry Run in C

R14 Count occurrences of an element (Recursion) is an interactive C dry run visualizer from the Recursion programs section. Study the source code, then use the execution controls to follow each step, variable update, highlighted line, and console output.

This page provides a browser-based dry run with source-code highlighting, auto-scroll, voice narration controls, and execution output for learning the program step by step.

R14 Count occurrences of an element (Recursion) Program Code

#include <stdio.h>

int countOccurrences(int arr[], int n, int target, int index) {
    if(index == n) {
        return 0;
    }

    int countFromRight = countOccurrences(arr, n, target, index + 1);
    int currentCount;

    if(arr[index] == target) {
        currentCount = 1;
    } else {
        currentCount = 0;
    }

    int totalCount = currentCount + countFromRight;
    return totalCount;
}

int main() {
    int arr[] = {8, 3, 8, 3, 9, 8, 2};
    int target = 8;
    int cnt = countOccurrences(arr, 7, target, 0);
    printf("%d appears %d times\n", target, cnt);
    return 0;
}

View the complete Recursion programs page.

Program Console R14 Count occurrences of an element (Recursion) Topic: C Vignaankosh.com
Execution Panel
Step 0/0
Console is empty.