c++ - 2d array in Arduino with different row size -
i'm going use arduino play melody, melody split 11 parts stored in array called notes
here's code:
string notes[0][]={"do", "re", "mi", "fa"}; string notes[1][]={"so", "rest", "mi", "do", "rest", "so", "rest", "fa", "re", "rest", "fa", "rest", "re", "ti", "rest", "fa", "rest", "mi", "do", "rest"}; string notes[2][]={"rest", "si,", "do", "fa", "mi", "so", "do", "rest", "mi" "fa", "mi", "fa", "mi", "fa", "mi", "do", "re"}; string notes[3][]={"la,", "la", "so", "fa", "mi", "fa", "so", "do", "do", "rest", "mi", "fa", "mi", "do", "fa", "mi", "do", "la", "so", "rest"}; string notes[4][]={"rest", "so,", "so,", "fa", "mi", "re", "mi", "do"}; string notes[5][]={"la", "ti", "so", "fa", "so", "fa", "fa", "mi", "re", "mi", "re", "do", "ti,", "do", "ti,", "la,", "so,"}; string notes[6][]={"rest", "do", "ti,", "do", "so,", "rest"}; string notes[7][]={"do", "ti,", "do", "mi", "rest"}; string notes[8][]={"fa", "mi", "re", "do", "fa", "mi", "re", "do", "do", "re", "re", "do`", "re`"}; string notes[9][]={"rest", "do", "ti,", "do", "so", "fa", "fa", "fa", "fa", "fa", "mi", "re", "mi", "rest", "re", "mi", "fa", "mi", "mi", "mi", "re", "do", "mi", "rest"}; string notes[10][]={"la,", "ti,", "do", "re", "la,", "la,", "ti,", "do", "re", "do", "so"};
when tried compile it, returns error message saying
declaration of 'notes' multidimensional array must have bounds dimensions except first
so should if want keep contents in array unchanged?
"la la la" sounds me learnt 40 (fourty!) years ago in school. asked google , found solmization.
thus, first conclusion: everything, learnt in school pay off 1 day (sooner or later).
as read concerns (wasted) storage (between lines), thought how make little bit more compact. (it become more compact using clever compression techniques beyond want present here.)
as there limited set of syllables not many distinct values, intended store them bytes. bytes can represented characters, sequences of bytes can defined constant c strings. constant c strings provide end-marker (0 terminator) free. it's possible declare/initialize arrays of constant c strings if have differing lengths (as array stores pointers strings). last not least, exploited fact multiple double-quoted sequences concatenated 1 string constant (if separated whitespace only).
furthermore, did "macro-magic" simplify code writing. not elegant quite convenient in "daily usage". (the "design goal" make source code robust against typos in definition of notes
keep "readable".)
my sample solmization.cc
:
#include <cassert> #include <cstring> #include <iostream> #define c_rest '0' #define c_do '1' #define c_re '2' #define c_mi '3' #define c_fa '4' #define c_sol '5' #define c_la '6' #define c_ti '7' #define c_di 'a' #define c_ri 'b' #define c_fi 'c' #define c_si 'd' #define c_li 'e' #define rest "0" #define "1" #define re "2" #define mi "3" #define fa "4" #define sol "5" #define la "6" #define ti "7" #define di "a" #define ri "b" #define fi "c" #define si "d" #define li "e" const char *notes[] = { re mi fa, sol rest mi rest sol rest fa re rest fa rest re ti rest fa rest mi rest, rest si fa mi sol rest mi fa mi fa mi fa mi re, la la sol fa mi fa sol do rest mi fa mi fa mi la sol rest, rest sol sol fa mi re mi do, la ti sol fa sol fa fa mi re mi re ti ti la sol, rest ti sol rest, ti mi rest, fa mi re fa mi re do re re re, rest ti sol fa fa fa fa fa mi re mi rest re mi fa mi mi mi re mi rest, la ti re la la ti re sol }; using namespace std; int main() { enum { n = sizeof notes / sizeof *notes }; (size_t = 0; < n; ++i) { cout << "line " << (i + 1) << ":" << endl; const char *line = notes[i]; (size_t j = 0, m = strlen(line); j < m; ++j) { switch (line[j]) { #define case(syl) case c_##syl: cout << ' ' << #syl; break case(rest); case(do); case(re); case(mi); case(fa); case(sol); case(la); case(ti); case(di); case(ri); case(fi); case(si); case(li); #undef case default: assert(false); // should unreachable } } cout << endl; } return 0; }
test , output:
$ g++ -std=c++11 -o solmization solmization.cc $ ./solmization line 1: re mi fa line 2: sol rest mi rest sol rest fa re rest fa rest re ti rest fa rest mi rest line 3: rest si fa mi sol rest mi fa mi fa mi fa mi re line 4: la la sol fa mi fa sol do rest mi fa mi fa mi la sol rest line 5: rest sol sol fa mi re mi line 6: la ti sol fa sol fa fa mi re mi re ti ti la sol line 7: rest ti sol rest line 8: ti mi rest line 9: fa mi re fa mi re do re re re line 10: rest ti sol fa fa fa fa fa mi re mi rest re mi fa mi mi mi re mi rest line 11: la ti re la la ti re sol $
notes:
i resisted first desire audible output , made print output instead.
while transforming original data realized
,
,`
in strings. didn't consider them keep sample code short. they've meaning don't know. guess, following idea of sample should possible extend definitions appropriately.i don't double definition of syllable macros (for character constants string constants) didn't better idea. although, couldn't remember stringize character constants exists googled , found in the c preprocessor – 3.4 stringizing (part of gnu gcc online docs.):
there no way convert macro argument character constant.
Comments
Post a Comment