0

could you please help. I just don´t understand why only A notes are calculated. Please see code below. Many thanks for your help in advance.

            //Program to calculate note frequency
            #include <cs50.h> //for, get_string, string,..
            #include <stdio.h> //for get string
            #include <math.h> // for multiplication, pow, round
            #include <string.h>//for strlen


            int main(void)

            {
            int octave = 0, a = 0;

                //parse string into note and octave
                string n = get_string("Give me a string: ");
                char note = n[0];


            if (strlen(n) == 3)
            {
                octave = n[2]-'0';
                if (n[1] == '#')
                {

                a += 1;
                eprintf("%c\n", note);
                }
                else if (n[1] == 'b')
                {
                a -= 1;
                eprintf("%c\n", note);
                }

            }
            else if (strlen(n) == 2)
            {
                octave = n[1]-'0';

                //This for loop checks for the letter in note and assigns the a value
                if (note == 'A')
                {
                    a = 0;
                    eprintf("%d\n", a);
                }
                else if (note == 'B')
                {
                    a += 2;
                    eprintf("%d\n", a);
                }
                else if (note == 'C')
                {
                    a -= 9;
                    eprintf("%d\n", a);
                }
                else if (note == 'D')
                {
                    a = -7;
                    eprintf("%d\n", a);
                }
                else if (note == 'E')
                {
                    a = -5;
                    eprintf("%d\n", a);
                }
                else if (note == 'F')
                {
                    a = -4;
                    eprintf("%d\n", a);
                }
                else if (note == 'G')
                {
                    a = -2;
                    eprintf("%d\n", a);
                }

            }
            else
            {
            printf("Please enter a note in the format A3 or A#3");
            }

            //This for loop assigns the correct octave and calculates the actual frequency.
            if (octave < 4)
            {

                int freq_octave =  440.0 / (pow (2.0, (4 - octave)));
                float frequency = round(pow (2.0, (a / 12.0))) * freq_octave;

                printf("end_frequency: %f\n", frequency);
            }

            else if (octave >= 4)
            {
                int freq_octave =  440 * (pow (2, (octave - 4)));
                printf("%d\n", freq_octave);
            }


            eprintf("Octave: %d\n", octave);

            //return frequency;

            }
0
float frequency = round(pow (2.0, (a / 12.0))) * freq_octave;

Are you rounding the correct value? Let's say a is 3. 2 to the power of a .25 is 1.189.. if you round that, it's 1. Shouldn't you be rounding the entire calculated value? Check your closing parenthesis.

1
  • Many thanks for your help. I added some eprintfs and indeed I had 3 issues: 1st pow function returned always 1. Also by seperating the round function from frequency and adding it to the end solved it. Also the -a values in above were converted to positive a´s in the power function. Following code works for me now: if (a < 0) { float freq = pow (2.0, (a/12.0)) * freq_octave; } float freq = freq
    – Johann
    Jul 17 '18 at 18:24
0

if "f" is the number of semitones of a note away from "A"

An octave = 12 semitones

you could subtract 4 from the octave, multipy it by 12 then add it to the note

//convert oct to int relative to A4
int octd = oct - '4';
//multiply octave by semitone disance and add to note
f += octd * 12;

then to calculate frequency of the note

    //calculate frequency of note
f = round(pow(2, f / 12) * 440);
return f;
0

Many thanks for your help. I followed the above mentioned advise and checked via eprintf the outcome of the equations. It turned out that I had 2 issues: 1. pow function returned always 1. 2. -a values in above mentioned code were converted to positive a´s in the power function.

Following code works for me now to calculate all frequencies:

//Program to calculate note frequency
#include <cs50.h> //for, get_string, string,..
#include <stdio.h> //for get string
#include <math.h> // for multiplication, pow, round
#include <string.h>//for strlen


int main(void)

{
int octave = 0, a = 0;

//parse string into note and octave
string n = get_string("Give me a string: ");
char note = n[0];


if (strlen(n) == 3)
{
octave = n[2]-'0';
if (n[1] == '#')
{

a += 1;
eprintf("%c\n", note);
}
else if (n[1] == 'b')
{
a -= 1;
eprintf("%c\n", note);
}

}
else if (strlen(n) == 2)
{
octave = n[1]-'0';

//This for loop checks for the letter in note and assigns the a value
if (note == 'A')
{
    a = 0;
    eprintf("a: %d\n", a);
}
else if (note == 'B')
{
    a += 2.0;
    eprintf("a: %d\n", a);
}
else if (note == 'C')
{
    a -= 9.0;
    eprintf("a: %d\n", a);
}
else if (note == 'D')
{
    a -= 7.0;
    eprintf("%d\n", a);
}
else if (note == 'E')
{
    a -= 5.0;
    eprintf("%d\n", a);
}
else if (note == 'F')
{
    a -= 4.0;
    eprintf("%d\n", a);
}
else if (note == 'G')
{
    a -= 2.0;
    eprintf("%d\n", a);
}

}
else
{
printf("Please enter a note in the format A3 or A#3");
}

//This for loop assigns the correct octave and calculates the actual      
if (octave < 4)
{
int freq_octave =  440.0 / (pow (2.0, (4 - octave)));

    if (a >= 0)
    {
    float freq = pow(2.0, (a / 12.0)) * freq_octave;
    printf("Frequency is: %f\t", freq);
    }
    else if (a < 0)
    {
    float freq = freq_octave / (pow (2.0, ( -a / 12.0)));
    printf("Frequency octave is : %d", freq_octave);
    printf("Frequency is: %f\t", freq);
    }

}

else if (octave >= 4)
{
int freq_octave =  440 * (pow (2, (octave - 4)));
printf("%d\n", freq_octave);

if (a >= 0)
    {
    float freq = pow(2.0, (a / 12.0)) * freq_octave;
    printf("Frequency is: %f\t", freq);
    }
    else if (a < 0)
    {
    float freq = freq_octave / (pow (2.0, ( -a / 12.0)));
    printf("Frequency octave is : %d", freq_octave);
    printf("Frequency is: %f\t", freq);
    }

}
//remember to round frequency to an int before submitting

eprintf("Octave: %d\n", octave);

//return round(frequency);

}

if (a <= 0)
{
float freq = pow (2.0, (a/12.0)) * freq_octave;
}
else if (a < 0)
{
float freq = freq_octave / (pow (2.0, (-a/12.0)));
}

You must log in to answer this question.

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