| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1253 人关注过本帖
标题:动态分配二维数组的疑惑
只看楼主 加入收藏
Serena2008
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2008-4-7
收藏
 问题点数:0 回复次数:15 
动态分配二维数组的疑惑
[bo]描述:动态分配二维数组
问题:1 编译时有两个警告性错误,不知错在哪
      2 参数传递时,x1[][N]是变成静态数组,怎样改,才能也是动态的呢[/bo]
#include"stdio.h"
#include"stdlib.h"

#define M 2
#define N 3
#define LEN sizeof(int)

main()
{
    void ex(int x1[][N]);
    
    int i;
    int **x;
    x=(int **)malloc(M*LEN);
    for(i=0;i<M;i++)
        x[i]=(int*)malloc(LEN*N);
    ex(x);
}

void ex(int x1[][N])
{
    int i, j;
    for(i=0;i<M;i++)
    {
        for(j=0;j<N;j++)
            printf("%d ", (i+1)*j);
        printf("\n");
    }
}
搜索更多相关主题的帖子: 动态 
2008-04-16 15:05
Serena2008
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2008-4-7
收藏
得分:0 
请高手帮忙
在线等
2008-04-16 15:13
mqh21364
Rank: 1
等 级:新手上路
帖 子:642
专家分:0
注 册:2008-2-28
收藏
得分:0 
void ex(int x1[][N])
{
    int i, j;
    for(i=0;i<M;i++)
    {
        for(j=0;j<N;j++)
            printf("%d ", (i+1)*j);
        printf("\n");
    }
}
你用到你传递的参数了没有啊?

还有, malloc返回值是一个指针,好像不能是指向指针的指针吧?

前不见古人,后不见来者。念天地之悠悠,独怆然而涕下。
2008-04-16 15:33
Serena2008
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2008-4-7
收藏
得分:0 
#include"stdio.h"
#include"stdlib.h"

#define M 2
#define N 3
#define LEN sizeof(int)

main()
{
    void ex(int x1[][N]);
    
    int i, j;
    int **x;
    x=(int **)malloc(M*LEN);
    for(i=0;i<M;i++)
        x[i]=(int*)malloc(LEN*N);

    ex(x);

    for(i=0;i<M;i++)
    {
        for(j=0;j<N;j++)
           printf("%d ", x[i][j]);
        printf("\n");
    }


}

void ex(int x1[][N])
{
    int i, j;

    for(i=0;i<M;i++)
        for(j=0;j<N;j++)
           x1[i][j]=(i+1)*j;
}
2008-04-16 15:46
bianfeng
Rank: 1
等 级:新手上路
帖 子:199
专家分:0
注 册:2007-11-28
收藏
得分:0 
程序代码:
#include"stdio.h"
#include"stdlib.h"

#define M 2
#define N 3
#define LEN sizeof(int)

main()
{
    void ex(int **c);
    
    int i;
    int **x;
    x=(int **)malloc(M*sizeof(int *));  //先开辟M个指针数组
    for(i=0;i<M;i++)
        x[i]=(int*)malloc(LEN*N);    //然后把开辟空间的首地址赋给指针数组的每一个元素 
    ex(x);
}

void ex(int **c)   //学了指向指针的指针就它接收吧..可以练习一下^_^
{
    int i, j;
    for(i=0;i<M;i++)
    {
        for(j=0;j<N;j++)
            printf("%-5d", *(*(c+i)+j));
        printf("\n");
    }
}


[[it] 本帖最后由 bianfeng 于 2008-4-16 20:42 编辑 [/it]]
2008-04-16 16:12
Serena2008
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2008-4-7
收藏
得分:0 
to bianfeng: 非常感谢!
             这个问题已经困扰我两天了,谢谢你帮我解决了这个难题。
2008-04-16 17:41
yd4433
Rank: 1
等 级:新手上路
帖 子:404
专家分:0
注 册:2008-3-9
收藏
得分:0 
sai  bianfeng 你的强项来了

