intro to C programming question:::: The Fibonacci number series is defined as fo
ID: 3775192 • Letter: I
Question
intro to C programming question::::
The Fibonacci number series is defined as follows: f(0) = 0, f(1) = 1, and for i > 1, f(i) = f(i-1) + f(i-2). The first 10 numbers from the series are 0,1,1,2,3,5,8,13,21,34. Thus, f(0)=0, f(1)=1, f(2)=1, f(3)=2, etc.
a) Write in the text box below a function called fib that returns void and that takes as parameters an array v of type int and an integer n that indicates the length of array v. This function computes each Fibonacci number f(i) and saves it to array element v[i], for 0 <= i < n. If parameter n is negative, the function must print an error message and call exit(1). Hint: when computing element v[i] use v[i-1] and v[i-2].
b) Write a main() function that declares an int array numbers of size N, where N is a constant equal to 100 and then calls the fib function defined above with parameters numbers and N. After that, print all elements from the numbers array to the terminal. Ensure the code is correct, aligned, and indented properly. Use meaningful variable names.
Explanation / Answer
#include<stdio.h>
//a)
void fib(int v[],int n)//function which generates n fibonocci numbers and stores in array v
{
int i=0;
if(n>2)
{
v[0]=0;
v[1]=1;
for(i=2;i<n;i++)
{
v[i]=v[i-1]+v[i-2];
}
}
else if(n>0&&n<=2)
{
int k=0;
while(n>0)
{ v[k]=k++;
n=n-1;
}
}
else
{
printf(" Error:--Array size should not be empty.. ");
}
}
//b)
int main()
{
int n=100,i=0;
int a[n];
printf("Fibonocci numbers:- ");
fib(a,n);
for(i=0;i<n;i++)
{
printf("Fib[%d]:%d ",i,a[i]);
}
return 0;
}
ouput:-
Fibonocci numbers:-
Fib[0]:0
Fib[1]:1
Fib[2]:1
Fib[3]:2
Fib[4]:3
Fib[5]:5
Fib[6]:8
Fib[7]:13
Fib[8]:21
Fib[9]:34
Fib[10]:55
Fib[11]:89
Fib[12]:144
Fib[13]:233
Fib[14]:377
Fib[15]:610
Fib[16]:987
Fib[17]:1597
Fib[18]:2584
Fib[19]:4181
Fib[20]:6765
Fib[21]:10946
Fib[22]:17711
Fib[23]:28657
Fib[24]:46368
Fib[25]:75025
Fib[26]:121393
Fib[27]:196418
Fib[28]:317811
Fib[29]:514229
Fib[30]:832040
Fib[31]:1346269
Fib[32]:2178309
Fib[33]:3524578
Fib[34]:5702887
Fib[35]:9227465
Fib[36]:14930352
Fib[37]:24157817
Fib[38]:39088169
Fib[39]:63245986
Fib[40]:102334155
Fib[41]:165580141
Fib[42]:267914296
Fib[43]:433494437
Fib[44]:701408733
Fib[45]:1134903170
Fib[46]:1836311903
Fib[47]:-1323752223
Fib[48]:512559680
Fib[49]:-811192543
Fib[50]:-298632863
Fib[51]:-1109825406
Fib[52]:-1408458269
Fib[53]:1776683621
Fib[54]:368225352
Fib[55]:2144908973
Fib[56]:-1781832971
Fib[57]:363076002
Fib[58]:-1418756969
Fib[59]:-1055680967
Fib[60]:1820529360
Fib[61]:764848393
Fib[62]:-1709589543
Fib[63]:-944741150
Fib[64]:1640636603
Fib[65]:695895453
Fib[66]:-1958435240
Fib[67]:-1262539787
Fib[68]:1073992269
Fib[69]:-188547518
Fib[70]:885444751
Fib[71]:696897233
Fib[72]:1582341984
Fib[73]:-2015728079
Fib[74]:-433386095
Fib[75]:1845853122
Fib[76]:1412467027
Fib[77]:-1036647147
Fib[78]:375819880
Fib[79]:-660827267
Fib[80]:-285007387
Fib[81]:-945834654
Fib[82]:-1230842041
Fib[83]:2118290601
Fib[84]:887448560
Fib[85]:-1289228135
Fib[86]:-401779575
Fib[87]:-1691007710
Fib[88]:-2092787285
Fib[89]:511172301
Fib[90]:-1581614984
Fib[91]:-1070442683
Fib[92]:1642909629
Fib[93]:572466946
Fib[94]:-2079590721
Fib[95]:-1507123775
Fib[96]:708252800
Fib[97]:-798870975
Fib[98]:-90618175
Fib[99]:-889489150
Process exited normally.
Press any key to continue . . .
#include<stdio.h>
//a)
void fib(int v[],int n)//function which generates n fibonocci numbers and stores in array v
{
int i=0;
if(n>2)
{
v[0]=0;
v[1]=1;
for(i=2;i<n;i++)
{
v[i]=v[i-1]+v[i-2];
}
}
else if(n>0&&n<=2)
{
int k=0;
while(n>0)
{ v[k]=k++;
n=n-1;
}
}
else
{
printf(" Error:--Array size should not be empty.. ");
}
}
//b)
int main()
{
int n=100,i=0;
int a[n];
printf("Fibonocci numbers:- ");
fib(a,n);
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
printf(" ");
return 0;
}
ouput:-
Fibonocci numbers:-
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 75025 121393 196418 317811 514229 832040 1346269 2178309 3524578 5702887 9227465 14930352 24157817 39088169 63245986 102334155 165580141 267914296 433494437 701408733 1134903170 1836311903 -1323752223 512559680 -811192543 -298632863 -1109825406 -1408458269 1776683621 368225352 2144908973 -1781832971 363076002 -1418756969 -1055680967 1820529360 764848393 -1709589543 -944741150 1640636603 695895453 -1958435240 -1262539787 1073992269 -188547518 885444751 696897233 1582341984 -2015728079 -433386095 1845853122 1412467027 -1036647147 375819880 -660827267 -285007387 -945834654 -1230842041 2118290601 887448560 -1289228135 -401779575 -1691007710 -2092787285 511172301 -1581614984 -1070442683 1642909629 572466946 -2079590721 -1507123775 708252800 -798870975 -90618175 -889489150
Process exited normally.
Press any key to continue . . .
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.