Writing a function that calculates the occurrence of an integer A in an integer array T of size N.
#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
int main()
{
int N,A;
do
{
cout<<"\nPlease, enter a size of array T (0 - exit program): ";
cin>>N;
if(N==0)break;
int *T=new int[N];
//Seed generator of random numbers
srand(static_cast<unsigned int>(time(0)));
//Fill array of N elements by random numbers from 0 to 10 and print it
for(int i=0;i<N;i++)
{
T[i]=rand()%10;
cout<<T[i]<<" ";
//Make line break after 20 numbers for easy reading
if((i+1)%20==0)cout<<endl;
}
//A can be from 0 to 9
cout<<"\nPlease, enter an integer A to find its occurences: ";
cin>>A;
//Find occurences of A
int count=0;
for(int i=0;i<N;i++)
{
if(T[i]==A)count++;
}
cout<<A<<" is present "<<count<<" times";
//Delete array T from memory
delete[] T;
}while(true);
}
Comments
Leave a comment