![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#11 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 884
Rep Power: 4
![]() |
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); occ [ ch ] = occ [ ch ] + 1 ; occ[ch-'a']++; void frequency(vector<int> &occ); ... frequency(occ); ... void frequency(vector<int> &occ) 5. Print out the frequencies using the same method you put them in (subtract 'a' from the letter cout<<letter << occ[letter-'a'] << endl ; |
|
|
|
|
|
#12 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 884
Rep Power: 4
![]() |
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;
} |
|
|
|
|
|
#13 |
|
Newbie
Join Date: Dec 2005
Posts: 14
Rep Power: 0
![]() |
[quote=The Dark]Give your occ vector an initial size of 26
occ.resize(26); occ [ ch ] = occ [ ch ] + 1 ; occ[ch-'a']++; void frequency(vector<int> &occ); ... frequency(occ); ... void frequency(vector<int> &occ) 5. Print out the frequencies using the same method you put them in (subtract 'a' from the letter cout<<letter << occ[letter-'a'] << endl ; 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? |
|
|
|
|
|
#14 |
|
Hobbyist Programmer
Join Date: Dec 2005
Posts: 118
Rep Power: 0
![]() |
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 ![]() |
|
|
|
|
|
#15 |
|
Newbie
Join Date: Dec 2005
Posts: 14
Rep Power: 0
![]() |
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 ... |
|
|
|
|
|
#16 | ||
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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:
Quote:
__________________
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 |
||
|
|
|
|
|
#17 |
|
Programmer
|
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] + " ");
}
}
} |
|
|
|
|
|
#18 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|