Create a dynamic array of user defined size. Now take input in array from user. Take a new (integer) value from user and search that how many times the entered number is present in the array.
#include<iostream>
#include<string>
using namespace std;
int main(){
int size;
int numberToSearch;
int counter=0;
int* numbers;
//Create a dynamic array of user defined size.
cout<<"Enter the size of the array: ";
cin>>size;
numbers=new int[size];
//Now take input in array from user.
for(int i=0;i<size;i++){
cout<<"Enter the number "<<(i+1)<<": ";
cin>>numbers[i];
}
//Take a new (integer) value from user and search that how many
//times the entered number is present in the array.
cout<<"\nEnter the number to search: ";
cin>>numberToSearch;
for(int i=0;i<size;i++){
if(numbers[i]==numberToSearch){
counter++;
}
}
cout<<"\n"<<counter<<" times the number "<<numberToSearch<<" is present in the array.\n\n";
delete[] numbers;
cin>>size;
return 0;
}
Comments
Leave a comment