Counting the occurrences of numbers in an array

Hello :)
I have to write a program that gets up to 30 numbers from the user and from those numbers give me the largest one. I was able to get that part. Aside from that I have to get the occurrences of those same numbers and output them like so:
How many input values [max: 30]?
8
Enter 8 numbers.
-5
1
5
3
10
2
3
5
Number Count
-5_______1
1________1
2________1
3________2
5________2
10_______1
this is what I have so far that runs...
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
#include <iostream>
using namespace std;

int main(){
	int values, list[30] = {};
	cout << "How many input values[max: 30]?\n";
	cin >> values;
	cout << "Enter " << values << " numbers.\n";
	for (int i = 0; i < values; i++){
		cin >> list[i];
	}

	int biggestNum = list[0], temp;
	for (int i = 0; i < values; i++){
		temp = list[i];
		if (temp > biggestNum){
			biggestNum = temp;
		}
		
	}
	cout << "Largest Number: " << biggestNum << endl;

	/*for (int i = 0; i < values; i++){
		if (list[i] == list[i + 1])
			;
	}*/

	return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/*now that you have the largest element in list do : */

int size = biggestNumber + 1;
int freq[size];

for(int i = 0; i < size ; i++)
     freq[i] = 0;

for(int i = 0; i < size; i++)
     ++freq[list[i]];

//now print number - freq

for(int i = 0; i < size+ 1; i++)
  if(freq[i] == 0)
       ;
  else
     cout<<i<<" -- "<<freq[i]<<endl;

I imagine that this is homework, and that you are not allowed to use a map, but a map is the ideal container for solving this problem:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <map>
#include <iostream>

int main(void )
{
  // seed data
  int array[] = { 1, 2, 3, 4, 3, 4, 5, 6, 5, 4, 3, 4, 3, 2, 1, 2, 3 };
  const int size = sizeof( array ) / sizeof( array[0] );

  // create and fill the map
  std::map< int, int > occurances;
  for ( int i = 0; i < size; ++i )
    ++occurances[ array[i] ];

  std::cout << "Greatest: " << occurances.rbegin()->first << '\n';

  // print the contents of the map
  using iterator = std::map< int, int >::iterator;
  for ( iterator iter = occurances.begin(); iter != occurances.end(); ++iter )
    std::cout << iter->first << ": " << iter->second << '\n';

  return 0;
}
Last edited on
Thank guys. I'm just now really trying to get my program to run. "LowestOne" this is a data structures class but I just started last week(8/25/14) and haven't got into the STL; I'm assuming "<map>" is in there; I'm not sure if it's in there though.
"ShadowCODE" I use Visual Studio and I don't think it likes "int freq[size];." Do you use Code::Blocks?; I ask because two buddies use it and it doesn't have a problem with taking a non-numerical "size declarator." i.e. "size." In Code::Blocks "size" also is a type it seems. Thanks again.
OK. So I'm not cheating but went ahead and installed Code::Blocks and; excuse my french; copied and pasted your code "ShadowCODE" and got this:

How many input values[max: 30]?
5
Enter 5 numbers.
1
2
3
4
5
Largest Number: 5
0 -- 88
1 -- 7
2 -- 1
3 -- 1
4 -- 1
5 -- 1
25 -- 1
100 -- 5


You should be learning this lesson using an associative container rather than a sequential one, so I would advise that you spend some free time learning about what that means. At least, keep this lesson in the back of your head for when you do learn about associative containers and re-do it. Hopefully though, std::map is where this lesson ends.

I guess the main lesson here though, is don't blindly copy/paste other people's code.

The reason you can't create that array is because the size of an array must be known at compile time.

ShadowCODE is on the right track though. You need one array to keep track of the numbers and one to keep track of frequencies. At the beginning of the program, all of the values of the frequency array are set to zero. As the user inputs numbers, that index of the frequency array is increased:
1
2
3
4
5
6
7
int numbers = { 0, 1, 2, 3, 2, 0 };  // a seed instead of asking for numbers
int frequencies[ 4 ];

// omitting the code to set each value of frequencies to zero

// set frequencies
for ( int i = 0; i < 6; ++i )  ++frequencies[ numbers[i] ];


There are a host of problems here. Since the value of a number maps to an index of the frequency array, the frequency array must be large enough for that not be out of bounds. That means that the user can't be allowed to enter negative numbers. Also, since you need to know the size of the frequency array at compile time, you will need to have limit the value of a number entered to be one less than the size of that array.
Topic archived. No new replies allowed.