Letter frequency

How can I comapare the results of the number_frequency[index]?

Page 1 of 1

14 Replies - 958 Views - Last Post: 26 May 2010 - 09:45 AM Rate Topic: -----

#1 Guest_anny*


Reputation:

Letter frequency

Posted 26 May 2010 - 06:11 AM

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;
struct list{
    double freq;
} ;

string getString(void) {

    ifstream SecondFile ("text1.txt");

    string test_string;
    do
    {

        while(! SecondFile.eof() ) //this while loop gets the text from the fil\
e line by line                                                                  
        {
            getline(SecondFile, test_string); //get the line and put it in the \
fileline                                                                        

        }
    }
    while(test_string.length() == 0);
    return test_string;
    SecondFile.close();
}

void Begin () {
    string str1;
    str1 = getString();
    int index;
    double number_frequency[26];
    int number_count [26];
    for(int index = 0; index < 26; index++)
    {
        number_frequency[index] = 0;
        number_count[index] = 0;
    }
    int number_of_numbers = 0;
    int test_string_length = static_cast<int>(str1.length());
    for(int index = 0; index < test_string_length; index++)
    {
     	char symbol = str1[index];
	symbol = toupper(symbol);
	if('A' <= symbol && symbol <= 'Z')
	{
            int number_index = symbol - static_cast<int>('A');
            number_count[number_index]++;
            number_of_numbers++;
	}
    }
    if(number_of_numbers > 0)
	for(int index = 0; index < 26; index++)
            number_frequency[index] = static_cast<double>(number_count[index]) \
/ number_of_numbers;





    ofstream out("test1.txt");
    if(!out) {
        cout << "Cannot open file.\n";

    }

    for(index = 0; index < 26; index++) {
        out << setprecision(2) << fixed;
        out << setw(2) << static_cast<char>(index + static_cast<int>('A'));
        out<< setw(2) <<" fi: " << setw(2)<< number_count[index];
        out<<setw(2) <<" Pi: "<<number_frequency[index] <<endl;
    }
    out<<"\n";
    out<<"N:"<<number_of_numbers<< endl;
    out<<"\n";

    cout << str1 << endl; //write the fileline onto the screen                  

    out.close();
    system("pause");

}

int
main ()
{
    cout << "** Jackpot game **\n";
    cout << "The goal of this game is to guess a number. You will be ask to typ\
e\n";
    cout << "a number (you have 5 guess)\n";
    cout << "Jackpot will then tell you if this number is too big of too small \
compared to the secret number to find\n\n";
    Begin();
    return 0;
}


This post has been edited by JackOfAllTrades: 26 May 2010 - 06:37 AM
Reason for edit:: Added code tags.


Is This A Good Question/Topic? 0

Replies To: Letter frequency

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6048
  • View blog
  • Posts: 23,473
  • Joined: 23-August 08

Re: Letter frequency

Posted 26 May 2010 - 06:13 AM

When posting code, USE CODE TAGS!

:code:

Do you have a question about this code? Is it giving you errors? If so, post the errors exactly as they appear.
Was This Post Helpful? 0
  • +
  • -

#3 Guest_anny*


Reputation:

Re: Letter frequency

Posted 26 May 2010 - 06:19 AM

I would actually like to ask how can I create a fuction that can compare the First output of number_frequency[index] with the second .And even though the programm is error free is very slow can I improve it.

This post has been edited by JackOfAllTrades: 26 May 2010 - 06:33 AM
Reason for edit:: Removed code tags. Sheesh.

Was This Post Helpful? 0

#4 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Letter frequency

Posted 26 May 2010 - 06:23 AM

Quote

How Can I Comapare The Results Of The Number_frequency[Index]?


Compare it to what?
A rose?
A summer's day?

View Postanny, on 26 May 2010 - 10:19 PM, said:

I would actually like to ask how can I create a fuction that can compare the First output of  number_frequency[index] with the second .And even though the programm is error free is very slow can I improve it.


No.
Code goes in code tags.
Text goes in the body of your posting.
Like this (without the quotes).

View Postanny, on 26 May 2010 - 10:19 PM, said:

I would actually like to ask how can I create a fuction that can compare the First output of number_frequency[index] with the second .And even though the programm is error free is very slow can I improve it.


So you want to know how to compare number_frequency[0] with number_frequency[1]?

How compare?
Which comparison?
If you want to check if they are equal
if(number_frequency[0] == number_frequency[1]){
    cout << "Equality here!!" << endl;
}



What is it you are are trying to do?

This post has been edited by janotte: 26 May 2010 - 06:26 AM

Was This Post Helpful? 0
  • +
  • -

#5 Guest_anny*


Reputation:

Re: Letter frequency

Posted 26 May 2010 - 06:26 AM

Sorry I have not used this site before :helpsmilie:
As I was saying my programm is error free but very slow,how can I improve it?
And I would also like to know how can I create a fuction that will compare the outputs of number_frequency[index].
Was This Post Helpful? 0

#6 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Letter frequency

Posted 26 May 2010 - 06:30 AM

View Postanny, on 26 May 2010 - 10:26 PM, said:

Sorry I have not used this site before :helpsmilie:

Not a problem.
We are all learning all the time.

View Postanny, on 26 May 2010 - 10:26 PM, said:

As I was saying my programm is error free but very slow,how can I improve it?

First get your program correct before you even think about performance.

View Postanny, on 26 May 2010 - 10:26 PM, said:

And I would also like to know how can I create a fuction that will compare the outputs of number_frequency[index].

You are going to have to actually say what it is, at length, in detail, with clarity and providing specifics that you are trying to do.
You have said the same thing 3 times and it is not clearer what you are trying to do than the first time.
Take some time to write out your issue in clear language (maybe giving examples).
Was This Post Helpful? 0
  • +
  • -

#7 Guest_anny*


Reputation:

Re: Letter frequency

Posted 26 May 2010 - 06:35 AM

Thank you !!!
Was This Post Helpful? 0

#8 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Letter frequency

Posted 26 May 2010 - 07:08 AM

In programming you need to at least know one of two things: Where you are going, or where you are. That is to say, in order to write good clear code you need to have a clear direction. If you don't have a clear direction and you are "playing around" then you really need to understand what you code does at each step so that you know what your playing is doing.

So why do you need such clear direction? Because we do not write programs all in one big go, we build them up piece by piece, function by function, class by class -- testing as we go. But to test unfinished code you need to know what it should be doing even when its not connected to the rest of the code.


Example: What is this function supposed to do:

Guest_anny said:

string getString(void) {
    ifstream SecondFile ("text1.txt");
    string test_string;

    do {
        while (! SecondFile.eof() ) { //this while loop gets the text from the file line by line
            getline(SecondFile, test_string); //get the line and put it in the fileline
        }
    } while (test_string.length() == 0);

    return test_string;
    SecondFile.close();
}


Did you mean to write a function to read though an entire file and then only keep the last line (if it is not empty)? I really don't think this function does what you were expecting, so what was it you were trying to do?


What is your program supposed to do?

BTW: in the above function, the line SecondFile.close(); never gets executed because it comes after the return statement.
Was This Post Helpful? 0
  • +
  • -

#9 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Letter frequency

Posted 26 May 2010 - 07:20 AM

So I created a little test program for your function:
#include <iostream>
#include <fstream>
#include <string>


using namespace std;

string getString(void) {
    ifstream SecondFile ("text1.txt");
    string test_string;

    do {
        while (! SecondFile.eof() ) { //this while loop gets the text from the file line by line
            getline(SecondFile, test_string); //get the line and put it in the fileline
        }
    } while (test_string.length() == 0);

    return test_string;
    SecondFile.close();
}


int main() {
    string str = getString();
    cout << "getString: \"" << str << "\"" << endl;
    return 0;
}


created a text1.txt with this:
1 This is a test
2 This is a test
3 This is a test
4 This is a test
5 This is a test
6 This is a test
7 This is a test
8 This is a test


and of course the program gave me:
> "C:\CProjects\Forum Help\175228-letter-frequency_getstr_test.exe " 
getString: "8 This is a test"


