-2

The question look like this

https://i.imgur.com/u0LJO0g.png

where do i get wrong?

#include<stdio.h>
int main(){
    int i,n;
    int a[] = {3,5,7};
    float x[] = {0,0,0};

    printf("function f(x)=(x^3-2x^2+10x-5)/(x-10)\n");

    for(i = 0;i<3;i++){
        x[i] = (a[i]^3-2*(a[i]^2)+10*a[i]-5)/(a[i]-10);
    }

    for(n = 0;n<3;n++){
        printf("if x is %d,f(x) is %f\n",a[n],x[n]);
    }
}

I expect the output will look that this

if x is 3,f(x) is -5.14
if x is 5,f(x) is -24.00
if x is 7,f(x) is -103.33

but the actual output is

if x is 3,f(x) is -3.000000
if x is 5,f(x) is -7.000000
if x is 7,f(x) is -20.000000
wolfheluo
  • 1
  • 3
  • 1
    You need to review operators in C as well as being aware of the difference between integer division and floating point division. The ^ operator does not raise a number to a power in C. In C ^ is the exclusive OR (XOR) operator. – Jim Rogers Sep 20 '19 at 17:17

1 Answers1

2
  1. ^ is XOR in C, not exponentiation.
  2. If you do math on ints you're going to get int results. You'll need to cast some of those a[i] to float or double to get floating point arithmetic.
Carl Norum
  • 219,201
  • 40
  • 422
  • 469