E. Gülsoylu
E. Gülsoylu

Reputation: 3

For Loop Doesn't Work Without printf

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.

Upvotes: 0

Views: 404

Answers (2)

Clifford
Clifford

Reputation: 93466

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.

Upvotes: 0

chqrlie
chqrlie

Reputation: 144695

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;
}

Upvotes: 1

Related Questions