A program to Search element in array , and if repeated so at which location it is present and how many time the number is repeated
CODE:-
//FIXED-FINAL UPDATE FULLY WORKING CODE
#include <iostream>
using namespace std;
int i;
// I've created available() and notavailable() function and call it in the function named searching ();
void available (int n,int key,int arr[]){
for(int i=0,j=1;i<n,j<n+1;i++,j++){
if(arr[i]==key){
cout<<"the element present at : "<<j<<" location" <<endl;
}
}
}
void notavailable(int n,int key,int[]){
int arr[n];
for(int i=0;i<n;i++){
if(arr[i]!=key){
cout<<"the element is not present ";
break;
}
}
}
// Function to search in the array
void searching(int arr[], int n , int key){
cout<<endl;
if(arr[i]==key){
available (n,key,arr);
}
else if(arr[i]!=key){
if(arr[i]!=key){
available(n,key,arr);
}
else if(arr[i]==key){
notavailable (n,key,arr);
}
}
}
int repeat=0;
// Function to know how many times it's repeated in array
void times(int arr[],int n,int key ){
for (int i =0;i<n ;i++){
if(arr[i]==key){
repeat++;
}
}
if(repeat==0){
cout<<"Element is not present in Array ";
cout<<endl;
}
else{
cout<<"Element repeated "<<repeat<<" times "<<endl;
}
}
//main body
int main (){
int n ; // n for Size of array
cout<<"Enter Size of array : ";
cin>>n;
int arr[n];
for(int i =0 ,j=1;i<n,j<n+1;i++,j++){
cout<<"Enter Element in "<<j<< " st location ";
cin>>arr[i];
}
int key ;
cout<<endl<<"enter the number you want to search in the given array: ";
cin>>key;
// This function will will print in which location the number is available or not
searching(arr,n,key);
cout<<endl;
//This function will print how many times the number is repeated
times(arr,n,key);
return 0;
}
Comments
Post a Comment