Project: Frequencies of Musical Notes with Loops and Arrays


5/5 - (2 votes)

Programming Project: Frequencies of Musical Notes with Loops and Arrays
CPSC-298-6 Programming in C++

Loops and arrays provide tremendous power and help you to express programming
problems simply. In this assignment, you’ll revisit your program to compute the
frequencies of musical notes given a reference frequency. This time, you’ll compute the
frequencies and wavelengths of all the notes from the zero’th to the eighth octave; that is,
from note C0 to note B8. Even though you’re computing more values this time, you’ll
write far less code.
The Assignment
To compute the frequency of the note in octave ν (nu) and half step k, you’ll use the
following formula. The value ν will range from 0 to 8, inclusive, and the value of k from
0 to 11, inclusive.
fk,ν = fR × 2(ν)+ k/12
fR is the Reference Frequency, in this case 16.35 Hz (cycles per second), the Frequency of the note C in
octave 0, the “zero’th octave, (denoted by C0).
ν is the octave number (which ranges from 0 to 9 for our purposes)
k is the half-step (or semitone number) within the octave, it has values between 0 and 11 inclusive.
fk,ν is the frequency of the note in octave ν whose half-step within the octave is k. 1
Implement the formula in the form shown. Use the pow() function to compute 2 raised to
the ν + k /12. pow() can handle a fractional exponent. Be careful to use floating point
division and not integer division when computing ν + k /12.
For the wavelength, use the following formula:
Wk,v = c/ fk,ν
Wk,v is the wavelength, c is the speed of sound in air (at room temperature), and fk,ν is the
The speed of sound in air at room temperature is (roughly) 345 meters per second (345
m/s) or 34500 centimeters per second. (Remember that there are 100 centimeters in a
1 More technically, it is frequency of equal-tempered interval k in octave ν. The octaves are named in
the order of their appearance on a standard 88-key piano keyboard, beginning with octave 0 (or the
“zero’th octave”).
You’ll also use the following array of strings containing the names of all the notes whose
frequencies and wavelengths you’ll be displaying.
std::string a_strNotes[] = {
“C0”, “C#0”, “D0″,”D#0”, “E0”, “F0”, “F#0”, “G0”, “G#0”, “A0”, “A#0”, “B0”,
“C1”, “C#1”, “D1″,”D#1”, “E1”, “F1”, “F#1”, “G1”, “G#1”, “A1”, “A#1”, “B1”,
“C2”, “C#2”, “D2″,”D#2”, “E2”, “F2”, “F#2”, “G2”, “G#2”, “A2”, “A#2”, “B2”,
“C3”, “C#3”, “D3″,”D#3”, “E3”, “F3”, “F#3”, “G3”, “G#3”, “A3”, “A#3”, “B3”,
“C4”, “C#4”, “D4″,”D#4”, “E4”, “F4”, “F#4”, “G4”, “G#4”, “A4”, “A#4”, “B4”,
“C5”, “C#5”, “D5″,”D#5”, “E5”, “F5”, “F#5”, “G5”, “G#5”, “A5”, “A#5”, “B5”,
“C6”, “C#6”, “D6″,”D#6”, “E6”, “F6”, “F#6”, “G6”, “G#6”, “A6”, “A#6”, “B6”,
“C7”, “C#7”, “D7″,”D#7”, “E7”, “F7”, “F#7”, “G7”, “G#7”, “A7”, “A#7”, “B7”,
“C8”, “C#8”, “D8″,”D#8”, “E8”, “F8”, “F#8”, “G8”, “G#8”, “A8”, “A#8”, “B8”,
You’ll implement an outer for loop over ν, which ranges from 0 to 8, inclusive. Inside this
loop, you’ll implement an inner for loop that iterates over k from 0 to 11, inclusive.
const int k_nOctaves = 9; // Number of octaves to consider
const int k_nHalfTones = 12; // Number of Half-tones in an octave
std::string strNote;
for (int nu = 0; /* ?? */, /* ?? */)
for (int k = 0; /* ?? */, /* ?? */)
// Calculate frequency (f)
// ??
// Calculate wavelength (dWavelengthCentimeters)
// ??
// Select name for note (e.g. “C#0”) from array (assign to strNote)
// ?? (Need to compute offset into array based on nu and k)
strNote = a_strNotes[? expression in terms of nu and k ?];
std::cout << “Note: ” << strNote << “; nu: ” << nu << “; k: ” << k
<< “; frequency: ” << f << ” Hz; wavelength: ”
<< dWavelengthCentimeters << ” cm” << std::endl;
The question marks, /* ?? */ , indicate code that you need to complete.
Within the inner for loop you’ll print the name of the note, the values of nu (ν) and k, and
the computed frequency and wavelength of the note. You can use setw() and other
methods to improve the appearance of the output if you’d like.
When printing out the name of the note, such as “C#0”, you’ll need to access the array
element within the a_strNotes array correspond to nu (ν) and k. In the code fragment
above, strNote is a string variable that is assigned to the appropriate element within array
a_strNotes. But which one? The solution involves k_nHalfTones and may look similar to
the following, where o and O represent two different numeric operators such +, -, * and /.
The question mark represents something else – a mystery you’ll need to solve.
strNote = a_strNotes[? o nu O k];
The output of the program will appear similar to the following

The exact values for the frequencies and wavelengths are given in Appendix A.
What might have taken a page of code to write on the original assignment, can now be
done in just a few lines, and it covers all of the notes now too.
Appendix: Table of Musical Notes and their Frequencies

Scroll to Top