Answer to Question #274912 in C++ for Mod

Question #274912

Writing a function that calculates the occurrence of an integer A in an integer array T of size N.


1
Expert's answer
2021-12-03T02:05:48-0500
#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);
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment