Any Suggestions?

Ok so i had been looking online for some challenges of things to try and program and some stuff to see what i know...btw I am very new to programming and c++...probably about 2 weeks...and i saw a challenge to get a program to display the fibonacci sequence...i started out by just having it display the first ten numbers...then added the option of choosing how many numbers the user would like to see in the sequence. Any suggestions of other things i could do to improve the code or just make it kinda neat...Thanks...oh and heres the code

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

using namespace std;

int main()
{
    int i;//number of variables user would like to see


    cout<< "how many numbers would you like to see in the sequence?\n";
    cin>> i;
    cin.ignore();
    cout<<"ok so you want to see "<<i<<" numbers! Here they are:\n";

    int number[i];            //array for sequence
    number[0]= 1;             //assigning first two numbers in the sequence
    number[1]= 1;

    cout<<number[0]<<"\n";
    cout<<number[1]<<"\n";

    for (int x=1; x<i-1; x++)
    {
         int y;
         y= x-1;

         number[x+1]=number[x]+ number[y]; //adds the current slot in the array to the previous slot in the array

         cout<<number[x+1]<<"\n";

         y++;

    }


}
closed account (D80DSL3A)
A couple of things I see...

1) You are using a static array with a dimension determined at runtime on line 15. This isn't supposed to work, but apparently it does with some compilers.
Correct method involves using a vector or dynamic memory allocation. Actually, you could declare an array with 50 elements or so and that would get you as far as you can go with using an integer for the Fibonacci numbers anyways (then the maximum value of an integer gets exceeded).

2) I think you could skip using the variable y. Why not just substitute x-1 where it belongs on line 27?

3) If you are just outputting the numbers to the console then you don't need such a large array. You don't need to store them all. An array of 3 integers will do if you use the modulus operator to cycle their values. Consider:
1
2
3
4
5
6
7
8
int number[3];
number[0] = 0;// the seed values I found at Wikipedia are 0 and 1
number[1] = 1;
for (int x=2; x<i; x++)
{
    number[x%3]=number[(x-1)%3]+ number[(x-2)%3];// the elements get cycled as needed here.
    cout<<number[x%3]<<"\n";
}
edit: @the things you can add to make it neat ( I assume you are just looking for ideas to practice):

Add the option to test if a number is part of the Fibonacci sequence. User inputs 13, program outputs true, user inputs 12, program outputs false.

Make a price is right option. The user picks a number, the program returns a number from the Fibonacci sequence that is the lowest number that does not exceed the number the user input.

User inputs 11, program outputs 8.
Last edited on
ok i appreciate the input, i'll play around with it some more and see what else i can come up with. Thanks again!
Topic archived. No new replies allowed.