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.