2

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) {
    printf("Your 'if' statement works");
    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 :)

1 Answer 1

6

For "A4", this returns 440 for me.

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 could reduce your code a bit by separating the note (mapped to half-tones over A) and the octave, and calculate the frequency from those two separate numbers in one line.

4
  • At first, I did use <if (n[0] == 'A')> but when I do that, my terminal returns this error: "helpers.c:33:14: error: result of comparison against a string literal is unspecified (use strncmp instead) [-Werror,-Wstring-compare]". The only way it doesn't return that error is if I use <strncmp>. I realize that my code is messy right now but I was trying to get the logistics down before I cleaned it up. I tried it again but I still get 449 returned for "A4". Do you think that there is something wrong with my terminal? Thank you
    – Tenacity
    Commented Jan 12, 2018 at 15:49
  • 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?
    – Blauelf
    Commented Jan 13, 2018 at 20:23
  • I didn't open my terminal for a few days and when I did I restarted it twice. After doing that, I finally got A4 to equal 440 like it should. I was also able to use <n[0] == 'A'> without receiving an error message. And I fixed all of my other mistakes, thank you!
    – Tenacity
    Commented Jan 23, 2018 at 16:08
  • Happy to hear that :)
    – Blauelf
    Commented Jan 23, 2018 at 16:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .