×

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

Factorial Program in C using For Loop

Before move on the factorial program. We have to know about the for loop in C programming language. The syntax of the For Loop is: for (initialization statement; test expression; an increment...

3 minutes read.

C Token

C Token and Keyword The C tokens are the basic buildings blocks in C language which are constructed together to write a C program. Each and every smallest individual unit in the C program is...

2 minutes read.

Pointers in C

Pointers in C: In the C programming language, a pointer is a pointer variable that points to the address of the other variable. It also stores the address of the...

4 minutes read.

OpenGL in C

OpenGL stands for Open Graphics Library. It is a low-level, widely supported modeling and rendering software package that is widely available across all the platforms. It is an API used...

4 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.

strcat() Function in C

Strings in c: A string is defined as the set of characters that are enclosed within the double quotations (" "). The String always ends with the null character ("\0"). The...

3 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.

Difference between Array and List in C

C# Array vs List is where the abstraction and implementation of human computing meet.  Arrays are incredibly related to the hardware concept of contiguous and contiguous memory, where each part is...

3 minutes read.

LCM of two numbers in C

LCM is a mathematical term which stands for Least Common Multiple. LCM of any two numbers is the smallest positive value(number) which is evenly divisible by the two given numbers. Consider...

4 minutes read.

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.

Dos.h Header File in C Language

Dos.h is a header file in C. Interrupt handling, sound generation, date and time functions, and other tasks can be performed using the functions in this library. This is exclusive to...

4 minutes read.

Pascal Triangle in C

The pascal triangle in c is an array of binomial coefficients in triangular form. Here the nth row contains the binomial coefficient of ncr.In a pascal triangle, every number is...

2 minutes read.

Multiplication table program in C using For loop

Before we move on the program of multiplication table of any natural number, first we have to know about the For loop statement. The syntax of ‘for’ loop in C programming...

3 minutes read.

What are linker and loader in C

Linker and loader are utility programs that have a significant role in executing a program. Linker: A linker is a program that joins the object files produced by the assembler/ compiler...

3 minutes read.

Prototype in C

A prototype is nothing but a model, a model of initial creation of an intended product. Similarly, in the C programming language, all functions have a prototype. In general, all...

4 minutes read.

Execution flow of C program

There are various steps of the execution of the C program that is given below. The following step of the execution Write source codes Preprocess Compile Link edit Load Execute Editor or...

1 minute read.

C printf and Scanf

Input-Output functions in C Programming In C Language, the printf() and scanf() are inbuilt library functions that used for input and output. It is defined in the header file“<stdio.h>”. printf() Function: In C...

2 minutes read.

Find the Largest Three Distinct Elements in an Array using C/C++

In this tutorial, we will demonstrate how to use a C/C++ programme to locate the highest three different elements in an array. C/C++ Program to Find the Largest Three Different Elements...

3 minutes read.

Union in C

Union is a user-defined data type that is used to hold the different types of elements like structure. In union, all members share the same memory location. Syntax: union [union name] { ...

1 minute read.

Difference between If Else and Nested If Else in C

What is the If Else statement in C? In C programs, if else conditions are used to perform some operations based on specific conditions. The operation is only executed if the...

6 minutes read.