Hi guys,

I am stuck in one of my code to count the number of occurrence of each element in an array. Can someone please nudge me towards where I am going wrong.

for(int i=0;i<r;i++){
for(int j=0;j<r;j++){
if(store[i]==store[j])//array where I need to count the no. of occurrence of elements
{
++counter;
}
}
}

Many Thanks

Recommended Answers

All 12 Replies

What kind of variable is store?

#include<iostream>
using namespace std;

void main()
{
    int a[100] = {1,1,1,2,3,3,4,5,5,5,5,6};
    int count[10] = {0};
    int i;
    for(i = 0; i< 100; i++)
        count[a[i]]++;
    for(i = 1; i< 10; i++)
    {
		if(count[i]==1) cout<<i<<"--appreances for\t"<<"one time"<<endl;
 		else if(count[i]!=0)
        cout<<i<<"--appreances for\t"<<count[i]<<" times"<<endl;
	
    }
    system("pause"); 
}

Hope it will be helpful for you!!!

#include<iostream>
using namespace std;

void main()
{
    int a[100] = {1,1,1,2,3,3,4,5,5,5,5,6};
   int count[10] = {0}; 
    int i;
    for(i = 0; i< 100; i++)
        count[a[i]]++;
    for(i = 1; i< 10; i++)
    {
		if(count[i]==1) cout<<i<<"--appreances for\t"<<"one time"<<endl;
 		else if(count[i]!=0)
        cout<<i<<"--appreances for\t"<<count[i]<<" times"<<endl;
	
    }
    system("pause"); 
}

Hope it will be helpful for you!!!

Thank you so much. It was helpful indeed. However could you explain me this fragment?

int count[10] = {0}; 
    int i;
    for(i = 0; i< 100; i++)
        count[a[i]]++;

Any reason you can't use standard facilities?
For example:

#include <iostream>
#include <vector>
#include <algorithm>

struct Equals {
    Equals(int x) : v_(x) {}
    bool operator() (int x) { return v_ == x; }
    int v_;
};

int main () {
    std::vector< int > items;
    for (int i = 0; i < 25; ++i) items.push_back (i % 3);
    int count = std::count_if (items.begin(), items.end(), Equals(2));
    std::cout << "Total number of 2's: " << count << std::endl;
    return 0;
}
commented: Yeah, look at his code. Yours is so far above him his nose bleeds looking at it. -4

Thank you so much. It was helpful indeed. However could you explain me this fragment?

int count[10] = {0}; 
    int i;
    for(i = 0; i< 100; i++)
        count[a[i]]++;

Supposing there is a array that contains 1,2,3,1,2,3,2,3,2,1,2,1


First of all,when i=0.a returns 1
counter[a] is equal to a[1] //1 is the value of the a

counter[1] increases

On the other hand, a[3] also returns the value 1.

So the counter[1] increases by 1 the second time .

Any reason you can't use standard facilities?
For example:

#include <iostream>
#include <vector>
#include <algorithm>

struct Equals {
    Equals(int x) : v_(x) {}
    bool operator() (int x) { return v_ == x; }
    int v_;
};

int main () {
    std::vector< int > items;
    for (int i = 0; i < 25; ++i) items.push_back (i % 3);
    int count = std::count_if (items.begin(), items.end(), Equals(2));
    std::cout << "Total number of 2's: " << count << std::endl;
    return 0;
}

to use even more stl,

#include <vector>
#include <algorithm>
#include <functional>
#include <iostream>
int main () {
   using namespace std;
    std::vector< int > items;
    for (int i = 0; i < 25; ++i) items.push_back (i % 3);
    int count = std::count_if (items.begin(), items.end(), std::bind2nd(std::equal_to<int>(),2) );
    std::cout << "Total number of 2's: " << count << std::endl;
    return 0;
}

>>to use even more stl,

To use even more STL, with C++0x:

#include <vector>
#include <algorithm>
#include <functional>
#include <iostream>

int main () {
  using namespace std;
  std::vector< int > items;
  for (int i = 0; i < 25; ++i) items.push_back (i % 3);

  std::map<int,int> counts;
  std::for_each( items.begin(), items.end(),
    [&counts](int x) {
      if(counts[x]) {
        std::cout << "Number " << x << " appears " 
                  << counts[x] = std::count(items.begin(), items.end(), x)
                  << " times in 'items'." << std::endl;
      };
    }
  );
  return 0;
};

And if you want to stick to the current standard, you can always use boost lambdas:

#include <boost/lambda/lambda.hpp>
#include <boost/lambda/bind.hpp>
#include <algorithm>
#include <iostream>
#include <vector>
#include <map>

int main()
{
    using namespace boost::lambda;

    std::vector<int> items(25);
    std::map<int, int> counts;

    for (int i = 0; i < 25; ++i) items[i] = i % 7;

    for_each(items.begin(), items.end(),
        ++bind(&std::map<int, int>::operator[], &counts, _1) );

    typedef std::map<int, int>::iterator::value_type  BigType;

    for_each(counts.begin(), counts.end(),
        std::cout
            << constant( "number " ) << bind(&BigType::first,  _1)
            << constant(" appears ") << bind(&BigType::second, _1)
            << constant(" times\n" )
            );

    return 0;
}

OK Guys, enough showing off. Let's help Sonia11 and less trying to impress her with your prowess. I'll bet she's not interested in your C++ muscle and just wants to finish her project.

commented: well said +9

Oh the standard libraries are very helpful and known to me but I want to avoid using it in the code. For that I just want to make use of the counters and loops. Any help in this context would be appreciated.

If tiredoy's post didn't help, you might need to give a more detailed explanation of what you need help with.

OK Guys, enough showing off. Let's help Sonia11 and less trying to impress her with your prowess. I'll bet she's not interested in your C++ muscle and just wants to finish her project.

Suggesting an alternative way to do something is not necessarily showing off. In fact, the introduction of new topics is exactly how people learn. In C++, looping over a set of something is generally accomplished via iterators; how else would you suggest the topic be introduced?

commented: Crap! It's obvious the instructor hasn't taught vectors yet. And two additional posts were made well beyond her level. Vectors are not the answer to all C++ questions! -4
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.