473,770 Members | 1,880 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

count the frequency of each letter of the alphabet in a file

3 New Member
so i'm attempting to make a program that reads in a file and then counts the frequency of each letter of the alphabet in the file and then outputs the letter with the frequency into a different file..
i didn't include the first part of the program because i'm pretty sure it's correct... i really have no clue what's wrong with this so any help would be great

Expand|Select|Wrap|Line Numbers
  1.     vector<int> occurrencesVec(26);
  2.     int i;
  3.     for(;;)
  4.     {
  5.         fin >> i;
  6.         if (fin.eof()) break;
  7.         else
  8.         {
  9.         if (('A'<=i<='Z') || ('a'<=i<='z'))
  10.         {
  11.             char lowercase = tolower(i);
  12.             occurrencesVec[lowercase - 'a']++;
  13.         }
  14.         }
  15.     }
  16.     for (int i = 1; i < occurrencesVec.size(); i++)
  17.     {
  18.         fout <<  i << occurrencesVec[i];
  19.     }
  20. }
  21.  
Dec 3 '07 #1
5 4540
Ganon11
3,652 Recognized Expert Specialist
1) Why are you reading in to an integer if there are going to be characters? Try making i a char.

2) Are there words in this file, or just letters separated by whitespace? If there are words, you may want to try fin.get rather than the >> operator.

Other than these minor things, your code looks fine. What exactly is not working?
Dec 3 '07 #2
svd
3 New Member
1) Why are you reading in to an integer if there are going to be characters? Try making i a char.

2) Are there words in this file, or just letters separated by whitespace? If there are words, you may want to try fin.get rather than the >> operator.

Other than these minor things, your code looks fine. What exactly is not working?
the file is just a bunch of words seperated by whitespaces, so i changed the fin >> i; to fin.get(i).. is that what you meant?

to answer your second question, the program builds fine, but when I "start without debugging" after I enter the infile name and the outfile name, a pop-up comes up that says "Debug assertion failed... vector subscript out of range"

it still does this after changing the >>
Dec 3 '07 #3
weaknessforcats
9,208 Recognized Expert Moderator Expert
file is just a bunch of words seperated by whitespaces, so i changed the fin >> i; to fin.get(i).. is that what you meant?

to answer your second question, the program builds fine, but when I "start without debugging" after I enter the infile name and the outfile name, a pop-up comes up that says "Debug assertion failed... vector subscript out of range"

it still does this after changing the >>
The >> operator cannot be used for strings. It skips whitespace so all you get is one word of the string. If that's your situation, then you are OK. Otherwise, use cin.getline() to fetch multiple word strings.

The debug assertion failure is almost certainly using the vector operator[] with an invalid index number. The only values you can use are the ones between 0 and size()-1 of the vector. Is this a case of an uninitialized index??
Dec 3 '07 #4
svd
3 New Member
The >> operator cannot be used for strings. It skips whitespace so all you get is one word of the string. If that's your situation, then you are OK. Otherwise, use cin.getline() to fetch multiple word strings.

The debug assertion failure is almost certainly using the vector operator[] with an invalid index number. The only values you can use are the ones between 0 and size()-1 of the vector. Is this a case of an uninitialized index??
what does uninitialized index mean? i'm trying to look at all the letters individually in the paragraph and put the frequency for each letter into a vector
Dec 3 '07 #5
weaknessforcats
9,208 Recognized Expert Moderator Expert
If you are using:
Expand|Select|Wrap|Line Numbers
  1. vector<int>  data;
  2.  
  3. cout << data[256];    //ERROR. There is no element 256
  4.  
Usually, the 256 is the value of a variable:
Expand|Select|Wrap|Line Numbers
  1. vector<int>  data;
  2.  
  3. cout << data[i];    //ERROR. 
  4.  
and the i has a bad value.

For arrays, the value between the [] is called the index value.
Dec 3 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

7
9699
by: Dung Ping | last post by:
Such as: <script> //code aaaa zzzz ccc
8
6342
by: Pete | last post by:
I need to write a program to do a frequency analysis on a string of text. Firstly I need to get the alphabet A to Z for comparison but I'm not sure how to go about this. Is the following the only way to do it: Alphabet = {'a','b','c',...,'z'}; or is there another way to represent the alphabet characters. Thankyou
8
21705
by: Mansi | last post by:
Given the following declaration: String letter = "A"; Is there a way that I can increment letter so that "B" is returned? Is there a way that I can add an offset so that let's say, "G" is returned? Thanks.
19
3144
by: jason_box | last post by:
I'm alittle new at C and I'm trying to write a simple program that will record the frequency of words and just print it out. It is suppose to take stdin and I heard it's only a few lines but I'm not sure where to start. The only example I have to work with is if you ran the program say: File list contains: This is a test.
1
1578
by: Rahul | last post by:
Hi, I tried many option, but did not found solution. Same Validation Error showing. If I have convert one table, it can easily converted. But If I have paste another table in same file, then showing validation Error. This Error is -- =======================================================
2
1757
by: Protoman | last post by:
How would I write a user defnable letter swapping algorithm? I've written an Enigma encrypting program, and I need to redo the plugboard function. Right now, the letter swapping is fixed. I need to allow the user to swap 23 pairs of characters. As you may have guessed, I'm using an alphabet of 36 characters, A-Z and 0-9. Thanks!!!!
4
13883
evilmonkey
by: evilmonkey | last post by:
Is there a way to use index of in java to pull the number of occurrences of every letter in a user defined sentence? I can brute force this but is there a faster more elegant way to achieve the same result. I've started to try this but not sure I am on the right track. any help or push in the right direction would be appreciated. import java.util.StringTokenizer; import java.util.Scanner; public class Main { public static void...
3
7794
by: waynejr25 | last post by:
can anyone debug my program and get it to run. #include <fstream> #include <iostream> #include <string> #include <cstdlib> #include <map> using namespace std;
13
3736
by: umpsumps | last post by:
Hello, Here is my code for a letter frequency counter. It seems bloated to me and any suggestions of what would be a better way (keep in my mind I'm a beginner) would be greatly appreciated.. def valsort(x): res = for key, value in x.items(): res.append((value, key))
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10099
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9904
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8931
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7456
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6710
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2849
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.