Hello i have a code here that partially works...the point in the program is to store 10 array integers then outputs how many times that integer is entered
ex. input: 1 1 1 1 1 2 3 3 2 99

output : 1 = 5
2 = 2
3 = 2
99 = 1
well, my code runs..but the output is that it displays all numbers in the array and the number of times it entered ..so i need help...

#include<iostream>
#include<limits>
using namespace std;
   int main()
{
	const int max =10;
	int a[max], count=0, n;

	cout<<"Enter 10 integer values: ";
	for(int i=0;i<max;i++)
	{
		cin>>a[i];//input numbers
	}
	for(int i=0;i<max;i++)
	{
		for(int j=0;j<max;j++)
			{
				if(a[i]== a[j])					
				{
					count++;// count houw many inegers are the same
				}
			}
			cout<<a[i]<< "  =  "<<count<<endl;
			count= 0;
	}
	cin.ignore(numeric_limits<streamsize>::max(),'\n');
	cin.get();
    return 0;
}

need advice on what to remove or add...
do i have to add functions?

Thanks

I think if you can understand why it is happening you will be able to fix it yourself. If you try and dry run the 2 loops in your mind:

iteration 1:
outer loop, i = 0
inner loop j = 0 - max
compare a[0] (1) with a[j] and increment count if equal
print a[0] and count
iterator 2:
outer loop i = 1
inner loop j = 0 - max
compare a[1] (1) with a[j] and increment count if equal
print a[1] and count

so basically even though you count the # of occurrences of 1 in the first loop you still go to a[1] and that is also equal to 1 and then you repeat it for all the 1's and so on.

Once you compare a number you should remove all its instances from the array, to avoid recounting. You might find it easier to do this with vector or even a list.

I would make 2 arrays, one holding numbers and one holding counts corresponding to the numbers. If you want to process the number at each entry, check and see if the number exists in numbers array. If it does, increment its corresponding count, if not add it to the numbers array.

Changing it to read all the numbers in at once and then process them wouldn't be too difficult.

EDIT: Agni nudges me out by a hair

if you are not averse to using standard generic algorithms, you can look at 'count' in <algorithm> header file.

@Agin: I think using generic algorithms goes against the purpose of the exercise..

First, a simple trick is to use a magic number like 0 or MAX_INTEGER or something to mark the already matched values as invalid, then check that a number is valid before looping for matches.
Second, the second loop (match search) only needs to start from i+1 because all previous values have already been looked at as a.

//set some magic number.
#define INVALID_VALUE -43425234 

{..}

for(int i=0;i<max;i++) {
  if(a[i] == INVALID_VALUE)
    continue;
  count=1;
  for(int j=i+1;j<max;j++) {
    if(a[i]== a[j]) {
      count++;// count houw many inegers are the same
      a[j] = INVALID_VALUE;
    }
  }
  cout<<a[i]<< " = "<<count<<endl;
  count= 0;
}

{..}

@mike_2000_17 you are assuming that this is an exercise. what if he wants to put this code in a project or in a bigger code?

@Agni, you're totally right, I'm sorry, from the looks of the problem it seemed like a sort of basic programming class exercise (you know, do a simple looping algorithm and output to the terminal, that's classic). But of course if it is part of a bigger project or something then <algorithm> is fair-game of course and a time-saver.

Thanks for helping mike and agni...you saved me time solving it...:)
by the way..im self learning this using a book...
@agni..not yet in vectors though...but i use algorithms in swapping...ill post if i really really need help..
@mike i know its basic for you but im still learning..:confused:

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.