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.
/*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>
usingnamespace std;
void getCharacterFrequency (char inputString [], unsignedint frequency[]);
int main ()
{
//main function is for TESTING only and does not get turned in.
//problem 2
unsignedint 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 [], unsignedint frequency[])
{
constint 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.
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.
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
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <string>
usingnamespace std;
void getCharacterFrequency (char inputString [], unsignedint frequency[]);
int main ()
{
//main function is for TESTING only and does not get turned in.
//problem 2
unsignedint 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 [], unsignedint frequency[])
{
constint 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;}
}
}
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.
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.
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.
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.