about program that counts a character frequency
Dear experienced programmers,
I was working on program that counts the frequency
distribution of a characters in a file. the algorithm is as follows :
1.open a file
2.read the first character and put it in an array
that counts the single character called singles[]
3. read the second charcter and put it in array
that counts the double called doubles[].
4. then read the rest of the file and update the relevant array
5. finally display the statistic of singles,doubles and triples.
here is my problem when I run the program
it only displays the singles result, but the program doesn't execute
the second character loop and the loop that does the rest of the charcter read(3rd while loop).
I am loosing the head why it doesn't excute that
portion of the code.
I AM NOT EXPECTING SOMEONE TO DO IT FOR ME BUT
DESREVE TO BE GUIDED SLOWLY TO SOLVE THE PROBLEM.
THANKS IN ADVANCE
about frequency distribution program
Dear experienced programmers,
I was working on program that counts the frequency
distribution of a characters in a file. the algorithm is as follows :
1.open a file
2.read the first character and put it in an array that counts the single character called singles[]
3. read the second charcter and put it in array
that counts the double called doubles[].
4. then read the rest of the file and update the relevant array
5. finally display the statistic of singles,doubles and triples.
here is my problem when I run the program
it only displays the singles result, but the program doesn't execute
the second character loop and the loop that does the rest of the charcter read(3rd while loop).
I am loosing the head why it doesn't excute that
portion of the code.
I AM NOT EXPECTING SOMEONE TO DO IT FOR ME BUT
DESREVE TO BE GUIDED SLOWLY TO SOLVE THE PROBLEM.
THANKS IN ADVANCE
#include<iostream.h>
#include <fstream.h> /* for file functions */
#include <stdio.h>
#include <ctype.h> /* for exit() */
#include <string.h>
#define MAX 26
#define Def_percent 0.005 /* starting percentage */
static int singles[ MAX ];
static int doubles[ MAX ][ MAX ];
static int triples[ MAX ][ MAX ][ MAX ];
int main(int argc, char *argv[])
{
char ch;
int first, last, next;
ifstream infile;
int letterCount;
int i , j, k;
double percentage;
double lowerpercent = Def_percent;
if (argc != 2)
{
cerr<<"\noops!! no file name specified " <<endl;
exit(-1);
}
infile.open( argv[1] );
if ( !infile )
{
cerr<<"\n cann't open " << argv[1] <<endl ;
exit(-1);
}
/* read first letter */
while( infile )
{
infile.get(ch);
first = tolower(ch);
if (( first >= int ('a') ) && ( first <= int ('z')))
{
++singles[ first -= int ('a')];
}
}
/* process the second chracter */
while ( infile )
{
infile.get(ch);
last = tolower(ch);
if (( last >= int ('a') ) && ( last <= int ('z')))
{
++doubles[first][last -= int ('a')];
++singles[last];
}
}
/* process the rest */
letterCount = 2; /* number of characters counted characters so far */
while ( infile.get(ch) != 0 )
{
//infile.get(ch);
next = tolower(ch);
if (( next < int ('a') ) || ( next > int ('z')))
{
continue;
/* process the current character */
++singles[next -= int ('a')];
++doubles[last][next];
++triples[first][last][next];
first = last;
last = next;
++letterCount;
}
}
/* Print the statistics */
cout << " the file contains" << letterCount <<" letters";
cout <<"\n\n the singles:\n" << endl ;
cout << " -------------------------------------------------------"<<endl;
for( i = 0 ; i < MAX ; i++)
{
cout <<char( i + int ('a')) <<" "<< double(singles[i]/letterCount) << "\t"
<< singles[i] << endl;
cout <<"\nDOUBLE LETTERS: \n"<< endl;
cout <<"----------------------------------------------------------"<< endl;
for ( i = 0; i < MAX; i++)
for( j = 0; j < MAX; j++)
if ( (percentage = double (doubles[i][j]) / (letterCount - 1)) >= lowerpercent )
cout << char(i + int ('a')) <<" "<< char (j + int ('a')) <<" "<< percentage << "\t"
<< doubles[i][j]<< endl;
cout <<"\n TRIPLE LETTERS: \n"<< endl;
cout <<"------------------------------------------------------------------"<< endl;
for ( i = 0; i < MAX; i++)
for( j = 0; j < MAX; j++)
for ( k = 0; k < MAX; k++)
if( (percentage = double (triples[i][j][k]) / (letterCount -2)) >= lowerpercent)
cout<< char(i + int ('a')) <<" "<< char (j + int ('a')) <<" "<< char (k + int ('a')) << "\t"
<<percentage << triples[i][j][k] << endl;
}
}