#include <stdio.h>
#include <stdlib.h>
#include <math.h>

  /*
   * Name:      Azogu, Chibuzo Desmond
   * Variant:     Method 5, Function 48
   * Description: The program calculates the function y(x) in already defined points. It allows user input for;
                  starting value A, stop value B, steps coefficient C and step value H.
                  It then calculates the function y(x) until function value exceeds B
                  and N <= 15 (Where N = number of total points).
  */

int main(void) {
  float A, B, H, C; // Variable declaration
  int N;

  printf("Enter starting value (A): "); // User inputs start value
  scanf("%f", & A);

  printf("Enter stop value (B): "); // User inputs stop value
  scanf("%f", & B);

  do {
    printf("Enter steps coefficient (C): "); // User inputs coefficient of step
    scanf("%f", & C);

    if (C < 1) // checks if the step value is greater than 0.
    {
      printf("Step coefficient must be greater than or equals to 1!\n");
    }
  }
  while (C < 1);

  do {
    printf("Enter Total Number of Points (N): "); // User inputs number of iterations desired
    scanf("%d", & N);

    if (N <= 0 || N > 15) // checks if the number of points is less than or equal to 0 or if the value is greater than 15.
    {
      printf("Number of points must be greater than 0 and less or equals to 15!\n");
    }
  }
  while (N <= 0 || N > 15);

  do {
    printf("Enter Step Value (H): ");
    scanf("%f", & H);

    if (H <= 0) // checks if the step value is greater than 0.
    {
      printf("Step value must be greater than 0!\n");
    }
  }
  while (H <= 0);

  printf("\nNumber\t\t Argument(x)\t\t Function y(x) \n");

  int num = 0;
  float x;
  float sequenceSum;
  for (int i = 0; i <= N; i++) // For loop is dependent on the user's input of 'N'.
  {
    if (i == 0) {
       x = A;
       sequenceSum = x;
    } else {
        x = sequenceSum + (H * pow(C, i - 1)); // equation for each iteration of X[n]
        sequenceSum = x;
    }
    float y = (14 * pow(x, 3) + 7 * pow(x, 2) - x + 20) / (pow(x, 2) - 4 * x); //Equation for y(x)

    num++;

    if (y <= B) // Satisfies the condition "Until function value exceeds B".
    {
      printf("%d.   \t\t x=%.f     \t\ty(x) = %.2lf\n", num, x, y); // prints out the values of y(x) equivalent to the x values
    } else {
      printf("%d.   \t\t x=%.f    \t\t\ty(x) = N/A \n", num, x, y); // If function value exceeds B, then y(x) is not available/not defined.
    }

  }

  return 0;
}