as I suspected... It read the last line of the file.... but what was it supposed to do? Is it functioning correctly?
Was This Post Helpful? 0
  • +
  • -

#10 Guest_anny*


Reputation:

Re: Letter frequency

Posted 26 May 2010 - 07:29 AM

Well I was trying to create subroutines
string getString(void) { 
    ifstream SecondFile ("text1.txt"); 
    string test_string; 
 
    do { 
        while (! SecondFile.eof() ) { //this while loop gets the text from the file line by line 
            getline(SecondFile, test_string); //get the line and put it in the fileline 
        } 
    } while (test_string.length() == 0); 
 
    return test_string; 
    SecondFile.close(); 
}



I want to get the data from a text file.But it can only read the first line ,sorry now I notised.Maybe I should use a different get(); function;
Was This Post Helpful? 0

#11 Guest_anny*


Reputation:

Re: Letter frequency

Posted 26 May 2010 - 07:47 AM

Well the programm should do something like this

A fi: 7 Pi: 0.70
B fi: 3 Pi: 0.30
C fi: 0 Pi: 0.00
D fi: 0 Pi: 0.00
E fi: 0 Pi: 0.00
F fi: 0 Pi: 0.00
G fi: 0 Pi: 0.00
H fi: 0 Pi: 0.00
I fi: 0 Pi: 0.00
J fi: 0 Pi: 0.00
K fi: 0 Pi: 0.00
L fi: 0 Pi: 0.00
M fi: 0 Pi: 0.00
N fi: 0 Pi: 0.00
O fi: 0 Pi: 0.00
P fi: 0 Pi: 0.00
Q fi: 0 Pi: 0.00
R fi: 0 Pi: 0.00
S fi: 0 Pi: 0.00
T fi: 0 Pi: 0.00
U fi: 0 Pi: 0.00
V fi: 0 Pi: 0.00
W fi: 0 Pi: 0.00
X fi: 0 Pi: 0.00
Y fi: 0 Pi: 0.00
Z fi: 0 Pi: 0.00

N:10
"text1.txt"
aaaaaaabbb

But after I created the subroutine it does not work!
Was This Post Helpful? 0

#12 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Letter frequency

Posted 26 May 2010 - 08:12 AM

The nice thing about using functions (which are close to the idea of a subroutine) is that we can often test them individually, and sometimes we can reuse them once we know we have then working. So I created a more general version of that function:
#include <iostream>
#include <fstream>
#include <string>


using namespace std;


string getString(string filename) {
    ifstream inputFile (filename);
    string retValue = "";
    string str;
    if (inputFile) { // ensure the file opened
        while (inputFile.good()) {
            if (getline(inputFile, str)) { 
                retValue += str; // append that to the values we already have...
            }
        }
    } else {
        cerr << "Error: did not open file" << endl;
    }

    inputFile.close();
    return retValue;
 }


int main() {
    string str = getString("text1.txt");
    cout << "getString: \"" << str << "\"" << endl;
    return 0;
}


This will read the whole file into 1 string... I would not really recommend this for large files, but it works for small ones.

By passing in the file name as an argument I made the function a little more versitile. It is generally not a good idea to hard code values like file names into functions which you may wish to use for something else (either in this program, or copy and paste into some future project).

Next I did if (inputFile) -- this is to ensure the file actually opened... sometimes the files don't open.

while (inputFile.good()) -- rather than .eof() I just check to see if inputFile when into an error state.
if (getline(inputFile, str)) check to see if we can read a string, if we CAN, then append it.

Note that the above two lines could actually be combined to:
        while (getline(inputFile, str)) {
            retValue += str; // append that to the values we already have...
        }
but I thought I would break it up a little to make it clearer.

retValue += str; we want to keep track of the last things read, so here we will append what we just read to what we already have. Thus building up the stirng.


So think clearly about what you have to do. Now that you have this function, next is to write a function that will count up each char...
Was This Post Helpful? 0
  • +
  • -

#13 Guest_anny*


Reputation:

Re: Letter frequency

Posted 26 May 2010 - 08:48 AM

Thank you very much!!! I rewrite my code and made the function that count up each character but because I have to create also a function that compares the probabilities Pi,I have to make subroutines can you please tell me the way to call a subroutine correct.I keep getting errors!
Well this is the error free code
 #include <iostream> 
#include <vector> 
#include <algorithm> 
#include <string> 
#include <iomanip> 
#include<fstream>
#include<conio.h>
#include<stdio.h>
using namespace std; 
string getString(string filename)
  { 
    ifstream inputFile ("text1.txt"); 
    string retValue = ""; 
    string str; 
    if (inputFile) { // ensure the file opened 
        while (inputFile.good()) { 
            if (getline(inputFile, str)) {  
                retValue += str; // append that to the values we already have... 
            } 
        } 
    } else { 
        cerr << "Error: did not open file" << endl; 
    } 
 
    inputFile.close(); 
    return retValue; 
 } 




    int main () { 
        int index;
string str = getString("text1.txt");     
        while(str.length() == 0);
         
        double number_frequency[64]; 
        int number_count [64]; 
        for(int index = 0; index < 64; index++) 
        { 
                number_frequency[index] = 0; 
                number_count[index] = 0; 
        } 
        int number_of_numbers = 0; 
        int test_string_length = static_cast<int>(str.length()); 
        for(int index = 0; index < test_string_length; index++) 
        { 
                char symbol = str[index]; 
                symbol = toupper(symbol); 
                if('!' <= symbol && symbol <= '`') 
                { 
                        int number_index = symbol - static_cast<int>('!'); 
                        number_count[number_index]++; 
                        number_of_numbers++; 
                } 
        } 
        if(number_of_numbers > 0) 
                for(int index = 0; index < 64; index++) 
                        number_frequency[index] = static_cast<double>(number_count[index]) / number_of_numbers; 
                        
                        ofstream out("anny.txt"); //save in out.txt 
  if(!out) { 
    cout << "Cannot open file.\n"; 
    return 1; 
   } 
     out << setprecision(2) << fixed; 
        for(int index = 0; index < 64; index++) {
               
     out << setw(6) << static_cast<char>(index + static_cast<int>('!')); 
     out<< setw(10) <<"fi:" << setw(2)<< number_count[index];  
     out<<setw(10) <<"Pi:  "<<number_frequency[index] <<setw(3)<<number_of_numbers<< endl; }
     
      
     out.close();
   cout << "getString: \"" << str << "\"" << endl;     
      system("pause");
        return 0;

}
         
    



I want to make main in a subroutine that will give me as return value the double number_frequency[index] ;
Was This Post Helpful? 0

#14 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Letter frequency

Posted 26 May 2010 - 09:34 AM

:) when I looked at your code I heard that little trombone bit "wha, whaaa, whaaaaaa" in my head... why did you paste back "text1.txt" into the "getString" function?

Anyway... not really important.



ok so looking at your current code... while (str.length() == 0); -- what is this supposed to do? -- What is does is enter an infinite loop if the file didn't contain any data.

Quote

    double number_frequency[64];
    int number_count [64];

    for (int index = 0; index < 64; index++) {
        number_frequency[index] = 0;
        number_count[index] = 0;
    }


a little trick... if you want to initialize an array to zero C++ has a shortcut:
    double number_frequency[64] = { 0 };
    int number_count [64] = { 0 };
-- initializes the whole array to 0.

you use static_cast<int> a great deal... I suppose this is not bad, but most of the time the compiler is totally capable of figuring out what you mean... not a big deal though, I suppose it is not a bad habit to get into.

BUT, as long as you are getting into habits... try to get into the habit of always using brackets { } arround if-blocks and for-blocks... you don't have to of course, but it will make your life easier in the long run.

So... just place your code into a function... looks like you are on your way...
Was This Post Helpful? 0
  • +
  • -

#15 Guest_anny*


Reputation:

Re: Letter frequency

Posted 26 May 2010 - 09:45 AM

I was getting an error message mabe I needed another include.
No matching function...
while (str.length() == 0); 

Well it was left from the many corrections!
Thank you now my programm is not so slow any more!!!
Was This Post Helpful? 0

Page 1 of 1