Frequency count of the Element in an array

kavyadev 170 views 6 slides Sep 12, 2020
Slide 1
Slide 1 of 6
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6

About This Presentation

This presentation gives you a very depth knowledge on algorithm and how to write the logic for a given problem and how to write a procedural program using C++ language


Slide Content

Find the frequency of presence of an element in an array???

Agenda Program in c++ Array declaration and definition Enter the element to be counted Counter or freq variable declaration Print the particular element along with a number how many times that number gets repeated in the given array

Logic Given array for example: arr[]={1,2,3,2,3,2,2} Element to be find is 2 ( No.of times repeated) How to find the repeated element present in an array Soln:Using for loop: for ( i =0;i< n;i ++) if(a[ i ]== ele )(main logic) counter++;

Let’s See Practically by writing the program

#include< iostream.h > #include< conio.h > void main() { int a[100], ele,n,i,freq ; clrscr (); cout<<"Enter the size of array"<< endl ; cin>>n; cout<<"Enter the elements of array"<< endl ; for( i =0;i< n;i ++) { cin>>a[ i ]; } cout<<"Enter the element to be counted"<< endl ; cin>> ele ; freq=0; for( i =0;i< n;i ++) { if(a[ i ]== ele ) { freq=freq+1; } } cout<<"Frequency of "<< ele <<" is "<<freq; getch (); }

Thank You