-
I/O streams
The following below is a function in my program. I am supposed to find out the frequency of the letters of the alphabet in the input file. The problem is that i can't get any frequency value after the letter a. I was wondering how to read the text each time a new letter is asked for and can i set my counter back to 0 every time.
Thanks
here is the function definition:
Code:
void read_data(ifstream& input)
{
char alpha[26] ;
alpha[0]= 'a';
alpha[1]= 'b';
alpha[2]= 'c';
alpha[3]= 'd';
alpha[4]= 'e';
alpha[5]= 'f';
alpha[6]= 'g';
alpha[7]= 'h';
alpha[8]= 'i';
alpha[9]= 'j';
alpha[10]= 'k';
alpha[11]= 'l';
alpha[12]= 'm';
alpha[13]= 'n';
alpha[14]= 'o';
alpha[15]= 'p';
alpha[16]= 'q';
alpha[17]= 'r';
alpha[18]= 's';
alpha[19]= 't';
alpha[20]= 'u';
alpha[21]= 'v';
alpha[22]= 'w';
alpha[23]= 'x';
alpha[24]= 'y';
alpha[25]= 'z';
cout<<"The Letter Frequencies are:"<<endl;
char ch;
double sum =0;
double frequency;
int counter = 0;
for (int i =0; i<26;i++)
{
int counter = 0;
while (input.get(ch))
{
if (!islower(ch))
{
tolower(ch);
}
if (ch == alpha[i])
{
counter ++;
}
if (i<1)
{
if(isalpha(ch))
{
sum ++;
}
}
}
frequency = (counter/sum)*100;
cout<<alpha[i]<<" = "<<frequency<<" %"<<"\n";
}
}
-
What's with the poll? Anyway, If you write a program flawless on
the first try, Then you've picked a program that's very easy and
can be created just by learning statements and doesn't require
you to have expirience in creating it.
-
RE: Poll
I seem to remember reading somewhere that, on average, every line of code gets re-written twice! (If you write a 100 line program, you're going to end up writing 300 lines.) We all like to think that we are better than that, but by the time you're done fixing your syntax errors, fixing your logical errors, improving your program, and "experimenting", this could easily be the case. Most of the time we don't know exactly what the problem is... so we have to change more than one line to narrow-down the problem.
I suppose it's a bit of a paradox that you should be somewhat of a perfectionist to be a programmer, yet you have to accept the fact that you're going to make mistakes.
So, compile often, and test your code as soon as you have a testable bit of code!
-
so can anyboady help me with my problem?
(trying to get the text to be read for each letter of the alphabet)
-
I personally would use a loop to initialize this 26 element array instead of writing each line out!!!
-
i would too but i have time and i don't want the user to enter the values they are predetermined (unless of course there is a way for the compiler to use the alphabet)
-
The reason it won´t is because you got your algorithm screwed...
Your reading the file through to check only for the letter 'a'.
Now I would do like (written in pseudo):
Code:
char c;
char array_of_characters[26]; //initialise them to zero!
file=open("your filename here something");
while(file.readch(&c)) // I think it needs the adress of a char
{
if(isalpha(c))
{
tolower(c);
toindex(c); // some function to convert your character to array index..
array_of_characters[c]++;
}
}
I guess this should do it...
-
thanks you gave me a good idea. Now i'll have to see if it will work after i type it up.
-
It's very hard to write a program without any mistakes from the first time( ot at least for me ), it's very notmal to have some errors while writting the program, I don't think you should be very concerned with it.
-
They say one should spend 90% working on the code in paper and 10% hands on; of course, I spend 90% on the computer and the other 10% scratching my head trying to figure out what's wrong. :D
You can write punct, chars, digits etc using a simple for loop so that you can test your algo, or put it in a function and write the stuff many times
Code:
for(int i = 0; i < 127; i++)
{
if(isalpha(i) || isdigit(i) || ispunct(i))
outFile << (char)i;
}
As for your function, I'm not positive as to what you need. The number of times each of the 26 alpha chars shows up (yikes) versus how many lower case letters there are among the total count of characters. The latter shouldn't be too difficult. I'm not saying that this is the best method but it *should* work in counting the type of each character.
Code:
void readFile(ifstream& input)
{
char dataIn;
float digits, total, lcase, ucase;
lcase = ucase = digits = total = 0;
input >> dataIn;
while(input)
{
if(islower(dataIn))
lcase++;
else if(isupper(dataIn))
ucase++;
else if(isdigit(dataIn))
digits++;
total++;
input >> dataIn;
}
cout << "The freq of lowr case is " lcase/total; // etc
}
-
the problem that i am trying to solve is to find out the total number of each letter of the alphabet that appears in a text, find the amount of times each one appears in teh whole text and then get the frequency by dividing the amount of times one leeter appears by the total amount of letters present and multipying it by 100.
-
thank you for those who helped me. The program is up and running. BTW, the code for program is intended to help decifering encrypted messages.
-
oh one more thing, does anyone know how to increase the amount of lines that the output can show on the screen because i can only see around 20 lines or so. I need the whole output.
-
re-direct the output to a file, if your program is named hist then type
hist > file.txt
at the dos/shell prompt. file.txt contains your output.
-
Re: I/O streams
Quote:
Originally posted by scottmanc
Code:
void read_data(ifstream& input)
{
char alpha[26] ;
alpha[0]= 'a';
alpha[1]= 'b';
alpha[2]= 'c';
alpha[3]= 'd';
alpha[4]= 'e';
alpha[5]= 'f';
alpha[6]= 'g';
alpha[7]= 'h';
alpha[8]= 'i';
alpha[9]= 'j';
alpha[10]= 'k';
alpha[11]= 'l';
alpha[12]= 'm';
alpha[13]= 'n';
alpha[14]= 'o';
alpha[15]= 'p';
alpha[16]= 'q';
alpha[17]= 'r';
alpha[18]= 's';
alpha[19]= 't';
alpha[20]= 'u';
alpha[21]= 'v';
alpha[22]= 'w';
alpha[23]= 'x';
alpha[24]= 'y';
alpha[25]= 'z';
//
}
You could replace all that code with :
Code:
char alpha[26];
for(int index=0;index < 26; index++)
alpha[index] = (char)(65+index);