8 Replies - 832 Views - Last Post: 25 March 2013 - 10:40 AM Rate Topic: -----

#1 littleking  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 24-March 13

Having trouble counting the occurrence of characters in an array

Posted 24 March 2013 - 05:47 PM

Hi everyone. I have an array that produces randomly generated characters.I have another array for which I'm trying to count how many times each letter has occurred. What I have now is just giving me a very large number for every letter. Obviously wrong. Anyone have ideas or advice? I've been at it for way to long trying to get this. the void charactSummary function is the one I can't get right!


#include <iostream>
#include <cstdlib>
using namespace std;
void seedSetUp(int& seed);
void numberOfCharacters(int& numOfChar);
void randomGen(char genCharArray[], int& numOfChar, int& seed);
void charactSummary(char counterArray[], char charSum, int& numOfChar, char genCharArray[]);
int main()
{
    int numOfChar, seed, MAX_SIZE = 500;
    char genCharArray[MAX_SIZE], counterArray[26], charSum;
    seedSetUp(seed);
    numberOfCharacters(numOfChar);
    randomGen(genCharArray, numOfChar, seed);
    charactSummary(counterArray, charSum, numOfChar, genCharArray);
   
system("PAUSE");
return 0;
}

void seedSetUp(int& seed)
{
    cout << "Enter a seed number: ";
    cin >> seed;
    cout << endl;
}

void numberOfCharacters(int& numOfChar)
{
    do
    {     
        cout << "Number of characters: ";
        cin >> numOfChar;
        cout << endl;
        if (numOfChar > 500)
        {
           cout<< "That's number is too big, please choose a smaller number.\n\n";
        }
    }
    while (numOfChar > 500);
    cout << numOfChar << " characters generated: \n";
}

