# Calculate n (semitones) for frequency in pset3

I'm having a bit of a hard time trying to calculate the value of n(the number of semitones from A4).

I'm currently using the following formula:

```if (octave > A4_OCTAVE) { n = (octave - A4_OCTAVE) * NUMBER_OF_SEMITONES; } else if (octave < A4_OCTAVE) { n = (A4_OCTAVE - octave) * NUMBER_OF_SEMITONES; } ```

where A4_OCTAVE = 4, NUMBER_OF_SEMITONES = 12

and octave is gotten from the last position of the note passed into the frequency function and then changed to an int using atoi().

Thanks for the help!

Why do you need an `if`/`else`? In my code, a negative number stands for octaves below, a positive for octaves above octave 4. This gets added to the number of semitones relative to A of the same octave (and possibly further modified by b or #). I don't mind the total going negative, as `pow` works fine with those.
• I have some table that maps `note-'A'` (similar to `caesar` and `vigenere`) to a value, for example element for index `2` (`'C'-'A'`) would be `-9`, as C4 is 9 semitones below A4. You could also use `switch`/`case` or an `if`/`else` chain. Sep 28 '18 at 9:34