5 Replies - 1716 Views - Last Post: 23 March 2013 - 07:15 PM Rate Topic: -----

#1 kingcrow  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 23-March 13

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,
#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;											  
											  }	
}



Is This A Good Question/Topic? 0
  • +

Replies To: counting occurrence of characters in random char array

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6078
  • View blog
  • Posts: 23,548
  • Joined: 23-August 08

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.
Was This Post Helpful? 1
  • +
  • -

#3 Midi_  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 109
  • Joined: 22-May 12

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

Was This Post Helpful? 0
  • +
  • -

#4 kingcrow  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 23-March 13

Re: counting occurrence of characters in random char array

Posted 23 March 2013 - 04:44 PM

View PostJackOfAllTrades, 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.

View PostMidi_, 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.
Was This Post Helpful? 0
  • +
  • -

#5 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: counting occurrence of characters in random char array

Posted 23 March 2013 - 07:00 PM

View PostMidi_, 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.
Was This Post Helpful? 0
  • +
  • -

#6 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1086
  • View blog
  • Posts: 4,564
  • Joined: 09-June 09

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).

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;


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1