×

Program to Find Mode of an Array in C

Mode is the highest occurring value or number in a given set of data elements. In a group of data values, a value that occurs most frequently is considered to be the mode of the group. Mode is a measure of central tendency. Central tendency includes mean, median, and mode.

The relationship between mean, median, and mode is:-

Mode = 3(Median) – 2(Mean)

Example:

Let R={1,2,3,5,3,2,2} be a set of data elements.

The mode of this set is 2. This is because value 2 has occurred three times in the set, which is the highest frequency in the given set.

Can there be more than one mode for a set?

Yes, a set can have more than one mode. A set can also have no mode.

Example 1

Set with no mode:

R={1,2,3,4}

Here, the set R has no mode because the highest frequency is one for every value in the set.

Example 2

Set with one mode:

R={1,1,2,3,4}

Here, the set R has one mode because the value 1 has the highest frequency in the given set.

Example 3

Set with two modes:

R={1,1,2,2,3,4}

Here, the set R has two modes because values 1 and 2 share the highest frequency in the given set.

Mode is based on the highest occurring number or the highest count for a value in a set. It becomes easier to find the mode by collecting and organizing the data.

Algorithm to write a mode program

An algorithm is a process where a set of rules are to be followed to produce results.

Step 1: Start
Step 2: Take an integer set ‘R’ of n values
Step 3: Find the frequency of occurrence for every value in ‘R’
Step 4: Display the value that has the highest occurrence in ‘R’
Step 5: Stop

Pseudocode to write a mode program

Pseudocode is one of the ways to write algorithms. It is the most straightforward way of writing an algorithm as it contains English -like statements.

Method mode()
BEGIN
Array  S
FOR EACH value p in S DO
Set Count to 0
FOR i FROM 0 to p DO
IF S[p] = S[i]
Increment Count
END IF
END FOR


IF Count > MaxCount
MaxCount = Count
Val = S[p]
END IF
END FOR
DISPLAY Val as Mode
END

Mode Program in C

#include <stdio.h>


int mode(int s[],int n) {
   int maxValue = 0, maxCount = 0, p, i;


   for (p = 0; p< n; ++p) {
      int count = 0;
      
      for (i= 0; i < n; ++i) {
         if (s[i] == s[p])
         ++count;
      }
      
      if (count > maxCount) {
         maxCount = count;
         maxValue = s[p];
      }
   }


   return maxValue;
}


int main() {
   int n = 5;
   int s[] = {0,6,5,2,6};


   printf("Mode for the set is %d ", mode(s,n));


   return 0;
}

Output

Mode for the set is 6

Mode Program in C(dynamic)

#include<stdio.h>  
  void main()
  {
    int p[10],q[10]; int b=0,j,a,i,count=1,n,big;
    printf("Enter the number of set elements : ");
    scanf("%d",&n);
    printf("Enter elements:\n");
    for(i=0;i<n;i++)
    {
        scanf("%d",&p[i]);
    }
    for(i=0; i<n; i++)
    {
        //printing element
        printf("%d ",p[i]);
    }
    for(i=0; i<n; i++)
    {
        for(j=i+1; j<=n; j++)
        {
            if(p[i]==p[j])
            count++;
        }
        q[b]=count;
        b++;
        count=1;
    }
    printf("\n");
    for(i=0; i<b; i++)
    {
        printf("%d ",q[i]);
    }
    big=q[0];
    for(i=1;i<=b;i++)
    {
        if(q[i]>big)
        {
            big=q[i];
            a=i;
        }
    }
    printf("\nPresent at position : %d ",a);
    printf("\nMode is %d Occurring %d times ",p[a],q[a]);
  }

Output

Enter the number of set elements : 5
Enter elements:
1
2
2
3
4
1 2 2 3 4 
1 2 1 1 1 
Present at position : 1 
Mode is 2 Occurring 2 times

Explanation

In the above program, the number of elements in the set is 5.

The elements are: {1,2,2,3,4}

Here, the value 1 has occurred only once in the set. Similarly, values 3 and 4 occurred only once. Therefore, the frequency of values 1,3 and 4 is one, whereas the frequency of value 2 is two as it occurred more than once.

