Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

defining of the cos X value using 2 function, HOW???

Status
Not open for further replies.

frex80i

Technical User
Mar 31, 2007
1
0
0
NL
actually, I've made the program. but it still doesn't work well...the task is from my lecturer..

the task is about the function. after I enter the real number X and an integer number N. the program computes the value for Z, using factoring over N terms. the program also prints out the value of Z, cos(Z), and the N-th term. only use the function, not the pointer and the array..

Z = term 1 + term 2 + term 3...=1 - X^2/2! + X^4/4!...

here is my program...
=======================================================

/*INCLUDES*/
#include <stdio.h>
#include <math.h>

/*FUNCTION DECLARATIONS*/
long int Fac(long int);
double Term(long int, double);

int main()
{
double dCosx,dCosinus_x,dX;
long int liI;

printf("enter the X value : ");
scanf("%lf",&dX);
printf("enter the I value : ");
scanf("%li",&liI);
while (liI <= 0);
{
printf("give the positive value only!");
printf("enter I : ");
scanf("%li",&liI);
}


dCosx=Term(liI,dX);
dCosinus_x=cos(dX);
printf("\nCosx = %lf",dCosx);
printf("\nvalue of Cos (x) = %lf",dCosinus_x);
printf("\nX = %lf",dX);

return (0);
}

/*Term Function*/
double Term(long int liI, double dX)

{
double dRes, dA, dTotal, dRad;
int nRem, nCount;

dRad=dX;
nCount=0;
dTotal=0;
while(liI>nCount)
{
dA=pow(dRad,(2*nCount));
dRes=dA/Fac(2*nCount);
nRem=(nCount+2)%2;
if(nRem!=0);
{
dRes=dRes*-1;
}
nCount=nCount+1;
dTotal=dTotal + dRes;
}
printf("\nthe n-th Term is %lf",&dRes);
return (dTotal);
}

/*Factorial Function*/
long int Fac(long int liI)

{
long int liFactorial;

if (liI == 0)
{
liFactorial = 1;
}
else
{
liFactorial = liI*Fac(liI-1);
}
return (liFactorial);
}


================================================

some body can help me, please?
 
1. Please use [code][/code] tags when posting code.

2. A consistent approach to indentation will help you figure out the flow of your program.

3. Ramp up the warning level of your compiler as far as it will go, and pay attention to everything it says.
[tt]
$ gcc -W -Wall -ansi -pedantic -O2 foo.c
foo.c: In function `main':
foo.c:28: warning: ISO C90 does not support the `%lf' printf format
foo.c:29: warning: ISO C90 does not support the `%lf' printf format
foo.c:30: warning: ISO C90 does not support the `%lf' printf format
foo.c: In function `Term':
foo.c:50: warning: empty body in an if-statement
foo.c:57: warning: ISO C90 does not support the `%lf' printf format
foo.c:57: warning: double format, pointer arg (arg 2)
[/tt]

Code:
/*INCLUDES*/
#include <stdio.h>
#include <math.h>

/*FUNCTION DECLARATIONS*/
long int Fac(long int);
double Term(long int, double);

int main()
{
double dCosx,dCosinus_x,dX;
long int liI;

printf("enter the X value : ");
scanf("%lf",&dX);
printf("enter the I value : ");
scanf("%li",&liI);
while (liI <= 0);               /*!! watch the ; */
{
    printf("give the positive value only!");
    printf("enter I : ");
    scanf("%li",&liI);
}


dCosx=Term(liI,dX);
dCosinus_x=cos(dX);
printf("\nCosx = %lf",dCosx);   /* 28 */
printf("\nvalue of Cos (x) = %lf",dCosinus_x);
printf("\nX = %lf",dX);

return (0);
}

/*Term Function*/
double Term(long int liI, double dX)

{
double dRes, dA, dTotal, dRad;
int nRem, nCount;

dRad=dX;
nCount=0;
dTotal=0;
while(liI>nCount)
{
    dA=pow(dRad,(2*nCount));
    dRes=dA/Fac(2*nCount);
    nRem=(nCount+2)%2;
    if(nRem!=0);                /* 50 */
    {
        dRes=dRes*-1;
    }
    nCount=nCount+1;
    dTotal=dTotal + dRes;
}
printf("\nthe n-th Term is  %lf",&dRes);    /* 57 */
return (dTotal);
}

/*Factorial Function*/
long int Fac(long int liI)

{
long int liFactorial;

if (liI == 0)
    {
        liFactorial = 1;
    }
    else
    {
        liFactorial = liI*Fac(liI-1);
    }
return (liFactorial);
}

--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top