Frequency of characters from a sorted array.

Hello everyone, I am having a hard time finding the frequency of characters from a sorted array for one of my classes. Any help would be greatly appreciated.

My goal was to input an array of characters and sort them using a Bubble sort, which I have done correctly. Now, with that sorted array, I must find any duplicates and state their frequency. Like so...

Letter: i Frequency: 2
Letter: l Frequency: 3
Letter: s Frequency: 3

Here is my function for frequency I have so far...
1
2
3
4
5
6
7
8
9
10
11
  void frequency(char sortedlist[80], char freq[80], int n){
	int i;
	
	for (i = 1; i <= n; i++){
		if (sortedlist[i] == sortedlist[i + 1]){
	            freq[i] = sortedlist[i];
			}
		}
	for (i = 1; i <= n; i++)
		cout << freq[i];
}


Now, I'm trying store the duplicated characters in an array.

This is what my output from that function looks like:


╠╠╠╠╠╠╠╠i╠ll╠╠ss╠╠╠

The characters I need are there but they are surrounded by a bunch of garbage and also have one less instance. **There should be two i's, three l's, and three s's.

Please any guidance you can give me will be greatly appreciated.
Since you haven't posted the broader context of your program, I can only guess at your intent for freq.

I would presume that it is an array used to count the frequency of each letter. If that is a correct assumption, you should be using an int array and line 6 should be incrementing the counter. As your code exists, if you don't find a duplicate, you don't store anything into freq, which is why you're getting garbage.

You also have problems with your for loops.
Line 4-5: You're skipping the first entry in sortedlist. Arrays are from 0 to n-1. Not 1 to n. Line 5 is also overrunning sortedlist on when i = n-1.

Line 9: Same comments regarding indexing an array. You're skipping element 0 and referencing an out of bounds element (sortedlist[n]).
Last edited on
Sorry maybe I wasn't clear enough, I would like the freq array to look something like this [i, i, l, l, l, s, s, s]. (or the opposite way)

Also just because I decide to disregard the 0 slot in an array does not make my code wrong.

Here is my full code: (I have updated my frequency function a little bit)

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 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], char freq[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, freq, 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], char freq[80], int n){
	int i;
	int j;
	
	for (i = 1; i <= n; i++){
		for(j = 1; j <= n-i; j++){
		if (sortedlist[i] == sortedlist[i + 1]){
				 freq[j] = sortedlist[i];
			}
		}
	}

	for (i = 1; i <= n; i++)
		cout << freq[i];
}

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 my full output:
Enter characters:
This class will be fun!#
Unsorted list of characters by ASCII code are:
T
h
i
s
c
l
a
s
s
w
i
l
l
b
e
f
u
n
!
Sorted list of characters by ASCII code are:
!
T
a
b
c
e
f
h
i
i
l
l
l
n
s
s
s
u
w
Reversed Sorted list of characters by ASCII code are:
w
u
s
s
s
n
l
l
l
i
i
h
f
e
c
b
a
T
!

Largest:!

Smallest:w

ssssllllii╠╠╠╠╠╠╠╠╠ (note: it's giving me an extra s and l for some reason)

Last edited on
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
// Frequency Counter in Sorted Array
#include <iostream>
#include <algorithm>
using namespace std;

int main(){
	
	string s = "aaavvvbh";	
		
	std::sort(s.begin(), s.end());	
		
	cout << "sorted string = " << s << "\n\n";	
	
	cout << "frequencies:\n";	
	for(int i=0; i<= s.size()-1; i++){
		int counT= 1;
		while(s[i]==s[i+1]){			
				if(i+1 == s.size()) break;
				counT++;
				i++;
		}
		cout << s[i] << " counts= " << counT << "\n";
	}	
		
return 0;		
}

same method can be applied to integer or char arrays.
Ah Yes! I am on my iPhone, so I cannot try it yet, but this makes alot of sense to me.

Thanks I appreciate your help!
well, it can be programmed even without sorting:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// number of occurence of each letter in Array (non-sorted)
// by anup 23.01.2015
#include <iostream>

int main(){

	char ch[11] = "abcdbhbhab"; // 10 chars
	
	for(int i=0; i<10; i++ ){
		int n=0;
		for(int j=0; j<10; j++){
			if(ch[i]==ch[j]) {
				if(i>j)	break;
				n++;
			}					
		}
		if(n) std::cout << ch[i] << " " << n << " times\n";	
	}
		
return 0;		
}


output:
a 2 times
b 4 times
c 1 times
d 1 times
h 2 times
just because I decide to disregard the 0 slot in an array does not make my code wrong.

No it does not, but ignoring the standard idiom of [0]-[size-1] is IMO, a poor practice. Especially when you have no comments that is what you are doing. To the casual observer looking at your code, the fact that you are indexing from 1 appears to be a noob error. It is also prone to errors such as indexing past the end of the array which IS wrong. By ignoring the zeroth entry, yoou're restricting yourself to 79 entries in an arrays that have 80 entries allocated, further increasing the probability of error. Stick with the standard idiom.

Your code at line 84 is still wrong. Lets assume n is 80. The last iteration through the loop, j will be 79. sortedlist[i + 1] now refers to element[80, which is an out of bounds reference.

Again at lines 90-91, 97-98, 105-106, you have the same problem with out of bound references when n = 80. I see nothing in your code limiting n to 79.



Last edited on
Topic archived. No new replies allowed.