2012-10-03 4 views
2

저는 코딩에 익숙하지 않고 3 차 스플라인을 보간하기위한 코드를 작성했지만 다음 방정식에 마지막으로 붙어 있습니다 : _ "2 진수에 대한 피연산자가 올바르지 않습니다.^('double'및 'double'을 가짐) |"이진수에 피연산자가 없습니다^(double 및 double을 가짐)

두 번째 첫 번째 줄에 "population ="이있는 코드의 마지막 비트에 문제가 있습니다. 누군가가 올바른 방향으로 나를 가리킬 수 있다면 정말 고마워.

#include <stdio.h> 


main() { 

    int x; 
    int y; 
    double stats[10][2]; 
    double gpp[8][9] = {0}; 
    double gppr[10] = {0}; 
    double year; 
    double population; 
    int xi = 0; 
    year=1950; 
    population=0; 
    x=0; 

    stats[0][0] = 1930; stats[1][0] = 1940; stats[2][0] = 1949; 
    stats[3][0] = 1955; stats[4][0] = 1960; stats[5][0] = 1970; 
    stats[6][0] = 1980; stats[7][0] = 1990; stats[8][0] = 2000; 
    stats[9][0] = 2005; 

    stats[0][1] = 21.058; stats[1][1] = 23.547; stats[2][1] = 20.167; 
    stats[3][1] = 21.502; stats[4][1] = 24.989; stats[5][1] = 30.852; 
    stats[6][1] = 37.407; stats[7][1] = 43.390; stats[8][1] = 45.985; 
    stats[9][1] = 47.041; 

    //Initiate g'' system of equation 
    for (x=0;x<8;x++) { 
     gpp[x][x] = ((stats[x+1][0]-stats[x][0])+(stats[x+2][0]-stats[x+1][0]))/3; 
     if (x<7) { 
      gpp[x][x+1] = (stats[x+2][0]-stats[x+1][0])/6; 
     } 
     if (x>0) { 
      gpp[x][x-1] = (stats[x+2][0]-stats[x+1][0])/6; 
     } 
     gpp[x][8] = ((stats[x+2][1]-stats[x+1][1])/(stats[x+2][0]-stats[x+1][0]))-((stats[x+1][1]-stats[x][1])/(stats[x+1][0]-stats[x][0])); 
    } 

    //Forward sweep 
    for (x=0;x<7;x++) { 
     gpp[x+1][x] = 0; 
     gpp[x+1][x+1] = gpp[x+1][x+1] - (gpp[x][x+1]/gpp[x][x])*gpp[x+1][x]; 
     gpp[x+1][8] = gpp[x+1][8] - (gpp[x][x+1]/gpp[x][x])*gpp[x][8]; 
    } 
    //Backward sweep 
    gppr[9] = 0;gppr[0] = 0; 
    gppr[8] = gpp[7][8]/gpp[7][7]; 
    for (x=7;x > 0;x=x-1) { 
     gppr[x] = (gpp[x][8]-(gppr[x+1]*gpp[x][x+1]))/gpp[x][x]; 
    } 

    //check where is xi 
    for (x=0;x<10;x++) { 
     if (stats[x][0] > year) { 
      xi = x; 
      break; 
     } 
    } 

//Calculate population at x 
population = (gppr[xi]/6)*((((stats[xi+1][0]-year)^3.0)/(stats[xi+1][0]-stats[xi][0]))-(stats[xi+1][0]-stats[xi][0])*((stats[xi+1][0]-year))) 
     + (gppr[xi+1]/6)*((((year-stats[xi][0])^3.0)/(stats[xi+1][0]-stats[xi][0]))-(stats[xi+1][0]-stats[xi][0])*((year-stats[xi+1][0]))) 
     + (stats[xi][1])*((stats[xi+1][0]-year)/(stats[xi+1][0]-stats[xi][0])) 
     + (stats[xi+1][1])*((year-stats[xi][0])/(stats[xi+1][0]-stats[xi][0])); 

} 

C에 대해 조금 더 배우기를 기대합니다!

휴고

+3

당신은'math.h'에서'pow()'함수를 찾고있는 것처럼 보입니다. – Mysticial

+0

C에는 지수 연산자가 없습니다. 정수 지수의 경우, 'pow' 함수는 지수 연산을 수동으로 수행하는 것보다 낮은 성능 (컴파일러와 C 라이브러리에 따라 다름)이 될 수 있습니다. – sfstewman

답변

5

^은 C에서 배타적 OR 연산자이며 복식에는 적합하지 않습니다. 그것은 비트 연산자이고 그것에 대한 자세한 내용은 찾을 수 있습니다 (및 다른 비트 연산자) here.

하나의 숫자를 다른 숫자의 지수로 올리려면 pow() 함수가 필요합니다.

그래서 뭔가 같은 :

((stats[xi+1][0]-year)^3.0) 

실제로 작성해야합니다

pow (stats[xi+1][0] - year, 3.0) 

제 최신 (C11) 표준 상태의 7.12.7.4 The pow functions : 내가 마지막으로 생각

Synopsis:
#include <math.h>
double pow(double x, double y);
float powf(float x, float y);
long double powl(long double x, long double y);

Description:
The pow functions compute x raised to the power y. A domain error occurs if x is finite and negative and y is finite and not an integer value. A range error may occur. A domain error may occur if x is zero and y is zero. A domain error or pole error may occur if x is zero and y is less than zero.

The pow functions return xy.

+0

감사합니다. 이 솔루션을 구현 한 후 내 솔루션을 얻었습니다! – imacube

3

C에는 지수 연산자가 없습니다. ^은 XOR 연산자이므로 비 정수에서는 작동하지 않으므로 오류가 발생합니다. 대신 pow 함수를 사용하십시오.

0

방정식은 수학의 힘 함수를 찾으려고합니다. 그러나 "^"에서이 연산자는 배타적 인 또는 배타적 인 의미입니다. 더 나은 시도 #include> math.h>

관련 문제