Results 1 to 7 of 7

Thread: Write A C programme

  1. #1
    Junior Member
    Join Date
    Dec 2010
    Posts
    7
    Rep Power
    15

    Write A C programme

    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

  2. #2
    thinBasic MVPs kryton9's Avatar
    Join Date
    Nov 2006
    Location
    Naples, Florida & Duluth, Georgia
    Age
    68
    Posts
    3,865
    Rep Power
    405
    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;
    }
    

  3. #3
    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
    Last edited by Charles Pegge; 14-12-2010 at 02:06.

  4. #4
    thinBasic MVPs danbaron's Avatar
    Join Date
    Jan 2010
    Location
    California
    Posts
    1,378
    Rep Power
    152
    Here is something.


    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);
    }
    
    //--------------------------------------------------------------------------------------------
    

    Last edited by danbaron; 14-12-2010 at 09:42.
    "You can't cheat an honest man. Never give a sucker an even break, or smarten up a chump." - W.C.Fields

  5. #5
    Junior Member
    Join Date
    Dec 2010
    Posts
    7
    Rep Power
    15
    Thanks for the help guy's.

  6. #6
    Junior Member
    Join Date
    Dec 2010
    Posts
    7
    Rep Power
    15
    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.

  7. #7
    This may help with formatted printing:

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

Similar Threads

  1. Long term suggestion: Executing bundled EXEs on medium with no write access
    By Petr Schreiber in forum thinBundle suggest new features
    Replies: 4
    Last Post: 07-11-2008, 09:26

Members who have read this thread: 0

There are no members to list at the moment.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •