hay all
how r u?
i have question about struct in c++ language... ind i don't know how to write this program!
..
the question is...write a program that count the frequency of each letter in alphabet (lower and upper case) in a text file named ("IT.TXT").
defined the structure that stores a letter and a frequency.
your program should use an array of structures corresponding to the letters found in the file.
the program should then print a report showing each letter and its frequency.
..
plase write the program 4 me .. i need it 4 after tomorrow very important.![]()
We won't write it for you, but it sounds like your struct needs to store a char and an integer.
Just post the code you have written so far and we will help where we can
Also if you don't understand structs, google it... ^_^
have you tried any code what so ever?
if you have, show it to us and we will direct you further... But we won't code it for you... Reason being you won't learn anything from that...
IMHO the easiest way to do that is to create an array of integers with 255 elements, such asThen read the file one character at a time, use the character as an index into that array, and increment it. For example, if the letter 'A' were read, then count the number of As like this:Code:int nums[255] = {0};If you look at an ascii table (google for that and you will easily find it) you will see that the letter 'A' has a decimal value of 65. So the compiler will replace 'A' with 65 in that formula, making itCode:numbes['A']++;. There are only 255 possible values that can be in any data file, so that will work with any of them.Code:nums[65]++;
After the file has been read, all you have to do is create a loop that counts from 0 to 255 and print out the items that are greater than zero.
yes i tried and i write the full program
but
i cant tell it to print each letter with its frequncy.
the program print the frequ 4 all capi letters or all samll letters!! and i dont want it like this!!
Code:#include <iostream> #include <fstream> #include <string> #include <iomanip> using namespace std; struct LetterType { int sLetterCount; int cLetterCount; }; bool openFile(ifstream &input,ofstream &output); void count(ifstream &input,LetterType letter[]); void printResult(ofstream &output,LetterType letter[]); void initialize(LetterType letter[]); int main() { { ifstream input; ofstream output; LetterType letter[26]; initialize(letter); if(!openFile(input,output)) { cout<<"cannot open the input file!"<<endl; system("pause"); return 1; } count(input,letter); printResult(output,letter); input.close(); output.close(); system("pause"); return 0; } bool openFile(ifstream &input,ofstream &output) { string inFile,outFile; cout<<"Enter full input file name without extension : "; cin >> inFile; cout<<"Enter full output file name without extension : "; cin >> outFile; inFile=inFile + ".txt"; outFile=outFile + ".txt"; input.open(inFile.c_str()); output.open(outFile.c_str()); if(!input) return false; return true; } void count(ifstream &input,LetterType letter[]) { char ch; int index; input.get(ch); while(input) { if(ch >= 'A' && ch <='Z') { index= static_cast<int>(ch) - static_cast<int>('A'); letter[index].cLetterCount++; } else if(ch >= 'a' && ch <='z') { index= static_cast<int>(ch) - static_cast<int>('a'); letter[index].sLetterCount++; } input.get(ch); } } // write the function switch + cases void printResult(ofstream &output,LetterType letter[]) { int i; int sumOfcLetter=0; int sumOfsLetter=0; int sum; double percentOfcLetter; double percentOfsLetter; for(i=0;i<26;i++) sumOfcLetter += letter[i].cLetterCount; for(i=0;i<26;i++) sumOfsLetter += letter[i].sLetterCount; sum= sumOfcLetter + sumOfsLetter; percentOfcLetter= static_cast<double>(sumOfcLetter)/sum *100; percentOfsLetter= static_cast<double>(sumOfsLetter)/sum *100; output<<"Number of capital letters is : "<< sumOfcLetter<<endl; output<<"Number of small letters is : "<< sumOfsLetter<<endl; output<<"The percentage of capital letters is : "<<static_cast<int>(percentOfcLetter)<<"%"<<endl; output<<"The percentage of small letters is : "<<static_cast<int>(percentOfsLetter)<<"%"<<endl; } void initialize(LetterType letter[]) { int i; for(i=0;i<26;i++) letter[i].cLetterCount=0; for(i=0;i<26;i++) letter[i].sLetterCount=0; }
![]()
Last edited by ZekeDragon; 03-22-2010 at 10:02 PM. Reason: Please use [code] tags (the # button) when posting code.
You are really trying to do that the hard way. There is no need for structures or linked lists. Just use an array as I said previously. Very short prolgram.
Code:int array[255] = {0}; char chr; // read the entire file while( infile >> chr ) { array[(int)chr]++; } // display all the characters and their counts for(int i = 0; i < 255; i++) { if( array[i] > 0 ) { cout << "Letter " << (char)array[i] << ": " << array[i]); } }
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks