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
Size: 33.27 KB
Language: en
Added: Sep 12, 2020
Slides: 6 pages
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 (); }