#include "stdafx.h" #include <iostream> using namespace std; int main() { int a[25]; int i,j; int fib[25]; int fibs; char z; a[0] = 1; a[1] = 1; { fib[1] = a[0]; fib[2] = a[1]; fibs = 0; } for ( i=2; i<25; i++ ) { fibs = fib[1] + fib[2]; fib[1] = fib[2]; fib[2] = fibs; a[i] = fibs; } for(i=0; i<25; i++) { cout << "a[" << i << "]=" << a[i] << endl; } cout << endl; cout << "Please enter two values from anywhere in the Fibonacci Series, please enter the smallest one first." << endl; cin >> a[23]; cin >> a[24]; { fib[1] = a[24]; fib[2] = a[23]; fibs = 0; } for ( i=22; i>=0; i) { fibs= fib[1]  fib[2]; fib[1] = fib[2]; fib[2] = fibs; a[1] = fibs; } for(i=0; i<25; i++) { cout << "a[" << i << "]=" << a[i] << endl; } cout << endl; cout << "Please enter two positive values, smallest value first." << endl; cin >> a[0]; cin >> a[24]; system ("Pause"); return 0;
5 Replies  3128 Views  Last Post: 11 July 2011  12:05 PM
#1
Filling in between two values using Fibonacci Series
Posted 11 July 2011  11:14 AM
This is my code so far, it works great and all. The thing that I need is some tips on the next step in my project. As you can see I asked the user for two positive values, smallest value first. When I get the two user values I place the smallest one at the start of array a at a[0], and the largest one at the end of array a at a[24]. The last step of this project is to use Fibonacci series to fill in between the two values the user gave me, if the large number the user gave me appears before reaching the end then I can stop the series right there. Any tips on how to do this?
Replies To: Filling in between two values using Fibonacci Series
#2
Re: Filling in between two values using Fibonacci Series
Posted 11 July 2011  11:34 AM
If I get what you are saying is you want the user to beable to "cut out" persay from the fib series from smaller number to larger number or 25th value (whichever comes first). My first guess since you have the array of fib numbers. A simple search algorithm to search for the index of the smaller number and keep iterating until you reach the end or if the next number is larger than the large number given. This is at least if I am reading this correctly.
#3
Re: Filling in between two values using Fibonacci Series
Posted 11 July 2011  11:46 AM
Your looking at it a little off. What I mean is say I ask someone for two random numbers smaller one first. They say 38 and 392. what I would then be needing to do is set a[0] = 38 and a[24] = 392.
My array now looks like this:
a[0] = 38
a[1] =
a[2] =
a[3] =
a[4] =
a[5] =
a[6] =
a[7] =
a[8] =
a[9] =
a[10] =
a[11] =
a[12] =
a[13] =
a[14] =
a[15] =
a[16] =
a[17] =
a[18] =
a[19] =
a[20] =
a[21] =
a[22] =
a[23] =
a[24] = 392
I now need to use fibonacci to fill in the spaces between a[0] and a[24]. The user wants the program to do all of the work. See what I'm saying?
My array now looks like this:
a[0] = 38
a[1] =
a[2] =
a[3] =
a[4] =
a[5] =
a[6] =
a[7] =
a[8] =
a[9] =
a[10] =
a[11] =
a[12] =
a[13] =
a[14] =
a[15] =
a[16] =
a[17] =
a[18] =
a[19] =
a[20] =
a[21] =
a[22] =
a[23] =
a[24] = 392
I now need to use fibonacci to fill in the spaces between a[0] and a[24]. The user wants the program to do all of the work. See what I'm saying?
#4
Re: Filling in between two values using Fibonacci Series
Posted 11 July 2011  11:57 AM
I think I might have figured something out. When you use fibonacci the first two values are alway the same because the second value is the sum of the previous two. There is no value infront of the the first value so it would be zero plus first value. The only tricky part thats left is figuring out how to make sure that the series stops when it reaches the upper value if it is reached before the final array element.
#5
Re: Filling in between two values using Fibonacci Series
Posted 11 July 2011  12:02 PM
hogsgonewild, on 11 July 2011  11:57 AM, said:
I think I might have figured something out. When you use fibonacci the first two values are alway the same because the second value is the sum of the previous two. There is no value infront of the the first value so it would be zero plus first value. The only tricky part thats left is figuring out how to make sure that the series stops when it reaches the upper value if it is reached before the final array element.
Stop when the last value in the array is larger than the last number?
#6
Re: Filling in between two values using Fibonacci Series
Posted 11 July 2011  12:05 PM
ipushmycar, on 11 July 2011  12:02 PM, said:
hogsgonewild, on 11 July 2011  11:57 AM, said:
I think I might have figured something out. When you use fibonacci the first two values are alway the same because the second value is the sum of the previous two. There is no value infront of the the first value so it would be zero plus first value. The only tricky part thats left is figuring out how to make sure that the series stops when it reaches the upper value if it is reached before the final array element.
Stop when the last value in the array is larger than the last number?
How do I nest an if function in that thing to test each value as it is made without possible causing a problem.
Page 1 of 1
