c. The language result value continues to be zero.

Asked 2 weeks ago, Updated 2 weeks ago, 1 views

If you change the return values of the formula1 and formula2 functions, the value will likely come out, but the value will continue to be zero. Can I get a little tip?

I want to get this value.

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

double fomula1(double (*pf1)(double), double n);
double fomula2(double m);
int comb(int n);
int factorial(int n);

int main() {
    printf("%.3lf\n", fomula1(fomula2, 3.0));
    return 0;
}

int comb(int n) {
    factorial(2 * n) / factorial(n) * factorial(n);
    return (int)comb;
};

int factorial(int n) {
    int p;
    for (p = n; p >= 1; p--)
        n *= p;
    return (int)factorial;
}

double fomula1(double (*pf1)(double), double n) {
    int i;
    int result, sum = 0;
    for (i = 1; i < n + 1; i++) {
        result = (1 / 2) * ((int)pf1);
        sum = sum + result;
    };
    return sum;
}

double fomula2(double m) {
    int k;
    int result, sum;
    result, sum = 0;
    for (k = 1; k < m + 1; k++) {
        result = 1 + comb(2 * k) * (1 / pow((2 + k), k));
        sum += result;
    };
    return sum;
}

c

2022-09-20 16:21

2 Answers

Since I don't know anything, I can't correct errors during verification/modification.

However, there is something strange about the code. Please refer to the comment below.

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

double fomula1(double (*pf1)(double), double n);
double fomula2(double m);
int comb(int n);
int factorial(int n);

int main() {
    printf("%f\n", fomula1(fomula2, 3.0));
    return 0;
}

/*
int comb(int n) {
    factorial(2 * n) / factorial(n) * factorial(n);
    This is the address value of the comb function.
};
*/

int comb(int n) {
    int q = 0;
    q = factorial(2 * n) / factorial(n) * factorial(n);
    return q;
};

/* 
int factorial(int n) {
    int p;
    for (p = n; p >= 1; p--)
        n *= p;
    return (int)factual. Address value of factor function...
}
*/

int factorial(int n) {
    if (n <= 1) return 1;
    return n * factorial(n - 1);
}

double fomula1(double (*pf1)(double), double n) {
    int i;
    //int result, sum = 0; if return is double, double
    double result, sum = 0.0;
    for (i = 1; i < n + 1; i++) {
        //result = (1/2) * ((int)pf1); pf1 is the function pointer value. If you do not want to write the address value of the function, you must include the parameter.
        The parameter of result = pf1(i)/2; // pf1 was entered as an arbitrary value.
        sum = sum + result;
    };
    return sum;
}

double fomula2(double m) {
    int k;
   //int result, sum = 0; if return is double, double
    double result, sum = 0;
    for (k = 1; k < m + 1; k++) {
        result = 1 + (double)comb(2 * k) * (1 / pow((2 + k), k));
        sum += result;
    };
    return sum;
}


2022-09-20 16:21

        result = (1 / 2) * ((int)pf1);

Here, (1/2) is calculated first, and this value is 0 because int is divided int. No matter what value 0 is multiplied by, it will all become 0.

There are many similar errors and other errors here and there.


2022-09-20 16:21

If you have any answers or tips


© 2022 pinfo. All rights reserved.