0

I was working through the Pset3 Algorithm, and have all ready written my code, and when check50 runs, I did pretty well. However, the check states that

:( produces all correct notes for octaves 3-5 Incorrect frequency for C3, should be 131, not 262

However, the octave doesn't even make sense, because if A4 is 440 Hz, then A3 (440 * 2^(-1)) would be 220. All of the other checks run as expected. Attached here below is my code.

int frequency(string note)

{ char name; char sh_fl = '\0'; string s_octave; double octave; long freq = 0.00000000000;

name = note[0];

//sharp or flat or none
if (strlen(note) == 3)
{
    sh_fl = note[1];
    s_octave = &note[2];
}
else
{
    s_octave = &note[1];
}

octave = atoi(s_octave);
double temp = 0;
double superpower = pow(2.00000, (octave - 4.00000));
freq = 440 * superpower;    //octave


//note
if (name == 'A')
{
    temp = 0;
}
else if (name == 'B')
{
    temp = 2;
}
else if (name == 'C')
{
    temp = 3;
}
else if (name == 'D')
{
    temp = 5;
}
else if (name == 'E')
{
    temp = 7;
}
else if (name == 'F')
{
    temp = 8;
}
else if (name == 'G')
{
    temp = 10;
}



//sharp or flat if needed
if (sh_fl == '#')
{
    temp++;
}
else if (sh_fl == 'b')
{
    temp--;
}

double thepower = pow(2, (temp / 12.000000000000000));
//printf("%f\n", thepower);
freq = round((freq * thepower));

return freq;

}

1

Your semitones should be relative to A of the same octave. But C of the same octave is -9 semitones, not +3. That one would be C of the next octave, explaining the double frequency the test found.

You must log in to answer this question.

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