Print formatting

Pages: 12
Hello,
I had questions about formatting for printing. My directions say" This function will take the array of frequency of letters as input argument and prints only the letters that have frequency more then zero. All other letters will be ignored and not printed. It will also print the count of occurrences of that letter. Next to the count, it will print the number of stars twice the number of the count (see attached sample)." The sample is something like this ..It doesn't line up correctly when I looked in preview there supposed to be colums and such.

Letter count Frequency
====== ===== =========
A 3 ******
D 3 ******
And so on. My question is this. I have the array of frequency of letters this array is just integers with 26 positions. I have the top part taken care of no problem ( letter count freq and the === under them) I'm just confused about printing the array. So I'm not sure how I can do this because it is an array of int there is no chars to print. I 'm just stuck on the letters really.How do I get the letters to print if All I'm giving is an array of int. I don't want to use a big ass switch statement that's the only other idea I had. I write my code for my project in another project so I can try and get it working correctly then just copy and paste it minimizes the crap for me but also looks sloppy here she is....just in case it helps. You can see the array of freq and such.


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
#include <iostream>
#include<iomanip>
#include <string>

using namespace std;

//Function to remove garbage off word
void onlyAlpha(string& str1)
{	
	string newStr1 ="";
	newStr1.reserve(str1.size()); 

	for(string::size_type i = 0; i < str1.size(); i++)
	{	
		if ( isalpha( str1[i] ) ) 
		newStr1.push_back( str1[i] );
	}
	str1 = newStr1;
}


 //A function to determine frequency of letters
void  fillFrequency(string str1, int freqcount[], const int alphabet, int& totalLetters)
{	
	totalLetters = 0;

	// To fill array with freqency of letters occurrences 
	for(string::size_type i = 0; i < str1.size(); i++)
	{	
		str1[i] = toupper(str1[i]);
		++(freqcount[str1[i] - 'A']);
	 totalLetters++;
	}
	
}


//Function to fill new array with new words.
void funfun(string str1[], const int isize, int& countWord)
{	

	for(int i = 0; i <  isize; i++)
	{
		onlyAlpha(str1[i]);
		countWord++;
	}		
}

//Function to display the highest frequency
int highestFreq(int freqcount[], const int alphabet)
{	
	int highestfreq = 0;
	
	for(int i = 0; i < alphabet; i ++)
	{
		if(freqcount[i] > highestfreq)
		{
			 highestfreq = freqcount[i];
		}
	}
		return highestfreq;
}

void printResults(int freqcount[], const int alphabet)
{
	
}

void main()
{
	const int isize = 3;	
	string str1[] = {"!!HELLO","WHAT###", "%%left"};
	string test = "jordan";
	const int size = 100;
	string wordsminus[size];
	int highfreq = 0;
	int wordCount = 0;
	int letterCount = 0;
	const int alphabet = 26;
	int  freqcount[alphabet] = {0};
	funfun(str1,isize,wordCount);
	onlyAlpha(test);
	
	fillFrequency(test, freqcount, alphabet, letterCount);
	
	for(int i = 0; i < isize; i++)
	{
		cout << str1[i]<< "\t";
	}
	
	cout << wordCount << endl;
	
	for(int j = 0; j < alphabet; j++)
	{
		cout << freqcount[j] << "\t";
	}
	
	
	cout <<" letters " << letterCount << endl; 
     
	//highest freq test
 highfreq = highestFreq(freqcount, alphabet);
 cout << highfreq << endl;

	system("pause");
}	
Even though it is not funny because I'm struggling it is funny still I got a bunch of smiley faces when I tried this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void  printResults(int freqcount[], const int alphabet)
{	
	const int alpha = 26;
	char castLetters[alpha] = {' '};
	
	for( int i = 0; i < alphabet ; i ++)
	{
		if( freqcount[i] > 0)
		{
			castLetters[i] = static_cast <char>(freqcount[i]);
		}
		
	}
	
	
	for( int j = 0; j < alphabet ; j ++)
	{
		cout << castLetters[j] << endl;
	}
	
	
}
I'm not sure I understand what you are supposed to print for frequency, but I think this is what you want to do:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void printResults(int freqcount[], const int alphabet)
{
	for (int i = 0; i < alphabet; i++)
	{
		if (freqcount[i] > 0)
		{
			cout << char('A' + i) << setw(6) << freqcount[i] << ' ';
			for (int j = 0; j < 2 * freqcount[i]; j++)
			{
				cout << '*';
			}
			cout << endl;
		}
	}
	return;
}
Hey cool thanks but I'm a little lost on a little of it.
char('A' + i) So will this just do all the letters of the alphabet??
For frequency I just print the number. So
A 6 ******

Like that. Can you help me understand that snipet of code please?
Oh I see dude that's good idea. I was making it very difficult. I was trying to convert an array to of int's to chars. I all I got was smiley faces and "B" ha.
Thanks.

