Vorzeitige Beendung von Schleifen - Rekursion?!

Aus meiner Wühlkiste, nur dein Array eingebaut:

#include <Streaming.h> // die Lib findest du selber ;-)
Print &cout = Serial; // cout Emulation für "Arme"


int numbers[]     {1, 2, 3, 4, 5};
char buchstaben[] {'c','n','5','t','h'};


struct Result
{
  bool found;
  unsigned cell; 
};

template<typename T, size_t N> Result findInArray(T (&haystack)[N], T needle) 
{
  for (unsigned i=0; i<N;i++) if (haystack[i] == needle) return{true,i};
  return{false,0};
}


void setup() 
{
  Serial.begin(9600);
  cout << F("Start: ") << F(__FILE__) << endl;

  Result res;
  
  res = findInArray(numbers,33);
  if(res.found) cout << "Zellenindex: " << res.cell << endl;
    else cout << "nix mit 33 " << endl;
  
  res = findInArray(numbers,2);
  if(res.found) cout << "Zellenindex: " << res.cell << endl;
    else cout << "nix mit 2 " << endl;
  
  res = findInArray(numbers,3);
  if(res.found) cout << "Zellenindex: " << res.cell << endl;
    else cout << "nix mit 3 " << endl;
    
  res = findInArray(buchstaben,'c');
  if(res.found) cout << "Zellenindex: " << res.cell << endl;
    else cout << "nix mit c " << endl;
  
}

void loop() 
{

}
1 Like