Re: CODE CHECK.!!
* samoukos:
HELLO .... I WROTE THIS CODE .... IT'S PORPOSE IS TO READ A GREEK TEXT
USING ASCII CODING AND THEN COUNTING HOW MANY TIMES EACH WORD
APPEARS...THEN IT MUST PRINT IT OUT WITH ASCEDING SEQUENCE.. CAN YOU
PLEASE CHECK THIS CODE AND TELL ME IF THERE ARE ANY MODIFICATIONS THAT
CAN BE MADE IN ORDER TO MAKE IT FASTER AND BETTER...THANK YOU....
Please don't shout.
Note that ASCII does not define any greek characters: presumably what
you mean is using an encoding with one byte per character.
Also, it doesn't seem like the program counts words, but character
instances (e.g. number of occurences of 'A' in the text).
HERE IS THE CODE::
#include<iostream>
#include<cstdlib>
#include<fstream>
using std::ifstream;
using std::ofstream;
using std::endl;
using std::ios;
int main ()
{
ifstream fin;
Indentation is a good idea. E.g. add four spaces at the start of lines
inside { and }.
ofstream fout;
int i,j,q,NumberUsed,max,imax;
These variables should most probably be declared locally where they're
used. It's a good idea to use consistent naming convention. I.e.
numberUsed or number_used, not NumberUsed which is unlike the others.
char alpha[1000000];
From the code below this is used to store the text.
A std::string is much more safe and convenient.
int sum[255];
If sum[i] is the number of occurences of the character encoded as value
i, then the above array can't count occurences of a character encoded as
255, because valid indices range from 0 to 254, inclusive.
The assumption of no 255-characters may be valid, or not.
fin.open("TEXT.txt");
fout.open("TEXT.out");
//-----------------------------------
for (i=0;i<1000000;i++)
{
alpha[i]=0;
}
Level 1 correction: use symbolic names for "magic" numbers such as
1000000, and use descriptive names such as "text" rather than "alpha".
Level 2 correction: use a locally declared loop variable,
for( int i = 0; i < textBufferSize; ++i )
{
text[i] = 0;
}
Level 3 correction: do this initialization in the declaration,
size_t const textBufferSize = 1000000;
char text[textBufferSize] = {0};
Level 4 correction: use a std::string instead,
std::string text;
//----------------------------------
for (i=0;i<255;i++)
{
sum[i]=0;
}
Level 1, 2 and 3 corrections as noted for 'alpha' above.
Rest snipped, I think the above comments enough for now.
Cheers & hth.,
- Alf
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?