Javascript required
Skip to content Skip to sidebar Skip to footer

Read Number From File Display Frequence of Number in Array C++

Hello, I'm reading a text file and I'm trying to count the frequency of each letter. Assuming the whole document is capital letter, this is what I take so far. In the end, I'm trying to obtain how many instances of each letter of the alphabet in that location are, and then I can split up by the total to get the percentage of each letter. I'm only stuck on keeping a running total of each letter. I know in that location are other means to do it. I am just trying to use an array to keep rail. Would I have to have variable for each assortment element, or is there a simpler mode?

                        one
two
three
four
v
half dozen
7
viii
nine
ten
11
12
thirteen
14
15
16
17
18
xix
20
21
                                                  #include "stdafx.h"                          #include "fstream"                          #include <iostream>                          using                          namespace                          std;                          int                          _tmain(int                          argc, _TCHAR* argv[]) {                          int                          total = 1;                          char                          array [26] = {'A','B','C','D','E','F','G','H','I',                          'J','K','Fifty','M','Due north','O','P','Q','R','S','T','U','V','W',                          'X','Y','Z'}; 	ifstream infile; 	infile.open("p4in.txt");                          while(!infile.eof()) 	{                          char                          c = infile.get();         full++; 	}                          return                          0; }                      

Concluding edited on

And then is there a fashion to practise this? Is there a mode to use an assortment to track frequency? I've read dozens of means to exercise it otherwise, and I know several, merely if you have an assortment, can that be used, in any way, to rail the amounts of letters in a document?

If you lot're going to track each letter, then yous need an array.
Here's the simplest way I know:

                        1
2
three
iv
5
6
                                                  int                          cnt[26]; ... memset(cnt,0,26*sizeof(int)); ... cnt[c-'A']++;                      

do I non already take an array? What are the ...? what is memset? what is c-'A'?

what cheshirecat meant was that you make a new array of the same size, made of integers, whose elements get incremented whenever the alphabetic character in the aforementioned index appears.

I'yard cheshirecat - the original poster, and I don't know how to do that.

I become that int cnt[26] is an assortment. But I don't know what is happening with memset, cnt, or those 3 dots.

memset initializes the cnt array to 0.
http://www.cplusplus.com/reference/cstring/memset/

The three dots were just an indication that other statements in your program get there. I causeless y'all could figure out where each line should go. i.e. int cnt[26]; goes after line 12 in your program. memset goes before line 15. cnt[c-'A']++; goes after line 18.

c-'A' is an expression that converts c to an array alphabetize. If c contains 'A', then c-'A' is naught. If c contains 'B', and so c-'A' is ane, etc.

Hi cheshirecat,
I read this post rapidly considering I saw your title I did something very like to this in my class where I had to go through a file and count frequency of each letter.
Anyways I don't know if this will assistance merely here is the role I had for my class maybe you can alter it ?

                        1
2
3
four
5
6
7
viii
9
                                                  //Role to fill frequency assortment                          void                          fillFrequency(cord str1,                          int                          freqcount[],                          const                          int                          alphabet) {                          for(string::size_type i = 0; i < str1.size(); i++) 	{	 		str1[i] = toupper(str1[i]); 		++(freqcount[str1[i] -                          'A']); 	} }                      

The str1[i]-'A' is considering of ascii tabular array. Then if I remember right it works like this. The size of the array is 26 for the letters of the alphabet. So after the function converts the letter to upper(str1[i] = toupper(str1[i]);) which y'all already have. Then the values (expect upwards ascii table) are betwixt 65 and 90. Starting with A = 65, to Z = 90. And then lets accept an example say the letter 'A' its value is 65. So, 65-65 is 0. Think the array has size 26 so A is now stored in position 0. If you lot did 'B' it would be 66-65 and so once more position 1 would now take B. This is what this function is doing.

++(freqcount[str1[i] - 'A']);
the ++ will increment the frequency. And then if A goes through position 0's VALUE is ane. Suppose 'A' goes through again then it will be 2 at position 0.

2 | | | |.....
That's what the contents of the array will have and so really you lot fill up the array and get the count of each letter of the alphabet with this role. This was hard to come up with for me to my lab help did a tutorial I idea this was was cool and it gets rid of all the bullsh*t it'due south pretty uncomplicated when you figure it out. Promise my explanation was OK I didn't read back over information technology to proof myself:)
Hope it helps dude:)

Last edited on

The array of ints he posted is sometimes called a "saucepan." Each array chemical element corresponds to the elements of the char array. As you check the input versus the char array yous increment the int array for the corresponding letter of the alphabet. This "counts" how many times each letter of the alphabet occurs in your input.

Instead of memset yous could initialize the int array similar this too:

                        1
2
iii
four
                                                  int                          cnt[26] {0};                          int                          cnt[26] = {0};                          int                          cnt[26] {};                          int                          cnt[26] = {};                          // not 100% sure on this one, just I can't find anywhere that says this isn't valid                                                                        

Terminal edited on

