0

could you help me to look at my codes, all my output frequency turn out 440. I am stuck in this for a week.

int accidental = 0;
int octave;

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

    if(note[1] == '#')
    {
        accidental += 1;
    }
    else if(note[1] == 'b')
    {
        accidental -= 1;
    }

}
else
{
     octave = atoi(&note[1]);
}

char c = note[0];
switch (c)
{
    case 'A':  accidental += 0;
    break;

    case 'B': accidental += 2;
    break;

    case 'C': accidental -= 9;
    break;

    case 'D': accidental -= 7;
    break;

    case 'E': accidental -= 5;
    break;

    case 'F': accidental -= 4;
    break;

    case 'G': accidental -= 2;
    break;

}

accidental += (octave-4)*12;
float power = accidental/12;
float frequency = round(pow(2, power)*440);
return frequency;

}

1 Answer 1

1

accidental is an int so accidental/12 will return an int (not a float). Change that to 12.0 to force floating point division.

You must log in to answer this question.

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