------...-.-..-...-----........-------.......----.....------....||- - !
2008-04-16 17:54
bianfeng
Rank: 1
等 级:新手上路
帖 子:199
专家分:0
注 册:2007-11-28
收藏
得分:0 
呵呵,大家有困难应该互相帮助..
2008-04-16 20:41
Serena2008
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2008-4-7
收藏
得分:0 
#include"time.h"
#include"stdio.h"
#include"stdlib.h"
#include"conio.h"
#include"math.h"

//Initialisation.
//===============
#define PI 3.1415
#define N 50                 // Number of time steps
#define Q 10                 // Process noise variance
#define R 1                  // Measurement noise variance
#define x0 0.1               //Initial state
#define initVar 5            
#define numSamples 500       // Number of Monte Carlo samples per time step
#define LEN  sizeof(double)


void main()
{
  double Normal(double x);
  double NormalRandom();
  double AverageRandom(double min, double max);
  void bootstrap(double **x1, double **q1, double yy[]);
  void predictstates(double *p, int tt);
  void importanceweights(double *pointer_1, double *pointer_2, double yy);
  void updatestates(double *pointer_3, double *pointer_1, double *pointer_2);


  int i, j, t;
  
  double xx[N];            // Hidden states
  double v[N],w[N];

  double y[N];             // Observations

  FILE *fp1, *fp2;         
 
  double **x, **q;
  x=(double **)malloc(N*sizeof(double*));  // The estimated state samples
  q=(double **)malloc(N*sizeof(double*));

  for(i=0;i<N;i++)
  {
       x[i]=(double*)malloc(numSamples*LEN);  // The estimated state samples
      q[i]=(double*)malloc(numSamples*LEN);
  }



  xx[0] = x0;                 // Initial state

  fp1=fopen("d:\\output_x.txt","wt+");   // Open a file
  fp2=fopen("d:\\output_q.txt","wt+");


// NormalRandom() is a function which generate a random number that obeys Gauss distribute.
// Generate process and measurement noise.
//==========================================================================================
  srand(time(NULL));
  for(i=0;i<N;i++)
  {
      v[i]=NormalRandom();            // v[50];Generate measurement noise

      w[i]=sqrt(10)*NormalRandom();   // w[50];Generate process noise
  }



//Generate state and measurements.
// ===============================  
  y[0]=(xx[0]*xx[0])/20 + v[0];
  
  for(t=1;t<N;t++)
  {
      xx[t]=0.5*xx[t-1]+25*xx[t-1]/(1+xx[t-1]*xx[t-1])+8*cos(1.2*(t-1))+w[t];

      y[t]=xx[t]*xx[t]/20+v[t];
  }



  
//Perform sequential Monte Carlo filtering.
//=========================================  

bootstrap(x, q, y);


//Result output a file.
if(fp1==NULL)
    {
        printf("cann't open this file\n");
        return;
    }
    for(i=0;i<N;i++)
    {
        for(j=0;j<numSamples;j++)

            fprintf(fp1,"%10.4f ", x[i][j]);

        fputs("\n",fp1);
        fputs("\n",fp1);
        fputs("\n",fp1);
    }

if(fp2==NULL)
    {
        printf("cann't open this file\n");
        return;
    }
    for(i=0;i<N;i++)
    {
        for(j=0;j<numSamples;j++)

            fprintf(fp2,"%10.4f ", q[i][j]);

        fputs("\n",fp2);
        fputs("\n",fp2);
        fputs("\n",fp2);
    }
    free(x);
    free(q);
}


//Generate a uniform random number range from min to max.
//=======================================================
double AverageRandom(double min, double max)
{

    int minInteger=(int)(min*10000);

    int maxInteger=(int)(max*10000);

    int randInteger=rand()*rand();

    int diffInteger=maxInteger-minInteger;

    int resultInteger=randInteger%diffInteger+minInteger;

    double result=resultInteger/10000.0;
    
    return result;
}



//Generate a normal Gauss random number.
//======================================
double Normal(double x)   
//Probility Density Function
{

    return 1.0/sqrt(2*PI) * exp(-1*(x*x)/2);
   
}  

//Generate a random number that is normal distribution
double NormalRandom()   

{        

    double x;
    double dScope;
    double y;     
    do
    {
        x = AverageRandom(-3,3);      
        y = Normal(x);   
        dScope = AverageRandom(0, Normal(0));   
      
    }while( dScope > y);   
    
    return x;         
 }



// Bootstrap algorithm.
//=====================
void bootstrap(double **x1, double **q1, double yy[])
{
    int i, t;

    int rows = N;
    
    double *xu;
    xu=(double *)malloc(numSamples*LEN);   // The estimated state samples

// Sample from the prior.
//=======================
    srand(time(NULL));
    for(i=0;i<numSamples;i++)
    {
        x1[0][i]=sqrt(initVar)*NormalRandom();  // x1[0]随机产生500个粒子

        xu[i]=x1[0][i];
    }


// Update and prediction stages.
// =============================

    for(t=0;t<rows-1;t++)
    {
        predictstates(xu, t);

        importanceweights(xu, q1[t+1], yy[t+1]);

        updatestates(x1[t+1], xu, q1[t+1]);

        for(i=0;i<numSamples;i++)
            xu[i]=x1[t+1][i];
      
    }
   

}



//Performs the prediction step of the sequential SIR algorithm for
//the model described in the file sirdemo1.m
//===============================================================

void predictstates(double *p, int tt)
{
    int j;
    double temp;

    double *w;
    w=(double *)malloc(numSamples*LEN);

    srand(time(NULL));
    for(j=0;j<numSamples;j++)
    {
        w[j]=sqrt(Q)*NormalRandom();
        
        temp=*(p+j);
        
        temp=0.5*temp+25*temp/(1+temp*temp)+8*cos(1.2*tt)+w[j];

        *(p+j)=temp;
    }

    free(w);
}


//Computes the normalised importance ratios
//===========================================

void importanceweights(double *pointer_1, double *pointer_2, double yy)
{
    int i, s;

    double temp;

    double sum=0;

    double *m;
    m=(double *)malloc(numSamples*LEN);

    for(i=0;i<numSamples;i++)
    {
        temp=*(pointer_1+i);

        m[i]=temp*temp/20;

        sum+=exp(-(yy-m[i])*(yy-m[i])/(2*R));
    }

   
    for(s=0;s<numSamples;s++)
    {
        temp=*(pointer_2+s);

        temp=(exp(-(yy-m[s])*(yy-m[s])/(2*R)))/sum;

        *(pointer_2+s)=temp;
    }

    free(m);
}


//Performs the resampling step of the SIR algorithm
//==================================================

void updatestates(double *pointer_3, double *pointer_1, double *pointer_2)
{
    int k;
    double temp, temp1;

    int i=0;
    int j=0;

    double *u, *t1, *T1, *Q1, *QT;
    u=(double *)malloc(numSamples*LEN);
    t1=(double *)malloc(numSamples*LEN);
    T1=(double *)malloc(numSamples*LEN);
    Q1=(double *)malloc(numSamples*LEN);
    QT=(double *)malloc(numSamples*LEN);

        
    u[0]=AverageRandom(0, 1);

    t1[0]=-log(u[0]);

    T1[0]=t1[0];

    Q1[0]=*pointer_2;

    srand(time(NULL));
    for(k=1;k<=numSamples;k++)
    {
        u[k]=AverageRandom(0, 1);

        t1[k]=-log(u[k]);

        T1[k]=T1[k-1]+t1[k];

        temp=*(pointer_2+k);

        Q1[k]=Q1[k-1]+temp;
    }

        while(j<=numSamples)
        {
            for(k=0;k<numSamples;k++)
                  QT[k]=Q1[j]*T1[k];
                
                if(QT[j]>T1[i])
                {
                    temp1=*(pointer_1+j);

                    *(pointer_3+i)=temp1;

                    i=i+1;
                }
                else
                
                    j=j+1;

        
        }

        free(u);
        free(t1);
        free(T1);
        free(Q1);
        free(QT);
}
2008-04-16 21:33
Serena2008
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2008-4-7
收藏
得分:0 
程序编译,链接都没有错误。运行时,提示Debug Error
                                      damage after normal block(#146)at 0x00609258

高手帮忙,先谢了
2008-04-16 21:37
快速回复:动态分配二维数组的疑惑
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.025976 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved