I am doing the pset3 (music). However, my code appears to be very buggy. Although it compiles just fine, the frequency outputs are all 440Hz. Also, it seems like it can't read the duration either. I am not sure what's the problem with the code as I am completely new to programming. Thanks!
- Your problem is not with the duration, but with
is_rest. The string does not come with a
'\n', you just have to test for the empty string (which starts with
'\0', the null terminator, as it has length zero)
0(integer division truncates the result to result in an integer again).
1/12.0would do the division in floating point.
- You truncate
440*powerby assigning it to an integer, but you should round it before any truncation.
- Personal opinion: The result of
log2is a floating-point number. By assigning it to an integer, you are truncating the number (discarding the fractional part). Yeah, the denominator is always a power of two, but still. What keeps you from just writing
numerator * 8 / denominator?
/have same precedence, and are evaluated left to right.
- Also personal opinion: The translation letter->frequency looks a bit complex, though it again seems as if it could work, I guess letters C..G might be off by an octave. I used a lookup array that would tell me how many semitones a given letter was away from that octave's
note-65as the index), this made my code much shorter and especially more uniform (no different cases to consider, as I had considered them in creating the lookup array).