0

I really don't know what I am doing wrong. I think I've mad a mistake in frequency. Check50 says:

:( note C3 has frequency 131
    expected "131", not "220"

:( note Bb5 has frequency 932
    expected "932", not "831"

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

I would really appreciate of someone would take a look at my code:

int frequency(string note)
{
    char letter = atoi(&note[0]);
    int octave = atoi(&note[1]);
    int n = 0;

    if (strlen(note) == 3)
    {
        octave = atoi(&note[2]);
        char accident = note[1];

        if (accident == '#')
        {
            n += 1;
        }
        else if (accident == 'b')
        {
            n -= 1;
        }

    }
    if (letter == 'C')
    {
        n-= 9;
    }
    else if (letter == 'D')
    {
        n-= 7;
    }
    else if (letter == 'E')
    {
        n-= 5;
    }
    else if (letter == 'F')
    {
        n-= 4;
    }
    else if (letter == 'G')
    {
        n-= 2;
    }
    else if (letter == 'A')
    {
        n-= 0;
    }
    else if (letter == 'B')
    {
       n+= 2;
    }

    n+= (octave - 4) * 12;

    float x = 2.0;
    float y = (n/12.0);
    float freq = round(pow(x,y) * 440);

    if (octave < 4)
    {
        for (int i = 0; i < 4 - octave; i++)
            {
                freq /= 2.0;
            }
    }

    if (octave > 4)
    {
        for (int i = 0; i < octave - 4; i++)
        {
            freq *= 2.0;
        }
    }

    int totalfreq = round(freq);
    return totalfreq;

}

Many many many thanks!

2
  • 1
    This is the perfect time to put debug50 to good use. It is introduced in Week2->Shorts->Debugging->@12:25. You may also find this chart helpful. It says that the program returned the frequency for A3 instead of C3. Happy debugging! – DinoCoderSaurus Dec 7 '18 at 18:22
  • What he said! ;-) – Cliff B Dec 8 '18 at 12:26
0

I think your first box code is not from your second box code. and your code should not include the code below

if (octave < 4)
{
    for (int i = 0; i < 4 - octave; i++)
    {
        freq /= 2.0;
    }
}

if (octave > 4)
{
    for (int i = 0; i < octave - 4; i++)
    {
        freq *= 2.0;
    }
}

And the problem is you can't convert char to integer, the result will be \ 0 its should be like this

char letter = note[0]

You must log in to answer this question.