Thread: conversion to c.

  1. #1
    Registered User
    Join Date
    Aug 2014
    Posts
    11

    conversion to c.

    Hello!
    I have just written a fortran 90 program and willing to convert it into C language.
    I have almost done it but when the fortran 90 program asks me to enter the upper limit, I enter the upper limit as 1 and the answer computed by the program is 0.3637185172 which is correct but when I enter the upper limit as 1 in the C conversion of the program ,the answer comes out to be 1.1741024473 which is wrong. I am posting the fortran 90 program and my attempt to convert it into C.
    fortran 90 code:
    Code:
    PROGRAM integraltest
    IMPLICIT NONE
    INTEGER, PARAMETER :: num = SELECTED_REAL_KIND(15,160)
    REAL(KIND=KIND(1.0d0)) :: zeta = 24.886266123440878231952771_num, frac(5), term, integral
    REAL (KIND=KIND(1.0d0)) ::p
    INTEGER :: v
    INTRINSIC EXP
    DO
    write(*, '(A)' , ADVANCE = "NO") "Enter the Upper Limit : "
    read(*,*) p
    integral = (p**5)/5
    integral = integral + zeta
    v = 1
    DO
        frac(1) = p**4/(1.0_num*v)
        frac(2) = (4_num*(p**3))/(1.0_num*(v**2))
        frac(3) = (12_num*(p**2))/(1.0_num*(v**3))
        frac(4) = (24*p)/(1.0_num*(v**4))
        frac(5) = (24_num)/(1.0_num*(v**5))
        ! sum of fraction terms
        term = (frac(1) + frac(2) + frac(3) + frac(4) + frac(5))
        term = term*EXP(-1.0_num*v*p) !sum of fraction multiplied by the exp(-v)
        integral = integral - term !substracting the terms from earlier computed value
        IF(term < 1.0e-14) EXIT !exit if value good enough
        v = v + 1 !next value for v
    END DO
    WRITE(*,'(I5, A31)') v, 'terms used in computing serie'
    WRITE(*,'(A40, F100.10)') 'an approximation for the integral : ', integral
    END DO
    END PROGRAM integraltest
    And here is my attempt to convert it into C Language:
    Code:
    #include<stdio.h>
    #include<math.h>
    int main()
    {
    double zeta = 24.886266123440878231952771 , a , b , c , d , e = 2.7182818284590452353602874713527 , f , term , integral , p , v ;
    printf("Enter the upper limit : ");
    scanf("%lf" ,&p);
    integral = ((pow(p,5))/5);
    integral = integral + zeta ;
    v = 1 ;
    while(1)
    {
    a = (pow(p,4))/v ;
    b = (4*(pow(p,3))/(pow(v,2))) ;
    c = (12*pow(p,2))/(pow(v,3)) ;
    d = (24*p)/(pow(v,4)) ;
    f = (24)/(pow(v,5)) ;
    term = a + b + c + d + f ;
    term = term*(pow(e,(-1*v*p))) ;
    integral = integral - term ;
    v = v + 1 ;
    printf("\nAn approximation for the integral is : %.10lf\n\n",integral) ;
    return;
    }
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It looks like your C version differs in that the loop always runs once rather than running until the value is good enough. Could that be the problem?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2014
    Posts
    11
    That may be a problem but the main problem is the wrong answer computed by the C program.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    He got someone to write the Fortran code for him. Guess he needed C. integration - Page 2 - My Math Forum

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by rags_to_riches
    He got someone to write the Fortran code for him. Guess he needed C.
    Sigh.

    Quote Originally Posted by prakhar
    That may be a problem but the main problem is the wrong answer computed by the C program.
    I gave you a possible reason for the wrong answer computed.

    It looks like the main problem is that you need to learn how to program.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ conversion help
    By farukyaz in forum C++ Programming
    Replies: 20
    Last Post: 10-17-2011, 10:39 AM
  2. Conversion!!!
    By GT70sgt in forum C++ Programming
    Replies: 1
    Last Post: 11-19-2003, 07:51 PM
  3. Conversion?
    By mart_man in forum C Programming
    Replies: 7
    Last Post: 12-08-2002, 07:23 PM
  4. conversion
    By sonict in forum C++ Programming
    Replies: 10
    Last Post: 11-22-2002, 08:01 PM
  5. Conversion
    By CyberMax8 in forum C Programming
    Replies: 7
    Last Post: 11-20-2002, 08:46 PM