Trying to find a Mode using Parallel Arrays

Good Afternoon All

I'm a newbie when it comes to C++, I would love to see if the bright minds here would be able to help with with my issue. I'm tasked with Figuring out calculation of the mode of a set of integers inputted by the user. I think i'm really close to the solution but I My output keeps getting the incorrect mode. I was hoping maybe someone with a different set of eyes could look at what I'm doing hopefully see what i'm missing.
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <iostream>
using namespace std;

void getMode(int num[], int freq[], int size);
int main()
{



	const int SIZE = 100;
	int i, z, a, m, size, index, temp, freq[SIZE], num[SIZE];
	int mode = freq[SIZE];
	int counter = 0;
	int counterMode = 0;
	bool swapNum;
	bool found;
	
	cout << "Enter size for Array: ";
	cin >> size;
	
	for (index = 0; index < size; index++)
	{
		cout << "Enter Values for the Array "
		<< (index +1) << ": ";
		cin >> num[index];
	}

	
	for (index = 0; index < size; index++)
	{
		cout << "The Values you have input are: " << num[index] <<endl;
	}
	do
	{	swapNum = false;
		for (z = 0; z < size-1; z++)
		{
			if(num[z] > num[z+1])
			{
				temp = num[z];
				num[z] = num[z+1];
				num[z+1] = temp;
				swapNum = true;
			}
		}
	} while(swapNum);
			
	for (index = 0; index < size; index++)
	{
		found = false;
		cout << "freq count loop" << endl;
		freq[index] = 1;
		int m=index+1;
		while (m<size)
		{
			cout << "inner freq count loop" << endl;
			if (num[m] == num[index])
			{
				cout << "inc freq count loop" << endl;
				freq[index]++;
			}
			else
			{
				for(int i =index+1; i<m; i++)
				{
					freq[i] = freq[index];
				}
				index = m-1;
			}
			m++;
		}
	}
	for (a = 0; a < size; a++)
	{
		cout<<"The Values you have input in Ascending Order are: " << num[a]  << " times appeared: " << freq[a] << endl;
	
	}
	
	getMode(num, freq, size);
	

	return 0; 
}

void getMode(int num[], int freq[], int size)	
{
	int counter=0, counterMode=0;
	int mode;
	for( int i = 0; i < size; i++)
	{
	
			if (num[i] == freq[i])
			{
				++counter;
			}
			else
			{
				if (freq[i] > counterMode)
					mode = freq[i];
			}
			counter = 1;
			freq[i] = num[i];

	}
	
		
	cout<<"The Mode is :"<< mode << endl;
			
}

The problem i'm running into is that the frequency of the is that i believe my accumulator is getting the wrong set of information, I've tried switching out variables to see if I maybe just be missing something. Any help would be immensely appreciated.
are you sure that mode is always assigned in that loop? could it follow a logical path of

for
if
++ counter
....
never hits else

end if

mode never assigned...

??

this would give you random gibberish for mode.
to test this, just set mode = 12345 or something.

if you keep getting that value, its not assigned in your loop sometimes.


Last edited on
Topic archived. No new replies allowed.