well thank you jlillie, I guess I but don't understand how strings work whatsoever. So, I'll have to take a look at that.

Then, I don't need information technology in a separate function - we're assuming information technology's all caps and then I don't know toUpper either.

Then, I would then have this -

for(string::size_type i = 0; i < str1.size(); i++)
{
++(freqcount[str1[i] - 'A']);
}

But why does it say 'A' there? I hateful, what if it'southward a B or a C? Is that only going to rail A? Information technology also looks like we're using two arrays here. Is that correct?

Concluding edited on

also, abstraction, if yous don't use memset, it won't commencement at 0?

OK, dude.
And then, there is only one array that is called freqcount[] .
That is correct. str1[] is a cord and [] is used as an alphabetize. The thing you take to call up is that information technology is used for both strings and arrays. So you lot see str1[] and yous know that information technology is a cord then it behaves as I accept tried to show in this example.

Compile this.

                        one
two
3
4
5
6
vii
eight
9
ten
11
12
xiii
fourteen
15
16
17
18
nineteen
20
21
22
23
24
25
26
27
28
                                                  #include<cord>                          #include <iostream>                          using                          namespace                          std;                          int                          main() {  	string south =                          "boat";                          //This is merely an example without using the loop [] is merely for the index                          // Y'all will see when you run it.                          cout << south[0] << endl; 	cout << s[1] << endl; 	cout << s[2] << endl; 	cout << s[iii] << endl; 	cout << endl;                          for(int                          i = 0; i < 4; i++) 	{ 		cout << due south[i] << endl;  	}   arrangement("pause");                          return                          0; }                      

Read my post in a higher place and go to the table to see the values for the messages y'all will encounter that if i was = 'B' = 66 that this ++(freqcount[str1[i] - 'A']); Is at present ++(freqcount[66-65]); Remember 'A' is 65. So it really is merely proverb this ++(freqcount[ane]); .
The ++ merely increments the freqcount at position 1 (or whatsoever the subtraction tells united states) So if the subtraction gave the value of 4 the poistion 4 is incremented this keeps rail of all the frequency of any letter that is uppercase. Remember the size of the array is 26 for each letter of alphabet. At present yous have an array filled with the frequency of each alphabetic character. Hopefully this makes more sense after I tried to explain [] for strings. Mess with the code you will meet ...Practiced luck ...Don't worry I'thousand stressed most my class also we will make it dude!

Sorry jlillie, I appreciate the effort, but I don't understand nigh of it, I ask specific questions about certain parts of it so that I can sympathize the whole, but you are just giving me everything in it's entirety and not answering my questions specifically. It'southward similar trying to explain what a paragraph ways in castilian when I'm asking you about specific words. It doesn't help me to know what the entire paragraph means. I need to know what the specific words mean and so that I can create my ain paragraphs. Or in simpler terms, I don't get this crap, then lets keep information technology uncomplicated.

What mukomo was saying was that I have two arrays -

so something like:

char keyboard[27] = {'A','B','C','D','E','F','G','H','I',
'J','K','Fifty','Thousand','N','O','P','Q','R','Due south','T','U','V','Westward',
'X','Y','Z'};
int frequency[27]

And then, if I get a 'B', how is frequency going to know to add something to number 1 (considering frequency starts at 0 for A)?

You lot have to read what I wrote. I'k telling you lot I tin't explicate it any better. I'chiliad distressing. Did you google ascii table? For your question you ask final I will try to explain this. It is really what I'grand trying to explain above.
The array that you are filling in my example is just an array of frequency
iii | 6 | 1 | nine | .....=> So position 0 represents 'A' and one represents 'B' and then along.

Expect I know I explained this relatively the same the specific thing you lot are looking for is here
I swear it is all here.
++(freqcount[str1[i] - 'A']); IT will know to add together something to number one because of the ++. 'A' is just a value of '65'. So if y'all got B it would be 65-64 which is 1. Then it volition get to position 1 then lastly information technology ++ position one. This does information technology for every letter you go. Delight get to ascii table look at the values of the uppercase messages. Remember that your assortment size is 26. Take any of the values for majuscule and walk through the code.
Take a pen and paper. Practise ++( inside here first) So pick any letter of the alphabet (capital letter from table)and substitute for i then evaluate the code.

Delight please go to ascii and just substitute a value for i and evaluate information technology. Like I suggested. Yous volition run across. I can't explain information technology whatsoever better. Really my response explains the specific question you asked it is only nosotros are not seeing i to i ....(become it) .
It'south OK though information technology happens. Please just evaluate it please. Use the ascii table!!!! This array is just the count retrieve at that place is no 'A' inside of it . Merely the position 0 represents 'A' position 1 represents 'B'.
Evaluate it!!!!! Expert luck.

I've skimmed through this thread merely not studied information technology in detail. Sad if I missed something. My version:

                        i