I have another questions I was playing with the code a little bit how did you know this j < 2 *freqcount[i]; j++; I don''t understand why you couldn't just do ** ? I tried to do that it got all messed up. So whats up with the j < 2 * ?

1
2
3
4
5
for (int j = 0; j < 2 * freqcount[i]; j++)
			{
				cout << '*';
			}
			cout << endl;
Last edited on
The char('A' + i) is just a different way of casting. It converts the integer in the parentheses into a character, so that the corresponding ASCII symbol is printed out instead of the number.

So in the loop, it will print out A ('A' + 0), B ('A' + 1), C ('A' + 2), etc.

The code I gave prints out twice the amount of stars as the frequency of the letter's occurrence. That's what I thought you wanted based on your first post.
Last edited on
Yeah it is I didn't see that its works I'm just trying to understand it better so i don't just say oh ok and move on. I don't get the j< 2* freqcount[i]; j ++)
Why did you do it that way? Like I said above I would of tried to do ** in the loop instead. Which doesn't work obviously :)
Like I said, I misunderstood your first post and thought you wanted twice the amount of stars as the frequency count.

** is not an operator, though I think it can be used as a pointer to a pointer.
NO that was right 2 times the amount. I just don't think I would of thought to do it j < 2 * freq.... I was just wondering about this statement. I don't even know what pointer to pointer is we never talked about pointers in class.
Hi,

I 'm close to getting this but not quite like always. So I have to print a paragraph 10 words per line. So I think I know what the problem is or a faint idea. The output should be this

The file sorted alphabetically:
a assignment file for from i is is line miss
read should test the third this this this this to

But what I end up getting is this.

The file sorted alphabetically:


(a huge space)


assignment file or from i is is line miss
should test the third this this this this to

So if you notice that the a in the beginning is gone also the word "read" is gone ( look at the first example) the beginning of the second line the.
I think that this is maybe because my array which i index though starts at 0 so 0%0 is 0 so no output possibly? The second part about the "read" missing I'm not sure I think that it's connected.
I have no clue why the big space.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void printTen(string inputarray[],const int size, int wordtotal)
{
	
	
	cout << "The file sorted alphabetically: " << endl;
	
	for(int i = 0 ; i < size; i++ )
	{
		if(i % 10 != 0)
		{
			cout << inputarray[i] << ' ' ;
		}
		else if( i % 10 == 0 )
		{
			cout << endl;
		}

	}
	
		
}

Thanks
So I got it to output correctly but I'm getting a huge space before it does.
I got all the words to be there. But it's just that this space. Any ideas?

The file sorted alphabetically:


(a huge space)


assignment file or from i is is line miss
should test the third this this this this to


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void printTen(string inputarray[],const int size, int wordtotal)
{
	
	cout << "The file sorted alphabetically: " << endl;
	
	for(int i = 0 ; i < size; i++ )
	{
		if(i % 10 != 0)
		cout << inputarray[i] << ' ';
		
		else if( i % 10 == 0 )
		cout<< endl << inputarray[i] << ' ';
	}
		
}
I can't think of anything that would cause that, except maybe you have some carriage returns ('\n') in your array.
how would i deal with that?
I don't know. Maybe they got there because you have a mistake somewhere else in your code.
Can you explain to me what exactly that is ? Like I just have a space stored in my array or something? I thought that it was an issue with the index in the second if else statement.

How would the '\n' get there? Is this common?
I don't think it's common at all. I've never seen it happen at least. But I don't know how else to explain the output you're getting.
Here this is my whole 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#include <iomanip>
#include <istream>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

// Function to test for Empty file
void peekFile(ifstream &fin)
{	
	char empty;
	// To check if the file is empty
	empty=fin.peek();
	if(empty = fin.eof())
	cout << "No data exists" << endl;
}

// To Print Heading
void print_heading(ifstream &fin)
{
	//Formatting for Heading 
	cout << "Letter " << '\t' << "Count " << '\t'<< "Frequency " << endl;
	cout << setw(6) << setfill('=') << '='  <<'\t' << 
	setw(5) << setfill ('=') << '=' << '\t' << setw(9) 
	<< setfill('=') << '=' << endl;
	
	// Function incase file is empty
	peekFile(fin);
	

}

//Function to remove non - alpha
void onlyAlpha(string& str1)
{	
	string newStr1 ;
	newStr1.reserve(str1.size()); 

	for(string::size_type i = 0; i < str1.size(); i++)
	{	
		if ( isalpha( str1[i] ) ) 
		newStr1.push_back( str1[i] );
	}

	str1 = newStr1;
}

//To fill array of strings
void fillString(ifstream &fin, string inputarray[], const int size)
{
	// Loop to fill input array
	for(int i = 0; size > i; i++)
	{	
		fin >> inputarray[i];
	} 
}

