To find the frequency of an element in an array


In this we are going to see a program to find the frequency of an element in an array in C++ Programming Language.



The Code given below can be used in TURBO C++ Compilers: -

#include <iostream.h>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>


void main()
{
    srand(time(0));
    int y;
    int a[10];
    cout << "Enter a number between 0 to 10: ";
    cin >> y;
    int count = 0;
    for (int x = 0; x < 10; x++)
    {

        int r = 1 + rand() % 10;
        a[x] = r;

        if (y == a[x])
        {
            count = count + 1;
        }
        cout << r << " ";
    }
    cout << "\nThe number of times " << y << " occurs is " << count << endl;
    getch();
    clrscr();
}


The Code given below can be used in gcc/g++ Compilers: -

#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;

int main()
{
    srand(time(0));
    int y;
    int a[10];
    cout << "Enter the number:";
    cin >> y;
    int count = 0;
    for (int x = 0; x < 10; x++)
    {

        int r = 1 + rand() % 10;
        a[x] = r;

        if (y == a[x])
        {
            count = count + 1;
        }
        cout << r << " ";
    }
    cout << "\nThe number of times " << y << " occurs is " << count << endl;
    return 0;
}


#ENJOY CODING

Post a Comment

FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP

Previous Post Next Post