w3resource

C Exercises: Return the number of clumps in a given array


Write a program in C to return the number of clumps (a series of 2 or more adjacent elements of the same value) in a given array.

Expected Output:
The given array is:
17 42 42 7 24 24 17 54 17
The number of clumps in the array is: 2

The task is to write a C program that counts the number of clumps in a given array. A clump is defined as a series of two or more adjacent elements with the same value. The program should traverse the array, identify and count these clumps, and then display the total number of clumps found.

Sample Solution:

C Code:

#include<stdio.h>

// Function to count the number of clumps in an array
int countClumps(int arr1[], int m) 
{
    int l = m;
    int current = -1, clump = 0;

    // Iterate through the array to find clumps
    for(int i = 0; i < l - 1; i++) 
    {
        // Check if the current element is equal to the next element and different from the current clump
        if(arr1[i] == arr1[i + 1] && arr1[i] != current) 
        {
            current = arr1[i]; // Set the current clump value
            clump++; // Increment clump count
        } 
        else 
        {
            if(arr1[i] != current) 
            {
                current = -1; // Reset current clump value if the current element is different
            }
        }
    }
    return clump; // Return the count of clumps found
}

int main() 
{ 
    int arr1[] = {17, 42, 42, 7, 24, 24, 17, 54, 17}; 
    int arr_size = sizeof(arr1) / sizeof(arr1[0]); 
    int i = 0; 

    // Print the original array
    printf("The given array is:  \n");
    for(i = 0; i < arr_size; i++)
    {
        printf("%d  ", arr1[i]);
    }
    printf("\n");

    // Count the number of clumps in the array and print the result
    printf("The number of clumps in the array is:  %d", countClumps(arr1, arr_size)); 
    return 0; 
}

Output:

The given array is:  
17  42  42  7  24  24  17  54  17  
The number of clumps in the array is:  2

Visual Presentation:

C Exercises: Return the number of clumps in a given array

Flowchart:/p> Flowchart:  Return the number of clumps in a given array

C Programming Code Editor:



Previous: Write a program in C to check if an array can be splitted in such a position that, the sum of left side of the splitting is equal to the sum of the right side.
Next: Write a program in C to rearrange an array such that arr[i]=i.

//------------- this portion above included on 07-02-2025 ------------------------------ var new_txt = 'Based on '+total_submit+' votes, average difficulty level of this exercise is '+difficulty+'.'; //'.  '+difficulty+'/3'; var txt_node = document.createTextNode(new_txt); var level_result = document.getElementById('level_result'); level_result.appendChild(txt_node); } } else { alert('There was a problem with the request.'); } } } } function insert_level(event) { event.preventDefault(); var path = window.location; var page = path.href; var page = page.split('?'); var page = page[0]; //console.log(page); //console.log(page); /*var btns = document.getElementsByClassName("mdl-button mdl-js-button mdl-button--raised mdl-button--colored"); for (var i = 0; i < btns.length; i++) { var clicked = btns[i].id; }*/ var clicked = this.id; if(clicked=="easy") clicked=1; if(clicked=="medium") clicked=2; if(clicked=="hard") clicked=3; console.log(clicked); var httpRequest1 = new XMLHttpRequest(); if (!httpRequest1) { alert('Giving up :( Cannot create an XMLHTTP instance'); //return false; } var url = "/assets/level_insert.php"; var data1 = "level=" + clicked + "&page=" + page; httpRequest1.onreadystatechange = displayContent1; httpRequest1.open("POST", url, true); httpRequest1.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); httpRequest1.send(data1); console.log(data1); //console.log("found"); function displayContent1(responseText) { if (httpRequest1.readyState === XMLHttpRequest.DONE) { if (httpRequest1.status === 200) { var op = httpRequest1.responseText; console.log(op); } else { alert('There was a problem with the request.'); } } } } var easy = document.getElementById("easy"); easy.addEventListener('click', insert_level, false); var medium = document.getElementById("medium"); medium.addEventListener('click', insert_level, false); var hard = document.getElementById("hard"); hard.addEventListener('click', insert_level, false);

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.