Using a function and a loop to make a Fibonacci sequence

Pages: 12
I have some code where I want to print as many numbers in the Fibonacci sequence as defined by "totalNumbers". I cannot get it to compile though, it has numerous errors. I am not sure if my for statement i<=20 is the totalNumbers that my instructor wants to be changed by to allow more or less numbers. Anyone know if this is right or why my code won't run? How would I convert my code to alow the parameter of totalNumbers to be what changes how many numbers print in the sequence?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
  #include <iostream>
using namespace std;

void Fibonacci(int totalNumbers);
int main()
{
    for(int i=1; i<=20; i++) // change i<= to change how far the sequence goes
    {
        cout << Fibonacci(i) << endl;
    }
    return 0;
}

void Fibonacci(int totalNumbers)
{
  if(1 == totalNumbers || 2 == totalNumbers)
  {
      return 1;
  }
  else
  {
      return Fibonacci(totalNumbers-1) + Fibonacci(totalNumbers-2);
  }

}
Last edited on
Hi,

==>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
using namespace std;

int Fibonacci(int totalNumbers);
int main()
{
    for(int i=1; i<=20; i++)
{
        cout << Fibonacci(i) << endl;
    }
    return 0;
}

int Fibonacci(int totalNumbers)
{
  if(totalNumbers == 1|| totalNumbers == 2)
  {
      return 1;
  }
  else  {
      return Fibonacci(totalNumbers-1) + Fibonacci(totalNumbers-2);
  }
}
Does that help you? :)
@closed account 5a8Ym39o6 It works, but you removed my void function, which I am supposed to use. Also, I needed to make the "totalNumbers" the variable to change, instead of my for loop. Any idea how I could do it? Basically, instead of changing the amount of times it prints using my loop, I want to use int totalNumbers.
So can you let us know more details of your assignment?
Also, a couple of sample outputs of the assignment would be also helpful too.
This is what he gave us:

Write a C++ program which uses a function

void Fibonacci(int totalNumbers);

The function generates as many Fibonaci number as indicated by the parameter totalNumbers.

You can do this either recursively (if you are a masochist) or using a loop!

If using recursion you will need 2 functions, and one will have to return a value.



In mathematics, the Fibonacci numbers or Fibonacci sequence are the numbers in the following integer sequence:

1 1 3 5 8 13 21 34 55 89 144...

or (often, in modern usage):

0, 1, 1 2 3 5 8 13 21 34 55 89 144...



Last edited on
> You can do this either recursively (if you are a masochist)
So you are a masochist??

Why not just take the easier approach?
closed account (E0p9LyTq)
So you want to get a Fibonacci series for a given number, using iteration. Eazy peazy:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>

void Fibonacci(unsigned long long position);

int main()
{
   std::cout << "Which position? ";
   unsigned long long position;
   std::cin >> position;
   std::cout << "\n";

   for (unsigned long long loop = 1; loop <= position; loop++)
   {
      Fibonacci(loop);
   }
   std::cout << "\n";
}


void Fibonacci(unsigned long long n)
{
   unsigned long long minusTwo = 1;
   unsigned long long minusOne = 1;
   unsigned long long answer = 2;

   if (n < 3)
   {
      std::cout << 1 << " ";
      return;
   }

   for (n -= 3; n != 0; n--)
   {
      minusTwo = minusOne;
      minusOne = answer;
      answer = minusOne + minusTwo;
   }

   std::cout << answer << " ";
}


Which position? 15

1 1 2 3 5 8 13 21 34 55 89 144 233 377 610
Last edited on
closed account (E0p9LyTq)
you removed my void function

You can't return a value with a void function.
Try this :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
using namespace std;

void Fibonacci(int totalNumbers);
int main()
{
    Fibonacci(20);
    return 0;
}

void Fibonacci(int totalNumbers)
{
    int i = 0, a = 1, b = 0;
    int my_fibonacci = 0;

    cout << "Fibonacci number: ";
    while(i < totalNumbers)
    {
        b = my_fibonacci;
        my_fibonacci = a + b;
        cout << my_fibonacci << " ";
        a = b; i++;
    }   
    cout << endl;
}
Does that help? :)

EDIT : Yes, it has really helped!! The OP is happy!!
Last edited on
okay, that worked! @closed account 5a8Ym39o6 Thank you! I was not sure what that word even meant, LOL.
Thank you both very much for helping this noobie out!
Last edited on
Glad it helped :)
@Austinomical

You could also pass the argument to the function by reference, that way it's value is altered in main.

void Fibonacci(const int& totalNumbers);

Not sure if you have learnt about references yet, but it is one way of achieving what you want. And you can avoid having the function printing things. Ideally functions should do one conceptual thing: In this case calculate the Fibonacci number. The responsibility to print the result should be elsewhere.

Also, rather than have "magic numbers" in your code, make them variables and refer to them in the code, like what FurryGuy did with the position variable.

const unsigned short int totalNumbers = 20;

That way you can change how any numbers are produced in one place. Note you are supposed to make use of the function parameter, not limit the loop with some other value.

Also totalNumbers needs to be checked, there is a maximum size it can be before it causes an overflow of the type being used. Notice that FurryGuy used unsigned long long? That is the largest unsigned type the system has. There are other names for it, but your teacher will ask where you learnt them.

Good Luck !!
@TheIdeasMan
It is an assignment, but I think his professor wouldn't ask him that much.
It is an assignment, but I think his professor wouldn't ask him that much.


How do you know? Passing by reference is something that might be taught in the first 3 or 4 weeks. The fact that a void function was asked for was a big clue. The other things beginners often miss, but might worth some brownie points - especially the concept of checking the validity of a value.
@TheIdeasMan
> It is an assignment, but I think his professor wouldn't ask him that much.

What I am asking is long long, not reference.
closed account (E0p9LyTq)
@TheIdeasMan,

My use of the position variable was simply a hold-over from a Fibonacci program that the user would enter a number and the program would output that number's Fibonacci number.

Regarding making the function use a reference, I considered doing that, but chose to move the output statements into the function itself.

Yes, that over-clutters the function but was a quick off-the-cuff choice that I wouldn't have likely done in a real-world situation.

Checking the output for accuracy/overflow is something I should have done, though. Mea Culpa.
Pages: 12