GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 03-Dec-2005, 10:16
sweeeeeeetlipss sweeeeeeetlipss is offline
New Member
 
Join Date: Dec 2005
Posts: 5
sweeeeeeetlipss is on a distinguished road

vectors.. calculating # of occurences


i wanna my program to read a text and calculate the number of occurrences of each letter in the file... this is what i have so far:

CPP / C++ / C Code:
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <cassert>
#include <vector>
#include <numeric>
#include <string>
using namespace std;

void read(ifstream & fin, vector<char> & letter, vector<double> & occ);
void results(ofstream & fout, vector<char> & letter, vector<double> & occ);

int _tmain(int argc, _TCHAR* argv[])
{
cout<<"To find the number of occurences of each letter " <<endl
<<"in a file, please enter the filename: ";
string infile;
cin>>infile;
cout<<"\nEnter the name of the output file: ";
string outfile;
cin>>outfile;

ifstream fin(infile.data()); 
ofstream fout(outfile.data());
assert(fin.is_open() && fout.is_open()); 

vector<char> letter;
vector<double> occ;

where do i go from here...
Last edited by LuciWiz : 03-Dec-2005 at 12:57. Reason: Please insert your C++ code between [c++] & [/c++] tags
  #2  
Old 03-Dec-2005, 21:57
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 927
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

Re: vectors.. calculating # of occurences


Hi blablabla(What is your name?)

Welcome to GIDForums.
Since your program is to read a text and calculate the number of occurrences of each letter in the file..., there is no need for a vector.

Dont use assert. It is only for debugging purposes.
Instead, use if statements.

So, to check the number of occurences, create a new character say c.
Create a counter say 'i' which is initialized to zero.
Then, use a while loop until the end of the file.
Get each character from the file one by one, and check whether it is equal to c.
It it is, then increment a counter i.

So, here is a sample:
CPP / C++ / C Code:
//code...
char c;
int i=0;
while(fin)
{
    fin>>c;
    if(c == 'a')       // 'a' is the character to  be searched from the file
        i++;
}  
cout<<"The number of occurences is "<<i;
//code...

If you want to find the occurence of any other character, simply change this statement:
CPP / C++ / C Code:
    if(c == 'a') // change the 'a' here to any letter you want

And remove all unnecessary headers: cassert, numeric, and vector.

If you are not going to get command line arguments, you can change the main function simply like this:
CPP / C++ / C Code:
int main() 
{
    // code..

Regards,
Paramesh.
__________________

Don't walk in front of me, I may not follow.
Don't walk behind me, I may not lead.
Just walk beside me and be my friend.
  #3  
Old 05-Dec-2005, 11:22
sweeeeeeetlipss sweeeeeeetlipss is offline
New Member
 
Join Date: Dec 2005
Posts: 5
sweeeeeeetlipss is on a distinguished road

Re: vectors.. calculating # of occurences


ok .. i get u ..
but the thing is the program has to find the number of occurrences of each letter ( all 26 letters) from the same loop ... not one by one

the thing is i was instructed (lol) to use a vector of length 26 to do this ...
sucks huh? .. bcuz i know there must be other easier ways to do this without using vectors... but i have to do it the vector way

so the program must be able to give the occurrences of all 26 letters and must recognize uppercase letters as the lowercase equivalent..

but i will try to work with what u gave me .. see if i can take it from there

thanks much
 
 

Recent GIDBlogA Week in Kuwait by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
vectors of references mirizar C++ Forum 1 12-Apr-2005 02:02
newbie needs help with vectors Quesius C++ Forum 0 30-Nov-2004 23:35
C++ Vectors Katie C++ Forum 5 17-May-2004 23:36
Declaring a vector of vectors? Lethal411 C++ Forum 2 20-Mar-2004 09:02
Calculating mathematical string flowercamel C Programming Language 9 04-Feb-2004 13:55

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 01:17.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.