# Thread: Count number!

1. ## Count number!

Read a list of ints into a array.The output is two-column list,first column is a list of the different
array elements ;second column is the count of that element occurs.We can assume that there are few than 50 ints.

Ok,the only problem for me is how to count the ints.Compare to letters,I can set up a array called letters[26],then contain from a~z,and use get function to get the char and compare:

like

Code:
```while(cin.get(next))
{
for(int i=0;i<26;i++)
{
if(next=letters[i])
count[i]++;
}
}```
so you can see in my count[26],I have the occurences.

but how to deal with an arbitrary int?

2. 1. Read your numbers into the array.
2. Sort the array.
3. Start at the first element.
4. Call the count STL function on that array for the current element we are looking at. Print the element and the count returned.
5. Advance through the array the same number of positions as step 4 returned
5A. If not at the end of the array return to step 4.
5B. If we are at the end, then exit.

3. Do you have to use an array? It becomes pretty trivial with a std::map. In fact, it would be what you described with letters, except that it is still efficient when dealing with arbitrary ints.

4. Yeah, I naturally thought that too, but he said "Read a list of ints into a array" so I worked with what I got.

Popular pages Recent additions