Finding highest frequency

Hello everyone, I have been working on a project that deals with an array of characters and finding there frequencies. I was able to determine the frequencies that where greater than 1, but I need to find the HIGHEST frequency.

Here is my full code:

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

void input(char unlisted[80], int& n);
void bubblesort(char unlisted[80], char sortedlist[80], int n);
void minMax(char sortedlist[80], int n);
void frequency(char sortedlist[80], int n);
void print(char list[80], int n);
void printReversed(char list[80], int n);

void main(){

	char unlisted[80], sortedlist[80], freq[80];
	int n;

	input(unlisted, n);

	cout << "Unsorted";
	print(unlisted, n);

	cout << "Sorted";
	bubblesort(unlisted, sortedlist, n);
	print(sortedlist, n);

	cout << "Reversed Sorted";
	printReversed(sortedlist, n);

	minMax(sortedlist, n);

	frequency(sortedlist, n);

	cin >> n;
}

void input(char unlisted[80], int& n){
	int i = 0;
	char value;
	cout << "Enter characters: \n";
	cin >> value;

	while (i < 80 && value != '#'){
		i++;
		unlisted[i] = value;
		if (i < 80){
			cin >> value;
		}
	}
	n = i;
}

void bubblesort(char unlisted[80], char sortedlist[80], int n){
	int i, j, temp;

	for (i = 1; i <= n; i++){
		sortedlist[i] = unlisted[i];
	}
	for (j = 1; j <= n -1; j++){
		for (i = 1; i <= n - j; i++){
			if (sortedlist[i] > sortedlist[i + 1]){
				temp = sortedlist[i];
				sortedlist[i] = sortedlist[i + 1];
				sortedlist[i + 1] = temp;
			}
		}
	}
}

void minMax(char sortedlist[80], int n){

	char min = sortedlist[1];
	char max = sortedlist[n];

	cout << "\nLargest:" << min << "\n\n";
	cout << "Smallest:" << max << "\n\n";

}

void frequency(char sortedlist[80], int n){
	int i;

	cout << "Frequencies:\n";	
	for(i=1; i <= n; i++){
		int maxCount = 1;
		while(sortedlist[i]==sortedlist[i+1]){
				maxCount++;
				i++;
		}
		if(maxCount > 1)
			cout << sortedlist[i] << " counts= " << maxCount<< "\n";
	}
}

void print(char list[80], int n){
	int i;
	cout << " list of characters by ASCII code are: \n ";
	for (i = 1; i <= n; i++){
		cout << list[i] << '\n';
	}
}

void printReversed(char list[80], int n){
	int i;
	cout << " list of characters by ASCII code are: \n ";
	for (i = n; i >= 1; i--){
		cout << list[i] << '\n';
	}
}


And this is the function that I am having trouble with:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void frequency(char sortedlist[80], int n){
	int i;

	cout << "Frequencies:\n";	
	for(i=1; i <= n; i++){
		int count = 1;
		while(sortedlist[i]==sortedlist[i+1]){
				count++;
				i++;
		}
		if(count > 1)
			cout << sortedlist[i] << " counts= " << count<< "\n";
	}
}


As you can see this will print out all frequencies that are bigger than 1, but I only want the highest frequency.
Basically I want to print out all frequencies when count is greater than 1 less than count, but because its in a loop count will always be greater than count-1 so it prints out all frequencies.

Please any guidance will be greatly appreciated.
Last edited on
use a map <char, int>
get your char

if char does not exist already in map, add to the map and set it's quantity (the second int part of the map) to 1.

if char does exist, find it in the map and increment it's quantity by one.
I'm sorry i'm not familiar with maps at all, is there any other way of doing this?
Hey guys, I have actually gotten very close to solving this . But cant seems to get it to work for if Multiple characters have the highest frequency.

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
void Maxfrequency(char sortedlist[80], int n){
	int i;
 
	cout << "Frequencies:\n";
	int maxCount = 1;
	int tempCount = 1;
	char tempChar = ' ';
	char maxChar = ' ';
	for (i = 1; i <= n; i++){
		while (sortedlist[i] == sortedlist[i + 1]){
			tempCount++;
			if (tempCount >= 1){
				tempChar = sortedlist[i];
			}
			i++;
		}
		if (tempCount > maxCount){
			maxCount = tempCount;
			maxChar = tempChar;
		}
		tempCount = 0;
		tempChar = ' ';
	}
	cout << "The char that appeared most is: \"" << maxChar << "\" which appeared: " << maxCount+1 << " times.";
}


Can anybody tell me how to change this program, so it print out multiple frequencies not just 1. Thank you
Last edited on
Topic archived. No new replies allowed.