can we put the restriction on some function of the loop variable in the loop conditions like the for loop in the following program

``````#include<iostream>
using namespace std;
int main()
{
int a,size=2,b[size],i;
cin>>a;
b[0]=1;
b[1]=1;
cout<<"The fabbonacci sequence is:"<<b[0]<<" "<<b[1]<<" ";
for(i=2;b[i]<=a;i++)
{
b[i]=b[i-1]+b[i-2];
size++;
cout<<b[i]<<" ";
}
system("pause");
return 0;
}
``````

this program is expected to give fabonacci sequence till the integer a.Pls help me with this program

Edited by mike_2000_17: Fixed formatting

3
Contributors
5
Replies
6
Views
7 Years
Discussion Span
Last Post by Fbody

That is an unusual notation, but it could work. That aside, this program won't run properly. Your b array is only 2 elements long.
Q. What happens if your user wants to generate a sequence that is greater than 2 numbers long?

what if i design the do while loop like the one below?
#include<iostream>
using namespace std;
int main()
{
int a,size=2,b,i=2;
cin>>a;
b[0]=1;
b[1]=1;
cout<<"The fabbonacci sequence is:"<<b[0]<<" "<<b[1]<<" ";
do
{
b=b[i-1]+b[i-2];
cout<<b<<" ";
i++;
size++;
}
while(b<=a);
system("pause");
return 0;
}
what's the problem with this?
what shall i use so that function first does the job alloted and then checks the condition?

i am using turbo c++ and i wrote another program where i define an integer
int size=5,arr;
but my compiler shows an error: on arr (constant value required).

``b[i]=b[i-1]+b[i-2];``

but i think there is error in syntax.
if you want i can re write your program by changing little syntax.
can you tell is this program compiled successfully on your compiler.

I'm using a developer c++ compiler and the above program is giving only first 3 digits of fabonacci seq as output.that is for any integer u enter it gives the output
1 1 2

By defining your b array the way you do and looping in that manner you're still overrunning array b's boundaries. An array is a static size, they are not dynamic. Code like this does not re-size an array, it accesses elements that are not part of the array.

``````int size = 2;     //define a variable to set the size of the array
int array[size];  //declare a size-element array of int (elements are 0 thru (size-1))
while ( /*...something...*/) {
/* ... do something ... */
array[size] = someValue;   //WARNING!!!!! SEG FAULT
//this assigns someValue to element size of the array, which does not exist
size++;
}``````

You can't expect your program to run correctly until you stop trying to do that. Each time that loop runs, it makes the problem worse.

Does your assignment require that you create an array? If so, what does it say about the array? I think there is something you are missing in your instructions.

To properly produce a Fibonacci Sequence, you need 3 temporary variables. You will use 2 of them to store the previous 2 numbers, and the third will be the new/current number. Then, once you have displayed the new/current number, you shift the numbers back 1 position and repeat the process.

``````int first = 1;
int second = 1;
int current = 0;
cout << first << " " << second;
while (current < input) {
current = (first + second);
cout << current << " ";
first = second;
second = current;
}``````

You can do this with an array as well, but not how you are doing it. Take this information and see what you can come up with.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.