Linked list output incorrect

Were are to implement a method countValue() that counts the number of times an item occurs in a linked list. Remember to use the STL <list>

int countValue(list<int> front ,const int item);

Generate 20 random numbers in the range of 0 to 4, and insert each number in the linked list. Output the list by using a method which you would call writeLinkedList which you would add to the ListP.cpp.

In a loop, call the method countValue() , and display the number of occurrences of each value from 0 to 4 in the list.

Remember that all the above is to be included in the file ListP.ccp

The output should be:
2 3 4 0 1 0 2 4 2 3 3 4 3 3 3 0 0 2 0 2

0 : 5, 1 : 1, 2 : 5, 3 : 6, 4 : 3

but I am getting:

1 2 4 0 4 4 3 3 2 4 0 0 1 2 1 1 0 2 2 1

0 : 4, 1 : 5, 2 : 5, 3 : 2, 4 : 4,

Help would be appreciated. Here is my code:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include<iostream>
#include<list>

using namespace std;

int countValue(list<int> front ,const int item);
void writeLinkedList(list<int> front);

int main()
{
	list<int> front;
	int listCount;
	int count=0;
	cout<<"Enter the size of list: ";
	cin>>listCount;
	
	for(int i=1;i<=listCount;i++)
	{
		front.push_back(rand()%5);
		//cout<<"Original List of Values: "<<endl;
	}
	writeLinkedList(front);	//call function to display linked list
	cout<<endl;

	for(int j=0;j<5;++j)
	{
		count=(front,j);
		count=countValue(front,j);
		cout << j << " : " <<count<<", ";
	}
	cout<<endl;
	system("pause");
	return 0;
}

int countValue(list<int> front ,const int item)
{
	int count=0;
	list<int>::iterator i;
	for(i=front.begin(); i != front.end(); ++i)
	
	{
		
		if(item==*i)
		count=count+1;
	}
	return count;
}
void writeLinkedList(list<int> front)
{
	   list<int>::iterator i;
	 
	   for(i=front.begin(); i != front.end(); ++i) cout << *i << " ";
	   cout << endl;
	   
}
¿do you understand what `random' means?
Topic archived. No new replies allowed.