Closed Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: how can i do this? ineed ur help

  1. #1
    iostream is offline Newbie
    Join Date
    Mar 2010
    Posts
    10
    Rep Power
    0

    Unhappy how can i do this? ineed ur help

    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.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: how can i do this? ineed ur help

    We won't write it for you, but it sounds like your struct needs to store a char and an integer.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  4. #3
    lintwurm's Avatar
    lintwurm is offline Learning Programmer
    Join Date
    Mar 2010
    Location
    Pretoria
    Posts
    81
    Rep Power
    0

    Re: how can i do this? ineed ur help

    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... ^_^

  5. #4
    iostream is offline Newbie
    Join Date
    Mar 2010
    Posts
    10
    Rep Power
    0

    Re: how can i do this? ineed ur help

    Quote Originally Posted by WingedPanther View Post
    We won't write it for you, but it sounds like your struct needs to store a char and an integer.
    i did my best to solve it .. but i can't!

    f u'll not write it 4 me.. tell me how can i do it! only ..

    please!

  6. #5
    iostream is offline Newbie
    Join Date
    Mar 2010
    Posts
    10
    Rep Power
    0

    Re: how can i do this? ineed ur help

    Quote Originally Posted by lintwurm View Post
    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... ^_^


    i think i can solve it by using switch-cases

    and ummmmmm

    while loop

    and?

    really i need ur help!

  7. #6
    lintwurm's Avatar
    lintwurm is offline Learning Programmer
    Join Date
    Mar 2010
    Location
    Pretoria
    Posts
    81
    Rep Power
    0

    Re: how can i do this? ineed ur help

    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...

  8. #7
    Ancient Dragon's Avatar
    Ancient Dragon is offline Programming Expert
    Join Date
    Jan 2008
    Location
    near St Louis, MO USA
    Posts
    393
    Rep Power
    17

    Re: how can i do this? ineed ur help

    IMHO the easiest way to do that is to create an array of integers with 255 elements, such as
    Code:
    int nums[255] = {0};
    Then 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:
    numbes['A']++;
    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 it
    Code:
    nums[65]++;
    . There are only 255 possible values that can be in any data file, so that will work with any of them.

    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.

  9. #8
    iostream is offline Newbie
    Join Date
    Mar 2010
    Posts
    10
    Rep Power
    0

    Re: how can i do this? ineed ur help

    Quote Originally Posted by lintwurm View Post
    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...
    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)<<"&#37;"<<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.

  10. #9
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: how can i do this? ineed ur help

    Quote Originally Posted by iostream View Post
    i think i can solve it by using switch-cases

    and ummmmmm

    while loop

    and?

    really i need ur help!
    I understand that, but have NO idea where you're at right now. What are you doing for your struct so far? What is in your while loop? What is the state of your code/pseudocode?
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  11. #10
    Ancient Dragon's Avatar
    Ancient Dragon is offline Programming Expert
    Join Date
    Jan 2008
    Location
    near St Louis, MO USA
    Posts
    393
    Rep Power
    17

    Re: how can i do this? ineed ur help

    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]);
       }
    }

Closed Thread
Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts