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"; } }



LinkBack URL
About LinkBacks



