# Music frequency accidental and note issue

My music program currently returns values for all notes and accidentals that equals the frequency of note A in that octave, but I cannot make the frequency output change for different notes or accidentals, i.e.:

A4=440, C4=440, D#5=880, A5=880 etc

Any input re how to amend my source code would be greatly appreciated!

``````// Calculates frequency (in Hz) of a note
int frequency(string note)
{
double octave;
double f_octave;
double f_sharpflat;
double frequency;

//getting octave as an int
{
if (note  == '#' || note  == 'b')
{
octave = atoi (&note);
}
else
{
octave = atoi (&note);
}

}

//putting frequency into right octave
{
if (octave < 4)
{
f_octave = round (A4 / (pow(2, (4 - octave))));
}

else if (octave > 4)
{
f_octave = round (A4 * (pow(2, (octave - 4))));
}

else
{
f_octave = A4;
}
}

{
if (note  == '#')
{
f_sharpflat = f_octave * (pow(2, 1/12));
}

else if (note  == 'b')
{
f_sharpflat = f_octave / (pow(2, 1/12));
}

else
{
f_sharpflat = f_octave;
}
}

{
if (note  == 'C')
{
frequency = f_sharpflat / (pow(2, 9/12));
}

else if (note  == 'D')
{
frequency = f_sharpflat / (pow(2, 7/12));
}

else if (note  == 'E')
{
frequency = f_sharpflat / (pow(2, 5/12));
}

else if (note  == 'F')
{
frequency = f_sharpflat / (pow(2, 4/12));
}

else if (note  == 'G')
{
frequency = f_sharpflat / (pow(2, 2/12));
}

else if (note  == 'A')
{
frequency = f_sharpflat;
}

else if (note  == 'B')
{
frequency = f_sharpflat * (pow(2, 1/12));
}

else
{
return 0;
}

}

return round(frequency);

}
``````

When you write `9/12`, that's an integer division. Integer division results in an integer again, truncating the result. You want a floating point division instead. For that, make at least one of the operands floating point, for example `9/12.0`.
BTW, you could make your code a bit simpler, by just calculating the number of semitones relative to A4, and doing the `pow` once at the very end of the function. And `pow` can perfectly handle zero or negative numbers, so no need to differentiate between less than 4 and greater than 4.