Jump to content

how can i do this? ineed ur help

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
12 replies to this topic

#1
iostream

iostream

    Newbie

  • Members
  • PipPip
  • 10 posts
hay all:love:

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

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
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

#3
lintwurm

lintwurm

    Learning Programmer

  • Members
  • PipPipPip
  • 77 posts
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... ^_^

#4
iostream

iostream

    Newbie

  • Members
  • PipPip
  • 10 posts

WingedPanther said:

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!:crying:

#5
iostream

iostream

    Newbie

  • Members
  • PipPip
  • 10 posts

lintwurm said:

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?:confused:

really i need ur help!:crying:

#6
lintwurm

lintwurm

    Learning Programmer

  • Members
  • PipPipPip
  • 77 posts
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...

#7
Ancient Dragon

Ancient Dragon

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 400 posts
IMHO the easiest way to do that is to create an array of integers with 255 elements, such as
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:
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
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.

#8
iostream

iostream

    Newbie

  • Members
  • PipPip
  • 10 posts

lintwurm said:

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


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



:rolleyes:

Edited by ZekeDragon, 22 March 2010 - 09:02 PM.
Please use [code] tags (the # button) when posting code.


#9
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts

iostream said:

i think i can solve it by using switch-cases

and ummmmmm

while loop

and?:confused:

really i need ur help!:crying:
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

#10
Ancient Dragon

Ancient Dragon

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 400 posts
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.
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]);
   }
}


#11
iostream

iostream

    Newbie

  • Members
  • PipPip
  • 10 posts
^
^

i tried to do your way to solve it !

but i cant^^

..

and i did it on the other way

like this

..

# include <iostream>

# include <fstream>

using namespace std;

struct wType
{
    char let;

    int count;

};

int maim()

{

    wType arr[52];

    int i,j;

    int sum1=0,sum2=0,sumT=0;

    float freq=0;

    ifstream infile;

    ofstream outfile;

    infile.open("IT.txt");

    outfile.open("IT2.txt");

    for(i=0;i<26;i++)

    {

        arr[i].let=static_cast<char>(65+i);

        arr[i].count=0;

        int x=0;

        for(j=26;i<52;j++)

        {

            arr[j].let=static_cast<char>(97+x);

            arr[j].count=0;

            x++;

        }

        char ch;

        int counter=0;

        infile>>ch;

        while(!infile.eof())

        {

             counter++;


             int i;

             for(i=0;i<52;i++)

                 if (ch==arr[i].let)

                     arr[i].count++;

                 infile>>ch;

        }

        
             for(i=0;i<52;i++)


                 outfile<<arr[i].let<<" "<<arr[i].count<<" "<<arr[i].count/static_cast<double>(counter)<<endl;

             }

    return 0;

}





>>

i know it is sooo long!
but i think it is ok!:rolleyes:

<< check it 4 me please:love:

Edited by ZekeDragon, 27 March 2010 - 02:53 AM.
Please use [code] tags (the # button) when posting code.


#12
Ancient Dragon

Ancient Dragon

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 400 posts
>><< check it 4 me please
Check it yourself. All you have to do is run the code and see the results.