Hey guys. I hate to have to ask for help but I've run out of ideas. The program I'm trying to write is supposed to write a random list of numbers 0 - 4 and then count the occurrences of each number. The only thing I can't get to work is the countValue function.

//count the number of occurrences of each number in the linked list
Node* countValue(Node *pHead) //<------HERE'S THE PROBLEM
{
      int count0;
      int count1;
      int count2;
      int count3;
      int count4;
      Node *pNode;
      for(pNode = pHead; pNode->next != NULL; pNode = pNode->next)
      {
          if(pNode->data == 0)
          {
              count0++;
          }
          if(pNode->data == 1)
          {
              count1++;
           
          }
          if(pNode->data == 2)
          {
              count2++;

          }
          if(pNode->data == 3)
          {
              count3++;
              
          }
          if(pNode->data == 4)
          {
              count4++;
          }
              
	  } 
	  cout << "0: " << count0 << endl;
	  cout << "1: " << count1 << endl;
	  cout << "2: " << count2 << endl;
	  cout << "3: " << count3 << endl;
	  cout << "4: " << count4 << endl;
     return (pNode);
}

Recommended Answers

All 4 Replies

The only thing I can't get to work is the countValue function.

...
//count the number of occurrences of each number in the linked list
Node* countValue(Node *pHead) //<------HERE'S THE PROBLEM
{
...

So you don't think actually explaining what the problem is could be important? Pointing to a line is enough?

Please reconsider...

So you don't think actually explaining what the problem is could be important? Pointing to a line is enough?

Please reconsider...

It's not counting. I thought that I could run it through a loop and increment count as each node value passes through, but the numbers come out to be whack.

And it's not just a single line, the whole function doesn't work properly.

> int count0;
Here's the problem, you don't initialise your counters to zero.

commented: solved thread +0

Well I'll be damned, that was it! Thanks buddy!

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.