count giving large number

When i build the solution, the program gives me an unnatural number for the count, which is supposed to mention how many times the most used random integer is found. I've scoured my code for the problem with count but I can't find it!

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
#include <iostream>
#include <stdlib.h>
#include <iomanip>
#include <ctime>
using namespace std;
int main()
{
	int numbers [10];
	int a;
	int b;
	int count[10];
	int fcount;
	char k;

	fcount=0;

	srand((unsigned)time(0));
	for (int i=0;i<10;i++)
	{
		numbers[i]=rand()%30+1;
		cout<<numbers[i]<<" ";
	}
	cout<<"\n";
	cout<<"\n";

	for (k=0;k<10;k++)
	{
		a=numbers[k];
		
		for (int j=0;j<10;j++)
		{
			b=numbers[j];
			
			if (b==a)
			{
				count[k]++;
			}
		}
	}

	fcount=count[0];

	for (int r=0;r<10;r++)
	{
		if (count[r+1]>count[r])
		{
			fcount=count[r+1];
			a=numbers[r+1];
		}
	}

	cout<<a<<" repeated "<<fcount<<" times.";

	cout<<"\n";
	cout<<"\n";
	cout<<"Enter any character to continue...";
	cout<<"\n";
	cin>>k;

	return 0;
}
You need to initialize your count values to zero. Automatic variables are not initialized and contain undefined data.
Why are you using a character pointer in line 28? You are declaring k as a character but in line 26, you're treating it as an integer and I'm not sure if that's what you wanted to do. That's probably why you're getting strange numbers.

Can you explain to me the purpose of your program? I'm confused what's going on because I don't know what you're trying to do.
k indicates which number in the series i am going to use. dhayden your reply worked very smoothly, but technical question: aren't members of arrays initialized to 0?
Topic archived. No new replies allowed.