i am trying to compare the elements of two arrays before the code goes on to the next sequence. The elements of note[x] are filled with the note values of a song. and i want to be able to input values from a midi controller into learn[x]. how can i compare array learn[ ] to array note[ ] before the showsequence(); is incremented?

 const int x = 100;
    int note[x];
    int learn[x];
    int largestindex = 0;
    int learnindex=0;
    void showsequence();
    void readsequnce();
    const int PLAY = 1;
    const int TRYAGIN = 2;
    int state=0;

 void loop() {
     if (state == PLAY) {
      else if (state == TRYAGIN) {
    void MyHandleNoteOn(byte channel, byte pitch, byte velocity) {

 void showsequence() {
      //Add a new index to the end of the songseq
      //loop through the songseq
      for (int index = 0; index < largestindex; index++) {

      void readsequnce() {
      static uint16_t nextkey;
      bool mademistake = false;

       if (array_cmp(learn, note, largestindex, largestindex) == true){
               // do this if they are equal
               lcd.setCursor(16, 2);
               // do this if they are different
               lcd.setCursor(16, 2);

 boolean array_cmp(int *a, int *b, int len_a, int len_b){
         int n;

         // if their lengths are different, return false
         if (len_a != len_b) return false;

         // test each element to be the same. if not, return false
         for (n=0;n<len_a;n++) if (a[n]!=b[n]) return false;

         //ok, if we have not returned yet, they are equal :)
         return true;

It depends what sort of comparison you want to do. If you want to see if note[x] == learn[x] then that's what you do.

If you want to see if all the notes match all of learn then you'll need to put the comparison in a loop.

for (int n = 0; n < MAX_NOTE && note[n] == learn[n]; ++n)
    // Empty loop

If you want to see if the first n notes match the current note and the proceeding n notes then its a bit more complex, but its just a loop.

int n = 0;
for (int l = currentPos;
(l > (currentPos - 10)) && (note[n] == learn[l]);
 l--, n++)
    // Empty loop

If it is something else you want, please can you try and describe it in idiot proof terms so I can understand it.

| improve this answer | |
  • i want to compare note[x] == learn[x] but then once the compare is matches to compare note[x] == learn[x] (x going from 0->1) increasing by 1 every time the match is correct – Hjhk90 Apr 25 '17 at 13:41
  • Then use the bottom example, but int n = x, int l = x and l++. You will have to be careful about n and l exceeding the maximum index of the array/data, so add two extra conditions into the third line && n < maxData && l < maxData (Need to set maxData to something sensible. – Code Gorilla Apr 26 '17 at 7:07

Start by assuming they match. Loop over each element pair, compare them, and set a flag when you find a pair that don't match.

bool they_match = true; // assume they match 
for ( int i = 0; i < x; i++ ) { // for each note/learn element
  if ( note[i] != learn[i] ) { // check if they don't match
    they_match = false; // they don't mach, so set flag
    break; // stop looping 

At the end they_match will be true if note[] and learn[] match, or false if they don't.

I would suggest you learn how control structures work. https://www.cs.fsu.edu/~myers/c++/notes/control1.html https://www.cs.fsu.edu/~myers/c++/notes/control2.html

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.