# Tone Melody Example

I've been working with the Tone Melody example built into the IDE to teach my students how to play tunes, but I'm confused as to why it hard codes the number of notes into the for loop.
Would the following not be better for the for loop, or am I missing something obvious?:

``````for (int thisNote = 0; thisNote < sizeof(melody)/sizeof(int); thisNote++) {
``````

This way, the code would automatically adapt to the size of the melody array (by calculating how many integers are in it), and all a beginner would need to be able to use it is to change the notes and durations in the two arrays.
I'm not sure if this is necessarily the best place to post this, but I'm curious to see why this might not be desirable, or if it is better, hopefully it will help someone who is starting out with making the Arduino play music!

I'd suggest another improvement - put the note and the duration in the same array so one can enter the note and its play length together. Be much easier to enter/edit a song that way and keep them in sync. Put a /2 in the sizeof somewhere, increment the loop by two, with array[thisNote] for the tone, and array[thisNote+1] for the duration.
Give it try, see how it works out.

Or even use a two dimensional array!

Same result. I find

``````byte noteArray[] = {
a,1,
b,1,
c,4,
e,2,
// etc
};
// or
byte noteArray[] = {
a,1, b,1,c,4,e,2,
};
``````

easy enough to follow and not cluttered with a bunch of extra { }