I need to write a program that takes characters (only alphabetical) input from the screen (ended with a period), puts them into an array, keeps count of how many there are of each character and prints out a list of the characters and their number of occurrences, sorted by their occurrence, highest to lowest. The main thing I don't know how to do is to sort the characters based on their occurrence and also the counting itself isn't completely working. Can anyone else correct this code?
#include %26lt;iostream%26gt;
#include %26lt;fstream%26gt;
int main(void)
{
using namespace std;
char chars[]="abcdefghijklmnopqrstuvwxyz";
int i;
int count[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0...
char inp;
cin%26gt;%26gt;inp;
while(inp !='.')
{
for(i=0;i%26lt;26;++i)
{
if(inp==chars[i])
count[i]++;
}
cin%26gt;%26gt;inp;
}
cout%26lt;%26lt;"Character Count"%26lt;%26lt;endl;
for(i=0;i%26lt;26;++i)
{
if(count[i]!=0)
{
cout%26lt;%26lt;chars[i]%26lt;%26lt;" "%26lt;%26lt;count[i]%26lt;%26lt;endl;
}
}
system("pause");
return 0;
}
C++ Question?
If you don't understand some of the concepts involved you should read up on them. Ask questions if necessary.
#include %26lt;iostream%26gt;
#include %26lt;vector%26gt;
#include %26lt;algorithm%26gt;
using namespace std;
struct elementType
{
char letter;
int count;
elementType(char l, int c) : letter(l), count(c) {};
};
bool compare(elementType a, elementType b )
{
return(a.count %26gt; b.count);
}
typedef vector%26lt;elementType%26gt; Array;
int main(void)
{
//creat vector of letter/count elements
Array array;
for (char i = 'a'; i %26lt;= 'z'; ++i)
{
elementType e(i, 0);
array.push_back(e);
}
//collect input chars
char inp;
cin %26gt;%26gt; inp;
while (inp !='.')
{
if ((isalpha(inp))
%26amp;%26amp; (islower(inp)))
array[inp - 'a'].count++;
cin %26gt;%26gt; inp;
}
//sort array
sort(array.begin(), array.end(), compare);
//spit out results
cout %26lt;%26lt; "Character Count" %26lt;%26lt; endl;
for (int i = 0; i %26lt; array.size(); ++i)
{
if (array[i].count %26gt; 0)
{
cout %26lt;%26lt; array[i].letter %26lt;%26lt; " " %26lt;%26lt; array[i].count %26lt;%26lt; endl;
}
}
system("pause");
return 0;
}
Reply:Convert the user input to an ascii value. Make a dummy array the same length as the user input array. Then all you have to do is.
for(int index=0; index%26lt;userInput.length();index++)
dummy[int(userInput[index])]++;
Print it out
for(int index=0; index%26lt;userInput.length();index++)
{
if(dummy[index]%26gt;0)
cout %26lt;%26lt; "The letter " %26lt;%26lt; char(index) %26lt;%26lt; "occured" %26lt;%26lt;dummy[index] %26lt;%26lt; " times" %26lt;%26lt; endl;
}
Reply:Seems like less work to use a map
You get the letter, increment the int.
Create some iterators and use the sort algorithm.
Reply:First, your while loop probably doesn't work because you need to get an index of inp. Something like:
int idx = 0;
while (inp[idx] != ".")
{
...
...
...
idx++;
}
Second, there are much better ways to add to the count than the following, but it should still work:
for(i=0;i%26lt;26;++i)
{
if(inp==chars[i])
count[i]++;
}
Lastly, you then need to sort. To do that, you might want to replace count with a struct that's made up of a character (the letter of the alphabet) and an integer (which is the count). Then do something simple like a bubble sort on it (you can google that).
Overall, there are better/easier ways to do this, but if you want to use your code as a starting point, then you need to look at these three areas.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment