1

The code below does not deal with any numerical-integration or anything related to it — just the basic rule in evaluating a definite integral of the form Ax^2 + Bx + C. I would love to hear from you guys since I'm relatively new and still learning.
This coding exercise is meant to test what we learned for the past few weeks, so anything more sophisticated than the keywords below is probably discouraged, as well as other standard library functions.

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

void inputDegree(int *deg) {
    printf("Enter the degree of the polynomial: ");
    scanf("%d", deg);
}

void inputCoeffs(int deg, double *coeffs) {
    printf("\nEnter the coefficients of the polynomial: \n\n");
    for(int i = 0; i <= deg; i++) {
        printf("Coefficient of degree %d: ", deg - i);
        scanf("%lf", &coeffs[i]);
    }
}

void inputLimits(double *lowerL, double *upperL) {
    printf("\nEnter the lower limit of integration: ");
    scanf("%lf", lowerL);
    printf("\nEnter the upper limit of integration: ");
    scanf("%lf", upperL);
}

void computeIntegralCoeffs(int deg, double *coeffs, double *integCoeffs) {
    for(int i = 0; i <= deg; i++) {
        integCoeffs[i] = coeffs[i] / (deg + 1 - i);
    }
}

double evaluateIntegral(int degree, double *integCoeffs, double lowerLimit, double upperLimit) {
    double lower = 1, upper = 1;
    for(int i = 0; i <= degree; i++) {
        lower += integCoeffs[i] * pow(lowerLimit, (degree + 1 - i));
    }
    for(int i = 0; i <= degree; i++) {
        upper += integCoeffs[i] * pow(upperLimit, (degree + 1 - i));
    }
    return upper - lower;
}

int main() {
    int degree;
    double lowerLimit;
    double upperLimit;
    double integral;
    double *coefficients = NULL;
    double *integralCoefficients = NULL;

    inputDegree(&degree);

    coefficients = (double *)malloc((degree + 1) * sizeof(double));
    integralCoefficients = (double *)malloc((degree + 1) * sizeof(double));

    inputCoeffs(degree, coefficients);

    inputLimits(&lowerLimit, &upperLimit);

    computeIntegralCoeffs(degree, coefficients, integralCoefficients);

    integral = evaluateIntegral(degree, integralCoefficients, lowerLimit, upperLimit);

    printf("\n\nEvaluating the definite integral gives us the following area: \t%lf\n", integral);

    free(coefficients);
    free(integralCoefficients);

    return 0;
}
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • I'm probably gonna submit this as is, but I would love to hear input from you guys so I can code better. – flamethrower10 Mar 01 '20 at 13:42
  • 3
    https://codereview.stackexchange.com/ – Mickael B. Mar 01 '20 at 13:43
  • Question: under `evaluateIntegral()` function, does the initialization value of `lower` and `upper` matter? I mean is it better to initialize it to 0 in this situation instead of 1? They give same result. – flamethrower10 Mar 01 '20 at 13:45
  • Mickael B. Thank you for referring me to the forum. I'll repost. – flamethrower10 Mar 01 '20 at 13:46
  • Use `int main(void)`. The construction `int main()` has been shunned for decades, and is allowed only to support legacy code. At some point between 1987 and 1992 (or maybe earlier) someone thought it would be a good idea to allow it, thinking that the old style would quickly die out. Clearly, it has not died but is still being taught. Don't use it. – William Pursell Mar 01 '20 at 13:49
  • Don't read the degree from stdin. It is a parameter, and should be taken from the command line argumetns. Can you imagine how difficult it would be to use `grep` if it prompted for a pattern to match instead of taking it as an argument? – William Pursell Mar 01 '20 at 13:51
  • `coefficients = malloc((degree + 1) * sizeof *coefficients);` – William Pursell Mar 01 '20 at 13:52
  • I'm voting to close this question as off-topic because it is about reviewing a working program, and should go to http://codereview.stackexchange.com. – Nate Eldredge Mar 01 '20 at 13:54

1 Answers1

0

In no particular order:

  1. Don't cast the return value from malloc.
  2. Not testing the return value from scanf is asking for trouble.
  3. It's int main(void) in C.
  4. printf without a terminating new-line may require fflush(stdout) to make the output appear.
  5. Indent your code so it stays within about 70 characters per line. This avoids ugly line wraps in editors and horizontal scrollbars on sites like stackoverflow.
  6. There is no point in assigning NULL to pointers when the next thing you do is assign them a value from malloc.
  7. Return from main with EXIT_SUCCESS (or EXIT_FAILURE) from stdlib.h.
  8. Reduce useless verbosity. us the following -> the.
Jens
  • 69,818
  • 15
  • 125
  • 179