Character frequency counter array.

Hi everyone, I've got another problem I'm stuck on, this one applies to arrays.
Background info, teacher gives instructions which must be followed to the letter, if they aren't, zero credit is given for the problem.
Instructions:
Write a function named "getCharacterFrequency. This function is passed a null terminated string as its first parameter. The second parameter is an uninitialized array of size 26 that will hold the function result.
The declaration shall be:
void getCharacterFrequency (char inputstring [], unsigned int frequency[]);
The function should first set all values in the frequency array to 0. The function should then use a loop to examine each character in the null terminated string. For each alphabetical character a through z, or A through Z, the function should increment the corresponding location in the array. For example, if the input string is "Hello", the frequency array will be as follows:
00001001000200100000000000
The first parameter is used to pass data into the function, and the second parameter is used to return the function results. The result should be returned. Do not display the result.


So here's the code I've got so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/*I know I don't need to use all these headers, I have a standard set of code I use for all my programs that includes all the headers and some comment notes that I've needed to include before, usually I start commenting the ones I don't need out once I get a working program.*/
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <string>

using namespace std;

void getCharacterFrequency (char inputString [], unsigned int frequency[]);

int main ()
{
    //main function is for TESTING only and does not get turned in.
    //problem 2
    unsigned int myIntArray2 [25];
    char myCharArray2 [25];
    cout << "test array for problem 2, start typing up to 26 characters" << endl;
    cin.getline(myCharArray2, 25);
    
    getCharacterFrequency(myCharArray2, myIntArray2);

    

return 0;
}

void getCharacterFrequency (char inputString [], unsigned int frequency[])
{
    const int ALPHA = 25;
    frequency[ALPHA];
    int index = 0;
    char ch;
    for (int count = 0; count <= ALPHA; count++)
    {
        frequency[count] = 0;
    }
    for(int i = 0; inputString[i]; ++i)
        {
        inputString[i] = tolower(inputString[i]);    
        }
    while ((ch = inputString[index++]) != '\0')
    {
        if (ch >= 'a' && ch <= 'z')
        {
         //I think I need to subtract 97 from the decimal value of ch and use that number for the array location of the frequency array and add 1 to that location.
        }
    }
}


So what I'm stuck on is at the very end, I have no idea how to increase the counter like that from one array to another array. Thank you everyone for help.
Last edited on
The first thing i notice is you are declaring your array with 25 instead of 26 elements.

You need to do something like this for example char myCharArray2 [26]

You have to do 26 then when you go to access the elements you can use myCharArray2[0] through myCharArray2[25] to see what's actually stored there.
good point, didn't think about the '\0' being auto appended to the end of the char array. Thank you for that.
Not sure I can help, your instructor seems so specific. Especially about the
void getCharacterFrequency (char inputstring [], unsigned int frequency[])

I'd say use a pointer to pass the information from the void function back to the main but they don't have the pointer listed.
Last edited on
your instructor seems so specific.


True story bro, I've literally gotten zero's on assignment programs because I used pass by reference instead of pass by value on a function, and I named a variable wrong on another assignment and got a zero for that one too. If it isn't exactly how he wants it, you get a zero. Does not matter if it compiles, or works, gets the right answer, or any of that.
I'd transfer classes if I was you.

Or just withdraw and take it next semester if you are in college.
Last edited on
Too late to transfer or withdraw.

Probably going to have to take it with a different teach next semester, not looking forward to the wasted money, but what are ya gonna do?

Does anyone else viewing this have any thoughts or helpful info? This assignment is due in 2 hours.
hmm I think i may have something, sec.

just one more problem to solve then I think it will do what he wants. It wont be pretty though lol.


Plus you should get bonus points for giving your instructor a headache. sec while i create this monstrous disgusting piece of code.
Last edited on
Ok, don't ask me why this works but you can see the value printing out correctly, you might want to remove that after or even comment it to show how it would print out so you can say the info is there.

If you want all you got to do is copy / paste the block for each letter d through z now.

If all your instructor cares about is his milestones being met then that should do it.

updated, he wanted it to increment each time.

If you only wanted to go through the character array once, you could use a serious of if statements

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <string>

using namespace std;

void getCharacterFrequency (char inputString [], unsigned int frequency[]);

int main ()
{
    //main function is for TESTING only and does not get turned in.
    //problem 2
    unsigned int myIntArray2 [26];
    char myCharArray2 [26];
    cout << "test array for problem 2, start typing up to 26 characters" << endl;
    cin.getline(myCharArray2, 25);
    myCharArray2[25] = '\0'; //Null Terminated String!!

    getCharacterFrequency(myCharArray2, myIntArray2);
    cout << myIntArray2[0] << myIntArray2[1] << myIntArray2[2]; // print out the number of a's, b's, and c's

return 0;
}

void getCharacterFrequency (char inputString [], unsigned int frequency[])
{
    const int ALPHA = 26;
    frequency[ALPHA];
    inputString[26];
    for (int i = 0; i < ALPHA; i++){//populate frequency array with 0's
        frequency[i] = 0;
    }

    for (int i = 0; i < ALPHA; i++)//loop through string and check each letter to increment the frequency array.
    {
        if (inputString[i] == 'a') {frequency[0] = frequency[0] + 1;}
        if (inputString[i] == 'b') {frequency[1] = frequency[1] + 1;}
        if (inputString[i] == 'c') {frequency[2] = frequency[2] + 1;}
    }
}
Last edited on
wow, you weren't kidding. I'm looking at that though, and I'm wondering if it can be condensed into a loop where frequency[index++] and something along the lines of where the inputString array == 97++ where 97 is the ASCII value of a. Seems like if counters started at the same value...
I just condensed it lol. I realised how inefficient it was.

That's how I normally do things though, find what I know works then adjust things to make it smaller.

You see what I mean by using the 26 if statements to check for each letter though?

You could still probably make it really hard to read for your instructor once you got it working. Use strange formatting and such.

There is probably some algorithm or beautiful code out there to do this more elegantly but if your instructor doesn't care about style at all and only cares for the milestones this should do it.
Last edited on
oh snap, that does look a lot better, there's got to be a way to do that with a loop though.
edit:
yeah, I see exactly how you are doing this, and no, I don't think making it purposely obfuscated will help me any, he is really prone to giving zeros on assignments.
Last edited on
It loops through the inputString[], I don't think you are required to loop through the array frequency[]. It only said increment.

There might be some way to loop a bitwise operations or ascii addition / subtraction but unless you get bonus points or something I'd just be done with it.
Last edited on
lol, yeah, it's not about bonus points, it's about getting any credit at all for the assignment. He is an asshat... I hate to say that about a teach, but, he should not be teaching this class. It's bad, cause I took him for my network security class and loved him, that's why I signed up for programming with him. Turns out, he can't teach programming worth a damn. If an assignment does not meet his exacting specifications and expectations zero credit is given. I have a very good feeling I will have to retake this class, and I am not happy about that.
Thank you though for all of your help, I do appreciate it.
Last edited on
Topic archived. No new replies allowed.