0

When I ran my code through the compiler for notes.c, I got the following error: helpers.c:88:26: runtime error: value inf is outside the range of representable values of type 'int'. It then returned these values in the terminal: C4: -2147483648 C#4: -2147483648 D4: -2147483648 D#4: -2147483648 E4: -2147483648 F4: -2147483648 F#4: -2147483648 G4: -2147483648 G#4: -2147483648 A4: -2147483648 A#4: -2147483648 .This is my code from the int frequency(string note, const string NOTES[]) function in helpers.c:

int frequency(string note, const string NOTES[]){
    int digit;
    char alphabetsTwo[] = {0,0,0};
    char alphabetsOne[] = {0,0};
    int positionOfAlpha;

//Determine length of note and split into alphabet and digit
if(strlen(note) == 2){
    alphabetsOne[0] = note[0];
    digit = note[1];
}
else if(strlen(note) == 3){
    alphabetsTwo[0] = note[0];
    alphabetsTwo[1] = note[1];
    digit = note[2];
}
//Convert flat to sharp
if(note[1] == 'b'){
    //Ensure that "b" changed to "#"
    alphabetsTwo[1] = '#';
    //Changing letter (e.g. from 'E' to 'D')
    if((int)(note[0]) <= 71 && (int)(note[0]) > 65){
        alphabetsTwo[0] = (char)((int)(note[0] - 1));
    //Only for changing from 'G' to 'A'
    }else if((int)(note[0]) == 65){
        alphabetsTwo[0] = (char)(71);
    }else{
        fprintf(stderr, "Alphabet doesn't exist!\n");
        return 1;
    }
}

//Iterate through NOTES[] to find position of alphabet
for(int i=0, n = 12; i<n; i++){
    if(NOTES[i] == alphabetsTwo || NOTES[i] == alphabetsOne){
        positionOfAlpha = i;
    }

}
//Find difference between position of alphabet and A in NOTES[]
int diffAlpha = positionOfAlpha - 9;
//Store in temp variable
float power = diffAlpha/12;
float temp = 440*powf(2.0, power);
//Find difference in octaves
int diffNum = digit - 4;
float floatFrequency = round(temp*powf(2.0, diffNum));
int finalFrequency = (int)(floatFrequency);
return finalFrequency;
}

Where is it that I've went wrong? Thank you.

1 Answer 1

1

In digit = note[1];, you assign a digit character, but you meant the value associated with the digit. Similar to caesar, you can subtract '0' to convert digits '0' to '9' to numbers 0 to 9.

String comparison is done with strcmp or one of its siblings, not ==. == would test whether those are the same string (same memory location of characters), not strings of same content.

I'd say string comparison here is overkill, and you could succeed with processing single characters. Also, diffAlpha/12 is an integer divison, creating an integer again. Using /12.0 would make it a floating point division.

12
  • Thank you for pointing out my mistakes. My code can now compile and it no longer returns the error. However, I still get 'expected exit code 0, not 1 ' when I ran my code through check50. Do you happen to know why this may happen? Once again, thank you.
    – Prav Elan
    Commented Jul 14, 2018 at 0:38
  • Does ./synthesize boat.wav < songs/boat.txt throw any error? Does valgrind ./synthesize boat.wav < songs/boat.txt point you to any mistake?
    – Blauelf
    Commented Jul 16, 2018 at 8:10
  • No, both of them have not pointed me to any mistake.
    – Prav Elan
    Commented Jul 19, 2018 at 7:52
  • Which test case throws this error? Can you link the check50 report?
    – Blauelf
    Commented Jul 19, 2018 at 8:05
  • Sure. cs50.me/checks/7a7775d8622d9d35b4818d96925f94f6aeabfff6
    – Prav Elan
    Commented Jul 19, 2018 at 8:42

You must log in to answer this question.

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