大家帮忙看看,为什么两个Printf("%f\n",pArray[i][j])的值不一样?

mrh123 2003-11-05 01:13:52
/* Guass xiaoyuan method */

#include <stdio.h>
#include <mem.h>
#include <stdlib.h>


void main(){

int rows = 1,
cols = 1;
int cur = 0;
int i,j;
char abstr[1024] = {0};
char *p = NULL;
float **pArray = NULL; /* buffer for element */

clrscr(); /*clear the screen*/

printf("please input the matrix A for equations:\n");
printf("input style:[1 1 1;2 2 2;3 3 3]\n");
printf("A=");
gets(abstr); /*if use printf,the blank wll be omitted*/

/* to calculate how many rows and cols*/
p=abstr;

p=strchr(p,';');
while(p){
rows++;
p=p+1;
p=strchr(p,';');
}

for(cur = 0 ; abstr[cur] != ';' ;cur++)
if (abstr[cur]==' ') cols++;

/* xiaoyuan */
pArray=(float**)malloc(sizeof(float)*rows*cols);
p=abstr+1;
for(i = 0; i < rows; i++)
for(j = 0; j <cols; j++){
pArray[i][j] = (float)atof(p);
p++;
while(((*p) != ' ') && ((*p) !=';')) p++;
p++;
printf("%f\n",pArray[i][j]);
}

for(i=0; i < rows; i++){
for(j=0; j <cols; j++)
printf("%f ",pArray[i][j]);
printf("\n");
}



}
如题,谢了
...全文
62 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
sysmaster 2003-11-05
  • 打赏
  • 举报
回复
同意楼上的用FOR来分配
bosedom 2003-11-05
  • 打赏
  • 举报
回复
pArray=(float**)malloc(sizeof(float)*rows*cols);
用这种方法动态分配二维数组是不支持[][]下标运算的。
double **a;
a=new double*[M];
for(int i=0;i<N;i++){
a[i]=new double[N];
}

试试上面的

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