two
3
iv
v
half dozen
vii
8
9
ten
11
12
13
14
xv
xvi
17
xviii
xix
xx
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
xl
41
                                                  #include <iostream>                          #include <fstream>                          #include <iomanip>                          using                          namespace                          std;                          int                          main() {                          int                          total[26] = {0};      ifstream infile("input.txt");                          if                          (!infile)     {         cout <<                          "Error opening input file"                          << endl;                          return                          0;     }                          char                          c;                          while                          (infile.get(c))                          // read characters 1 at a time                          {                          if                          (isalpha(c))                          // check information technology is a-z or A-Z                          {             c = toupper(c);                          // brand it e'er A-Z                          // char A-Z has ascii code  65 to 90                          // Decrease 'A' to become                          int                          index = c -                          'A';                          // alphabetize in range 0 to 25;                          total[alphabetize]++;                          // increment corresponding total                          }     }                          for                          (int                          i=0; i<26; i++)                          // Impress the results                          {         cout <<                          "  "                          <<                          char(i+'A') <<                          " occurs "                          << setw(five) << total[i] <<                          " times"                          << endl;     }                          return                          0; }                      

Input:

Lorem ipsum dolor sit down amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore european union fugiat nulla pariatur. Excepteur sint occaecat cupidatat not proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

Output:

                                  A occurs    29 times   B occurs     three times   C occurs    16 times   D occurs    19 times   E occurs    38 times   F occurs     3 times   G occurs     3 times   H occurs     1 times   I occurs    43 times   J occurs     0 times   One thousand occurs     0 times   L occurs    22 times   M occurs    17 times   N occurs    24 times   O occurs    29 times   P occurs    11 times   Q occurs     five times   R occurs    22 times   S occurs    18 times   T occurs    32 times   U occurs    29 times   V occurs     iii times   West occurs     0 times   X occurs     three times   Y occurs     0 times   Z occurs     0 times                              

In my file, there are various nonalphabetic characters, such as spaces and punctuation, hence the use of isalpha() . Similarly, there are both lower and upper example messages, hence toupper() is used.

Last edited on

Well, you enquire me to compile this simply it doesn't work.

                        1
2
3
4
v
vi
7
8
ix
ten
eleven
12
xiii
14
xv
16
17
18
19
20
21
22
23
24
25
26
27
28
                                                  #include<string>                          #include <iostream>                          using                          namespace                          std;                          int                          primary() {  	cord southward =                          "boat";                          //This is only an example without using the loop [] is just for the index                          // You lot will see when you run it.                          cout << southward[0] << endl; 	cout << s[1] << endl; 	cout << s[2] << endl; 	cout << s[iii] << endl; 	cout << endl;                          for(int                          i = 0; i < 4; i++) 	{ 		cout << s[i] << endl;  	}   organization("pause");                          return                          0; }                      

I also understand that 'A' has value of zippo. 'B' has value of 1. I only don't become how you become that information from the A to the frequency assortment. No code really works when I run information technology, so I tin can't mess around with information technology to come across the bodily steps that the programme is taking behind the scenes. I would just copy some lawmaking from the net and mess effectually with it, but all the stuff I've found - 100% of it - for tracking frequency of information read from a file, doesn't utilise arrays. Everyone keeps going back to strings, but I never get a working piece of lawmaking that I tin integrate into my program, merely the idea. So, if I did sympathise strings, which I don't, I could use one in my plan to get the arrays to sync up somehow. I am continuing to pursue an respond, don't go me wrong, just at this point information technology looks similar I'm non going to observe it in a forum.

Final edited on

No. 'A' has the value 65, 'B' has the value 66 and so on.
http://world wide web.asciitable.com/
That'south why, in lodge to find the right value for int index I have to adjust information technology, effectively subtracting 65 from the code of the each particular character.

No code really works when I run it

My lawmaking is a working instance. If it doesn't work for you, delight let me know what goes incorrect.

Yous say y'all don't understand strings. At its center, a cord is simply an array of characters. What in particular don't y'all understand?

It's disappointing to read this

I am standing to pursue an answer, don't go me wrong, but at this point it looks like I'yard non going to discover it in a forum

, after I went to the trouble of posting sample code, and spent time calculation comments to explain the important parts.

Last edited on

Dude I don't know what to tell you. I just copy and pasted my code that I you said didn't work as is on my visual studio and information technology works just fine. That example is supposed to help you see what [] is doing.

I but don't go how y'all become that information from the A to the frequency array.

This would say freqcount[12] This is at position 12 Correct ? Become TO THE ASCII TABLE A HAS A VALUE OF 65 (Uppercase). 66(B) - 65(A) is 1
This now says freqcount[1] This one is position 1 just like 12 a few sentences agone. Now the ++ on the outside of ++(freqcount[1]) litterally saying Increment POSITION 1 Past One COUNT
If B came through in one case POSITION 1 in the array freqcount will take a value of one if it came over again it will accept a value of 2.

Y'all need to look at ascii tabular array Please LOOK AT ASCII
Goodluck dude

Topic archived. No new replies immune.

Read Number From File Display Frequence of Number in Array C++

Source: https://www.cplusplus.com/forum/beginner/86186/