# Pset3 Helpers.c Frequency Not Working

Right now I am working on Pset3 with Music. I'm focusing on getting the frequency (in Hz) of all the octaves of A. I am including accidentals in my work as well. The problem is that A4 returns 449 instead of the expected 440. I've tried to use Google and I even checked my work but I couldn't find any logical issues.

``````int frequency(string note)
{
const char* n = note;
if (strncmp(&n[0],"A", 1) == 0 && strncmp(&n[1],"4", 1) == 0) {
return 440;
}
else if (strncmp(&n[0],"A", 1) == 0){
if (strncmp(&n[1],"#", 1) == 0){
if (atoi(&n[2]) > 4) {
int freq = round(440*2^(atoi(&n[2])-4));
return round(freq * 2^(1/12));
}
else {
int freq = round(440/2^(4-atoi(&n[2])));
return round(freq * 2^(1/12));
}
}
else {
if (n[1] > 4) {
return round(440*2^(atoi(&n[1])-4));
}
else {
return round(440/2^(4-atoi(&n[1])));
}
}
}
}
``````

Any and all help would be appreciated :)

Note that C's `^` operator is bitwise XOR, not power. Also, in C, `1/12` equals `0` (integer division truncates, you could avoid this by using floating point values like `1.`, yes this decimal point makes it a floating point number). You might want to include `math.h`.
And if you want to compare single characters, I find `strncmp` a bit overkill, `if (n[0] == 'A')` should work as well. Since octaves are single-digit, you could even use `n[2]-'0'` instead of `atoi(&n[2])`. And in `n[1] > 4` you're comparing a character to a number. As characters are 8-bit integers, the compiler thinks this is fine. And you seem to access the wrong element `n[2]` on the last few lines, where the number is in `n[1]`.
• You probably did `n[0] == "A"`, not `n[0] == 'A'`, the kind of quotation marks matter in C (unlike for example Python or JavaScript). I still don't get how you might receive 449 as an answer with above code. But have you understood all the other mistakes I mentioned? Jan 13 '18 at 20:23