// Function to sort array of strings
void selectionSort(string list[], int size)
{
	int i = 0, j = 0, smallest = 0; 
		string temp ;

	for(i = 0; i < size -1; i++)
	{
		smallest = i;
		
		for(j = i + 1; j < size; j++)
		{
			if(list[j] < list[smallest])
				smallest = j;
		}

		//swap
		temp = list[smallest];
		list[smallest] = list [i];
		list [i] = temp;
	}


}

//Function to fill array with only the words
void onlyWords(string str1[], const int isize, int& countWord)
{	
	//Loop to pass string at index i to onlyAlpha function
	for(string::size_type i = 0; i <  isize; i++)
	{
		onlyAlpha(str1[i]);
		if(str1[i].size()>0)
		countWord++;
	}		
}

//Function to fill frequency array
void  fillFrequency(string str1, int freqcount[], const int alphabet)
{		
	
	for(string::size_type i = 0; i < str1.size(); i++)
	{	
		str1[i] = toupper(str1[i]);
		++(freqcount[str1[i] - 'A']);
	}

}

//Function to display the highest frequency
int highestFreq(int freqcount[], const int alphabet)
{	
	int highestfreq = 0;
	
	for(int i = 0; i < alphabet; i ++)
	{
		if(freqcount[i] > highestfreq)
		{
			 highestfreq = freqcount[i];
		}
	}
		return highestfreq;
}

// Function to format and print 
void  printResults(int freqcount[], const int alphabet)
{	
	for (int i = 0; i < alphabet; i++)
	{
		if (freqcount[i] > 0)
		{
			cout <<' ' << char('A' + i) << setfill(' ')<<setw(9)
			<< freqcount[i] << setw(6);
			for (int j = 0; j < 2 * freqcount[i]; j++)
			{
				cout << '*';
			}
			cout << endl;
		}
	}
			cout << '\n' << endl;
}

//Function to print words ten to a line
void printTen(string inputarray[],const int size, int wordtotal)
{
	
	cout << "The file sorted alphabetically: " << endl;
	
	// This is the problem code below. 
	//I'm just trying to print 10 words then break the line
	
	int counter = 0;
	for(int i = 0 ; i < size; i++)
	{	
		counter ++;
		if( counter % 10 !=0 )
		{
		cout << inputarray[i] << ' ';
		}
		if(counter % 10 == 0)
		{
			cout << inputarray[i] << endl;
		}
	}
		
}
	
		
void main()
{	
	ifstream fin;
	fin.open("mp6input.txt");
	int wordCount = 0;
	int letterCount = 0;
	int highfreq = 0;
	const int size = 100;
	string inputarray[size];
	const int alphabet = 26;
	int  freqcount[alphabet] = {0};
	
	
	// to print heading
	print_heading(fin);
	
	// to fill string array
	fillString(fin, inputarray, size);
	
	// To sort array 
	selectionSort(inputarray, size);
	
	//Only words function
	onlyWords(inputarray, size, wordCount);
	
	// This loop gives words to fillfreqency function from inputarray
	for( string::size_type i = 0; i < size; i++)
	{
		fillFrequency( inputarray[i], freqcount, alphabet);
	}

	// Prints the table 
	  printResults(freqcount, alphabet);
	
	  //Loop to total letters
	for( int i = 0; i < alphabet ; i++)
	{
		letterCount += freqcount[i];
	}
	
	//Displays the number of words
	cout << "This file has " << wordCount << " words and "  
	<< letterCount <<" letters. " << endl;
	
	// To display the highest frequency
	highfreq = highestFreq(freqcount, alphabet);
	cout << "The highest frequency is " << highfreq 
	<< "." <<'\n' << endl;
	
	// Print the words 10 to a line
	printTen(inputarray, size, wordCount);

	

system("pause");


}
Just jumping in, re: "That Huge Space".
Is that because it is printing all of those ZERO characters?
That else you're printing CNTL chars.

Take a look at the output in a FILE.TXT with a good editor ( one that shows CONTROL CHARACTERS and see if anything is in that huge space.

BTW,
someone above mentioned that you can take the ASCII value of the letter
e.g. "A" = 65
65 - (65-1) = 1

So now we have A=1
So now we print a single "**" for "A"
remember to convert to UPPER CASE letters first.



I figured out the problem after some testing. It's because your fillString() function. You're forcing it to loop 100 times (constant int size = 100) though there are only twenty words.

The last 80 times that the program runs through the loop, it stores an empty string into your array. When you sort your words, those empty strings come out before the rest of the words.

The blank lines are from the 80 empty "words" that the program is printing out.
So How do I not store 80 empty string lol :)
You can like peek at it or something?


Thanks man.

1
2
if( inputarray[i] != "" )
		fin >> inputarray[i];


I tried it and got no output
Last edited on
Pages: 12