Practical 7.5

Posted: January 23, 2012 in Lab 3

Problem Statement- Using the two previous functions, and using a starting array [1, 1], calculate the first 20 numbers in the Fibonacci sequence.

#include<stdio.h>
int last2(int *, int);
int last2(int *arr,int n)
{
int i,sum=0;
for(i=n-1;i>(n-3);i--)
sum=sum+*(arr+i);
return sum;
}
int append(int *, int, int);
int append(int *arr,int n, int a)
{
*(arr+n)=a;
return 0;
}
int main()
{
int arr[20],i;
arr[0]=1;
arr[1]=1;
for(i=2;i<20;i++)
append(&arr[0],i,last2(&arr[0],i));
for(i=0;i<20;i++)
printf("%d\n",*(arr+i));
return 0;
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s