Checking if a value is part of an array?
in
Programming Questions
•
2 months ago
Ok, noob here:
So I've initialized an array with the start of the Fibonacci series. Now I'd like to display on a grid representing the numbers 0 to n. I tried to create a function checkFibo() that iterates though the array and check my counter variable against it and return a True or False but it does not work ...
- int[] fibo = new int[30];
- int fibo1 = 0, fibo2 = 1;
- int counter = 0;
- void setup() {
- size(500, 500);
- for (int i = 0; i < fibo.length; i++) {
- fibo[i] = fibo2 + fibo1;
- fibo1 = fibo2;
- fibo2 = fibo[i];
- println(fibo[i]);
- }
- for (int y = 0; y < 500; y += 5) {
- for (int x = 0; x < 500; x += 5) {
- counter +=1;
- if (isFibo(counter)) {
- fill(255, 0, 0);
- }
- else {
- fill(255);
- }
- rect(x, y, 5, 5);
- }
- }
- }
- boolean isFibo(int c) {
- for (int i = 0; i < fibo.length; i++) {
- if (fibo[i] == c) {
- return true;
- }
- else {
- return false;
- }
- }
- }
Thanks a lot!
1