void randomGen(char genCharArray[], int& numOfChar, int& seed)
{
    int randomNum; 
    char charRand;
    srand(seed);
    for (int i = 0; i < numOfChar; i++)
    {
        randomNum = rand() % 52;
        if (randomNum < 26)
        {
            randomNum += 'a';
        }else{
            randomNum -= 26;
            randomNum += 'A';
        }
        charRand = (char) randomNum;
        genCharArray[i] = charRand;
    }
    for (int i = 0; i < numOfChar ; i++)
    {
        if (i % 10 == 0)
        {
            cout<< endl;
        }
        cout << genCharArray[i];
    }
    cout << endl << endl;
}
void charactSummary(char counterArray[], char charSum, int& numOfChar, char genCharArray[])
{
    int frequencyCounter[26] = {0};
    int i = 0, j = 0;
    for(charSum = 0; charSum < 26 ; charSum++)
    {
       counterArray[charSum] = 'a' + charSum;
    }
    for(charSum = 0; charSum < 26; charSum++)
    {
        counterArray[charSum];
        for(i = 0; i < numOfChar; i++)
        {
            genCharArray[i];
            for(j = 0; j < 26; j++)
            {
                frequencyCounter[j];
            }
            if (genCharArray[i] == frequencyCounter[j])
            {
                frequencyCounter[j] + 1;
            }
        }
        cout << counterArray[charSum] << " = " << frequencyCounter[j] << endl;
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Having trouble counting the occurrence of characters in an array

#2 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2934
  • View blog
  • Posts: 10,147
  • Joined: 08-August 08

Re: Having trouble counting the occurrence of characters in an array

Posted 24 March 2013 - 06:00 PM

What do you think this:
            genCharArray[i];


and this:
                frequencyCounter[j];

do?
Was This Post Helpful? 0
  • +
  • -

#3 littleking  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 24-March 13

Re: Having trouble counting the occurrence of characters in an array

Posted 24 March 2013 - 06:11 PM

What I want them to do is have:
frequencyCounter[j];

keep count of how many a's how many b's, c's, etc. occurred in
genCharArray[i];


I was trying to set
frequencyCounter[j];

to 0 for every letter in the alphabet and when it came across a letter it would add a 1 into the slot where that element is being stored.

should I be comparing it to my
counterArray[charSum];

which holds the letters a-z in an array,
and having it find ones that match it? If I do that, how would I set the counter up for that? Sorry, I'm still very very new to c++ and computer science in general.
Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2934
  • View blog
  • Posts: 10,147
  • Joined: 08-August 08

Re: Having trouble counting the occurrence of characters in an array

Posted 24 March 2013 - 06:19 PM

I didn't ask what you wanted them to do. I asked what you thought they did. You're trying to run but you don't know how to walk.
Was This Post Helpful? 0
  • +
  • -

#5 littleking  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 24-March 13

Re: Having trouble counting the occurrence of characters in an array

Posted 24 March 2013 - 06:27 PM

okay, well I know arrays hold a series of elements. I think that
frequencyCounter[j];

is holding 26 elements with 0 as their value.
Then I think that
genCharArray[i];

is holding randomly generated characters, size of which is decided by the user.
Was This Post Helpful? 0
  • +
  • -

#6 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

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

Re: Having trouble counting the occurrence of characters in an array

Posted 24 March 2013 - 09:52 PM

This is C, rather than C++, but if you study it, you'll see how the logic uses the index, and some subtraction, to count up the frequency of each letter.


You will need to save this with a dot c extension to compile it with the C compiler.

#include <stdio.h>
#include <string.h>

int main(void) {
   int i,len;
   int frequency[26];  
   char text[]={"Out in the West Texas town of El Paso,\nI fell in love with a Mexican girl.\n"};

   printf("%s\n",text);
   //get the length of text string
   len=strlen(text);
   printf("len: %d\n\n",len);
   
   for(i=0;i<26;i++) {   //set all frequency array elements to 0
      frequency[i]=0;
   }
   printf("\n\n");
   //get frequency of letters
   for(i=0;i<len;i++) {
      if(text[i]>='a' && text[i]<='z') {        //count up lowercase letters
         frequency[text[i]-'a']++;
         printf("frequency of %c: %d\n",text[i], frequency[text[i]-'a']);
         
      }else if(text[i]>='A' && text[i]<='Z') {    //counts up uppercase letters
         frequency[text[i]-'A']++;
         printf("frequency of %c: %d\n",text[i], frequency[text[i]-'A']);
      }
      if(i%8==0) {
         printf("Hit enter to continue\n");
         getchar();
      }

   }
   printf("\n\n");
   for(i=0;i<26;i+=2) {                  //show the frequencies of each letter
      printf("%c: %3d   %c: %3d \n",i+'a',frequency[i],i+1+'a',frequency[i+1]);
   }
   printf("\n");
   return 0;
}


Was This Post Helpful? 1
  • +
  • -

#7 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2934
  • View blog
  • Posts: 10,147
  • Joined: 08-August 08

Re: Having trouble counting the occurrence of characters in an array

Posted 25 March 2013 - 06:08 AM

View Postlittleking, on 24 March 2013 - 09:27 PM, said:

okay, well I know arrays hold a series of elements. I think that
frequencyCounter[j];

is holding 26 elements with 0 as their value.
Then I think that
genCharArray[i];

is holding randomly generated characters, size of which is decided by the user.

No, Both are accessing one element (numbers j and i) of their arrays but doing nothing with them! Study Adak's code and try again.
Was This Post Helpful? 0
  • +
  • -

#8 littleking  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 24-March 13

Re: Having trouble counting the occurrence of characters in an array

Posted 25 March 2013 - 08:58 AM

Thank you! Helped out a lot. Brand new to this stuff and I know I should study arrays more and I will, but projects don't wait for me. But you two were very helpful!
Was This Post Helpful? 0
  • +
  • -

#9 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,495
  • Joined: 23-August 08

Re: Having trouble counting the occurrence of characters in an array

Posted 25 March 2013 - 10:40 AM

Potentially helpful blog entry.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1