Sunday, August 2, 2009

Fibonacc in C++?

Hi guys, I'm learning some C++, and wanted te make a fibonacci sequence in C++, but when I compile with gcc, i get some complicated errors.


could someone say what's wrong with my code?


thanks





#include%26lt;iostream%26gt;





using namespace std;





int main(void)


{





int max = 0;








cout %26lt;%26lt; endl %26lt;%26lt; "Geef het aantal elementen van de tabel in: ";


while (max %26lt;= 0)


cin %26gt;%26gt; max;





char fibo[max];





fibo[0] = fibo[1] = 1;


for(int n=2;n%26lt;max;n++)


{


fibo[n] = fibo[n-1] + fibo[n-2];


}





for(int i=0;i%26lt;max;i++)


{


cout %26lt;%26lt; fibo[i] %26lt;%26lt; "\t" ;


}





return 0;





}

Fibonacc in C++?
#include%26lt;iostream%26gt;





using namespace std;





int main(void)


{





int max = 0;





cout %26lt;%26lt; endl %26lt;%26lt; "Geef het aantal elementen van de tabel in: ";


while (max %26lt;= 2)


cin %26gt;%26gt; max;





long *fibo = new long[max];





fibo[0] = fibo[1] = 1;


for(int n=2;n%26lt;max;n++)


{


fibo[n] = fibo[n-1] + fibo[n-2];


}





for(int i=0;i%26lt;max;i++)


{


cout %26lt;%26lt; fibo[i] %26lt;%26lt; "\t" ;


}


delete fibo;


return 0;





}
Reply:hit the alt+f4 button until your mature enough to handle a computer. wow people are so lame and gay. If you dont approve why answer the ******* question? Report It

Reply:I don't think you are declaring the array


char fibo[max];


correctly. I think you need to do it


below int max = 0;





and in order to make you array size the size of the input you need to look into the dynamic array allocation. Its a pain to figure out, but can be done.





And by the way when you do move the array declaration to where I told you, your for loops will not longer work and will generate an error if you try to use you array inside them.





Also, when you will put the array declaration where I told you, you need to change the max to be greater than 0, otherwise it can't create an array of size 0;





Good Luck


No comments:

Post a Comment