PDA

View Full Version : Write A C programme



Aladdin
13-12-2010, 19:34
I'll show some work here.

-Calculate the Nth term of the Fiboncci sequence is given by the recurrence relation:
U1=1 ; U2=1 ; UN=U(N-1) + U(N-2) ; For N>2.

-Determine the rank N and the value of A maximum term that can be used to calculate if UN :
a)Type int
b)Type long
c)Type dua
d)Type long double.

My Work in the first part;

#include<stdio.h>
main()
{
int UN,N;
int U1=1;
int U2=1;

for(N=2;N<=3;N++)
{
UN=U(N-1) + U(N-2);
printf(The sequence is UN=%d\n",UN)
}
}

Well, I'm first engineering student I have no idea why they teach us programming.

Any help will be greatly appreciated.
Thanks

kryton9
13-12-2010, 21:33
I am not sure about the math, but U(N-1) would be a function call. Anyways here is a version for int. Once someone, with better math skills than I, can explain what is needed we can straighten the math out later.


#include <stdlib.h>
#include<stdio.h>

int U(int N)
{
return (N - 1) + (N - 2);
}

int main()
{
int UN,N;
int U1=2;
int U2=3;

for( N = U1; N <= U2; N++ )
{
UN = U(N);
printf("The sequence is UN=%d\n", UN);
}
return 0;
}

Charles Pegge
14-12-2010, 01:55
The fibonacci algorithm should look something like this:



#include <stdlib.h>
#include<stdio.h>

int fibo(int n)
{
int i,u1,u2,un;
u1=1;
u2=1;
un=1;
for i=3 : i<=n : i++;
{
un=u1+u2;
u1=u2;
u2=un;
}
return un;
}

int main()
{
int u;
u=fibo(4); /* example */
printf("The 4th term is %d\n", u);
return 0;
}


where n>0.

in thinbasic:



function fibo(byval n as integer) as integer
dim as integer i,u1,u2,un
u1=1
u2=1
un=1
for i=3 to n
un=u1+u2
u1=u2
u2=un
next
return un
end function

msgbox 0, fibo(4)



The second part of the task is more tricky and I can't think of a clean solution. You can drive the sequence till the integer types overflow and go negative but depending on your compiler, overflowing float types will trigger a run time error.

(For Longs I make the limit N=47)

Charles

danbaron
14-12-2010, 09:38
Here is something.

:p
Dan


// In C99, a single line comment, begins with "//".

// You need "stdio.h" for the function "printf", in the function "main".
#include <stdio.h>

// You need "limits.h" for INT_MAX and LONG_MAX.
#include <limits.h>

// You need "float.h" for DBL_MAX and LDBL_MAX.
#include <float.h>

// You need to list the function prototype.
long fibonacci(long n);

//--------------------------------------------------------------------------------------------

// Here is a simple, but slow way to solve your problem.
// It is slow because it uses recursion.
// It is shown for type long, but you can change it to any of the other three data types.

// It can completely solve your problem for types int and long.
// If on your machine, LDBL_MAX is larger than DBL_MAX, then, it can completely solve your problem for the double data type.
// It will not completely solve your problem for the long double data type.

// The problem is, with the variable t. For type long double (and maybe for type double),
// sooner or later the sum of f_old and f_new, will cause it (t) to overflow, and the program will malfunction.

//--------------------------------------------------------------------------------------------

int main(int argc, char *argv[])
{
// The maximum sizes of the four data types are machine dependent.
// Here they are for the machine that you run this program on.

printf("The maximum int on this machine is %u\n", INT_MAX);
printf("The maximum long int on this machine is %u\n", LONG_MAX);
printf("The maximum double on this machine is %e\n", DBL_MAX);
printf("The maximum long double on this machine is %Le\n", LDBL_MAX);
printf("\n");

long i, f_old, f_new;
long LM = LONG_MAX;
long double t;

f_old = 0;
f_new = 0;

for(i=1; i<=LM; i++)
{
t = (long double)f_old + (long double)f_new;
if(t > LM)
{
printf("Sequence completed for this data type.\n");
return 0;
}
f_old = f_new;
f_new = fibonacci(i);
printf("For n = %u, fibonacci(n) = %u\n", i, f_new);
}
return 0;
}

//--------------------------------------------------------------------------------------------

long fibonacci(long n)
{
if(n==1) return 1;
if(n==2) return 1;
return fibonacci(n-1) + fibonacci(n-2);
}

//--------------------------------------------------------------------------------------------

Aladdin
14-12-2010, 21:23
Thanks for the help guy's.

Aladdin
14-12-2010, 21:44
Another C program ;

There is table which consist of Rows and column's

Y/X 0 1 2 3

0 0 0 0 0
1 0 1 2 3
2 0 2 4 6
3 0 3 6 9

I'm multiplying each element.
How can I write this C program, please.'
I know I must use \n" for new line.

Charles Pegge
15-12-2010, 18:47
This may help with formatted printing:

http://www.cplusplus.com/reference/clibrary/cstdio/printf/