Answer to Question #225952 in C++ for Croft

Question #225952

b) Write a template version of the function count to determine the number of times a specific value occurs in a vector of any base type and test it by declaring two or more vectors with different base types and determining how many times a specific value occurs in these two vectors.


1
Expert's answer
2021-08-14T01:39:48-0400
#include <iostream>
#include <vector>

using namespace std;

int main()
{
   std::vector <double>values;
  
   //...
   //Erase all elements and read the elements from the input standard
   values.clear();
   double x;
   int n;
   cout<<"Enter the number you want to show its occurance"<<'\n';
   cin>>n;
   while(std::cin>>x)
   {
   values.push_back(x);
   }
   std::cout<<"Numbers of values read: "<<values.size()<<'\n';
   //Loop over all the elements and print the number of negative elements found.
   int count=0;
   for(auto i=values.cbegin(); i!=values.cend(); ++i)
   {
   if(*i==n)
   {
   ++ count;
   }
   }
  cout<<"The number of times "<<n<<" appears in the vector is: "<<count<<" times"<<'\n';
      return 0;
}

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

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS