problems with running a counting program

I want to write a program that reads at most 100 integers between 1 and 100 and counts the occurrence of each number.(input ends with 0)
I don't know why the codes fail to run. I'm using visual c++ 2010 express.

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
 #include <iostream>
using namespace std;

int main()
{
	int num[100];
	int count=0;
	cout << "Enter the integers between 1 and 100: ";
	for(int i=0; i<100; i++)
	{
		count++;
		cin >> num[i];
		if(num[i]==0||num[i]<1||num[i]>100)
			break;
	}
	for(int i=0; i<count; i++)
	{
		int currentmin=num[i];
		int currentminindex=i;
		for(int j=i; j<100; j++)
		{
			if(currentmin>num[j])
			{currentmin=num[j];
			currentminindex=j;}
		}
	if(currentminindex!=i)
	{num[currentminindex]=num[i];
	num[i]=currentmin;}
	}
	int count2[100]={0};
	for(int i=0; i<100; i++)
	{
		count2[num[i]]++;
	}
	for(int i=0; i<count; i++)
	{
		cout << num[i] << " occurs " << count2[num[i]] << " time " << endl;
	} 
	return 0;
}
Last edited on
What do you mean by "fail to run"?

You do have two potential "out-of-range" errors:

1. Lines 30 and 33. 100 is a valid value, but count2[100] will be out of range by one.

2. Lines 13. you do break the input on out-of-range values, but you have already stored one such value to array on lines 11-12.
for(int j=i; j<100; j++)
and
for(int i=0; i<100; i++)

should be

for(int j=i; j<count; j++)
and
for(int i=0; i<count; i++)

Another thing to consider is when you get the input from the user.

1
2
3
4
5
6
7
for (int i = 0; i<100; i++)
{
	count++;
	cin >> num[i];	//Any invalid value is inserted in the array
	if (num[i] == 0 || num[i]<1 || num[i]>100)
		break;
}


Any invalid value is inserted into the array.Consider doing something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int input;	//hold user's input
for (int i = 0; i<100; i++)
{
	cin >> input;	//get user's input
	if (input == 0 || input<1 || input>100)	//check for invalid input
	{
		break;
	}
	else
	{
		num[i] = input;	//if input is valid insert it into the array 
		count++;		//and increment the counter
	}		
}

Thanks for corrections.
then i have the source code like this:
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
#include <iostream>
using namespace std;
int main()
{
	int input;
	int count=0;
	int num[100];
	cout << "Enter the integers between 1 and 100: ";
	for(int i=0; i<100; i++)
	{
		cin >> input;
		if(input==0||input<1||input>100)
		{break;}
		else
		{num[i]=input;
		count++;}
	}
	for(int i=0; i<count; i++)
	{
		int currentmin=num[i];
		int currentminindex=i;
		for(int j=i; j<count; j++)
		{if(currentmin>num[j])
		currentmin=num[j];
		currentminindex=j;}
		if(currentminindex!=i)
			num[currentminindex]=num[i];
		num[i]=currentmin;
	}

	int count2[100]={0};
	for(int i=0; i<count; i++)
	{
		count2[num[i]]++;
	}
	for(int i=0; i<count; i++)
	{
		cout << num[i] << " occurs " << count2[num[i]];
		if(count2[num[i]]==1)
		{cout << " time" << endl;}
		else
		{cout << " times" << endl;}
	} 
	return 0;
}

I entered 5 5 5 44 33 12 1, and the result is :

Enter the integers between 1 and 100: 5
5
5
44
33
12
1
0
1 occurs 1 time
5 occurs 3 times
5 occurs 3 times
5 occurs 3 times
12 occurs 3 times
12 occurs 3 times
12 occurs 3 times
 

33 and 44 are missing, but 12 becomes 3 times.
Also, I would like to know how to make the result like 5 occurs 3 times, but not repeating the
statement 3 times.
closed account (48T7M4Gy)
Store the progressive results in an array as you go through the list of numbers. Increment each element as appropriate and then loop through the array once all the input numbers have been accounted for. :)
@OP - are you allowed to use STL? A better solution would use something like std::multimap and check the frequencies directly - it's exactly what it's designed for. Given this looks like a basic C++ homework question, using STL might not be an option, and no-ones going to write your code for you, so you may be stuck trying to fix the original code which doesn't look very well designed (at first glance, it looks too 'hard-coded' and not generic enough), in which case kemort's answer would work.
What are the lines 18-29 supposed to do?


The count2 is still used inappropriately. If the input contains value 100, then you dereference count2[100], which does not exist.

You don't need array num in the loop that prints the counts.
problem solved, thanks a lot.
Topic archived. No new replies allowed.