1

I am having trouble with my code, I have spent a really long time trying to figure out why my code is only returning frequency with zeroes throughout. I would really appreciate if some could look at my code and see where my problem is. So far check50 has shown a smiley face for helpers.c compiles and bday text is correct, everything else is a frown.

Here is my code:

// Calculates frequency (in Hz) of a note int frequency(string note) { // TODO char note_letter = note[0]; int octave; int i= 0; char accidental = note[1];

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

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

//check whether note letter and octave are contained within this range
float a=0., b=2., c=-9., d=-7., e=-5., f=-4., g=-2.;
if (note_letter< 'A' || note_letter> 'G')
{
    return true;
}
if (octave<0 || octave>8)
{
    return true;
}

switch (note_letter)
{
    case 'A':
    i=a;
    break;

    case 'B':
    i=b;
    break;

    case 'C':
    i=c;
    break;

    case 'D':
    i=d;
    break;

    case 'E':
    i=e;
    break;

    case 'F':
    i=f;
    break;

    case 'G':
    i=g;
    break;

    default:
    return true;

}

//position of octave
i += ((octave-4)*12);
octave -= 48;
float oct_power = i/12.;
int middle_note = 440;

//frequency
float freq = 0.0;
freq = round(middle_note * pow(2., oct_power));

return (freq);

}

2

It looks like there's some work to be done here. The frequency() function is supposed to "return as an int the note’s corresponding frequency, rounded to the nearest integer" according to the spec. The code that you referenced has a bunch of return true statements and at least one return false statement. True and false are bools. False will always return 0 and true will return a non-zero, usually 1, but not necessarily. In any case, these are not note frequencies! The return values must be the note frequencies!

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

1
  • I think I understand what you mean. But as I look at my code again, shouldn't my "return (value_freq)" be giving me a value of the notes' frequency rounded to the nearest integer. I don't have a return true statement there, I only have that for identifying the note letters and rests. Or am I missing something here? May 26 '18 at 18:20
0

Try declaring the variables used in your frequency calculation as float instead of int. This should let you keep the precision until you round your final answer.

4
  • Thanks, I tried that, but it still returns all the frequencies as zeroes :/ May 28 '18 at 15:34
  • Edit: since I replied last time I decided to redo my frequency calculation and changed the way I was checking for accidentals. I managed to get frequency values, but I think my calculation is still off, because I am not getting the correct values for A#5, Ab3 and Bb5. My new code is updated in the original post. May 28 '18 at 18:59
  • Also here is what check50 gives me: cs50.me/checks/5806f701b89ae7589d9b42a00e0aaea2b654e6af It seems that I also have problems with the program timing out. May 28 '18 at 19:36
  • Few things first: - Have you implemented is_rest properly, because if you haven't that could be where your timeout issue is coming from. - You don't need to do the note letter and octave checks. Your calculations are outputting only the A5 and A3 frequencies for A#5 and Ab3 respectively. This means your calculations for accidentals are not being taken into account. Use debug50 (after fixing is_rest and duration to walk through what your code is doing or do it yourself on paper to isolate the problem. I wouldn't be helping you if I just told you what to fix. You'll figure it out.
    – Kel
    May 29 '18 at 16:06

You must log in to answer this question.

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