954,496 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

keeping a frequency of each character entered.

Hey all,

I have an array of uppercase letters, mixed with other non-alphaitalic charcters. I need to keep a frequency of the each of the uppercase letters, and how many other characters entered.

JONN:sad:

paradoxxx
Light Poster
36 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
 

Hmm.. ok I will give you a simple algo, you attempt it and post your code if you get stuck:Create a variable character count which counts the non uppercase chars and initialize it to zero.
Create an array of 26 integers and initialize it to zero. This array will keep track of the uppercase alphabet count.
Accept the input ( uppercase and other characters ) from the user into a array of characters ( C style string ).
Loop through the character array from the start ( i.e. position 0 ) till the end of string is encountered ( '\0' ).
For each character use the isupper( ) function to check whether the character is upper case.
If yes then increment the value corresponding to that character in the array. (Hint characters are actually integer values, so manipulate them to obtain the correct array index.
IF the char is not uppercase then just increment the count var.
Hope it helped, bye.

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

I already wrote the code to change the array entered to uppercase. I don't know how to go about with the frequency count.


#include

usingnamespace std;

#include

constchar DECLARED_SIZE = 96;

void uppercase(char letters[]);

constchar DELIMITER = '#';

int main()
{
cout << "ENTER UPTO 96 CHARACTERS, FOLLOWED BY A '#':\n";
char letters[DECLARED_SIZE], next;
int index = 0;
int count = 0;

for(index = 0; index < DECLARED_SIZE; ++index)letters[index] = 0;
index = 0;
cin >> next;

while ((next != DELIMITER) && (index < DECLARED_SIZE))
{
letters[index] = next;
index++;
cin >> next;
}
letters[index] = next;
int num_used = index;
cout << "YOU ENTERED: \n";
for (index = 0; index < num_used; index++)
cout << letters[index] << " ";
cout <= 'a' && letters[k] <= 'z')
{
letters[k] -= 32;
}
else
{
letters[k] = letters[k];
}
}
return;
}

paradoxxx
Light Poster
36 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
 

Did you read the algo I posted above? It exactly describes how you should go about finding freqency count.

And btw is there any restriction on using in built functions in your assignment, coz there is a function toupper( ) which accepts a character and returns its uppercase. So you should use that function instead of writing your own.

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

This is all I can do, I'm sorry.
char others = 0;
int counter[26] = {0};
cin >> letters[k]; //from above code//

paradoxxx
Light Poster
36 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
 

I haven't learnt how to loop through an array, or how to use the isupper() function.

paradoxxx
Light Poster
36 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
 

Okay, the thing is that moderataors arent allowed to post code to help people out, its against the rules so I will just give you a short tutorial.

For learning about loops go here
http://www.cprogramming.com/tutorial/c/lesson3.html

For learning about accepting user input go here
http://www.cprogramming.com/tutorial/string.html

For use of toupper function go here
http://www.cprogramming.com/fod/toupper.html

Create a program from start with the help of these tutorials, adding feature one by one so that you dont mess it up, and then post your new program.

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

Thanks, and God Bless,
Jonn.

paradoxxx
Light Poster
36 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
 

Ah... yes thank you "Father" :D

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

Paradox: didn't you read this thread that you started a couple days ago ?? The question was answered there, complete with examples.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

I did, and it crashed. I tried this:
char i;

for (i = 0 ; i < 255; i++)
{
if( count[i] > 0)
printf("Char: %c was found %d times", count[i], count[i]);

}

That's why I posted the question again, to see if anyone has another alternate code. The case, switch code is too long.

paradoxxx
Light Poster
36 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
 

The problem is somewhere else. You should post the rest of your program. You should have declare array count like this:

int count[255] = {0}
Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 
I did, and it crashed. I tried this:


That's strange because I tested dragon's code from the other post and it works fine for me...
As far as the switch-case you're right, it's to many lines.
Instead of

for (int i=0; i<MAX_SIZE; i++)
    {
        switch (letters[i])
        {
            case 'a': counter[0]++; break;
            case 'b': counter[1]++; break;
            case 'c': counter[2]++; break;
              //etcetcetc
 
        }
    }

You could use:

for (int i=0; i<MAX_SIZE; i++)
{
counter[letters[i] - 48]++;
}
Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 

Thanks,

Should the bracket be there between counter and letters?

for (int i=0; i

paradoxxx
Light Poster
36 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
 

Yes, 2 opening and 2 closing brackets

Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 

Thanks niek e. I will try to implement the code tonight. I have a question though, why 48?

for (int i=0; i

paradoxxx
Light Poster
36 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
 

Since 48 is the ASCII value of character '0'.

It would be better if you replace 48 in your formula to '0' since it would make more sense and you dont have to remember all the integer equivalents of the different characters. Just using random literals in teh program makes it difficult to understand for others and even for you after some time has elasped.

In short just mysterious literals should be avoided, follow my method and use '0'.

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

Ah... tricky, thanks. Then cout << letters[i] <<"appears "<< counter[i] "times\n";
to see each character.

paradoxxx
Light Poster
36 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
 
Ah... tricky, thanks. Then to see each character.

Hmm... no its incorrect. Correct one is:

cout << letters[i] <<" appears "<< counter[i] << " times\n";
~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

Hmm... no its incorrect. Correct one is:

cout << letters[i] <<" appears "<< counter[i] << "
 times\n";

I did as the doctor ordered, the program ran, but did not calculate the count. Here's my code:

#include <iostream>
using namespace std;
#include <stdio.h>
const char DECLARED_SIZE = 96;
void uppercase(char letters[]); 
void frequency(char count[]);
const long LIMIT = 96;
const char DELIMITER = '#';

int main()
{
    cout << "ENTER UPTO 96 CHARACTERS, FOLLOWED BY A '#':\n";
    char letters[DECLARED_SIZE], next;
    int index = 0;
    int count = 0;
    
    for(index = 0; index < DECLARED_SIZE; ++index)letters[index] = 0;
        index = 0;
        cin >> next;
    
    while ((next != DELIMITER) && (index < DECLARED_SIZE))
    {
        letters[index] = next;
        index++;
        cin >> next;
    }
    letters[index] = next;
    int num_used = index;
    cout << "YOU ENTERED: \n";
    for (index = 0; index < num_used; index++)
    cout << letters[index] << " ";
    cout <<endl;
    uppercase(letters); 
    
    return 0;
}  
//CHANGE LOWERCASE TO UPPERCASE.
void uppercase(char letters[])
{    
    int size = 0;
    int k = 0;
    char * answer;
    for (k = 0; letters[k] != DELIMITER; k++)
    {
        size++;
    }
    answer = new char[size + 1];
    for (k = 0; k < size; k++)
    {
        if(letters[k] >= 'a' && letters[k] <= 'z')
            {
                letters[k] -= 32;
            }
        else 
            {
                letters[k] = letters[k];
            }
    
    
    int counter[] = {0};
        for (int i = 0; i < 255; i++) 
        {
            counter[letters[i] - 48]++;
            cout << "LETTER - " << letters[i] << " APPEARS " << counter[i] << "TIMES\n";
        }

    }
    for (k = 0; k < size; k++)
    {
        if (letters[k] >= 'A' && letters[k] <= 'Y')
    
        {
                answer[k] = letters[k] + 1;
        }                              
        else if (letters[k] == 'Z')
            {
                answer[k] = letters[k] - 25;
            }
        else if (letters[k] < 'A' || letters[k] > 'Z')
            {
                answer[k] = letters[k];
            }
                
            
        cout << answer[k] << " ";
    
        
    

    }
    return;
}
paradoxxx
Light Poster
36 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You