Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Dec 8th, 2005, 2:34 AM   #11
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 884
Rep Power: 4 The Dark is on a distinguished road
First off code tags are supposed to preserve indenting, to make it easier for people to read and hence to help you - in this case there doesn't seem to be any indenting - hopefully that it just because it has been swallowed by the forum, not because you don't indent.

OK, you are pretty close, here are some pointers, let me know if you need more details:
1. You don't need a vector for the letters, just using your occ vector is enough.
2. Give your occ vector an initial size of 26
occ.resize(26);
3. Once you have found a character and lower cased it, you can find its fequency position in the vector by subtracting 'a' from it (e.g. 'b'-'a' gives 1). You can then use ++ to increment the count. Change
occ [ ch ] = occ [ ch ] + 1 ;
to
occ[ch-'a']++;
4. Pass the occ vector to the frequence function, so that it can loop through and print it out
void frequency(vector<int> &occ);
...
  frequency(occ);
...
void frequency(vector<int> &occ)
The & there is to pass the vector by reference, rather than taking a copy of it.
5. Print out the frequencies using the same method you put them in (subtract 'a' from the letter
    cout<<letter << occ[letter-'a'] << endl ;
You might want to put a space in between the letter and the count, and possibly not print out letters which have a zero count.
The Dark is offline   Reply With Quote
Old Dec 8th, 2005, 2:39 AM   #12
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 884
Rep Power: 4 The Dark is on a distinguished road
Also note that assert is a very ugly way of checking that your file opened - you would be much better off with something like:
  ifstream fin(infile.data());
  if (!fin.is_open())
  {
     cout << "Couldn't open input file " << infile.data() << endl;
     return 10;
  }
  ofstream fout(outfile.data());
  if (!fout.is_open())
  {
     cout << "Couldn't open output file " << outfile.data() << endl;
     return 10;
  }
The return value of 10 is so that the OS (or any other caller program) can tell that your program failed.
The Dark is offline   Reply With Quote
Old Dec 8th, 2005, 2:28 PM   #13
c_newbie
Newbie
 
Join Date: Dec 2005
Posts: 14
Rep Power: 0 c_newbie is on a distinguished road
[quote=The Dark]Give your occ vector an initial size of 26
occ.resize(26);
3. Once you have found a character and lower cased it, you can find its fequency position in the vector by subtracting 'a' from it (e.g. 'b'-'a' gives 1). You can then use ++ to increment the count. Change
occ [ ch ] = occ [ ch ] + 1 ;
to
occ[ch-'a']++;
4. Pass the occ vector to the frequence function, so that it can loop through and print it out
void frequency(vector<int> &occ);
...
  frequency(occ);
...
void frequency(vector<int> &occ)
The & there is to pass the vector by reference, rather than taking a copy of it.
5. Print out the frequencies using the same method you put them in (subtract 'a' from the letter
    cout<<letter << occ[letter-'a'] << endl ;
QUOTE]

thanks much .. this was alot of help ... your advice seemed so much more logical than what i had ...
but there are certain things i think i just need to remove completely from this program because i dont think they are necessary .. i just have too much going on in there and because of that i am still not gettiing any result .. i have worked on this so much i dont even know what i am doing again .. i probably just need to start from scratch maybe?
c_newbie is offline   Reply With Quote
Old Dec 8th, 2005, 2:42 PM   #14
Klipt
Hobbyist Programmer
 
Join Date: Dec 2005
Posts: 118
Rep Power: 0 Klipt is an unknown quantity at this point
I think your problem was more an error in logic than an error in programming. Your initial source code looks like you're trying to store each letter instead of just the frequencies of each letter. The letters themselves are unneeded info.

The most important part of learning to program is learning to think like a computer scientist, not just to write like one
Klipt is offline   Reply With Quote
Old Dec 8th, 2005, 3:11 PM   #15
c_newbie
Newbie
 
Join Date: Dec 2005
Posts: 14
Rep Power: 0 c_newbie is on a distinguished road
yea ure right .. i think i had it figured all wrong from the beginning thats y i'm having so much trouble

i created a vector for the letters which i dont think was needed...
i'm just gonna have to start all over ...
c_newbie is offline   Reply With Quote
Old Dec 8th, 2005, 6:16 PM   #16
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
You're at post #15. I posted pseudo-code in post #7 to help you think about the problem logically. Did you read and think about that? Is it just a waste of freakin' time to devote time to these questions? I took that pseudo-code and dumped it into a blank cpp file as comments, then fleshed it out. Here's your work done for you. Of course, it isn't the only way. It's compiled and run, but not exhaustively tested. No warranty.
#include <iostream>
#include <fstream>
#include <vector>
#include <string>

using namespace std;

// For error conditions
int utOh (string elProblemo)
{
    cerr << elProblemo << endl;
    return 1;
}

int main (int argc, char *argv [])
{
   // Generate a C++ string containing the desired alphabet in lower case.
   string theSet = "abcdefghijklmnopqrstuvwxyz";
   // Establish a vector of ints of the same length, elements initialized to zero.
   vector <int> theCount;
   for (int i = 0; i < theSet.size (); i++) theCount.push_back (0);
   // Open an input file
   ifstream inFile ("inputstuff.txt", ios::binary | ios::in);
   if (!inFile.good ()) return utOh ("File didn't open");
   string aLine;

   while (true)
   {
       // Read a line.
       getline (inFile, aLine, '\n');
       if (inFile.fail ()) break;
       // For each character in the line, convert it to lower case 
       // and use a string function to find its position in the 
       // alphabet string (observe that all characters may not be present 
       // -- pay attention to that search result).
       string::size_type thePos;
       for (int i = 0; i < aLine.size (); i++)
       {
           thePos = theSet.find (tolower (aLine [i]), 0);
           // Use that index to increment the corresponding element 
           // in the integer array.
           if (thePos != string::npos) theCount [thePos]++;
       }
   }
   for (int i = 0; i < theSet.size (); i++)
       cout << theSet [i] << " : " << theCount [i] << endl;
   cin.get ();
   return 0;
}
Quote:
Originally Posted by Output
a : 3
b : 1
c : 3
d : 4
e : 13
f : 3
g : 2
h : 7
i : 11
j : 1
k : 1
l : 5
m : 4
n : 7
o : 15
p : 1
q : 1
r : 5
s : 5
t : 12
u : 3
v : 1
w : 3
x : 1
y : 2
z : 1
Quote:
Originally Posted by The input file
This is line one.
This is line two.
Now is the time for all good men to come to the aid of their country.
The quick brown fox jumped over the lazy dog.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Dec 9th, 2005, 9:26 AM   #17
JDStud6
Programmer
 
Join Date: Jan 2005
Location: Charleston, SC www.wareonearth.com
Posts: 57
Rep Power: 4 JDStud6 is on a distinguished road
Send a message via AIM to JDStud6
I'm bored, so I wrote it out in C# for some practice...using TheDark's way of subtracting the chars.

using System.IO;

class Occuring
{
	static void Main()
	{
        StreamReader reader = File.OpenText("test.txt");
        string input = reader.ReadLine().ToLower();
        int[] occur = new int[26];
        char[] alphabet = ("abcdefghijklmnopqrstuvwxyz").ToCharArray();
        char[] in1 = null;

        while (input != null)
        {
            in1 = input.ToLower().ToCharArray();

            for (int i = 0; i < input.Length; i++)
            {
                if (in1[i] != ' ' && in1[i] != '.')
                    occur[in1[i] - 'a'] += 1;
            }
            input = reader.ReadLine();
        }
        
        for (int i = 0; i < 26; i++)
        {
            System.Console.WriteLine(alphabet[i] + ":" + occur[i] + " ");
        }
	}
}
JDStud6 is offline   Reply With Quote
Old Dec 9th, 2005, 9:38 AM   #18
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Looks essentially identical. The use of 'a' as a basis for deriving array offsets constrains the operation to a particular codeset.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:37 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC