# Need help with Frequency in pset3. Been stuck for a while

So to start off, I'm going to include my code at the end of the question. Basically, I've built this function a few times now and I'm kind of at my wits end here.

My code uses a for loop to iterate through an array with all the notes in it. If it finds a match to the inputted note, it executes the proper equations to spit out a proper frequency. I've tested this several times and it works.....outside of helpers.c. Once I add it there i run into the control may reach end of non-void function error. The only problem is that when i add a return statement to fix this issue it seems to stop my for loop from looping all together meaning i get nothing unless I enter A4 into it. If someone could give this a look and give me some advice I'd be very appreciative I'm so close to finishing this I can taste it.

p.s. the big issue I'm running into is with trying to test with ./synthesize test.wav like I said I can get 440 as my output when I put in A4 but it seems like with every other note the program just disregards the for loop and moves on giving me nothing. I think it's because of my return statement but i need that to even compile the thing.

int frequency(string note) {

const string NOTES[] = {"C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B" };

``````string f;
string n = note;
int c;
char d;
int e = 0;
int freq = 0;
int counter = 0;

for (int i = 0; i >= counter && i < 12; i++)
{
f = NOTES[i];
d = f[0];
int a = d;
int b = n[0];
c = n[1] - '0';
int str = strlen(n);
counter++;

if ( b == 65 && c == 4 )
{
counter++;
freq = 440;
return freq;

}

else if ( str == 2 && a == b)
{

e = (i - 9);
freq = pow(2, ((double)e)/12.0)*440;

switch(c)
{

case 0:
freq = freq/16;
counter++;
return freq;

case 1:
counter++;
freq = freq/8;
return freq;

case 2:
counter++;
freq = freq/4;
return freq;

case 3:
counter++;
freq = freq/2;
return freq;

case 4:
counter++;
return freq;

case 5:
counter++;
freq = freq*2;
return freq;

case 6:
counter++;
freq = freq*4;
return freq;

case 7:
counter++;
freq = freq*8;
return freq;

case 8:
counter++;
freq = freq * 16;
return freq;

}
}

else if ( a == b && str == 3)
{
int oct = n[2] - '0';

switch(c)
{
case -13:
e = ( i - 8);
freq = pow(2, ((double)e)/12.0)*440;
break;

case 50:
e = ( i - 10);
freq = pow(2, ((double)e)/12.0)*440;
break;

}

switch(oct)
{
case 0:
freq = freq/16;
counter++;
return freq;

case 1:
counter++;
freq = freq/8;
return freq;

case 2:
counter++;
freq = freq/4;
return freq;

case 3:
counter++;
freq = freq/2;
return freq;

case 4:
counter++;
return freq;

case 5:
counter++;
freq = freq*2;
return freq;

case 6:
counter++;
freq = freq*4;
return freq;

case 7:
counter++;
freq = freq*8;
return freq;

case 8:
counter++;
freq = freq * 16;
return freq;

}

}

}
return 0;
``````

}

p.p.s please don't be rude about my sloppy code thanks.

IMPORTANT NOTE

This exercise is not included in CS50x 2019. The courseware changed at the beginning of 2019; `music` has been retired. Which is not to say you shouldn't persevere. But as far as I know, it will not be counted in your "progress". Don't forget to review the course homepage to see what you should expect for 2019 version.

If you decide to continue (good on ya!), here is some advice for debugging and troubleshooting.

Start with the hints from the spec

In the context of frequency specifically, taking baby steps might mean:

• Only implement support initially for A0 through A8, no other notes. Ensure that frequency returns the expected values for those notes, as by running notes or using debug50 or eprintf. Compare your function’s output against your own calculations on paper or on a calculator.

• Then add support for # and b but still only for A0 through A8 (i.e., A#0 through A#8 and Ab0 through Ab8).

• Then add support for B. Then for C. Then beyond.

`./notes 4` is a great troubleshooting tool. Your target is:

`````` C4: 262
C#4: 277
D4: 294
D#4: 311
E4: 330
F4: 349
F#4: 370
G4: 392
G#4: 415
A4: 440
A#4: 466
B4: 494
``````

You may find Piano key frequencies at Wikipedia helpful.

The best way to debug the program is, well, with a debugger. Here is a link to the debug50 short, desired content starts around 12:30. debug50 will not likely be a quick solution. It has a learning curve just like everything else. But knowing how to use it will payoff big time.

I know this does not answer the question. I would never dream of calling your code "sloppy". I will admit that we "think different", so it is difficult for me to follow. I would have to run it through the debugger, and frankly, I've already done that (ad nauseum) with my version :)

Good luck!