Array..tallying numbers

Jan 2, 2013 at 12:02am
so i have an array of 300 numbers.
lets say like Tally [300];
i need to write a code which checks if any of the numbers of the arrays repeat it self and how many times does the number repeats it self.
so like if Tally [1] contains 33, and tally [4] and tally [50] and tally [70] all contain 33...
i will like to at the end have on the screen something like..33 came up 4 times or 33 = 4, or something like that...help please :)/> thanks in advance...
am not asking for the code to be written for me...but giving me an idea on how to go about it even in words and not in codes...but some guidance in words will be enough..thanks
Jan 2, 2013 at 12:17am
You could do a loop to see weather they repeat,for example a for loop,start at Tally[0] if it contains 33 then print it on the screen and then move on to the next array and if the next array is equal to Tally[0] then print it on the screen and do that until all the arrays indices are checked.
Jan 2, 2013 at 12:47am
you could build a map<int, int> mapping each value to the number of times it appears in the array (takes a single pass doing ++stats[value] for every value in the array, or simply convert the array directly into a multiset<int>, and then do a pass over the map/multiset printing the values whose counts are greater than one.
Jan 2, 2013 at 3:52am
closed account (18hRX9L8)
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/* code by usandfriends */
#include <iostream>

#define RESET 0

main()
{
	int howmany,i,n,j,count,counter,lowest,highest,tmp;
	
	count=0;
	lowest=0;
	highest=0;
	
	std::cout<<"How long is your list?  ";
	std::cin>>howmany;
	std::cout<<std::endl<<std::endl;
	
	int tally[howmany];
	
	for(i=0;i<howmany;i++)
	{
		std::cout<<"Item number "<<i+1<<":  ";
		std::cin>>tally[i];
		if(lowest>=tally[i])
		{
			lowest=tally[i];
		}
		if(highest<=tally[i])
		{
			highest=tally[i];
		}
	}
	
	std::cout<<std::endl<<std::endl<<"We will now find out how many times values are repeated in your list."<<std::endl<<std::endl;
	
	  for(counter=0;counter<=howmany;counter++) //bubble sort
      {
            for(j=1;j<howmany;j++)
            {
                  if(tally[j]>tally[j-1])
                        {;}
                        
                  else
                  {
                        tmp=tally[j];
                        tally[j]=tally[j-1];
                        tally[j-1]=tmp;
                  }
            }
      }
	
	for(i=0;i<howmany;i++) //count repeating array items
	{
		if(i!=0)
		{
			if(tally[i]!=tally[i-1])
			{
				count=RESET;
			}
			if(tally[i]!=tally[i+1])
			{
				count=count+1;
				std::cout<<"Items with "<<tally[i]<<" value: "<<count<<std::endl;
			}
		}
		for(n=lowest;n<=highest;n++)
		{
			if(tally[i]==n)
			{
				count++;
			}
		}
	}
}


DO NOT USE THIS CODE. It is merely an example. This code is: too long(not simplified enough) and takes up a ton of memory. Please use this AS AN EXAMPLE.

If anyone has any suggestions to fix my code up, please feel free to comment. ;D
Last edited on Jan 2, 2013 at 3:54am
Topic archived. No new replies allowed.