#include <iostream> #include <cstdlib> using namespace std; void getInput (int& seed, int& numChar); void charGen (int currentNumChar, int numChar, char alpha [52]); int main(){ int seed, numChar, currentNumChar; char alpha [52] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S' ,'T' ,'U', 'V', 'W', 'X', 'Y', 'Z','a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}; getInput (seed, numChar); srand (seed); charGen (currentNumChar, numChar, alpha); cout<<endl<<"Summary for the characters: "<<endl<<endl; system("pause"); return 0; } void getInput (int& seed, int& numChar){ cout<<"Enter a seed number: "; cin>>seed; cout<<"Number of characters: "; cin>>numChar; cout<<endl<<numChar<<" characters generated:"<<endl<<endl; } void charGen (int currentNumChar, int numChar, char alpha [52]){ for (currentNumChar = 1; currentNumChar <= numChar; currentNumChar++){ if (currentNumChar < numChar) cout<<alpha [rand()%53]; else cout<<alpha [rand()%53]<<endl; } }
5 Replies - 4228 Views - Last Post: 23 March 2013 - 07:15 PM
#1
counting occurrence of characters in random char array
Posted 23 March 2013 - 03:22 PM
Hi, I have to create a program that generates an array of random characters. The program then prints how many times each character occurs. So far I have been able to generate the random characters but I have no idea how to count each character's occurrence. Any help is greatly appreciated. Here is my code so far,
Replies To: counting occurrence of characters in random char array
#2
Re: counting occurrence of characters in random char array
Posted 23 March 2013 - 04:24 PM
Maybe this blog entry might give you some ideas.
#3
Re: counting occurrence of characters in random char array
Posted 23 March 2013 - 04:27 PM
well man since you have a fixed number of characters you could create a separate function, maybe call it something like "charTracker();" and every time your random number generator chooses a letter it sends that character into the tracker. In the tracker you can make a 26 line switch case, put upper and lowercase in each character with an if statement to decide if its upper or lower, then have it assign the value to that char (etc.A=A+1;) etc for each character. Then youll need to make a separate function that will print out all of the characters. Its alot of hard coding,but it will work
This post has been edited by Midi_: 23 March 2013 - 04:27 PM
#4
Re: counting occurrence of characters in random char array
Posted 23 March 2013 - 04:44 PM
JackOfAllTrades, on 23 March 2013 - 04:24 PM, said:
Maybe this blog entry might give you some ideas.
The blog entry that you linked is very helpful, thank you.
Midi_, on 23 March 2013 - 04:27 PM, said:
well man since you have a fixed number of characters you could create a separate function, maybe call it something like "charTracker();" and every time your random number generator chooses a letter it sends that character into the tracker. In the tracker you can make a 26 line switch case, put upper and lowercase in each character with an if statement to decide if its upper or lower, then have it assign the value to that char (etc.A=A+1;) etc for each character. Then youll need to make a separate function that will print out all of the characters. Its alot of hard coding,but it will work
Thank you for your suggestion.
#5
Re: counting occurrence of characters in random char array
Posted 23 March 2013 - 07:00 PM
Midi_, on 23 March 2013 - 04:27 PM, said:
well man since you have a fixed number of characters you could create a separate function, maybe call it something like "charTracker();" and every time your random number generator chooses a letter it sends that character into the tracker. In the tracker you can make a 26 line switch case, put upper and lowercase in each character with an if statement to decide if its upper or lower, then have it assign the value to that char (etc.A=A+1;) etc for each character. Then youll need to make a separate function that will print out all of the characters. Its alot of hard coding,but it will work
Ouch!

Think of the letter as an ascii number (which they are), and then as that number, as an index to an array.
Along the lines of:
int frequncyCounter[SIZE]={0}; //All elements set to zero frequencyCounter[asciiValueOfLetter]++;
Won't work with huge numbers, but for smaller numbers and all letters, it's great.
#6
Re: counting occurrence of characters in random char array
Posted 23 March 2013 - 07:15 PM
Expanding on what Adak said, each ascii character as a decimal representation (Ascii Table). Knowing this, you can create an array which covers the scope of all ascii characters (0 - 127).
Iterate over the random input characters, and increment the charFreq array at the index of the input character.Once you do this for every character in the input, the charFreq array will have the frequency of each letter at each index.
i.e.
int charFreq[127];
Iterate over the random input characters, and increment the charFreq array at the index of the input character.Once you do this for every character in the input, the charFreq array will have the frequency of each letter at each index.
i.e.
get input for each char in input charFreq[input[i]] += 1;
Page 1 of 1