/*****************************************************************************
Problem: A) To write a program using subroutines ( procedures
or functions )
to evaluate the following sum:
f(x) = 1 + x^2/2! + x^4/4! +...........+ x^(2*(n-1))/(2*(n-1))!
Allow to the user enters a specific value
for x and the number
of terms to evaluate as inputs. The program should output
the sum.
Example ( for Output ):
Enter a x value: 2.5
Enter the number of terms to evaluate: 8
f(x) = 6.13
A.1) Try to solve the problem using the
regular structure:
Declaration part, main body, and implementation part
A.2) Try to solve the same problem using a Header File
Course: C++
Tutor : Guillermo Julca
Mercy College
****************************************************************************/
//---------------------------------------------------------------------------
#include <iostream.h>
#include <conio.h>
#include <iomanip.h>
#include "Sum.h"
#pragma hdrstop
//---------------------------------------------------------------------------
#pragma argsused
//Declaration
/*double FactorialN(int n);
double Term(double x,int NTerm); */
//Main body
int main(int argc, char* argv[])
{
double x;
int NumberTerm;
double sum;
cout<<"Enter a value for X : ";
cin>>x;
cout<<"Enter the number of terms that you want to evaluate:
";
cin>>NumberTerm;
sum=0;
for(int i=1; i<=NumberTerm; i++)
sum = sum + Term(x,i);
cout<<"F(x) = "<<setprecision(3)<<sum<<endl;
getchar();
return 0;
}
//Implementation
/*double FactorialN(int n)
{
int i;
double f;
f=1 ;
for(i=1;i<=n;i++)
f=f*i;
return f;
}
double Term(double x,int NTerm)
{
double aTerm;
aTerm=pow(x,2*(NTerm -1))/FactorialN(2*(NTerm - 1));
return aTerm;
} */
//---------------------------------------------------------------------------
/*****************************************************************************************
#ifndef SUM_H
#define SUM_H
double FactorialN(int n);
double Term(double x,int NTerm);
#endif //SUM_H
********************************************************************************************/
/********************************************************************************************
#include <math.h>
#include "Sum.h"
double FactorialN(int n)
{
int i;
double f;
f=1 ;
for(i=1;i<=n;i++)
f=f*i;
return f;
}
double Term(double x,int NTerm)
{
double aTerm;
aTerm=pow(x,2*(NTerm -1))/FactorialN(2*(NTerm - 1));
return aTerm;
}
******************************************************************************************/
//--------------------------------------------------------------------------------------------------------------------------
Copyright © 2002
GJ
GUILLERMO JULCA