#include <stdio.h>

// function prototypes
int getnumterms();
double coshyper(double xvalue, double nterms);

/*
Name main:
input: x value
output: x value, and computed value of cosh
Description: Main function prompts the user to enter the x value, calls the getnumterms function to
get the number of terms from the user, then calls the function coshyper to caluclate the value of
cosh using the user inputed values. Finally, it prints out the final cosh value.
*/
double main()
{

//declaration of variables
double xvalue;
int nterms;
char terminate;
double cosh;


/*
the following do while loop is used to repeat the entire sequence based on if the user wants to
keep entering values of terminate the program
*/
do{

printf("Please enter the x value\n");
scanf("%lf", &xvalue);

nterms = getnumterms(); //calling function getnumterms

cosh = coshyper(xvalue, nterms);    // calling function coshyper and passinh variables xvalue and nterms
printf("cosh(%f) is %.8e\n", xvalue, cosh); // prints out the final value

printf("Do you wish to continue Y/N\n");
scanf("%s", &terminate);

// if statement to continue or terminate the program
    if (terminate == 'n' || terminate == 'N')
    {
    printf("Program Terminated\n");
    }

}
while (terminate == 'y' || terminate == 'Y  ');


return 0;
}

/*
Name: getnumterms
input: number of terms from user
output: variable n to main function
description: This function prompts the user to enter the number of terms,
and verifies that the entered value is positibe using a nested if statement
*/



int getnumterms()
{
int flag, n;    // variable declaration

    do        // do while loop to repeat the program if the inputed value is negative
{

    flag = 1;

    printf("Please enter the number of terms\n");
    scanf("%d", &n);

    if (n < 0)  //if statement to prompt the user to input a positive value and also causes loop to repeat
    {
        printf("The Number of terms needs to be positive\n");
        flag = 0;
    }

}
while (flag==0);
return n;   //return variable n to main function
}

/*
Name: coshyper
Input: xvalue and nterms from main
Output: finalvalue to main
description: This function uses a for loop to calculate the
value of cosh using the given formulas, then send the
value back to the main function to be printed
*/

double coshyper(double xvalue, double nterms)
{
//declaration of variables
long double finalvalue = 1;
double prevterm = 0;
double newterm;
int initializer = 1;

for (initializer = 1; initializer <= nterms; initializer++)
    {
        if (initializer==1)
        {
            newterm = (xvalue*xvalue/(2*initializer*(2*initializer-1)))*1;
        }
        else
        {
            newterm = (xvalue*xvalue/(2*initializer*(2*initializer-1)))*prevterm;
        }
        finalvalue = finalvalue + newterm;  //setting the value of the final value to the previous value plus new value
        prevterm = newterm; // setting the newterm to equal the value of the old term
    }
    return finalvalue; // returning the value to main to be printed

}

