November 30th, 2008, 06:49 PM
|
#1 (permalink)
| | Junior Member
Join Date: Nov 2008
Posts: 2
|
Im writing a program that counts the amount of letters with the user input. I would love it if someone could correct my coding. I am not sure if it runs because I wrote it from the top of my head. I dont have a compiler so it would be really great if someone could fix it for me.
my output needs to be something like this:
Enter a sequence of characters (end with '.'): bcccacaca. dfg
Letter: Number of Occurrences
a 3
b 1
c 5
This is what I have so far. I wrote some comment. If someone could fix up my code that would be great. Im really stuck on it
#include <iostream>
#include <iomanip>
#include <cctype>
using namespace std;
const int NUM_LETTERS = 26;
int frequency;
char letter;
};
int indexOfLargest(letterFrequency frequencyArray[], int startingPoint); void sortFrequencyArray(letterFrequency frequencyArray[]); void countLetters(letterFrequency freqencyArray[]); void initFrequencyArray(letterFrequency frequencyArray[]); void printFrequencyTable(letterFrequency frequencyArray[]);
int main()
{
letterFrequency frequencyArray[NUM_LETTERS];
initFrequencyArray(frequencyArray);
countLetters(frequencyArray);
sortFrequencyArray(frequencyArray);
printFrequencyTable(frequencyArray);
system("PAUSE");
}
void initFrequencyArray(letterFrequency frequencyArray[])
{
for (int count = 0; count < NUM_LETTERS; count++){
frequencyArray[count].frequency = 0;
frequencyArray[count].letter = 'a' + count;
}
}
void countLetters(letterFrequency frequencyArray[]) {
char ch;
cout << "Please enter a sequence of characters terminated with a period ('.'): "<<endl;
cin.get(ch);
while (ch != '.')
{
for (int i = 0; i < strlen(s); i++)
{
s[i] = tolower(s[i]);
for (int j = 0; j < SIZE; j++)
{
if (s[i] == alphabet[j])
{
count[j]++;
cin.get(ch);
}
}
void sortFrequencyArray(letterFrequency frequencyArray[]) {
int indexOfLargest(letterFrequency frequencyArray[], int startingPoint) {
for (int j = 0; j < SIZE; j++)
{
if (s[i] == alphabet[j])
{
count[j]++;
void printFrequencyTable(letterFrequency frequencyArray[])
{
cout <<endl<< "Letter:" << setw(5) << "" << "Number of Occurrences:" << endl;
for (int i = 0; i < SIZE; i++)
{
//Only print the letters that occurred at least once in the sentence
if (count[i] > 0)
{
cout << alphabet[i] << setw(5) << count[i] << endl;
} |
| |
November 30th, 2008, 07:39 PM
|
#2 (permalink)
| | Member
Join Date: Oct 2003
Posts: 255
|
c++?...eclipse makes a fee c++ ide...never used it, but its free...try that out. |
| |
November 30th, 2008, 07:41 PM
|
#3 (permalink)
| | Junior Member
Join Date: Nov 2008
Posts: 2
|
I use a mac now. I have xcode on my computer but I dont know how to use it. when I had a PC I just used Bloodshed Dev C++
The assignment is due tomorrow so Im rushing to get it done. But I am runnning into many problems unfortunately |
| |
December 2nd, 2008, 12:34 AM
|
#4 (permalink)
| | Ultimate Member
Join Date: Mar 2005 Location: Out of my mind
Posts: 2,792
|
Wow...that's a big "mess" of code
Why not set up an array.
int alpha_counter[26];
To load it...for example if letter = a, alpha_counter[0] +=1
psuedo code
while letter not equal '.'
{
you could get fancy with for loops or easier,
just code 26 if statements as above
}
Then just for loop through array and print results
cout << "number of A's was " << alpha_counter[i] << endl;
Last edited by Rootstonian : December 2nd, 2008 at 12:39 AM.
|
| | |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | | |
Posting Rules
| You may post new threads You may post replies You may not post attachments You may not edit your posts HTML code is Off | | | | Most Active Discussions | | | | | Recent Discussions  | | | | | |