请问,我的程序哪里出错了,如果要实现功能嘎怎么办?很简单的,朋友们请进!
我现在要解一个 模线性方程,但是事前不知道它的解会有多少个,是由具体参数而定的,所以,我在调用前建立了一个 solution数组,其大小在求解的函数里确定,用于存放所有的解,我的程序是这样的,但为什么结果不能放到solution里头??
#include<malloc.h>
#include<iostream.h>
template <class IntType>
IntType Modular_Linear_Equation(IntType a,IntType b,IntType n,IntType Solution[])
{
IntType d,i,X0,x,y;
d=Ext_Euclid(a,n,x,y);
if(b%d !=0) cout<<"No Solution! "<<endl;
else
{
Solution=(IntType*)malloc(d*sizeof(IntType));
//这个地方有问题,在函数里分配了空间,但在主函数里头体现不出来,该怎么办?
X0=( x*(b/d) )%n;
for(i=0;i<d;i++)
Solution[i]=( X0+i*(n/d) )%n;
return d;
}
}
void main()
{
int a=2,b=5,n=11;
int* solution;
int d=Modular_Linear_Equation(a,b,n,solution);
for(int i=0;i<d;i++)
cout<<solution[i]<<endl;//由于上述原因,这里就出错了,请赐教
}
请高手赐教!
问题点数:50、回复次数:3Top
1 楼yjh1982(血精灵)回复于 2003-08-04 17:01:49 得分 50
IntType Solution[]改为IntType**Solution;
Solution=(IntType*)malloc(d*sizeof(IntType));改为(*Solution)=(IntType*)malloc(d*sizeof(IntType));
int d=Modular_Linear_Equation(a,b,n,solution);改为int d=Modular_Linear_Equation(a,b,n,&solution);Top
2 楼panda_lin(熊猫)回复于 2003-08-04 17:12:19 得分 0
用内存动态分配解决,或者事先开个够大的数组。Top
3 楼chinazcw(笑口常开)回复于 2003-08-04 17:16:03 得分 0
这是因为你所选的参数传递方式出错了~~~
把参数表中的IntType Solution[],改成IntType **Solution。
把模板函数中的Solution=(IntType*)malloc(d*sizeof(IntType));改成
*Solution=(IntType*)malloc(d*sizeof(IntType));
把主函数中的int d=Modular_Linear_Equation(a,b,n,solution);改成
int d=Modular_Linear_Equation(a,b,n,&solution);
再应该差不多了!~Top



