0

I have two questions.

I wanted to write a function that solves an equation:

int equation() {
    int n, r, s;

    printf("\nEnter the value of N: ");
    scanf("%d", &n);

    printf("Enter the value of R: ");
    scanf("%d", &r);

    printf("Enter the value of S: ");
    scanf("%d", &s);

    int i, j, k;
    int c = 0;
    int a[r], b[s];

    a[0] = 1;
    for (i = 1; i <= r; i++) {
        a[i] = a[i-1] * ((((i * i * i) * 3) + 5) / (i * i));
    }

    for (j = 1; j <= s; j++) {
        b[j] = b[j-1] + sqrt(3 * (j * j * j) + j + 2) / (2 * j);
    }
    // The last for loop
    for (k = 1; k <= n; k++) {
        c += a[k] / b[k];
    }

    printf("Result: %d \n \n", c);

    return c;
}

It works well if the last for loop has this line in it:

printf("%d, %d, %d", c, a[k], b[k]);

But if the last one doesn't have the line above, it returns 0. What can be the problem?

Expected values:

n, r, s = 1 the result should be 8.

n, r, s = 2 the result should be 36.

n, r, s = 3 the result should be 204.

I get these values if I write the printf line into the last for.

Also I want to ask another question. When I change this line

a[i] = a[i-1] * ((((i * i * i) * 3) + 5) / (i * i));

to this

a[i] = a[i-1] * ((((pow(i, 3) * 3) + 5) / (i * i));

it gives me a different result. Why?

Thanks.

3
  • You should really have posted two separate questions. Now you have an answer that addresses only one of your questions. Commented Dec 13, 2015 at 19:33
  • Provide example input and actual and expected output. Commented Dec 13, 2015 at 19:39
  • Oh, thank you. Next time I ask my questions one by one. I edited my post to provide expected output. Commented Dec 13, 2015 at 20:03

2 Answers 2

1

Integer arithmetic vs floating point arithmetic.

The first expression ((((i * i * i) * 3) + 5) / (i * i)) uses integer arithmetic, therefore integer division. The second expression ((((pow(i, 3)) * 3) + 5) / (i * i)), because pow() is defined to return a double will be evaluated using floating point arithmetic and therefore will return a floating point value. This value multiplied by integer a[i-1] likely gives a different result, itself converted back to int for storage into a[i].

The second loop refers to b[0] that has not been initialized. The whole computation depends on this value, changing the code before or after that may change the random value that happens to be there in the absence of any initialization and cause the code to appear to work. Initialize b[0] to what it should be and run your tests again. Use my version below with double arithmetic for that.

For your problem, you should use double type instead of int for a[], b[] and c, convert the integers to double with a cast (double) and use floating point constants 3.0 and 5.0 to force floating point computation:

double equation(void) {
    int n, r, s;

    printf("\nEnter the value of N: ");
    if (scanf("%d", &n) != 1) return -1;

    printf("Enter the value of R: ");
    if (scanf("%d", &r) != 1) return -1;

    printf("Enter the value of S: ");
    if (scanf("%d", &s) != 1) return -1;

    if (r < n || s < n) {
        printf("Invalid values, N must be greater or equal to noth R and S\n");
        return -1;
    }

    int i, j, k;
    double c = 0.0;
    double a[r+1], b[s+1];

    a[0] = 1.0;
    for (i = 1; i <= r; i++) {
        a[i] = a[i-1] * (((((double)i * i * i) * 3.0) + 5.0) /
                         ((double)i * i));
    }

    b[0] = 1.0; // you forgot to initialize b[0], what should it be?
    for (j = 1; j <= s; j++) {
        b[j] = b[j-1] + sqrt(3.0 * ((double)j * j * j) + j + 2.0) / (2.0 * j);
    }

    // The last for loop
    for (k = 1; k <= n; k++) {
        c += a[k] / b[k];
    }

    printf("Result: %f\n\n", c);

    return c;
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you very much. I am a begginer this is why I missed that part. Do you have any idea why for loop doesn't work well whithout printf line?
@E.GÜLSOYLU: maybe a side effect of undefined behavior since you do not initalize b[0]. I updated my answer.
But if I put printf line I am not initalizing b[0] but I got the right c value.
@E.GÜLSOYLU: you get the correct value for c by chance. Expected results from incorrect code is one possible outcome of undefined behavior.
0

The result in c is zero regardless of the printf() - the loop does not work with or without the printf - you are misinterpreting the your own debug output - a newline in the printf would help. For example the output for n=5, r=5, s=5 when the line:

printf("*** %d, %d, %d\n", c, a[k], b[k]);

is included in the loop is:

*** 0, 8, -144230090
*** 0, 56, -144230088
*** 0, 504, -144230086
*** 0, 6048, -144230084
*** 0, 90720, -144230082
Result: 0 

Note the that c is always zero.

The problem is that you are performing integer arithmetic, and the fractional part is lost. Integer division truncating the fractional part, rounding toward zero. For example 1/2 == 0, 5/2 == 2.

You should change the data types of c, a, b and the function itself to double.

1 Comment

Thanks for your answer and patience. Fractional part was not important for me. After initilizing b[0] it worked well. But again, thank you for your answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.