Fibonacci series

Pages: 12
hi guys,nid to write a fibonacci series less than 100 but dont to use #define fibonacci,anyone help?
It is interesting what is the reason that #define was mentioned?!
Do you have code for us to look at?
Some useful info for you, if you don't already know it, is that each number in a fibonacci sequence is the sum of the two numbers preceding it, except for the starting number, which is a 1.

So the first couple numbers are:

1, 1, 2, 3, 5, 8, 13, 21
And if any other member of the forum will show the next fibonacci number then you can get all fibonacci numbers less than 100.:)
So my fibonacci number is

34
Last edited on
There's two more. 55, 89.
OP should absolutely submit:

1
2
3
4
void main()
{
     cout << "1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89" << endl;
}
rollie, that's brilliant wish i had thought of that when i was at University :):):)

Edit: Apart from the comedy I don't see the OP's code.

Edit: One problem

void main()

should be

int main()

You could loose serious marks for that :):):):)
Last edited on
the thing is i know the series up to 100 but what i nid is either a loop or selection statement that will enable the computer to select or calculate the fibonacci series on its own up to 100
OP, so where is your code, we won't do your homework for you. You must have some knowledge of loops, unless you have been sleeping through the lectures. ;) I am not being sarcastic , you need to do somethings yourself - we look forward to helping out.
hello i have quited c++ a few months ago and now i am looking if i still can do anything of this language so ill have a try. the main concepts of the program will work i think.

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 "stdafx.h"
#include <stdio.h>
#include <time.h>
#include <iostream>

using namespace std;

int main(void)
{
  int fibonacci [] = {0,1};
  while (1) {
    int arraysize=sizeof(fibonacci) / sizeof(int); //gets amount of items in array
    if (fibonacci[arraysize-1]+fibonacci[arraysize-2]<=100) {
      fibonacci[arraysize]=fibonacci[arraysize-1]+fibonacci[arraysize-2];
    } else {
      break;
    }
  }
  for (int i=0;i<sizeof(fibonacci) / sizeof(int);i++)
  {
     cout << fibonacci[i] <<endl;
  }
  return 0;
  system("pause");
}
Last edited on
Ok, quite a few bads here

while (1)

is an infinite loop. It ends with the break statement, but that is a poor way of doing it. A better test expression might involve the number 100. A for loop is better.

for (i=0;i<arraysize;i++)

arraysize is 2, is this what you want - to just print the last two numbers?




it gets arraysize adds a number to the array, arraysize increases adds a number etc...

changed my code a bit:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int main(void)
{
  int fibonacci [] = {0,1};
  int arraysize=1;
  while (fibonacci[arraysize-1]+fibonacci[arraysize]<=100) {
    int arraysize=sizeof(fibonacci) / sizeof(int); //gets amount of items in array
    fibonacci[arraysize]=fibonacci[arraysize-1]+fibonacci[arraysize-2];
  }
  for (int i=0;i<sizeof(fibonacci) / sizeof(int);i++)
  {
     cout << fibonacci[i] <<endl;
  }
  system("PAUSE");
  return 0;

}
Last edited on
Array size doesn't increase automatically. You might be confusing them with vectors or something else.
Also, for simplicity, remember you don't even need to STORE all the fibonacci numbers. You can do:

1
2
3
4
5
6
7
8
9
10
int main()
{
    while (fib2 < 100)
    {
        // calc fib2
        cout << fib2 << endl;
    }

    return (int)atof("just for ideas man :P");
}
Last edited on
ofc you have to store them ...
it is a recursive sequence which means that the values are calculated by previous values. so if you don't store the previous values how would you calculate them ?you just say calc fib2 in comment but there is no explicit formula for fibonacci numbers
Last edited on
well even though it's recursive, you don't have to store ALL the numbers. Just remembering the previous two numbers is enough
If it is enough only to print out fibonacci numbers then you can wtite something as

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>
#include <cstdlib>

int main()
{
   unsigned fibonacci [] = { 0, 1 };

   std::cout << "Fibonacci seria less than 100: " 
                 << fibonacci[0] << ' '
                 << fibonacci[1];

   while ( fibonacci[0] + fibonacci[1] <= 100 ) 
   {
      unsigned tmp = fibonacci[0] + fibonacci[1];
      fibonacci[0] = tmp - fibonacci[0];
      fibonacci[1] = tmp;

      std::cout << ' ' << fibonacci[1];
   }

   std::cout << std::endl;

   std::system( "PAUSE" );

   return 0;
}


Maybe it will be better to rewrite the loop the following way

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>
#include <cstdlib>

int main()
{
   unsigned fibonacci [] = { 0, 1 };

   std::cout << "Fibonacci seria less than 100: " 
                 << fibonacci[0] << ' '
                 << fibonacci[1];

   unsigned tmp;
   while ( ( tmp = fibonacci[0] + fibonacci[1] ) <= 100 ) 
   {
      fibonacci[0] = tmp - fibonacci[0];
      fibonacci[1] = tmp;

      std::cout << ' ' << fibonacci[1];
   }

   std::cout << std::endl;

   std::system( "PAUSE" );

   return 0;
}

Last edited on
return (int)atof("just for ideas man :P");

@rollie: I was hoping someone would that also !!

Funny, I did the same challenge a month ago.

After writing a solution using an array, which wasn't as clever as vlad's solution, I experimented solving it with a formula.

Won't generate the most efficient code but it sure gives you credit for research :P

check this out:
http://upload.wikimedia.org/wikipedia/nl/math/1/7/4/1747ee745fbe1fbf10fb3d9de36b8927.png (http://nl.wikipedia.org/wiki/Rij_van_Fibonacci)
Last edited on
Pages: 12