C Exercises: Return the number of clumps in a given array
C Array: Exercise-100 with Solution
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.
Sample Solution:
C Code:
#include<stdio.h>
int countClumps(int arr1[], int m)
{
int l = m;
int current = -1, clump = 0;
for(int i = 0; i < l - 1; i++)
{
if(arr1[i] == arr1[i + 1] && arr1[i] != current)
{
current = arr1[i];
clump++;
}
else
{
if(arr1[i] != current)
{
current = -1;
}
}
}
return clump;
}
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 original array ------------------
printf("The given array is: \n");
for(i = 0; i < arr_size; i++)
{
printf("%d ", arr1[i]);
}
printf("\n");
//-----------------------------------------------------------
printf("The number of clumps in the array is: %d", countClumps(arr1,arr_size));
return 0;
}
Sample Output:
The given array is: 17 42 42 7 24 24 17 54 17 The number of clumps in the array is: 2
Pictorial Presentation:
Flowchart:

C Programming Code Editor:
Improve this sample solution and post your code through Disqus.
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.
C Programming: Tips of the Day
C Programming - What is the argument for printf that formats a long?
Put an l (lowercased letter L) directly before the specifier.
unsigned long n; long m; printf("%lu %ld", n, m);
Ref : https://bit.ly/3dIwfkP
- Weekly Trends
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
- JavaScript functions Exercises
- Python Tutorial
- Python Array Exercises
- SQL Cross Join
- C# Sharp Array Exercises