Question #18469

7- Write a function that displays the frequency of occurrence of an element entered by the user in a stored array. The array always has the following elements: 4, 2, 7, 10, 9, 7, 6, 10, -8, 7, 9.
Sample Executions:
Enter a number: 7
The number 7 occurred 3 times in your array.
Press any key to continue

Enter a number:17
The number 17 occurred 0 times in your array.
Press any key to continue

Expert's answer


#include <iostream>

using namespace std;


int arrayofnumbers[]={4, 2, 7, 10, 9, 7, 6, 10, -8, 7, 9};
int main()
{
int number=0;
int times=0;
cout<<"Enter a number: ";
cin>>number;
for(int i=0;i<11;i++){
if(number==arrayofnumbers[i]){
times++;
}
}
cout<<"The number "<<number<<" occurred
"<<times<<" times in your array.";
cout<<"
Press any key to continue";
cin>>number;
return 0;
}
LATEST TUTORIALS
APPROVED BY CLIENTS