C語言傳陣列問題及簡化

#include <stdio.h>

#include <stdlib.h>

#include <math.h>

#include <time.h>

int Demical(float ,float );

float Change(float );

float *Power(float );

float *Count_x1_and_x2(float [],float ,float ,float );

float *Count_function(float [],float[]);

void main (void)

{

float x11=-3.0,x12=12.1,x21=4.1,x22=5.8,d[20],e[20],f[20],g[20],*stuptr1,*stuptr2,*stuptr3,*stuptr4;

/*printf("Please input two numbers to define the scope of x1:");

scanf("%f%f",&x11,&x12);

printf("Please input two numbers to define the scope of x2:");

scanf("%f%f",&x21,&x22);*/

int n1=(Demical(x11,x12));

stuptr1=Power(n1);

for(int i=0;i<=19;i++)

{

d[i]=(float)stuptr1[i];

}

stuptr3=Count_x1_and_x2(d,n1,x11,x12);

for(int i=0;i<19;i++)

{

f[i]=(float)stuptr3[i];

}

printf("\n");

int n2=(Demical(x21,x22));

stuptr2=Power(n2);

for(int i=0;i<=19;i++)

{

e[i]=(float)stuptr2[i];

}

stuptr4=Count_x1_and_x2(e,n2,x21,x22);

for(int i=0;i<19;i++)

{

g[i]=(float)stuptr4[i];

}

Count_function(f,g);

system("pause");

}

int Demical(float h1,float h2)

{

int point=10000;

int n=Change((int)((h2-h1)*point));

return n;

}

float Change(float N)

{

int count=0;

while(N>1)

{

N=N/2;

count++;

}

return count;

}

float *Power(float n)

{

static float a[20];

float base=2;

srand((unsigned)time(NULL));

for(int i=0;i<=19;i++)

{

a[i]=(pow(base,n)-1)*rand()/RAND_MAX+1; //(b-a)*rand()/RAND_MAX + a

}

return a;

}

float *Count_x1_and_x2(float *a,float n,float low,float high)

{

static float Xn[20];

float base=2;

for(int i=0;i<=19;i++)

{

Xn[i]=(float)(low+(a[i]*(high-low)/(pow(base,n)-1)));

}

printf("\n");

return Xn;

}

float *Count_function(float *a,float*b)

{

static float ans[20];

float p=3.14159265;

for(int i=0;i<=19;i++)

{

ans[i]=21.5+a[i]*sin(4*p*a[i])+b[i]*sin(20*p*b[i]);

printf("%f\n",ans[i]);

}

return ans;

}

以上程式是自己寫的,可是第20個答案的前面兩個第20個值一直傳不回去,所以都只有抓到記憶體的值...希望有能可以幫我找出BUG在哪?

還有因為是傳陣列值新手,所以不知道還有沒有辦法簡化它?

以上兩個問題,謝謝您!

1 Answer

Rating
  • 夏明
    Lv 4
    9 years ago
    Favorite Answer

    第 2 個 for() 迴圈和第 4 個 for() 迴圈,條件判斷少了 = 。

    2011-10-12 04:11:30 補充:

    //......省略

    void main (void)

    {

    float x11=-3.0,x12=12.1,x21=4.1,x22=5.8;

    float d[20],e[20],f[20],g[20],*stuptr1,*stuptr2,*stuptr3,*stuptr4;

    int n1=(Demical(x11,x12));

    int n2=(Demical(x21,x22));

    stuptr1=Power(n1);

    stuptr2=Power(n2);

    2011-10-12 04:11:58 補充:

    int i;

    for(i=0;i<=19;i++)

    {

    d[i]=(float)stuptr1[i];

    e[i]=(float)stuptr2[i];

    }

    stuptr3=Count_x1_and_x2(d,n1,x11,x12);

    stuptr4=Count_x1_and_x2(e,n2,x21,x22);

    2011-10-12 04:12:10 補充:

    for(i=0;i<=19;i++)

    {

    f[i]=(float)stuptr3[i];

    g[i]=(float)stuptr4[i];

    }

    printf("\n");

    Count_function(f,g);

    system("pause");

    }

    //......省略

    2011-10-12 05:38:00 補充:

    沒辦法繼續簡化下去的原因在於要先求出 stuptr3, stuptr4 才有辦法求 f[i], g[i]

    2011-10-12 05:45:00 補充:

    可使用二維陣列與指標陣列,只要解決上述原因可以更簡化程式碼。

    void main (void){

    float x11=-3.0,x12=12.1,x21=4.1,x22=5.8;

    float d[4][20];

    float *stuptr[4];

    int n1=(Demical(x11,x12));

    int n2=(Demical(x21,x22));

    stuptr[0]=Power(n1);

    stuptr[1]=Power(n2);

    2011-10-12 05:45:20 補充:

    int i;

    for(i=0;i<=19;i++){

    d[0][i]=(float)(stuptr[0][i]);

    d[1][i]=(float)(stuptr[1][i]);

    }

    stuptr[2]=Count_x1_and_x2(d[0],n1,x11,x12);

    stuptr[3]=Count_x1_and_x2(d[1],n2,x21,x22);

    2011-10-12 05:45:26 補充:

    for(i=0;i<=19;i++){

    d[2][i]=(float)(stuptr[2][i]);

    d[3][i]=(float)(stuptr[3][i]);

    }

    printf("\n");

    Count_function(d[2],d[3]);

    system("pause");

    }

Still have questions? Get your answers by asking now.