0

So i am changed the code a little bit. It still doesn't work but i think i am little bit closer. The program should check if the number you put in (inside bar[100]) match the fibonacci numbers(stored in fib[30]) and then print the ones that match.

#include <iostream>
#include <math.h>

using namespace std;

int main() {
    long x, bar[100], fib[30];

    cout << "Cate numere sunt in array? = ";
    cin >> x;
        for (int i = 0; i < x; i++) {
            cout << "bar[" << i << "]=";
            cin >> bar[i];
            }

    fib[0] = 1;
    fib[1] = 1;
    fib[2] = 2;
        for (int i = 3; i <= 30; i++) {

            fib[i] = fib[i - 2] + fib[i - 1];
        //  cout << fib[i] << " ";
        }
        for (int i = 0; i < x; i++) {
            bool fibonacci = false;
            int j = 0;
                while (j < 30  && !fib) {
                // the mistake is here ( ' || ' instead of ' && ')
                    if (fib[j] == bar[i]) {
                        fibonacci = true;
                        }
                    j++;
                }
                if (fibonacci) {
                    cout << bar[i] << " ";
            }
    }


return 0;
}```
  • `1 / sqrt(5)` -- This yields an integer, not a floating point value. Use `1.0 / sqrt(5);` or since these are just constants, just define a constant instead of computing the square root of 5 repeatedly. – PaulMcKenzie Jan 10 '20 at 15:23
  • 1
    @PaulMcKenzie, why would `1 / sqrt(5)` yield an `int`? `sqrt`, afaik, always returns a floating point value, and `1 / [float]` would be a float too... See [here](https://godbolt.org/z/_wWdbE) – ChrisMM Jan 10 '20 at 15:30
  • oops, my mistake. Should be a double. – PaulMcKenzie Jan 10 '20 at 15:35
  • This is not the way to use Binet's formula in a loop. The `bar[i]` need not be used at all in the formula. Instead, just an increasing value of `i` is used, and then the result is assigned to `bar[i]`. – PaulMcKenzie Jan 10 '20 at 15:37
  • 1
    Also, the goal of your program seems confusing to me. The user is supposed to enter what exactly? Their guess as to what the `nth` Fibonacci number is, and your loop says if they are correct or not? It's hard to read your code, since it is not using English as the strings. Also `if (bar[i] = ` -- shouldn't that be `if (bar[i] == `? – PaulMcKenzie Jan 10 '20 at 15:50
  • @PaulMcKenzie you need to put any numbers you want , then the program needs to check if they are a part of fibonacci – Victor Bînzar Jan 10 '20 at 16:21
  • If that's the case, then you should place those computed Fibonacci numbers in a set, and then take the user's input and search the set to see if the value is there. The only part of your code I see that's understandable is the attempt of using Binet's formula. Otherwise, your explanation doesn't fit at all with what you've coded. – PaulMcKenzie Jan 10 '20 at 16:23
  • @PaulMcKenzie yeah i think that should be a possible answer also, but that what i need to do. I was thinking that I could verify if a number is part of the fibonacci sequence IF it's a match for the Binet's formula. – Victor Bînzar Jan 10 '20 at 16:25
  • @VictorBînzar -- Yes, but to do what you've suggested, you need to precompute the Binet values and put them in a map or set, regardless of what the user inputted. Then you check the user's guess against the map / set. – PaulMcKenzie Jan 10 '20 at 16:28
  • @PaulMcKenzie ok, I don't know how to do it, but I was thinking maybe another array with the elements of the Fibonacci sequence? – Victor Bînzar Jan 10 '20 at 16:31
  • 1
    Yes another array, and just do a linear search (that is the simplest in concept). – PaulMcKenzie Jan 10 '20 at 16:32
  • @PaulMcKenzie what do you think now? Isn't it better now? And of course there a mistake because it doesnt work properly. – Victor Bînzar Jan 13 '20 at 15:01
  • Lol i discovered it on my own! So there at the logical operator in while-loop, it should be 'or' || instead of 'and' &&. Now it works pretty well. It feels sooo good. – Victor Bînzar Jan 13 '20 at 15:09

2 Answers2

0

The code above works pretty well for me. It prints out every right number of the Fibonacci sequence you introduce in the bar[100] array. Special thanks to @PaulMcKenzie and everyone who helped!

-1

Move the bar[1] bar[2] and bar[3] initialization outside of the for loop. They are not defined outside the scope of it.

Karov
  • 13
  • 2