Mode is the highest occurring value which is 2 in the above case.

Mode program in C (when mean and median are provided)

#include<stdio.h>
void mode(double mean,double median);
void main(){
    double m,me;
    scanf("%lf%lf",&m,&me);
    mode(m,me);
}
void mode(double mean,double median)
{
    double mode;
    mode=3*median-2*mean;
    printf("mode = %.2lf",mode);
}

Input

54 55.5

Output

mode = 58.50

Related Topics

Isalnum() function in C

Introduction: The isalnum () is a function used in C programming language. This function checks the passing number or argument is an alphanumeric number or not. The alphanumeric number consists of the...

3 minutes read.

Macros in C

In the C programming language, the macro is defined as a segment of the code replaced by the macro defined value at the beginning. ‘#define’ is the directive that defines...

3 minutes read.

Bank Account System in C using File Handling

This tells about the creation of bank account system using the C language and handling of files in C. Approach Let us see the approaches and the functions how they are covering...

5 minutes read.

What is a buffer in C?

A buffer is an area of memory set aside for the temporary storage of data. A data buffer (or just buffer) is a region of a physical memory storage used to...

5 minutes read.

Passing Array to Function in C

Need to pass Arrays? The need to pass arrays to a function arises when we need to pass a list of values to a given function. During the course of our programming...

4 minutes read.

FIFO Example in the C Language

FIFO is an acronym that means "First In, First Out." It is a data structure handling method in which the first element is handled first, and the last element is...

3 minutes read.

Control statement in C

What is a control statement? A control statement helps us to control the flow of the program. The control statement helps us to execute the program's instructions in a user-defined order....

8 minutes read.

Prime Number Program in C using for Loop

In this article, we will know about the procedure of checking whether a natural number inputted by the user is a prime number or non-prime number. Definition of the prime number A...

3 minutes read.

Find a subarray with a given sum.

Find a subarray with a given sum. The simple solution is to recognize all subarrays one by one and to check each subarray's sum. The quick solution follows the following program. Algorithm: From...

4 minutes read.

Program to Find Mode of an Array in C

Mode is the highest occurring value or number in a given set of data elements. In a group of data values, a value that occurs most frequently is considered to...

3 minutes read.

Booleans in C

Introduction to Boolean With all the complexities of programming, it can take time to understand the basics. One concept, in particular, that confuses many beginners is the use of Booleans in...

6 minutes read.

Bigint (BIG INTEGERS) in C with Example

The maximum number of digits that a long, long int can have in C/C++ is 20. The issue is how to store the 22-digit number, which is difficult to do...

9 minutes read.

Modulus on Negative Numbers in C

In this tutorial, we will explore some examples of modulus on negative number. What is Modulus of Negative number? By omitting the minus sign, one can determine the modulus of a negative...

3 minutes read.

Cbrt() function in C

Introduction: The Cbrt is a function used in C programming language. The cbrt() function is a math function. Using the cbrt function, we can do the cube root of a function. This...

4 minutes read.

Range of int in C

In this session we know about the rage of the int in C language with the suitable examples and the programs. Firstly, we can learn the range of int: The int is...

3 minutes read.

Git Hooks

Git Hooks Overview Just like other version control systems, Git has its way to deliver customized scripts whenever some important activity occurs. The hooks act just like the triggers or catalysts...

4 minutes read.

abs() function in C

abs() function is a built-in function present in the stdlib.h header file. It returns an integer value. abs() function converts a negative value into a positive value. For example, if...

4 minutes read.

Fahrenheit to Celsius in C

Before going into conversion, we have to know what Fahrenheit and Celsius mean, these are both units to measure the temperature.In our daily life, we use both Fahrenheit and Celsius,...

1 minute read.

Volatile in C

Introduction A volatile keyword is a qualifier in C. Qualifiers are nothing but keywords which are used to modify the properties of a variable. Qualifiers are of two types: 1) Const The const type...

3 minutes read.

Pi() Function in C

Pi: Mathematic constants are not defined in the c or c++ programming languages. To use the Mathematical constants firstly, we have to define the _USE_MATH_DEFINES and then declare the cmath and...

3 minutes read.